{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:13.42505Z","iopub.execute_input":"2023-10-12T06:04:13.425423Z","iopub.status.idle":"2023-10-12T06:04:13.761094Z","shell.execute_reply.started":"2023-10-12T06:04:13.425392Z","shell.execute_reply":"2023-10-12T06:04:13.760058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install -q git+https://github.com/keras-team/keras-cv\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:03:47.530973Z","iopub.execute_input":"2023-10-12T06:03:47.531325Z","iopub.status.idle":"2023-10-12T06:04:05.385493Z","shell.execute_reply.started":"2023-10-12T06:03:47.531279Z","shell.execute_reply":"2023-10-12T06:04:05.384286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install Pillow\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:03:07.504629Z","iopub.execute_input":"2023-10-12T06:03:07.505266Z","iopub.status.idle":"2023-10-12T06:03:41.993091Z","shell.execute_reply.started":"2023-10-12T06:03:07.505222Z","shell.execute_reply":"2023-10-12T06:03:41.991863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = \"/kaggle/input/rsna-atd-512x512-png-v2-dataset\"\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:07.833865Z","iopub.execute_input":"2023-10-12T06:04:07.834229Z","iopub.status.idle":"2023-10-12T06:04:07.840055Z","shell.execute_reply.started":"2023-10-12T06:04:07.834198Z","shell.execute_reply":"2023-10-12T06:04:07.838741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = f\"{file_path}/train.csv\"\ntrain_df = pd.read_csv(train_csv)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:18.153011Z","iopub.execute_input":"2023-10-12T06:04:18.153505Z","iopub.status.idle":"2023-10-12T06:04:18.231415Z","shell.execute_reply.started":"2023-10-12T06:04:18.153472Z","shell.execute_reply":"2023-10-12T06:04:18.230455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:22.127282Z","iopub.execute_input":"2023-10-12T06:04:22.127654Z","iopub.status.idle":"2023-10-12T06:04:22.160651Z","shell.execute_reply.started":"2023-10-12T06:04:22.127625Z","shell.execute_reply":"2023-10-12T06:04:22.159492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:25.593204Z","iopub.execute_input":"2023-10-12T06:04:25.593586Z","iopub.status.idle":"2023-10-12T06:04:25.60099Z","shell.execute_reply.started":"2023-10-12T06:04:25.593555Z","shell.execute_reply":"2023-10-12T06:04:25.599848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_file = f\"{file_path}/test.csv\"\ntest_df = pd.read_csv(test_file)\ntest_df","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:28.554609Z","iopub.execute_input":"2023-10-12T06:04:28.555675Z","iopub.status.idle":"2023-10-12T06:04:28.574535Z","shell.execute_reply.started":"2023-10-12T06:04:28.555633Z","shell.execute_reply":"2023-10-12T06:04:28.573389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/rsna-atd-512x512-png-v2-dataset/train_images/10004/21057\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:29.62271Z","iopub.execute_input":"2023-10-12T06:04:29.623889Z","iopub.status.idle":"2023-10-12T06:04:30.707905Z","shell.execute_reply.started":"2023-10-12T06:04:29.623844Z","shell.execute_reply":"2023-10-12T06:04:30.706719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_cols = ['bowel_healthy',\n 'bowel_injury',\n 'extravasation_healthy',\n 'extravasation_injury',\n 'kidney_healthy',\n 'kidney_low',\n 'kidney_high',\n 'liver_healthy',\n 'liver_low',\n 'liver_high',\n 'spleen_healthy',\n 'spleen_low',\n 'spleen_high',\n 'any_injury']","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:30.710543Z","iopub.execute_input":"2023-10-12T06:04:30.710941Z","iopub.status.idle":"2023-10-12T06:04:30.716392Z","shell.execute_reply.started":"2023-10-12T06:04:30.710901Z","shell.execute_reply":"2023-10-12T06:04:30.715436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"shape of train files\",train_df.shape)\nprint(\"shape of test files\",test_df.shape)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:31.619435Z","iopub.execute_input":"2023-10-12T06:04:31.619762Z","iopub.status.idle":"2023-10-12T06:04:31.625621Z","shell.execute_reply.started":"2023-10-12T06:04:31.619737Z","shell.execute_reply":"2023-10-12T06:04:31.624641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[0:1000]\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:32.436658Z","iopub.execute_input":"2023-10-12T06:04:32.437334Z","iopub.status.idle":"2023-10-12T06:04:32.44253Z","shell.execute_reply.started":"2023-10-12T06:04:32.437282Z","shell.execute_reply":"2023-10-12T06:04:32.441271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train\ntrain_df['image_path'] = f'{file_path}/train_images'\\\n                    + '/' + train_df.patient_id.astype(str)\\\n                    + '/' + train_df.series_id.astype(str)\\\n                    + '/' + train_df.instance_number.astype(str) +'.png'\n\ntrain_df = train_df.drop_duplicates()\nprint('Train:')\ndisplay(train_df.head(2))\n\n# test\ntest_df['image_path'] = f'{file_path}/test_images'\\\n                    + '/' + test_df.patient_id.astype(str)\\\n                    + '/' + test_df.series_id.astype(str)\\\n                    + '/' + test_df.instance_number.astype(str) +'.png'\ntest_df = test_df.drop_duplicates()\nprint('\\nTest:')\ndisplay(test_df.head(2))","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:33.173867Z","iopub.execute_input":"2023-10-12T06:04:33.17499Z","iopub.status.idle":"2023-10-12T06:04:33.222295Z","shell.execute_reply.started":"2023-10-12T06:04:33.174946Z","shell.execute_reply":"2023-10-12T06:04:33.221262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:34.722781Z","iopub.execute_input":"2023-10-12T06:04:34.723485Z","iopub.status.idle":"2023-10-12T06:04:34.733875Z","shell.execute_reply.started":"2023-10-12T06:04:34.723453Z","shell.execute_reply":"2023-10-12T06:04:34.729862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"shape of train files\",train_df.shape)\nprint(\"shape of test files\",test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:36.50744Z","iopub.execute_input":"2023-10-12T06:04:36.507775Z","iopub.status.idle":"2023-10-12T06:04:36.513097Z","shell.execute_reply.started":"2023-10-12T06:04:36.50775Z","shell.execute_reply":"2023-10-12T06:04:36.511892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ndef preprocess_image(image_path, target_size=(224, 224)):\n    # Read the image file\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_image(image, channels=3)\n    image = tf.image.resize(image, target_size)\n    image = tf.cast(image, tf.float32) / 255.0\n    \n    return image","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:37.973099Z","iopub.execute_input":"2023-10-12T06:04:37.973468Z","iopub.status.idle":"2023-10-12T06:04:45.670299Z","shell.execute_reply.started":"2023-10-12T06:04:37.973439Z","shell.execute_reply":"2023-10-12T06:04:45.669048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import StratifiedGroupKFold\n\nnum_folds = 3\nrandom_seed = 42\nskf = StratifiedGroupKFold(n_splits=num_folds, shuffle=True, random_state=random_seed)\n\ntrain_df['stratify'] = ''\nfor col in train_df[target_cols]:\n    train_df['stratify'] += train_df[col].astype(str)\n\ntrain_df = train_df.reset_index(drop=True)\n\ntrain_df['fold'] = -1  # Initialize a 'fold' column\n\n# Assuming 'target_cols' is your list of target column names, and 'patient_id' is the column representing patient IDs\nfor fold, (train_idx, val_idx) in enumerate(skf.split(train_df, train_df['stratify'], train_df['patient_id'])):\n    train_df.loc[val_idx, 'fold'] = fold\n\ndisplay(train_df.groupby(['fold', 'patient_id']).size())","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:45.672287Z","iopub.execute_input":"2023-10-12T06:04:45.673251Z","iopub.status.idle":"2023-10-12T06:04:46.074354Z","shell.execute_reply.started":"2023-10-12T06:04:45.673209Z","shell.execute_reply":"2023-10-12T06:04:46.073231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nclass Config:\n    SEED = 42\n    IMAGE_SIZE = [256, 256]\n    BATCH_SIZE = 64\n    EPOCHS = 10\n    TARGET_COLS  = [\n        \"bowel_injury\", \"bowel_healthy\",\"extravasation_injury\",\n        \"extravasation_healthy\",\n        \"kidney_healthy\", \"kidney_low\", \"kidney_high\",\n        \"liver_healthy\", \"liver_low\", \"liver_high\",\n        \"spleen_healthy\", \"spleen_low\", \"spleen_high\"\n    ]\n    AUTOTUNE = tf.data.AUTOTUNE\n\nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:46.076205Z","iopub.execute_input":"2023-10-12T06:04:46.076987Z","iopub.status.idle":"2023-10-12T06:04:46.083932Z","shell.execute_reply.started":"2023-10-12T06:04:46.076948Z","shell.execute_reply":"2023-10-12T06:04:46.082852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n\nimport keras_cv\nimport keras_core as keras\nfrom keras_core import layers\n\n\n\ndef decode_image_and_label(image_path, label):\n    file_bytes = tf.io.read_file(image_path)\n    image = tf.io.decode_png(file_bytes, channels=3, dtype=tf.uint8)\n    image = tf.image.resize(image, config.IMAGE_SIZE, method=\"bilinear\")\n    image = tf.cast(image, tf.float32) / 255.0\n    \n    label = tf.cast(label, tf.float32)\n    #         bowel       fluid       kidney      liver       spleen\n    labels = (label[0:2], label[2:4], label[4:7], label[7:10], label[10:13])\n    \n    return (image, labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:46.0872Z","iopub.execute_input":"2023-10-12T06:04:46.087969Z","iopub.status.idle":"2023-10-12T06:04:51.755844Z","shell.execute_reply.started":"2023-10-12T06:04:46.087932Z","shell.execute_reply":"2023-10-12T06:04:51.754863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef apply_augmentation(images, labels):\n    augmenter = keras.Sequential(\n        \n        layers=[\n            keras_cv.layers.RandomFlip(mode=\"horizontal_and_vertical\"),\n            keras_cv.layers.RandomCutout(height_factor=0.2, width_factor=0.2),\n            \n        ]\n    )\n    aug = augmenter(images)\n    return (aug, labels)\n\ndef aug(image,labels):\n    #image = preprocessing.Rescaling(1.0 / 255)(image)  # Rescale pixel values\n    image = preprocessing.RandomFlip(mode=\"horizontal_and_vertical\")(image)\n    image = preprocessing.RandomRotation(factor=0.2)(image)\n    #image = preprocessing.RandomZoom(height_factor=(0.8, 1.2), width_factor=(0.8, 1.2))(image)\n    #image = preprocessing.RandomContrast(factor=0.2)(image)\n    image = preprocessing.RandomTranslation(height_factor=0.2, width_factor=0.2)(image)\n    return (image, labels)\n\n\ndef build_dataset(image_paths, labels):\n    ds = (\n        tf.data.Dataset.from_tensor_slices((image_paths, labels))\n        .map(decode_image_and_label, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(config.BATCH_SIZE)\n        .map(apply_augmentation, num_parallel_calls=config.AUTOTUNE)\n        .prefetch(config.AUTOTUNE)\n    )\n    return ds","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:51.757414Z","iopub.execute_input":"2023-10-12T06:04:51.758386Z","iopub.status.idle":"2023-10-12T06:04:51.766801Z","shell.execute_reply.started":"2023-10-12T06:04:51.758345Z","shell.execute_reply":"2023-10-12T06:04:51.765807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to handle the split for each group\nfrom sklearn.model_selection import train_test_split\ndef split_group(group, test_size=0.2):\n    if len(group) == 1:\n        return (group, pd.DataFrame()) if np.random.rand() < test_size else (pd.DataFrame(), group)\n    else:\n        return train_test_split(group, test_size=test_size, random_state=42)\n\n# Initialize the train and validation datasets\ntrain_data = pd.DataFrame()\nval_data = pd.DataFrame()\n\n# Iterate through the groups and split them, handling single-sample groups\nfor _, group in train_df.groupby(config.TARGET_COLS):\n    train_group, val_group = split_group(group)\n    train_data = pd.concat([train_data, train_group], ignore_index=True)\n    val_data = pd.concat([val_data, val_group], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:51.768247Z","iopub.execute_input":"2023-10-12T06:04:51.768908Z","iopub.status.idle":"2023-10-12T06:04:51.819847Z","shell.execute_reply.started":"2023-10-12T06:04:51.768867Z","shell.execute_reply":"2023-10-12T06:04:51.818816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths  = train_data.image_path.tolist()\nlabels = train_data[config.TARGET_COLS].values\n\nds = build_dataset(image_paths=paths, labels=labels)\nimages, labels = next(iter(ds))\nimages.shape, [label.shape for label in labels]\n\n\n# get image_paths and labels\nprint(\"[INFO] Building the dataset...\")\ntrain_paths = train_data.image_path.values; train_labels = train_data[config.TARGET_COLS].values.astype(np.float32)\nvalid_paths = val_data.image_path.values; valid_labels = val_data[config.TARGET_COLS].values.astype(np.float32)\n\n# train and valid dataset\ntrain_ds = build_dataset(image_paths=train_paths, labels=train_labels)\nval_ds = build_dataset(image_paths=valid_paths, labels=valid_labels)\n\ntotal_train_steps = train_ds.cardinality().numpy() * config.BATCH_SIZE * config.EPOCHS\nwarmup_steps = int(total_train_steps * 0.10)\ndecay_steps = total_train_steps - warmup_steps\n\nprint(f\"{total_train_steps=}\")\nprint(f\"{warmup_steps=}\")\nprint(f\"{decay_steps=}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:04:51.821527Z","iopub.execute_input":"2023-10-12T06:04:51.822265Z","iopub.status.idle":"2023-10-12T06:05:17.378241Z","shell.execute_reply.started":"2023-10-12T06:04:51.822222Z","shell.execute_reply":"2023-10-12T06:05:17.377204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nimport pandas as pd\nfrom sklearn.model_selection import KFold\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom sklearn.metrics import accuracy_score\n\n\n# Define the model\ndef create_cnn_model():\n    # Input layer\n    input_layer = layers.Input(shape=(image_height, image_width, num_channels))\n    \n    # Convolutional layers\n    x = layers.Conv2D(32, (3, 3), activation='relu')(input_layer)\n    x = layers.MaxPooling2D((2, 2))(x)\n    x = layers.Conv2D(64, (3, 3), activation='relu')(x)\n    x = layers.MaxPooling2D((2, 2))(x)\n    x = layers.Conv2D(64, (3, 3), activation='relu')(x)\n    x = layers.MaxPooling2D((2, 2))(x)\n    \n    # Flatten layer\n    x = layers.Flatten()(x)\n    \n    # Bowel output (binary)\n#     any_injury = layers.Dense(1, activation='sigmoid', name='any_injury')(x)\n\n#     bowel_injury_output = layers.Dense(1, activation='sigmoid', name='bowel_injury_output')(x)\n    bowel_output = layers.Dense(num_classes_bowel, activation='softmax', name='bowel_output')(x)\n\n    \n    # Extravasation output (binary)\n    extravasation_output = layers.Dense(num_classes_extravasation, activation='softmax', name='extravasation_output')(x)\n   \n    \n    # Kidney output (multi-class)\n    kidney_output = layers.Dense(num_classes_kidney, activation='softmax', name='kidney_output')(x)\n    \n    # Liver output (multi-class)\n    liver_output = layers.Dense(num_classes_liver, activation='softmax', name='liver_output')(x)\n    \n    # Spleen output (multi-class)\n    spleen_output = layers.Dense(num_classes_spleen, activation='softmax', name='spleen_output')(x)\n    \n    \n    # Create the model with multiple outputs\n    model = models.Model(inputs=input_layer, outputs=[bowel_output, extravasation_output, kidney_output, liver_output, spleen_output])\n    \n    return model\n\n# Example usage:\nimage_height, image_width = config.IMAGE_SIZE\nnum_channels = 3\nnum_classes_bowel =2\nnum_classes_extravasation=2\nnum_classes_kidney = 3  # Adjust based on the number of kidney classes\nnum_classes_spleen = 3  # Adjust based on the number of spleen classes\nnum_classes_liver = 3   # Adjust based on the number of liver classes","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:05:17.38286Z","iopub.execute_input":"2023-10-12T06:05:17.385457Z","iopub.status.idle":"2023-10-12T06:05:17.402579Z","shell.execute_reply.started":"2023-10-12T06:05:17.385419Z","shell.execute_reply":"2023-10-12T06:05:17.401574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_cnn_model()\nmodel.compile(optimizer='adam',\n              loss={'bowel_output': 'categorical_crossentropy',\n                    'extravasation_output': 'categorical_crossentropy',\n                    'kidney_output': 'categorical_crossentropy',\n                    'liver_output': 'categorical_crossentropy',\n                    'spleen_output': 'categorical_crossentropy'\n                   },\n              metrics={'bowel_output': 'accuracy',\n                       'extravasation_output': 'accuracy',\n                       'kidney_output': 'accuracy',\n                       'liver_output': 'accuracy',\n                       'spleen_output': 'accuracy'})\n\n\n\nmodel.fit( train_ds,epochs=config.EPOCHS,validation_data=val_ds,)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:05:17.407104Z","iopub.execute_input":"2023-10-12T06:05:17.409694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = ['bowel_healthy', 'bowel_injury', 'extravasation_healthy',\n       'extravasation_injury', 'kidney_healthy', 'kidney_low', 'kidney_high',\n       'liver_healthy', 'liver_low', 'liver_high', 'spleen_healthy',\n       'spleen_low', 'spleen_high',]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_df = test_df.reindex(columns = header_list) \ntarget = pd.DataFrame(columns=columns)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.concat([test_df,target])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n\nimport keras_cv\nimport keras_core as keras\nfrom keras_core import layers\n\n\n\ndef decode_test_image(image_path):\n    file_bytes = tf.io.read_file(image_path)\n    image = tf.io.decode_png(file_bytes, channels=3, dtype=tf.uint8)\n    image = tf.image.resize(image, config.IMAGE_SIZE, method=\"bilinear\")\n    image = tf.cast(image, tf.float32) / 255.0   \n    return image\n\ndef apply_test_augmentation(images):\n    augmenter = keras.Sequential(\n        \n        layers=[\n            keras_cv.layers.RandomFlip(mode=\"horizontal_and_vertical\"),\n            keras_cv.layers.RandomCutout(height_factor=0.2, width_factor=0.2),\n            \n        ]\n    )\n    aug = augmenter(images)\n    return aug\n\ndef aug_test(image):\n    #image = preprocessing.Rescaling(1.0 / 255)(image)  # Rescale pixel values\n    image = preprocessing.RandomFlip(mode=\"horizontal_and_vertical\")(image)\n    image = preprocessing.RandomRotation(factor=0.2)(image)\n    #image = preprocessing.RandomZoom(height_factor=(0.8, 1.2), width_factor=(0.8, 1.2))(image)\n    #image = preprocessing.RandomContrast(factor=0.2)(image)\n    image = preprocessing.RandomTranslation(height_factor=0.2, width_factor=0.2)(image)\n    return image\n\n\ndef build_test_dataset(image_paths):\n    ds = (\n        tf.data.Dataset.from_tensor_slices((image_paths))\n        .map(decode_test_image, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(config.BATCH_SIZE)\n        .map(apply_test_augmentation, num_parallel_calls=config.AUTOTUNE)\n        .prefetch(config.AUTOTUNE)\n    )\n    return ds","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:17:32.859147Z","iopub.execute_input":"2023-10-11T17:17:32.860203Z","iopub.status.idle":"2023-10-11T17:17:32.869173Z","shell.execute_reply.started":"2023-10-11T17:17:32.860151Z","shell.execute_reply":"2023-10-11T17:17:32.868257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# modell =\"/kaggle/working/abdominal.keras\"\n# savedmodel = models.load_model(modell)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:19:15.077277Z","iopub.execute_input":"2023-10-11T17:19:15.077956Z","iopub.status.idle":"2023-10-11T17:19:15.082245Z","shell.execute_reply.started":"2023-10-11T17:19:15.077924Z","shell.execute_reply":"2023-10-11T17:19:15.08101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get image_paths and labels\nprint(\"[INFO] Building test the dataset...\")\ntest_paths = test_df.image_path\n \n# train and valid dataset\ntest_ds = build_test_dataset(image_paths=test_paths)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:17:43.444637Z","iopub.execute_input":"2023-10-11T17:17:43.445341Z","iopub.status.idle":"2023-10-11T17:17:44.861474Z","shell.execute_reply.started":"2023-10-11T17:17:43.445306Z","shell.execute_reply":"2023-10-11T17:17:44.860524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:17:46.29183Z","iopub.execute_input":"2023-10-11T17:17:46.292226Z","iopub.status.idle":"2023-10-11T17:17:46.297852Z","shell.execute_reply.started":"2023-10-11T17:17:46.292196Z","shell.execute_reply":"2023-10-11T17:17:46.297003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test_ds)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:17:46.799065Z","iopub.execute_input":"2023-10-11T17:17:46.799439Z","iopub.status.idle":"2023-10-11T17:17:47.434524Z","shell.execute_reply.started":"2023-10-11T17:17:46.799409Z","shell.execute_reply":"2023-10-11T17:17:47.433604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:32:55.133029Z","iopub.execute_input":"2023-10-11T18:32:55.133398Z","iopub.status.idle":"2023-10-11T18:32:55.141257Z","shell.execute_reply.started":"2023-10-11T18:32:55.133372Z","shell.execute_reply":"2023-10-11T18:32:55.140261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_to_dict(index_list, data_list):\n  \"\"\"Converts two lists into a dictionary using the indices of the first list as keys.\n\n  Returns:\n    A dictionary with the indices of the first list as keys and the data of the\n    second list as values.\n  \"\"\"\n\n  dict = {}\n  for i in range(len(index_list)):\n    dict[index_list[i]] = data_list[i]\n  return dict\n\n\ndef create_label_map(predictions):\n    # Define your labels\n    labels = [['bowel_healthy', 'bowel_injury'], ['extravasation_healthy',\n            'extravasation_injury'], ['kidney_healthy', 'kidney_low', 'kidney_high'],\n            ['liver_healthy', 'liver_low', 'liver_high'], ['spleen_healthy',\n            'spleen_low', 'spleen_high']]\n    label_map = []\n\n    for index, l in enumerate(labels):\n        for i in predictions[index]:\n            label_map.append(convert_to_dict(l, i))\n    return label_map\n\n\ndef create_dataframe(label_map):\n    # Initialize an empty dictionary to store the data\n    result_data = {}\n    # Iterate through the list of dictionaries\n    for item in label_map:\n        for key, value in item.items():\n            # Check if the key is already in the result_data dictionary\n            if key in result_data:\n                result_data[key].append(value)\n            else:\n                # If not, create a new list with the value\n                result_data[key] = [value]\n\n    # Create a Pandas DataFrame from the result_data dictionary\n    df = pd.DataFrame(result_data)\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:33:08.397541Z","iopub.execute_input":"2023-10-11T18:33:08.397919Z","iopub.status.idle":"2023-10-11T18:33:08.405508Z","shell.execute_reply.started":"2023-10-11T18:33:08.397875Z","shell.execute_reply":"2023-10-11T18:33:08.404617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = create_label_map(prediction)\npred_df = create_dataframe(label_map)","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:17:57.923047Z","iopub.execute_input":"2023-10-11T17:17:57.923381Z","iopub.status.idle":"2023-10-11T17:17:57.929437Z","shell.execute_reply.started":"2023-10-11T17:17:57.923354Z","shell.execute_reply":"2023-10-11T17:17:57.92849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:33:14.101796Z","iopub.execute_input":"2023-10-11T18:33:14.102165Z","iopub.status.idle":"2023-10-11T18:33:14.120416Z","shell.execute_reply.started":"2023-10-11T18:33:14.102138Z","shell.execute_reply":"2023-10-11T18:33:14.119373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df = pd.concat([test_df['patient_id'].astype(int), pred_df],axis=1)\nresult_df","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:33:20.702453Z","iopub.execute_input":"2023-10-11T18:33:20.702775Z","iopub.status.idle":"2023-10-11T18:33:20.717391Z","shell.execute_reply.started":"2023-10-11T18:33:20.702749Z","shell.execute_reply":"2023-10-11T18:33:20.716503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T06:20:51.534843Z","iopub.execute_input":"2023-10-12T06:20:51.535223Z","iopub.status.idle":"2023-10-12T06:20:51.544442Z","shell.execute_reply.started":"2023-10-12T06:20:51.535184Z","shell.execute_reply":"2023-10-12T06:20:51.543236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"/kaggle/working/abdominal.keras\")","metadata":{"execution":{"iopub.status.busy":"2023-10-11T17:56:02.407903Z","iopub.execute_input":"2023-10-11T17:56:02.409044Z","iopub.status.idle":"2023-10-11T17:56:02.484167Z","shell.execute_reply.started":"2023-10-11T17:56:02.409001Z","shell.execute_reply":"2023-10-11T17:56:02.483036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}