{"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":"https://www.kaggle.com/code/mirenaborisova/rsna-3-model-epoch-3-dense-32/input","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter('ignore')\n\nimport pandas as pd\n\npd.set_option('display.max_columns', None)\n\nimport os\nos.environ['KERAS_BACKEND'] = 'tensorflow'","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:06.181998Z","iopub.execute_input":"2023-10-07T09:34:06.182373Z","iopub.status.idle":"2023-10-07T09:34:06.554294Z","shell.execute_reply.started":"2023-10-07T09:34:06.182342Z","shell.execute_reply":"2023-10-07T09:34:06.553177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nclass Config:\n    \n    SEED = 42\n    IMAGE_SIZE = [256, 256]\n    BATCH_SIZE = 64 # 64\n    EPOCHS = 2 # 2\n    TARGET_COLS = [\n        'bowel_injury', 'extravasation_injury',\n        'kidney_healthy', 'kidney_low', 'kidney_high',\n        'liver_healthy', 'liver_low', 'liver_high',\n        'spleen_healthy', 'spleen_low', 'spleen_high',\n    ]\n    \n    AUTOTUNE = tf.data.AUTOTUNE\n    \nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:06.556001Z","iopub.execute_input":"2023-10-07T09:34:06.556532Z","iopub.status.idle":"2023-10-07T09:34:15.476271Z","shell.execute_reply.started":"2023-10-07T09:34:06.556501Z","shell.execute_reply":"2023-10-07T09:34:15.475078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install -q git+https://github.com/keras-team/keras-cv","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:15.477829Z","iopub.execute_input":"2023-10-07T09:34:15.478665Z","iopub.status.idle":"2023-10-07T09:34:43.57536Z","shell.execute_reply.started":"2023-10-07T09:34:15.478597Z","shell.execute_reply":"2023-10-07T09:34:43.573829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras_core as keras\n\nkeras.utils.set_random_seed(seed=config.SEED)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:43.578221Z","iopub.execute_input":"2023-10-07T09:34:43.57858Z","iopub.status.idle":"2023-10-07T09:34:44.047145Z","shell.execute_reply.started":"2023-10-07T09:34:43.578546Z","shell.execute_reply":"2023-10-07T09:34:44.046005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = '/kaggle/input/rsna-atd-512x512-png-v2-dataset'","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:44.048253Z","iopub.execute_input":"2023-10-07T09:34:44.048586Z","iopub.status.idle":"2023-10-07T09:34:44.053287Z","shell.execute_reply.started":"2023-10-07T09:34:44.04856Z","shell.execute_reply":"2023-10-07T09:34:44.052413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/rsna-atd-512x512-png-v2-dataset/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:44.054969Z","iopub.execute_input":"2023-10-07T09:34:44.055368Z","iopub.status.idle":"2023-10-07T09:34:44.154208Z","shell.execute_reply.started":"2023-10-07T09:34:44.055331Z","shell.execute_reply":"2023-10-07T09:34:44.153095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['image_path'] = '/kaggle/input/rsna-atd-512x512-png-v2-dataset/train_images' + '/' + \\\n    train.patient_id.astype(str) + '/' + \\\n    train.series_id.astype(str) + '/' + \\\n    train.instance_number.astype(str) + '.png'\n","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:44.155579Z","iopub.execute_input":"2023-10-07T09:34:44.156516Z","iopub.status.idle":"2023-10-07T09:34:44.19781Z","shell.execute_reply.started":"2023-10-07T09:34:44.156482Z","shell.execute_reply":"2023-10-07T09:34:44.196948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop_duplicates()\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:44.199223Z","iopub.execute_input":"2023-10-07T09:34:44.199828Z","iopub.status.idle":"2023-10-07T09:34:44.234351Z","shell.execute_reply.started":"2023-10-07T09:34:44.199798Z","shell.execute_reply":"2023-10-07T09:34:44.233501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.model_selection import train_test_split\n\ndef split_group(group, test_size=0.2):\n    \n    if len(group) == 1:\n        \n        return (group, pd.DataFrame()) if np.random.rand() < test_size else (pd.DataFrame(), group)\n    \n    else:\n        \n        return train_test_split(group, test_size=test_size, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:44.235848Z","iopub.execute_input":"2023-10-07T09:34:44.236928Z","iopub.status.idle":"2023-10-07T09:34:44.668052Z","shell.execute_reply.started":"2023-10-07T09:34:44.236895Z","shell.execute_reply":"2023-10-07T09:34:44.667158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_train = pd.DataFrame()\ntrain_validation = pd.DataFrame()\n\nfor _, group in train.groupby(config.TARGET_COLS):\n    \n    train_group, validation_group = split_group(group)\n    \n    train_train = pd.concat([train_train, train_group], ignore_index=True)\n    train_validation = pd.concat([train_validation, validation_group], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:44.670996Z","iopub.execute_input":"2023-10-07T09:34:44.671849Z","iopub.status.idle":"2023-10-07T09:34:44.783368Z","shell.execute_reply.started":"2023-10-07T09:34:44.671788Z","shell.execute_reply":"2023-10-07T09:34:44.782053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras_cv\n\ndef apply_augmentation(images, labels):\n    \n    augmenter = keras_cv.layers.Augmenter([\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    return (augmenter(images), labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:44.78488Z","iopub.execute_input":"2023-10-07T09:34:44.785202Z","iopub.status.idle":"2023-10-07T09:34:48.612682Z","shell.execute_reply.started":"2023-10-07T09:34:44.785175Z","shell.execute_reply":"2023-10-07T09:34:48.611204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image_and_labels(image_path, label):\n    \n    file_bytes = tf.io.read_file(image_path)\n    img = tf.io.decode_png(file_bytes, channels=3, dtype=tf.uint8)\n    img = tf.image.resize(img, config.IMAGE_SIZE, method='bilinear')\n    img = tf.cast(img, tf.float32) / 255.0\n    \n    label = tf.cast(label, tf.float32)\n    \n    labels = (label[0:1], label[1:2], label[2:5], label[5:8], label[8:11])\n    \n    return (img, labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:48.614177Z","iopub.execute_input":"2023-10-07T09:34:48.615167Z","iopub.status.idle":"2023-10-07T09:34:48.62308Z","shell.execute_reply.started":"2023-10-07T09:34:48.615133Z","shell.execute_reply":"2023-10-07T09:34:48.621772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_train_image_paths = train_train.image_path.tolist()\ntarget_values_to_labels = train_train[config.TARGET_COLS].values\n\ndef build_dataset(image_paths, labels):\n    \n    ds = (tf.data.Dataset.from_tensor_slices((image_paths, labels))\n    .map((decode_image_and_labels), 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-07T09:34:48.624683Z","iopub.execute_input":"2023-10-07T09:34:48.625165Z","iopub.status.idle":"2023-10-07T09:34:48.654244Z","shell.execute_reply.started":"2023-10-07T09:34:48.625122Z","shell.execute_reply":"2023-10-07T09:34:48.653381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(warmup_steps, decay_steps):\n    \n    inputs = keras.Input(shape=config.IMAGE_SIZE + [3,], batch_size=config.BATCH_SIZE)\n    \n    backbone = keras_cv.models.ResNetBackbone.from_preset('resnet50_imagenet')\n    backbone.include_rescaling = False\n    \n    x = backbone(inputs)\n    \n    gap = keras.layers.GlobalAveragePooling2D()\n    x = gap(x)\n    \n    x_bowel = keras.layers.Dense(16, activation='silu')(x) # 32\n    x_extra = keras.layers.Dense(16, activation='silu')(x) # 32\n    x_liver = keras.layers.Dense(16, activation='silu')(x) # 32\n    x_kidney = keras.layers.Dense(16, activation='silu')(x) # 32\n    x_spleen = keras.layers.Dense(16, activation='silu')(x) # 32\n    \n    out_bowel = keras.layers.Dense(1, activation='sigmoid', name='bowel')(x_bowel)\n    out_extra = keras.layers.Dense(1, activation='sigmoid', name='extra')(x_extra)\n    out_liver = keras.layers.Dense(3, activation='softmax', name='liver')(x_liver)\n    out_kidney = keras.layers.Dense(3, activation='softmax', name='kidney')(x_kidney)\n    out_spleen = keras.layers.Dense(3, activation='softmax', name='spleen')(x_spleen)\n    \n    outputs = [out_bowel, out_extra, out_liver, out_kidney, out_spleen]\n    \n    model = keras.Model(inputs=inputs, outputs=outputs)\n    \n    cosine_decay = keras.optimizers.schedules.CosineDecay(\n        initial_learning_rate=1e-4,\n        decay_steps=decay_steps,\n        alpha=0.0,\n        warmup_target=1e-3,\n        warmup_steps=warmup_steps\n    )\n    \n    optimizer = keras.optimizers.Adam(learning_rate=cosine_decay)\n    \n    loss = {\n        'bowel': keras.losses.BinaryCrossentropy(),\n        'extra': keras.losses.BinaryCrossentropy(),\n        'liver': keras.losses.CategoricalCrossentropy(),\n        'kidney': keras.losses.CategoricalCrossentropy(),\n        'spleen': keras.losses.CategoricalCrossentropy()\n    }\n    \n    metrics = {\n        'bowel': ['accuracy'],\n        'extra': ['accuracy'],\n        'liver': ['accuracy'],\n        'kidney': ['accuracy'],\n        'spleen': ['accuracy'],\n    }\n    \n    model.compile(optimizer=optimizer, loss=loss, metrics=metrics)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:48.656249Z","iopub.execute_input":"2023-10-07T09:34:48.65671Z","iopub.status.idle":"2023-10-07T09:34:48.66919Z","shell.execute_reply.started":"2023-10-07T09:34:48.656669Z","shell.execute_reply":"2023-10-07T09:34:48.668275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_paths = train_train.image_path.values\ntrain_labels = train_train[config.TARGET_COLS].values.astype(np.float32)\n\nvalid_paths = train_validation.image_path.values\nvalid_labels = train_validation[config.TARGET_COLS].values.astype(np.float32)\n\n\ntrain_ds = build_dataset(image_paths=train_paths, labels=train_labels)\nvalid_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","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:48.670228Z","iopub.execute_input":"2023-10-07T09:34:48.671413Z","iopub.status.idle":"2023-10-07T09:34:56.32934Z","shell.execute_reply.started":"2023-10-07T09:34:48.671375Z","shell.execute_reply":"2023-10-07T09:34:56.328163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model(warmup_steps, decay_steps)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:34:56.330989Z","iopub.execute_input":"2023-10-07T09:34:56.331284Z","iopub.status.idle":"2023-10-07T09:35:02.409908Z","shell.execute_reply.started":"2023-10-07T09:34:56.331259Z","shell.execute_reply":"2023-10-07T09:35:02.408813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:35:02.41125Z","iopub.execute_input":"2023-10-07T09:35:02.411564Z","iopub.status.idle":"2023-10-07T09:35:02.446318Z","shell.execute_reply.started":"2023-10-07T09:35:02.411536Z","shell.execute_reply":"2023-10-07T09:35:02.445362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_ds, epochs=config.EPOCHS, validation_data=valid_ds)","metadata":{"execution":{"iopub.status.busy":"2023-10-07T09:35:02.447547Z","iopub.execute_input":"2023-10-07T09:35:02.447897Z","iopub.status.idle":"2023-10-07T16:59:04.853555Z","shell.execute_reply.started":"2023-10-07T09:35:02.44787Z","shell.execute_reply":"2023-10-07T16:59:04.849694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig, axes = plt.subplots(5, 1, figsize=(5, 15))\n\naxes = axes.flatten()\n\nfor i, name in enumerate(['bowel', 'extra', 'kidney', 'liver', 'spleen']):\n    \n    axes[i].plot(history.history[name + '_accuracy'], label='Training ' + name)\n    axes[i].plot(history.history['val_' + name + '_accuracy'], label='Validation ' + name)\n    axes[i].set_title(name)\n    axes[i].set_xlabel('Epoch')\n    axes[i].set_ylabel('Accuracy')\n    axes[i].legend()\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T16:59:04.859715Z","iopub.execute_input":"2023-10-07T16:59:04.860245Z","iopub.status.idle":"2023-10-07T16:59:06.247814Z","shell.execute_reply.started":"2023-10-07T16:59:04.860194Z","shell.execute_reply":"2023-10-07T16:59:06.246804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='loss')\nplt.plot(history.history['val_loss'], label='val_loss')\n\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-07T16:59:06.249357Z","iopub.execute_input":"2023-10-07T16:59:06.249662Z","iopub.status.idle":"2023-10-07T16:59:06.4903Z","shell.execute_reply.started":"2023-10-07T16:59:06.249636Z","shell.execute_reply":"2023-10-07T16:59:06.489175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_epoch = np.argmin(history.history['val_loss'])\nbest_loss = history.history['val_loss'][best_epoch]\nbest_acc_bowel = history.history['val_bowel_accuracy'][best_epoch]\nbest_acc_extra = history.history['val_extra_accuracy'][best_epoch]\nbest_acc_liver = history.history['val_liver_accuracy'][best_epoch]\nbest_acc_kidney = history.history['val_kidney_accuracy'][best_epoch]\nbest_acc_spleen = history.history['val_spleen_accuracy'][best_epoch]\n\nbest_acc = np.mean([\n    best_acc_bowel,\n    best_acc_extra,\n    best_acc_liver,\n    best_acc_kidney,\n    best_acc_spleen\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-07T16:59:06.492087Z","iopub.execute_input":"2023-10-07T16:59:06.492397Z","iopub.status.idle":"2023-10-07T16:59:06.50054Z","shell.execute_reply.started":"2023-10-07T16:59:06.492371Z","shell.execute_reply":"2023-10-07T16:59:06.499311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'>>>> BEST Loss  : {best_loss:.3f}\\n>>>> BEST Acc   : {best_acc:.3f}\\n>>>> BEST Epoch : {best_epoch}\\n')\nprint('ORGAN Acc:')\nprint(f'  >>>> {\"Bowel\".ljust(15)} : {best_acc_bowel:.3f}')\nprint(f'  >>>> {\"Extravasation\".ljust(15)} : {best_acc_extra:.3f}')\nprint(f'  >>>> {\"Liver\".ljust(15)} : {best_acc_liver:.3f}')\nprint(f'  >>>> {\"Kidney\".ljust(15)} : {best_acc_kidney:.3f}')\nprint(f'  >>>> {\"Spleen\".ljust(15)} : {best_acc_spleen:.3f}')","metadata":{"execution":{"iopub.status.busy":"2023-10-07T16:59:06.502112Z","iopub.execute_input":"2023-10-07T16:59:06.502398Z","iopub.status.idle":"2023-10-07T16:59:06.516114Z","shell.execute_reply.started":"2023-10-07T16:59:06.502375Z","shell.execute_reply":"2023-10-07T16:59:06.514854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"rsna-resnet50.keras\")","metadata":{"execution":{"iopub.status.busy":"2023-10-07T16:59:06.517904Z","iopub.execute_input":"2023-10-07T16:59:06.518667Z","iopub.status.idle":"2023-10-07T16:59:08.138883Z","shell.execute_reply.started":"2023-10-07T16:59:06.518597Z","shell.execute_reply":"2023-10-07T16:59:08.137971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}