{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"\n**<span style=\"color:#023e8a;\">Import all the library needed</span>**\n","metadata":{}},{"cell_type":"code","source":"!pip install -q keras-cv-attention-models\n!pip install -qU scikit-learn\n!pip install -q seaborn","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:21.327769Z","iopub.execute_input":"2023-08-23T01:04:21.32861Z","iopub.status.idle":"2023-08-23T01:04:56.416753Z","shell.execute_reply.started":"2023-08-23T01:04:21.328564Z","shell.execute_reply":"2023-08-23T01:04:56.415379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport os\nfrom tqdm import tqdm\nimport glob\nimport matplotlib.pyplot as plt\nimport tensorflow as tf, re, math\nimport tensorflow.keras.backend as K\nimport sklearn\nimport matplotlib.pyplot as plt\nimport tensorflow_addons as tfa\nimport tensorflow_probability as tfp\nimport pydicom","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-23T01:04:56.419747Z","iopub.execute_input":"2023-08-23T01:04:56.420653Z","iopub.status.idle":"2023-08-23T01:04:56.427412Z","shell.execute_reply.started":"2023-08-23T01:04:56.420615Z","shell.execute_reply":"2023-08-23T01:04:56.426468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">all the version of current running library. I notice if we change to other accelerator, we may get different version of pd and sklearn. I have not figure out how to run this model on TPU yet.</span>**\n","metadata":{}},{"cell_type":"code","source":"print('np:', np.__version__)\nprint('pd:', pd.__version__)\nprint('sklearn:', sklearn.__version__)\nprint('tf:',tf.__version__)\nprint('tfp:', tfp.__version__)\nprint('tfa:', tfa.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:56.428869Z","iopub.execute_input":"2023-08-23T01:04:56.429234Z","iopub.status.idle":"2023-08-23T01:04:56.445562Z","shell.execute_reply.started":"2023-08-23T01:04:56.429202Z","shell.execute_reply":"2023-08-23T01:04:56.444492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:#023e8a;font-size:100%\"><center> check out below notebook for majority of the code! </center></span>**\n\n## **<center><span style=\"color:#FEF1FE;border-radius: 5px;padding: 5px\">[https://www.kaggle.com/code/awsaf49/rsna-atd-cnn-tpu-train#Build-Model](https://www.kaggle.com/code/awsaf49/rsna-atd-cnn-tpu-train#Build-Model)\n</span></center>**","metadata":{}},{"cell_type":"markdown","source":"# **<a id=\"Content\" style=\"color:#023e8a;\">Idea Implementation</a>**\n* [**<span style=\"color:#023e8a;\">1. read in data directly from dcm files (I am grabbing a few as I can just to get CNN running)</span>**]\n* [**<span style=\"color:#023e8a;\">2. decode and preprocess all samples </span>**]  \n* [**<span style=\"color:#023e8a;\">3. data augmentation</span>**]\n* [**<span style=\"color:#023e8a;\">4. Split data into batches(I will scale later as much as this notebook can run)</span>**]\n* [**<span style=\"color:#023e8a;\">5. feed training data into model</span>**]","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">Side note, I know reading from PNG will be faster and more optimized. But I was thinking all the data already are here in the dcm files and easier for beginner like me to to run the notebook.</span>**","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">Configuration setup below</span>**","metadata":{}},{"cell_type":"code","source":"# Source: https://www.kaggle.com/code/awsaf49/rsna-atd-cnn-tpu-train\n\nclass CFG:\n    competition   = 'rsna-atd' \n    \n    debug         = False\n    comment       = 'EfficientNetV1B0-256x256-low_lr-vflip'\n    exp_name      = 'baseline-v4: new_ds + multi_head' # name of the experiment, folds will be grouped using 'exp_name'\n    \n    # use verbose=0 for silent, vebose=1 for interactive,\n    verbose      = 0\n    display_plot = True\n\n    # device\n    device = \"TPU-VM\" #or \"GPU\"\n\n    model_name = 'EfficientNetV1B0'\n\n    # seed for data-split, layer init, augs\n    seed = 42\n\n    # number of folds for data-split\n    folds = 4\n    \n    # which folds to train\n    selected_folds = [0, 1, 2]\n\n    # size of the image\n    img_size = [256, 256]\n#     eq_dim = np.prod(img_size)**0.5\n\n    # batch_size and epochs\n    batch_size = 32\n    epochs = 10\n\n    # loss\n    loss      = 'BCE & CCE'  # BCE, Focal\n    \n    # optimizer\n    optimizer = 'Adam'\n\n    # augmentation\n    augment   = True\n\n    # scale-shift-rotate-shear\n    transform = 0.90  # transform prob\n    fill_mode = 'constant'\n    rot    = 2.0\n    shr    = 2.0\n    hzoom  = 50.0\n    wzoom  = 50.0\n    hshift = 10.0\n    wshift = 10.0\n\n    # flip\n    hflip = True\n    vflip = True\n\n    # clip\n    clip = False\n\n    # lr-scheduler\n    scheduler   = 'cosine' # cosine\n\n    # dropout\n    drop_prob   = 0.6\n    drop_cnt    = 5\n    drop_size   = 0.05\n    \n    # cut-mix-up\n    mixup_prob = 0.0\n    mixup_alpha = 0.5\n    \n    cutmix_prob = 0.0\n    cutmix_alpha = 2.5\n\n    # pixel-augment\n    pixel_aug = 0.90  # prob of pixel_aug\n    sat  = [0.7, 1.3]\n    cont = [0.8, 1.2]\n    bri  = 0.15\n    hue  = 0.05\n\n    # test-time augs\n    tta = 1\n    \n    # target column\n    target_col  = [ \"bowel_injury\", \"extravasation_injury\", \"kidney_healthy\", \"kidney_low\",\n                   \"kidney_high\", \"liver_healthy\", \"liver_low\", \"liver_high\",\n                   \"spleen_healthy\", \"spleen_low\", \"spleen_high\"] # not using \"bowel_healthy\" & \"extravasation_healthy\"","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:56.447328Z","iopub.execute_input":"2023-08-23T01:04:56.448085Z","iopub.status.idle":"2023-08-23T01:04:56.459777Z","shell.execute_reply.started":"2023-08-23T01:04:56.448054Z","shell.execute_reply":"2023-08-23T01:04:56.458706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\n# train_series_meta = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:56.463414Z","iopub.execute_input":"2023-08-23T01:04:56.464378Z","iopub.status.idle":"2023-08-23T01:04:56.484526Z","shell.execute_reply.started":"2023-08-23T01:04:56.464346Z","shell.execute_reply":"2023-08-23T01:04:56.483644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">Create patient_ids and series_id df then merge with train. I am wary of the order here. Therefore a left merge. \n    </span>** <br>\n**<span style=\"color:#023e8a;\">Now I can create DCM path easily\n    </span>**    \n     ","metadata":{}},{"cell_type":"code","source":"directory = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images\"\npatient_ids = []\nseries_ids = []\nfor patient_id in tqdm(os.listdir(directory)):\n    for series_id in os.listdir(os.path.join(directory,patient_id)):\n        patient_ids.append(patient_id)\n        series_ids.append(series_id)\n        \n        \ndf = pd.DataFrame({\n    'patient_id': patient_ids,\n    'series_id': series_ids\n})\ndf['patient_id'] = df['patient_id'].astype('int64')\ndf['series_id'] = df['series_id'].astype('int64')\n\ndf_train = df.merge(train, how = 'left', left_on = 'patient_id', right_on = 'patient_id')","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:56.486153Z","iopub.execute_input":"2023-08-23T01:04:56.486792Z","iopub.status.idle":"2023-08-23T01:04:57.870144Z","shell.execute_reply.started":"2023-08-23T01:04:56.48676Z","shell.execute_reply":"2023-08-23T01:04:57.869181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">we have roughly 3147 patients, and 4711 unique series of dicom folders</span>**","metadata":{}},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:57.871705Z","iopub.execute_input":"2023-08-23T01:04:57.872717Z","iopub.status.idle":"2023-08-23T01:04:57.88009Z","shell.execute_reply.started":"2023-08-23T01:04:57.87268Z","shell.execute_reply":"2023-08-23T01:04:57.878959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">function to standardize pixel array from dicom images</span>**","metadata":{}},{"cell_type":"code","source":"def standardize_pixel_array(dcm: pydicom.dataset.FileDataset) -> np.ndarray:\n    \"\"\"\n    Source : https://www.kaggle.com/competitions/rsna-2023-abdominal-trauma-detection/discussion/427217\n    \"\"\"\n    # Correct DICOM pixel_array if PixelRepresentation == 1.\n    pixel_array = dcm.pixel_array\n    if dcm.PixelRepresentation == 1:\n        bit_shift = dcm.BitsAllocated - dcm.BitsStored\n        dtype = pixel_array.dtype \n        pixel_array = (pixel_array << bit_shift).astype(dtype) >>  bit_shift\n#         pixel_array = pydicom.pixel_data_handlers.util.apply_modality_lut(new_array, dcm)\n\n    intercept = float(dcm.RescaleIntercept)\n    slope = float(dcm.RescaleSlope)\n    center = int(dcm.WindowCenter)\n    width = int(dcm.WindowWidth)\n    low = center - width / 2\n    high = center + width / 2    \n    \n    pixel_array = (pixel_array * slope) + intercept\n    pixel_array = np.clip(pixel_array, low, high)\n\n    return pixel_array","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:57.881737Z","iopub.execute_input":"2023-08-23T01:04:57.882728Z","iopub.status.idle":"2023-08-23T01:04:57.892688Z","shell.execute_reply.started":"2023-08-23T01:04:57.88269Z","shell.execute_reply":"2023-08-23T01:04:57.891573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n**<span style=\"color:#023e8a;\">I am manually dropping 80% of healthy images. So that healthy images are roughly 34% in the dataset. The rest are anyone with any injury.\n</span>**\n","metadata":{}},{"cell_type":"code","source":"# List of target columns\ntarget_cols = ['bowel_healthy', 'bowel_injury', 'extravasation_healthy', \n               'extravasation_injury', 'kidney_healthy', 'kidney_low', \n               'kidney_high', 'liver_healthy', 'liver_low', 'liver_high',\n               'spleen_healthy', 'spleen_low', 'spleen_high', 'any_injury']\n\n# Convert target columns to string and concatenate\ndf_train['stratify_col'] = df_train[target_cols].astype(str).apply(''.join, axis=1)\n\n# remove the ones samples with only 1 in target cols concatetation, or else cause problem in stratifying\nclass_counts = df_train['stratify_col'].value_counts()\nto_remove = class_counts[class_counts == 1].index\ndf_train_stratify = df_train[~df_train['stratify_col'].isin(to_remove)]\n\n# Filtering rows with stratify_col == '10101001001000' aka healthy person\nrows_to_drop = df_train_stratify[df_train_stratify['stratify_col'] == '10101001001000']\n\n# Randomly selecting 80% of those rows\ndrop_indices = rows_to_drop.sample(frac=0.8, random_state=42).index\n\n# Dropping those rows from the original dataframe\ndf_train_stratify = df_train_stratify.drop(drop_indices)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:57.894099Z","iopub.execute_input":"2023-08-23T01:04:57.894545Z","iopub.status.idle":"2023-08-23T01:04:58.003725Z","shell.execute_reply.started":"2023-08-23T01:04:57.894492Z","shell.execute_reply":"2023-08-23T01:04:58.002771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">Data split to validation and training\n</span>**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Assuming target_cols is a list of your target columns and 'stratify_col' exists in df_train\nX_train, X_val, y_train, y_val = train_test_split(df_train_stratify[['patient_id', 'series_id']], \n                                                  df_train_stratify[CFG.target_col], \n                                                  stratify=df_train_stratify['stratify_col'], \n                                                  test_size=0.5)  # small training size to see if this will work","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:58.005127Z","iopub.execute_input":"2023-08-23T01:04:58.00551Z","iopub.status.idle":"2023-08-23T01:04:58.022593Z","shell.execute_reply.started":"2023-08-23T01:04:58.005428Z","shell.execute_reply":"2023-08-23T01:04:58.021451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n**<span style=\"color:#023e8a;\">we can adjust here to import more dcm images.\nGenerate all the paths and its corresponding labels in train. As of now i am grabbing every 80th images to ensure small sample size.\n</span>**\n","metadata":{}},{"cell_type":"markdown","source":"Prepare for training dataset","metadata":{}},{"cell_type":"code","source":"paths = []\nlabels = []\nfor i in tqdm(range(X_train.shape[0])):\n    for f in sorted(glob.glob(directory + f\"/{X_train.iloc[i,0]}/{X_train.iloc[i,1]}/*.dcm\"))[::80]:\n            paths.append(f)\n            labels.append(y_train.iloc[i,:].values)\n\nlabels = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:04:58.024199Z","iopub.execute_input":"2023-08-23T01:04:58.024837Z","iopub.status.idle":"2023-08-23T01:05:00.105163Z","shell.execute_reply.started":"2023-08-23T01:04:58.0248Z","shell.execute_reply":"2023-08-23T01:05:00.104237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n**<span style=\"color:#023e8a;\"> Generate all the paths and its corresponding labels in validation dataset\n</span>**","metadata":{}},{"cell_type":"code","source":"# len(paths) # this will be number of dcm images for training\nprint(f\"we have {len(paths)} images in training \")","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:00.10677Z","iopub.execute_input":"2023-08-23T01:05:00.107375Z","iopub.status.idle":"2023-08-23T01:05:00.112548Z","shell.execute_reply.started":"2023-08-23T01:05:00.107341Z","shell.execute_reply":"2023-08-23T01:05:00.111652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_paths = []\nval_labels = []\nfor i in tqdm(range(X_val.shape[0])):\n    for f in sorted(glob.glob(directory + f\"/{X_val.iloc[i,0]}/{X_val.iloc[i,1]}/*.dcm\"))[::80]:\n            val_paths.append(f)\n            val_labels.append(y_val.iloc[i,:].values)\n\nval_labels = np.array(val_labels)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:00.113951Z","iopub.execute_input":"2023-08-23T01:05:00.114528Z","iopub.status.idle":"2023-08-23T01:05:02.355334Z","shell.execute_reply.started":"2023-08-23T01:05:00.114495Z","shell.execute_reply":"2023-08-23T01:05:02.354367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# len(paths) # this will be number of dcm images for training\nprint(f\"we have {len(val_paths)} images in training \")","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:02.36078Z","iopub.execute_input":"2023-08-23T01:05:02.361701Z","iopub.status.idle":"2023-08-23T01:05:02.367407Z","shell.execute_reply.started":"2023-08-23T01:05:02.361665Z","shell.execute_reply":"2023-08-23T01:05:02.365999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_decoder(with_labels=True, target_size=CFG.img_size, ext='png'):\n    \n    def read_dicom_file(path):\n        path = path.numpy().decode('utf-8')\n        dicom = pydicom.dcmread(path) # i was missing .numpy() since path is a tensor\n        pixels = standardize_pixel_array(dicom)\n        return np.array(pixels)\n    \n    @tf.function\n    def decode_image(path):\n        #this is where path become tensor\n#         image = decode_dicom_image(image)\n        pixels = tf.py_function(read_dicom_file, [path], tf.float32)\n#         img = (img - img.min()) / (img.max() - img.min() + 1e-6)\n        img = tf.convert_to_tensor(pixels, dtype=tf.float32)\n        img = tf.cast(img, tf.float32) / 255.0\n        img = tf.expand_dims(img, axis=-1)\n        img = tf.tile(img, [1, 1, 3])\n        img = tf.image.resize(img, target_size, method='bilinear')\n        \n        return img\n    \n    def decode_label(label):\n        label = tf.cast(label, tf.float32)\n        return (label[0:1], label[1:2], label[2:5], label[5:8], label[8:11])\n    \n    def decode_with_labels(path, label):\n        return decode_image(path), decode_label(label)\n    \n    return decode_with_labels if with_labels else decode_image\n\n\ndef build_augmenter(with_labels=True, dim=CFG.img_size):\n    def augment(img, dim=dim):\n        if random_float() < CFG.transform:\n            img = transform(img,DIM=dim)\n        img = tf.image.random_flip_left_right(img) if CFG.hflip else img\n        img = tf.image.random_flip_up_down(img) if CFG.vflip else img\n        if random_float() < CFG.pixel_aug:\n            img = tf.image.random_hue(img, CFG.hue)\n            img = tf.image.random_saturation(img, CFG.sat[0], CFG.sat[1])\n            img = tf.image.random_contrast(img, CFG.cont[0], CFG.cont[1])\n            img = tf.image.random_brightness(img, CFG.bri)\n        img = tf.clip_by_value(img, 0, 1)  if CFG.clip else img         \n        img = tf.reshape(img, [*dim, 3])\n        return img\n    \n    def augment_with_labels(img, label):    \n        return augment(img), label\n    \n    return augment_with_labels if with_labels else augment\n\n\ndef build_dataset(paths, labels=None, batch_size=32, cache=True,\n                  decode_fn=None, augment_fn=None,\n                  augment=True, repeat=True, shuffle=1024, \n                  cache_dir=\"\", drop_remainder=False):\n    \n    if cache_dir != \"\" and cache is True:\n        os.makedirs(cache_dir, exist_ok=True)\n    \n    if decode_fn is None:\n        decode_fn = build_decoder(labels is not None)\n    \n    if augment_fn is None:\n        augment_fn = build_augmenter(labels is not None)\n    \n    AUTO = tf.data.experimental.AUTOTUNE\n    slices = paths if labels is None else (paths, labels)\n    \n    ds = tf.data.Dataset.from_tensor_slices(slices)\n    ds = ds.map(decode_fn, num_parallel_calls=AUTO)\n    ds = ds.cache(cache_dir) if cache else ds\n    ds = ds.repeat() if repeat else ds\n    if shuffle: \n        ds = ds.shuffle(shuffle, seed=CFG.seed)\n        opt = tf.data.Options()\n        opt.experimental_deterministic = False\n        ds = ds.with_options(opt)\n    ds = ds.map(augment_fn, num_parallel_calls=AUTO) if augment else ds\n    if augment and labels is not None:\n        ds = ds.map(lambda img, label: (dropout(img, \n                                               DIM=CFG.img_size, \n                                               PROBABILITY=CFG.drop_prob, \n                                               CT=CFG.drop_cnt,\n                                               SZ=CFG.drop_size), label),num_parallel_calls=AUTO)\n    ds = ds.batch(batch_size, drop_remainder=drop_remainder)\n    if augment and labels is not None:\n        if CFG.cutmix_prob:\n            ds = ds.map(get_cutmix(alpha=CFG.cutmix_alpha,prob=CFG.cutmix_prob),num_parallel_calls=AUTO)\n        if CFG.mixup_prob:\n            ds = ds.map(get_mixup(alpha=CFG.mixup_alpha,prob=CFG.mixup_prob),num_parallel_calls=AUTO)\n    ds = ds.prefetch(AUTO)\n    return ds","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:02.369154Z","iopub.execute_input":"2023-08-23T01:05:02.369873Z","iopub.status.idle":"2023-08-23T01:05:02.394576Z","shell.execute_reply.started":"2023-08-23T01:05:02.369839Z","shell.execute_reply":"2023-08-23T01:05:02.393431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Augmentation code","metadata":{}},{"cell_type":"code","source":"def random_int(shape=[], minval=0, maxval=1):\n    return tf.random.uniform(\n        shape=shape, minval=minval, maxval=maxval, dtype=tf.int32)\n\n\ndef random_float(shape=[], minval=0.0, maxval=1.0):\n    rnd = tf.random.uniform(\n        shape=shape, minval=minval, maxval=maxval, dtype=tf.float32)\n    return rnd\n\n# mixup\ndef get_mixup(alpha=0.2, prob=0.5):\n    @tf.function\n    def mixup(images, labels, alpha=alpha, prob=prob):\n        if random_float() > prob:\n            return images, labels\n\n        image_shape = tf.shape(images)\n        label_shape = tf.shape(labels)\n\n        beta = tfp.distributions.Beta(alpha, alpha)\n        lam = beta.sample(1)[0]\n\n        images = lam * images + (1.0 - lam) * tf.roll(images, shift=1, axis=0)\n        labels = lam * labels + (1.0 - lam) * tf.roll(labels, shift=1, axis=0)\n\n        images = tf.reshape(images, image_shape)\n        labels = tf.reshape(labels, label_shape)\n        return images, labels\n    return mixup\n\n# cutmix\ndef get_cutmix(alpha, prob=0.5):\n    @tf.function\n    def cutmix(images, labels, alpha=alpha, prob=prob):\n        if random_float() > prob:\n            return images, labels\n        image_shape = tf.shape(images)\n        label_shape = tf.shape(labels)\n        \n        W = tf.cast(image_shape[2], tf.int32)\n        H = tf.cast(image_shape[1], tf.int32)\n\n        beta = tfp.distributions.Beta(alpha, alpha)\n        lam = beta.sample(1)[0]\n\n        images_rolled = tf.roll(images, shift=1, axis=0)\n        labels_rolled = tf.roll(labels, shift=1, axis=0)\n\n        r_x = random_int([], minval=0, maxval=W)\n        r_y = random_int([], minval=0, maxval=H)\n        r = 0.5 * tf.math.sqrt(1.0 - lam)\n        r_w_half = tf.cast(r * tf.cast(W, tf.float32), tf.int32)\n        r_h_half = tf.cast(r * tf.cast(H, tf.float32), tf.int32)\n\n        x1 = tf.cast(tf.clip_by_value(r_x - r_w_half, 0, W), tf.int32)\n        x2 = tf.cast(tf.clip_by_value(r_x + r_w_half, 0, W), tf.int32)\n        y1 = tf.cast(tf.clip_by_value(r_y - r_h_half, 0, H), tf.int32)\n        y2 = tf.cast(tf.clip_by_value(r_y + r_h_half, 0, H), tf.int32)\n\n        # outer-pad patch -> [0, 0, 1, 1, 0, 0]\n        patch1 = images[:, y1:y2, x1:x2, :]  # [batch, height, width, channel]\n        patch1 = tf.pad(\n            patch1, [[0, 0], [y1, H - y2], [x1, W - x2], [0, 0]])  # outer-pad\n\n        # inner-pad patch -> [1, 1, 0, 0, 1, 1]\n        patch2 = images_rolled[:, y1:y2, x1:x2, :]\n        patch2 = tf.pad(\n            patch2, [[0, 0], [y1, H - y2], [x1, W - x2], [0, 0]])  # outer-pad\n        patch2 = images_rolled - patch2  # inner-pad = img - outer-pad\n\n        images = patch1 + patch2  # cutmix img\n\n        lam = tf.cast((1.0 - (x2 - x1) * (y2 - y1) / (W * H)), tf.float32)  # no H as (y1 - y2)/H = 1\n        labels = lam * labels + (1.0 - lam) * labels_rolled  # cutmix label\n\n        images = tf.reshape(images, image_shape)\n        labels = tf.reshape(labels, label_shape)\n\n        return images, labels\n\n    return cutmix","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:02.396024Z","iopub.execute_input":"2023-08-23T01:05:02.396363Z","iopub.status.idle":"2023-08-23T01:05:02.41801Z","shell.execute_reply.started":"2023-08-23T01:05:02.39633Z","shell.execute_reply":"2023-08-23T01:05:02.417091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Augmentation code","metadata":{}},{"cell_type":"code","source":"def get_mat(shear, height_zoom, width_zoom, height_shift, width_shift):\n    # returns 3x3 transformmatrix which transforms indicies\n        \n    # CONVERT DEGREES TO RADIANS\n    #rotation = math.pi * rotation / 180.\n    shear    = math.pi * shear    / 180.\n\n    def get_3x3_mat(lst):\n        return tf.reshape(tf.concat([lst],axis=0), [3,3])\n    \n    # ROTATION MATRIX\n#     c1   = tf.math.cos(rotation)\n#     s1   = tf.math.sin(rotation)\n    one  = tf.constant([1],dtype='float32')\n    zero = tf.constant([0],dtype='float32')\n    \n#     rotation_matrix = get_3x3_mat([c1,   s1,   zero, \n#                                    -s1,  c1,   zero, \n#                                    zero, zero, one])    \n    # SHEAR MATRIX\n    c2 = tf.math.cos(shear)\n    s2 = tf.math.sin(shear)    \n    \n    shear_matrix = get_3x3_mat([one,  s2,   zero, \n                               zero, c2,   zero, \n                                zero, zero, one])        \n    # ZOOM MATRIX\n    zoom_matrix = get_3x3_mat([one/height_zoom, zero,           zero, \n                               zero,            one/width_zoom, zero, \n                               zero,            zero,           one])    \n    # SHIFT MATRIX\n    shift_matrix = get_3x3_mat([one,  zero, height_shift, \n                                zero, one,  width_shift, \n                                zero, zero, one])\n    \n\n    return  K.dot(shear_matrix,K.dot(zoom_matrix, shift_matrix)) #K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))                  \n\ndef transform(image, DIM=CFG.img_size):#[rot,shr,h_zoom,w_zoom,h_shift,w_shift]):\n    if DIM[0]>DIM[1]:\n        diff  = (DIM[0]-DIM[1])\n        pad   = [diff//2, diff//2 + diff%2]\n        image = tf.pad(image, [[0, 0], [pad[0], pad[1]],[0, 0]])\n        NEW_DIM = DIM[0]\n    elif DIM[0]<DIM[1]:\n        diff  = (DIM[1]-DIM[0])\n        pad   = [diff//2, diff//2 + diff%2]\n        image = tf.pad(image, [[pad[0], pad[1]], [0, 0],[0, 0]])\n        NEW_DIM = DIM[1]\n    \n    rot = CFG.rot * tf.random.normal([1], dtype='float32')\n    shr = CFG.shr * tf.random.normal([1], dtype='float32') \n    h_zoom = 1.0 + tf.random.normal([1], dtype='float32') / CFG.hzoom\n    w_zoom = 1.0 + tf.random.normal([1], dtype='float32') / CFG.wzoom\n    h_shift = CFG.hshift * tf.random.normal([1], dtype='float32') \n    w_shift = CFG.wshift * tf.random.normal([1], dtype='float32') \n    \n    transformation_matrix=tf.linalg.inv(get_mat(shr,h_zoom,w_zoom,h_shift,w_shift))\n    \n    flat_tensor=tfa.image.transform_ops.matrices_to_flat_transforms(transformation_matrix)\n    \n    image=tfa.image.transform(image,flat_tensor, fill_mode=CFG.fill_mode)\n    \n    rotation = math.pi * rot / 180.\n    \n    image=tfa.image.rotate(image,-rotation, fill_mode=CFG.fill_mode)\n    \n    if DIM[0]>DIM[1]:\n        image=tf.reshape(image, [NEW_DIM, NEW_DIM,3])\n        image = image[:, pad[0]:-pad[1],:]\n    elif DIM[1]>DIM[0]:\n        image=tf.reshape(image, [NEW_DIM, NEW_DIM,3])\n        image = image[pad[0]:-pad[1],:,:]\n    image = tf.reshape(image, [*DIM, 3])    \n    return image\n\ndef dropout(image,DIM=CFG.img_size, PROBABILITY = 0.6, CT = 5, SZ = 0.1):\n    # input image - is one image of size [dim,dim,3] not a batch of [b,dim,dim,3]\n    # output - image with CT squares of side size SZ*DIM removed\n    \n    # DO DROPOUT WITH PROBABILITY DEFINED ABOVE\n    P = tf.cast( tf.random.uniform([],0,1)<PROBABILITY, tf.int32)\n    if (P==0)|(CT==0)|(SZ==0): \n        return image\n    \n    for k in range(CT):\n        # CHOOSE RANDOM LOCATION\n        x = tf.cast( tf.random.uniform([],0,DIM[1]),tf.int32)\n        y = tf.cast( tf.random.uniform([],0,DIM[0]),tf.int32)\n        # COMPUTE SQUARE \n        WIDTH = tf.cast( SZ*min(DIM),tf.int32) * P\n        ya = tf.math.maximum(0,y-WIDTH//2)\n        yb = tf.math.minimum(DIM[0],y+WIDTH//2)\n        xa = tf.math.maximum(0,x-WIDTH//2)\n        xb = tf.math.minimum(DIM[1],x+WIDTH//2)\n        # DROPOUT IMAGE\n        one = image[ya:yb,0:xa,:]\n        two = tf.zeros([yb-ya,xb-xa,3], dtype = image.dtype) \n        three = image[ya:yb,xb:DIM[1],:]\n        middle = tf.concat([one,two,three],axis=1)\n        image = tf.concat([image[0:ya,:,:],middle,image[yb:DIM[0],:,:]],axis=0)\n        image = tf.reshape(image,[*DIM,3])\n\n#     image = tf.reshape(image,[*DIM,3])\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:02.419542Z","iopub.execute_input":"2023-08-23T01:05:02.419914Z","iopub.status.idle":"2023-08-23T01:05:02.446734Z","shell.execute_reply.started":"2023-08-23T01:05:02.419857Z","shell.execute_reply":"2023-08-23T01:05:02.445857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = build_dataset(paths, labels, cache=True, batch_size=CFG.batch_size,\n                   repeat=True, shuffle=True, augment=True)\n# ds = ds.unbatch().batch(20)\nbatch = next(iter(ds))","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:02.448144Z","iopub.execute_input":"2023-08-23T01:05:02.448486Z","iopub.status.idle":"2023-08-23T01:05:05.383833Z","shell.execute_reply.started":"2023-08-23T01:05:02.448452Z","shell.execute_reply":"2023-08-23T01:05:05.382809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_ds = build_dataset(val_paths, val_labels, cache=True, batch_size=CFG.batch_size,\n                   repeat=True, shuffle=True, augment=True)\n# val_ds = val_ds.unbatch().batch(20)\nval_batch = next(iter(val_ds))","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:05.387696Z","iopub.execute_input":"2023-08-23T01:05:05.388619Z","iopub.status.idle":"2023-08-23T01:05:07.342714Z","shell.execute_reply.started":"2023-08-23T01:05:05.388583Z","shell.execute_reply":"2023-08-23T01:05:07.341698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_batch(batch, size=2):\n    if isinstance(batch, tuple):\n        imgs, tars = batch\n        tars = tf.concat(tars,axis=-1).numpy()\n    else:\n        imgs = batch\n        tars = None\n    plt.figure(figsize=(size*5, 10))\n    for img_idx in range(size):\n        plt.subplot(1, size, img_idx+1)\n        if tars is not None:\n            plt.title(f'{tars[img_idx].round(2)}', fontsize=12)\n        img = imgs[img_idx,]\n        plt.imshow(img)\n        plt.xticks([]); plt.yticks([])\n    plt.tight_layout()\n    plt.show() ","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:07.344071Z","iopub.execute_input":"2023-08-23T01:05:07.345078Z","iopub.status.idle":"2023-08-23T01:05:07.352441Z","shell.execute_reply.started":"2023-08-23T01:05:07.34505Z","shell.execute_reply":"2023-08-23T01:05:07.351182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_batch(batch, 5);","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:07.354208Z","iopub.execute_input":"2023-08-23T01:05:07.354812Z","iopub.status.idle":"2023-08-23T01:05:08.168179Z","shell.execute_reply.started":"2023-08-23T01:05:07.35478Z","shell.execute_reply":"2023-08-23T01:05:08.163662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_batch(val_batch, 5);","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:08.169828Z","iopub.execute_input":"2023-08-23T01:05:08.170463Z","iopub.status.idle":"2023-08-23T01:05:08.95831Z","shell.execute_reply.started":"2023-08-23T01:05:08.17043Z","shell.execute_reply":"2023-08-23T01:05:08.957432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras_cv_attention_models import efficientnet\n\ndef build_model(model_name=CFG.model_name,\n                loss_name=CFG.loss,\n                dim=CFG.img_size,\n                compile_model=True,\n                include_top=False):         \n    \n    # Define backbone\n    base = getattr(efficientnet, model_name)(input_shape=(*dim,3),\n                                    pretrained='imagenet',\n                                    num_classes=0) # get base model (efficientnet), use imgnet weights\n\n    inp = base.inputs\n    x = base.output\n    x = tf.keras.layers.GlobalAveragePooling2D()(x) # use GAP to get pooling result form conv outputs\n\n    # Define 'necks' for each head\n    x_bowel = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_extra = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_liver = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_kidney = tf.keras.layers.Dense(32, activation='silu')(x)\n    x_spleen = tf.keras.layers.Dense(32, activation='silu')(x)\n\n    # Define heads\n    out_bowel = tf.keras.layers.Dense(1, name='bowel', activation='sigmoid')(x_bowel) # use sigmoid to convert predictions to [0-1]\n    out_extra = tf.keras.layers.Dense(1, name='extra', activation='sigmoid')(x_extra) # use sigmoid to convert predictions to [0-1]\n    out_liver = tf.keras.layers.Dense(3, name='liver', activation='softmax')(x_liver) # use softmax for the liver head\n    out_kidney = tf.keras.layers.Dense(3, name='kidney', activation='softmax')(x_kidney) # use softmax for the kidney head\n    out_spleen = tf.keras.layers.Dense(3, name='spleen', activation='softmax')(x_spleen) # use softmax for the spleen head\n\n    # Combine outputs\n#     out = tf.keras.layers.Concatenate()([out_bowel, out_extra, \n#                                          out_liver, out_kidney, out_spleen])\n    out = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n\n    # Create model\n    model = tf.keras.Model(inputs=inp, outputs=out)\n\n    \n    if compile_model:\n        # optimizer\n        opt = tf.keras.optimizers.Adam(learning_rate=0.0001)\n        # loss\n        loss = {\n            'bowel':tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05),\n            'extra':tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05),\n            'liver':tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.05),\n            'kidney':tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.05),\n            'spleen':tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.05),\n        }\n        # metric\n        metrics = {\n            'bowel':['accuracy'],\n            'extra':['accuracy'],\n            'liver':['accuracy'],\n            'kidney':['accuracy'],\n            'spleen':['accuracy'],\n        }\n        # compile\n        model.compile(optimizer=opt,\n                      loss=loss,\n                      metrics=metrics)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:08.961476Z","iopub.execute_input":"2023-08-23T01:05:08.961861Z","iopub.status.idle":"2023-08-23T01:05:08.98003Z","shell.execute_reply.started":"2023-08-23T01:05:08.961827Z","shell.execute_reply":"2023-08-23T01:05:08.97876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if \"TPU\" in CFG.device:\n    tpu = 'local' if CFG.device=='TPU-VM' else None\n    print(\"connecting to TPU...\")\n    try:\n        tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect(tpu=tpu)\n        strategy = tf.distribute.TPUStrategy(tpu)\n    except:\n        CFG.device = \"GPU\"\n        \nif CFG.device == \"GPU\"  or CFG.device==\"CPU\":\n    ngpu = len(tf.config.experimental.list_physical_devices('GPU'))\n    if ngpu>1:\n        print(\"Using multi GPU\")\n        strategy = tf.distribute.MirroredStrategy()\n    elif ngpu==1:\n        print(\"Using single GPU\")\n        strategy = tf.distribute.get_strategy()\n    else:\n        print(\"Using CPU\")\n        strategy = tf.distribute.get_strategy()\n        CFG.device = \"CPU\"\n\nif CFG.device == \"GPU\":\n    print(\"Num GPUs Available: \", ngpu)\n    \n\nAUTO     = tf.data.experimental.AUTOTUNE\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:08.982104Z","iopub.execute_input":"2023-08-23T01:05:08.982572Z","iopub.status.idle":"2023-08-23T01:05:09.390753Z","shell.execute_reply.started":"2023-08-23T01:05:08.982449Z","shell.execute_reply":"2023-08-23T01:05:09.389765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:09.392506Z","iopub.execute_input":"2023-08-23T01:05:09.392848Z","iopub.status.idle":"2023-08-23T01:05:09.397502Z","shell.execute_reply.started":"2023-08-23T01:05:09.392815Z","shell.execute_reply":"2023-08-23T01:05:09.396428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_lr_callback(batch_size=8, plot=False):\n    lr_start   = 0.000005\n    lr_max     = 0.00000050 * REPLICAS * batch_size\n    lr_min     = 0.000001\n    lr_ramp_ep = 4\n    lr_sus_ep  = 0\n    lr_decay   = 0.8\n   \n    def lrfn(epoch):\n        if epoch < lr_ramp_ep:\n            lr = (lr_max - lr_start) / lr_ramp_ep * epoch + lr_start\n            \n        elif epoch < lr_ramp_ep + lr_sus_ep:\n            lr = lr_max\n            \n        elif CFG.scheduler=='exp':\n            lr = (lr_max - lr_min) * lr_decay**(epoch - lr_ramp_ep - lr_sus_ep) + lr_min\n            \n        elif CFG.scheduler=='cosine':\n            decay_total_epochs = CFG.epochs - lr_ramp_ep - lr_sus_ep + 3\n            decay_epoch_index = epoch - lr_ramp_ep - lr_sus_ep\n            phase = math.pi * decay_epoch_index / decay_total_epochs\n            cosine_decay = 0.4 * (1 + math.cos(phase))\n            lr = (lr_max - lr_min) * cosine_decay + lr_min\n        return lr\n    if plot:\n        plt.figure(figsize=(10,5))\n        plt.plot(np.arange(CFG.epochs), [lrfn(epoch) for epoch in np.arange(CFG.epochs)], marker='o')\n        plt.xlabel('epoch'); plt.ylabel('learnig rate')\n        plt.title('Learning Rate Scheduler')\n        plt.show()\n\n    lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)\n    return lr_callback\n\n_=get_lr_callback(CFG.batch_size, plot=True )","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:09.399301Z","iopub.execute_input":"2023-08-23T01:05:09.399746Z","iopub.status.idle":"2023-08-23T01:05:09.752743Z","shell.execute_reply.started":"2023-08-23T01:05:09.399713Z","shell.execute_reply":"2023-08-23T01:05:09.751823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model(CFG.model_name, dim=CFG.img_size, compile_model=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:09.753986Z","iopub.execute_input":"2023-08-23T01:05:09.754584Z","iopub.status.idle":"2023-08-23T01:05:12.395595Z","shell.execute_reply.started":"2023-08-23T01:05:09.754539Z","shell.execute_reply":"2023-08-23T01:05:12.394714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(paths)/CFG.batch_size//REPLICAS","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:12.39685Z","iopub.execute_input":"2023-08-23T01:05:12.39723Z","iopub.status.idle":"2023-08-23T01:05:12.403886Z","shell.execute_reply.started":"2023-08-23T01:05:12.397196Z","shell.execute_reply":"2023-08-23T01:05:12.402836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = []\nmodel.fit(\n        ds, \n        epochs=2, \n        callbacks = callbacks, \n        steps_per_epoch=len(paths)/CFG.batch_size//REPLICAS,\n        validation_data=val_ds, \n        validation_steps=len(paths)/CFG.batch_size//REPLICAS,\n        verbose=1\n)\n\nmodel.save('/kaggle/working/CNN_v1.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:05:12.405396Z","iopub.execute_input":"2023-08-23T01:05:12.406101Z","iopub.status.idle":"2023-08-23T01:13:54.261218Z","shell.execute_reply.started":"2023-08-23T01:05:12.406069Z","shell.execute_reply":"2023-08-23T01:13:54.259097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = \"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_images\"\ntest_patient_ids = []\ntest_series_ids = []\nfor patient_id in tqdm(os.listdir(test_path)):\n    for series_id in os.listdir(os.path.join(test_path,patient_id)):\n        test_patient_ids.append(patient_id)\n        test_series_ids.append(series_id)\n        \n        \ntest = pd.DataFrame({\n    'patient_id': test_patient_ids,\n    'series_id': test_series_ids\n})\ntest['patient_id'] = test['patient_id'].astype('int64')\ntest['series_id'] = test['series_id'].astype('int64')\n","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:54.265237Z","iopub.execute_input":"2023-08-23T01:13:54.265588Z","iopub.status.idle":"2023-08-23T01:13:54.329938Z","shell.execute_reply.started":"2023-08-23T01:13:54.265562Z","shell.execute_reply":"2023-08-23T01:13:54.328932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_paths = []\n\nfor i in tqdm(range(test.shape[0])):\n    for f in sorted(glob.glob(test_path + f\"/{test.iloc[i,0]}/{test.iloc[i,1]}/*.dcm\")):\n        test_paths.append(f)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:54.331476Z","iopub.execute_input":"2023-08-23T01:13:54.332227Z","iopub.status.idle":"2023-08-23T01:13:54.354783Z","shell.execute_reply.started":"2023-08-23T01:13:54.332185Z","shell.execute_reply":"2023-08-23T01:13:54.353867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:54.356118Z","iopub.execute_input":"2023-08-23T01:13:54.35668Z","iopub.status.idle":"2023-08-23T01:13:54.37188Z","shell.execute_reply.started":"2023-08-23T01:13:54.356647Z","shell.execute_reply":"2023-08-23T01:13:54.370957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_paths","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:54.375502Z","iopub.execute_input":"2023-08-23T01:13:54.37576Z","iopub.status.idle":"2023-08-23T01:13:54.383888Z","shell.execute_reply.started":"2023-08-23T01:13:54.375737Z","shell.execute_reply":"2023-08-23T01:13:54.382845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds = build_dataset(test_paths,\n                        labels = None, \n                        batch_size=CFG.batch_size,\n                        repeat=False,\n                        shuffle=False,\n                        decode_fn = build_decoder(with_labels = False, target_size=CFG.img_size),\n                        augment=False)\n# ds = ds.unbatch().batch(20)\n# batch = next(iter(test_ds))","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:54.385553Z","iopub.execute_input":"2023-08-23T01:13:54.386374Z","iopub.status.idle":"2023-08-23T01:13:54.702524Z","shell.execute_reply.started":"2023-08-23T01:13:54.386342Z","shell.execute_reply":"2023-08-23T01:13:54.701551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_ds, steps = 1, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:54.704161Z","iopub.execute_input":"2023-08-23T01:13:54.704496Z","iopub.status.idle":"2023-08-23T01:13:56.88032Z","shell.execute_reply.started":"2023-08-23T01:13:54.704463Z","shell.execute_reply":"2023-08-23T01:13:56.879377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.concatenate(pred, axis = -1)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:56.881866Z","iopub.execute_input":"2023-08-23T01:13:56.882311Z","iopub.status.idle":"2023-08-23T01:13:56.887794Z","shell.execute_reply.started":"2023-08-23T01:13:56.882277Z","shell.execute_reply":"2023-08-23T01:13:56.886858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:56.889136Z","iopub.execute_input":"2023-08-23T01:13:56.889827Z","iopub.status.idle":"2023-08-23T01:13:56.903809Z","shell.execute_reply.started":"2023-08-23T01:13:56.889795Z","shell.execute_reply":"2023-08-23T01:13:56.902939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.DataFrame(pred,columns = CFG.target_col)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:56.90508Z","iopub.execute_input":"2023-08-23T01:13:56.905488Z","iopub.status.idle":"2023-08-23T01:13:56.91337Z","shell.execute_reply.started":"2023-08-23T01:13:56.905456Z","shell.execute_reply":"2023-08-23T01:13:56.912155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred['bowel_healthy'] = 1 - pred['bowel_injury']\npred['extravasation_healthy'] = 1 - pred['extravasation_injury']","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:56.914676Z","iopub.execute_input":"2023-08-23T01:13:56.915149Z","iopub.status.idle":"2023-08-23T01:13:56.926489Z","shell.execute_reply.started":"2023-08-23T01:13:56.915116Z","shell.execute_reply":"2023-08-23T01:13:56.925631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.concat([test['patient_id'], pred], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:13:56.928192Z","iopub.execute_input":"2023-08-23T01:13:56.928524Z","iopub.status.idle":"2023-08-23T01:13:56.938033Z","shell.execute_reply.started":"2023-08-23T01:13:56.92849Z","shell.execute_reply":"2023-08-23T01:13:56.93716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:14:48.52979Z","iopub.execute_input":"2023-08-23T01:14:48.530186Z","iopub.status.idle":"2023-08-23T01:14:48.545139Z","shell.execute_reply.started":"2023-08-23T01:14:48.530154Z","shell.execute_reply":"2023-08-23T01:14:48.543956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = submit.merge(submission['patient_id'], how = 'right', left_on = 'patient_id',right_on = 'patient_id')[submission.columns]","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:14:50.587814Z","iopub.execute_input":"2023-08-23T01:14:50.588518Z","iopub.status.idle":"2023-08-23T01:14:50.607617Z","shell.execute_reply.started":"2023-08-23T01:14:50.588485Z","shell.execute_reply":"2023-08-23T01:14:50.606367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:14:52.847997Z","iopub.execute_input":"2023-08-23T01:14:52.848372Z","iopub.status.idle":"2023-08-23T01:14:52.859124Z","shell.execute_reply.started":"2023-08-23T01:14:52.84834Z","shell.execute_reply":"2023-08-23T01:14:52.858086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit","metadata":{"execution":{"iopub.status.busy":"2023-08-23T01:14:58.403095Z","iopub.execute_input":"2023-08-23T01:14:58.403459Z","iopub.status.idle":"2023-08-23T01:14:58.426423Z","shell.execute_reply.started":"2023-08-23T01:14:58.40343Z","shell.execute_reply":"2023-08-23T01:14:58.425112Z"},"trusted":true},"execution_count":null,"outputs":[]}]}