{"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":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nimport albumentations as album\nimport matplotlib.pyplot as plt\nfrom skimage.io import imread\nfrom sklearn.model_selection import StratifiedKFold","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-12T02:52:34.822231Z","iopub.execute_input":"2022-07-12T02:52:34.822702Z","iopub.status.idle":"2022-07-12T02:52:34.827168Z","shell.execute_reply.started":"2022-07-12T02:52:34.822665Z","shell.execute_reply":"2022-07-12T02:52:34.826419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"N_SPLITS = 3\nIMG_HEIGHT = 224\nIMG_WIDTH = 224\nBATCH_SIZE = 16\nEPOCHS = 200\nPATIENCE = 10\nMAX_LR = 1e-5\nMIN_LR = 1e-6","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:52:36.706048Z","iopub.execute_input":"2022-07-12T02:52:36.706303Z","iopub.status.idle":"2022-07-12T02:52:36.710652Z","shell.execute_reply.started":"2022-07-12T02:52:36.706274Z","shell.execute_reply":"2022-07-12T02:52:36.709866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset","metadata":{}},{"cell_type":"code","source":"class_names = np.array([\"Abdomen\", \"Ankle\", \"Cervical Spine\", \"Chest\", \n                        \"Clavicles\", \"Elbow\", \"Feet\", \"Finger\",\n                        \"Forearm\", \"Hand\", \"Hip\", \"Knee\",  \n                        \"Lower Leg\", \"Lumbar Spine\", \"Others\", \"Pelvis\", \n                        \"Shoulder\", \"Sinus\", \"Skull\", \"Thigh\", \n                        \"Thoracic Spine\", \"Wrist\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:52:40.384935Z","iopub.execute_input":"2022-07-12T02:52:40.385194Z","iopub.status.idle":"2022-07-12T02:52:40.390408Z","shell.execute_reply.started":"2022-07-12T02:52:40.385165Z","shell.execute_reply":"2022-07-12T02:52:40.389071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/unifesp-x-ray-body-part-classifier/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:52:43.795955Z","iopub.execute_input":"2022-07-12T02:52:43.796207Z","iopub.status.idle":"2022-07-12T02:52:43.826120Z","shell.execute_reply.started":"2022-07-12T02:52:43.796179Z","shell.execute_reply":"2022-07-12T02:52:43.825266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = \"../input/unifesp-xray-body-224-224/train/\"\ntemp_SOPInstanceUID = []\nfor i in df.SOPInstanceUID:\n    temp_SOPInstanceUID.append(images_path + i + \".png\")\ndf.SOPInstanceUID = temp_SOPInstanceUID","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:52:51.081186Z","iopub.execute_input":"2022-07-12T02:52:51.081554Z","iopub.status.idle":"2022-07-12T02:52:51.094421Z","shell.execute_reply.started":"2022-07-12T02:52:51.081516Z","shell.execute_reply":"2022-07-12T02:52:51.093769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 22\ntargets = np.zeros((len(df.Target), num_classes), dtype=int)\nfor i, t in enumerate(df.Target):\n    target = t.strip().split()\n    for j in target:\n        targets[i, int(j)] = 1","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:06.522088Z","iopub.execute_input":"2022-07-12T02:53:06.522353Z","iopub.status.idle":"2022-07-12T02:53:06.530170Z","shell.execute_reply.started":"2022-07-12T02:53:06.522311Z","shell.execute_reply":"2022-07-12T02:53:06.529271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.DataFrame(targets, columns=class_names)\ndf = pd.concat([df, df2], axis=1) \ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:07.995062Z","iopub.execute_input":"2022-07-12T02:53:07.995315Z","iopub.status.idle":"2022-07-12T02:53:08.017557Z","shell.execute_reply.started":"2022-07-12T02:53:07.995287Z","shell.execute_reply":"2022-07-12T02:53:08.016893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 5))\nplt.bar(np.arange(22), targets.sum(axis=0))\nplt.xticks(ticks=np.arange(22), labels=class_names, rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:16.387050Z","iopub.execute_input":"2022-07-12T02:53:16.387394Z","iopub.status.idle":"2022-07-12T02:53:16.788925Z","shell.execute_reply.started":"2022-07-12T02:53:16.387354Z","shell.execute_reply":"2022-07-12T02:53:16.788240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(9):\n    # image\n    ax = plt.subplot(3, 3, i+1)\n    filename = df.iloc[i].SOPInstanceUID\n    img = imread(filename)\n    plt.imshow(img, cmap='gray')\n    # titles\n    labels = df.iloc[i].Target.strip().split()\n    if len(labels) == 1:\n        plt.title(class_names[int(labels[0])])\n    else:\n        titles = []\n        for label in labels:\n            titles.append(class_names[int(label)])\n        plt.title(\", \".join(titles))\n    plt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:23.855753Z","iopub.execute_input":"2022-07-12T02:53:23.856215Z","iopub.status.idle":"2022-07-12T02:53:24.634551Z","shell.execute_reply.started":"2022-07-12T02:53:23.856177Z","shell.execute_reply":"2022-07-12T02:53:24.633890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(\"../input/unifesp-x-ray-body-part-classifier/sample_submission.csv\")\ndf_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:27.657250Z","iopub.execute_input":"2022-07-12T02:53:27.657916Z","iopub.status.idle":"2022-07-12T02:53:27.676179Z","shell.execute_reply.started":"2022-07-12T02:53:27.657880Z","shell.execute_reply":"2022-07-12T02:53:27.675550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_path = \"../input/unifesp-xray-body-224-224/test/\"\ntemp_SOPInstanceUID = []\nfor i in df_test.SOPInstanceUID:\n    temp_SOPInstanceUID.append(images_path + i + \".png\")\ndf_test.SOPInstanceUID = temp_SOPInstanceUID","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:35.060607Z","iopub.execute_input":"2022-07-12T02:53:35.060866Z","iopub.status.idle":"2022-07-12T02:53:35.066726Z","shell.execute_reply.started":"2022-07-12T02:53:35.060838Z","shell.execute_reply":"2022-07-12T02:53:35.065885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:37.288502Z","iopub.execute_input":"2022-07-12T02:53:37.288921Z","iopub.status.idle":"2022-07-12T02:53:37.297800Z","shell.execute_reply.started":"2022-07-12T02:53:37.288883Z","shell.execute_reply":"2022-07-12T02:53:37.297125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_test.SOPInstanceUID.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:53:43.464865Z","iopub.execute_input":"2022-07-12T02:53:43.465161Z","iopub.status.idle":"2022-07-12T02:53:43.472102Z","shell.execute_reply.started":"2022-07-12T02:53:43.465126Z","shell.execute_reply":"2022-07-12T02:53:43.471435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split Dataset","metadata":{}},{"cell_type":"code","source":"X = df.SOPInstanceUID.to_numpy()\ny = df.iloc[:, 2:].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:54:06.450470Z","iopub.execute_input":"2022-07-12T02:54:06.451404Z","iopub.status.idle":"2022-07-12T02:54:06.463084Z","shell.execute_reply.started":"2022-07-12T02:54:06.451316Z","shell.execute_reply":"2022-07-12T02:54:06.462403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=N_SPLITS, shuffle=True, random_state=42)\n\nfor train_index, valid_index in skf.split(X, np.argmax(y, axis=1)):\n    X_train, X_valid = X[train_index], X[valid_index]\n    y_train, y_valid = y[train_index], y[valid_index]\n\nprint(f\"train size: {y_train.shape[0]}\")\nprint(f\"valid size: {y_valid.shape[0]}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:54:11.411844Z","iopub.execute_input":"2022-07-12T02:54:11.412103Z","iopub.status.idle":"2022-07-12T02:54:11.423712Z","shell.execute_reply.started":"2022-07-12T02:54:11.412075Z","shell.execute_reply":"2022-07-12T02:54:11.422903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augmentation","metadata":{}},{"cell_type":"code","source":"class DataAugmentation:\n    def __init__(self):\n        self.transforms = album.Compose([\n            album.HorizontalFlip(p=0.5),\n            album.ShiftScaleRotate(shift_limit=0.15, \n                                   scale_limit=0.2, \n                                   rotate_limit=25, \n                                   border_mode=cv2.BORDER_CONSTANT, p=0.75),\n            album.OneOf([\n                album.RandomBrightnessContrast(brightness_limit=0.2, \n                                               contrast_limit=0.2, p=0.5),\n                album.RandomGamma(gamma_limit=(70, 130), p=0.5),\n                ], p=0.5),\n            album.OneOf([\n                album.Blur(p=0.1),\n                album.GaussianBlur(p=0.1),\n                album.MotionBlur(p=0.1),\n                ], p=0.1),\n            album.OneOf([\n                album.GaussNoise(p=0.1),\n                album.GridDropout(ratio=0.5, p=0.2),\n                album.CoarseDropout(max_holes=16, max_height=16, max_width=16,\n                                    min_holes= 8, min_height= 8, min_width= 8, p=0.2)\n                ], p=0.5),\n            ])\n\n    def aug_fn(self, image):\n        data = {\"image\":image}\n        aug_data = self.transforms(**data)\n        aug_img = aug_data[\"image\"]\n        aug_img = tf.cast(aug_img, tf.float32)\n        return aug_img\n\n    def augment_iamge(self, img, label):\n        aug_img = tf.numpy_function(func=self.aug_fn, inp=[img], Tout=tf.float32)\n        return aug_img, label","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:54:20.250785Z","iopub.execute_input":"2022-07-12T02:54:20.251038Z","iopub.status.idle":"2022-07-12T02:54:20.260549Z","shell.execute_reply.started":"2022-07-12T02:54:20.251011Z","shell.execute_reply":"2022-07-12T02:54:20.259867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## tf.data","metadata":{}},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:54:23.635592Z","iopub.execute_input":"2022-07-12T02:54:23.635911Z","iopub.status.idle":"2022-07-12T02:54:23.640025Z","shell.execute_reply.started":"2022-07-12T02:54:23.635875Z","shell.execute_reply":"2022-07-12T02:54:23.639293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_path(file_path, label):\n    img = tf.io.read_file(file_path)\n    img = tf.image.decode_png(img, channels=1, dtype=tf.uint8)\n    return img, label\n\ndef process_test_path(file_path):\n    img = tf.io.read_file(file_path)\n    img = tf.image.decode_png(img, channels=1, dtype=tf.uint8)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:54:26.880617Z","iopub.execute_input":"2022-07-12T02:54:26.881436Z","iopub.status.idle":"2022-07-12T02:54:26.887619Z","shell.execute_reply.started":"2022-07-12T02:54:26.881393Z","shell.execute_reply":"2022-07-12T02:54:26.886857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train))\nvalid_ds = tf.data.Dataset.from_tensor_slices((X_valid, y_valid))\ntest_ds = tf.data.Dataset.from_tensor_slices((X_test))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:15.083423Z","iopub.execute_input":"2022-07-12T02:55:15.084019Z","iopub.status.idle":"2022-07-12T02:55:17.791929Z","shell.execute_reply.started":"2022-07-12T02:55:15.083980Z","shell.execute_reply":"2022-07-12T02:55:17.791231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.map(process_path, num_parallel_calls=AUTOTUNE)\nvalid_ds = valid_ds.map(process_path, num_parallel_calls=AUTOTUNE)\ntest_ds = test_ds.map(process_test_path, num_parallel_calls=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:26.291026Z","iopub.execute_input":"2022-07-12T02:55:26.291375Z","iopub.status.idle":"2022-07-12T02:55:26.509522Z","shell.execute_reply.started":"2022-07-12T02:55:26.291321Z","shell.execute_reply":"2022-07-12T02:55:26.508737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_train = DataAugmentation()\ntrain_ds = train_ds.map(aug_train.augment_iamge, num_parallel_calls=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:28.414767Z","iopub.execute_input":"2022-07-12T02:55:28.415019Z","iopub.status.idle":"2022-07-12T02:55:28.464267Z","shell.execute_reply.started":"2022-07-12T02:55:28.414992Z","shell.execute_reply":"2022-07-12T02:55:28.463594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.batch(BATCH_SIZE)\nvalid_ds = valid_ds.batch(BATCH_SIZE)\ntest_ds = test_ds.batch(BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:30.698661Z","iopub.execute_input":"2022-07-12T02:55:30.698918Z","iopub.status.idle":"2022-07-12T02:55:30.707051Z","shell.execute_reply.started":"2022-07-12T02:55:30.698892Z","shell.execute_reply":"2022-07-12T02:55:30.706257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, label_batch = next(iter(train_ds))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:32.777326Z","iopub.execute_input":"2022-07-12T02:55:32.777818Z","iopub.status.idle":"2022-07-12T02:55:32.939505Z","shell.execute_reply.started":"2022-07-12T02:55:32.777782Z","shell.execute_reply":"2022-07-12T02:55:32.938773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(9, 6))\nfor i in range(6):\n    ax = plt.subplot(2, 3, i + 1)\n    plt.imshow(image_batch[i].numpy().astype(\"uint8\"), cmap='gray')\n    label = label_batch[i].numpy()\n    if label.sum() == 1:\n        plt.title(class_names[np.argmax(label)])\n    else:\n        titles = []\n        for j in np.where(label == 1)[0]:\n            titles.append(class_names[j])\n        plt.title(\", \".join(titles))\n    plt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:42.161987Z","iopub.execute_input":"2022-07-12T02:55:42.162245Z","iopub.status.idle":"2022-07-12T02:55:42.503959Z","shell.execute_reply.started":"2022-07-12T02:55:42.162215Z","shell.execute_reply":"2022-07-12T02:55:42.503220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE).repeat()\nvalid_ds = valid_ds.cache().prefetch(buffer_size=AUTOTUNE).repeat()\ntest_ds = test_ds.prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:56.217628Z","iopub.execute_input":"2022-07-12T02:55:56.217981Z","iopub.status.idle":"2022-07-12T02:55:56.235043Z","shell.execute_reply.started":"2022-07-12T02:55:56.217943Z","shell.execute_reply":"2022-07-12T02:55:56.234385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build Model","metadata":{}},{"cell_type":"code","source":"tf.keras.mixed_precision.set_global_policy('mixed_float16')\n\ndef get_model(input_shape, output_shape):\n    base_model = tf.keras.applications.efficientnet.EfficientNetB0(input_shape=(input_shape[0],\n                                                                                input_shape[1],\n                                                                                3),\n                                                                   include_top=False)\n    inputs = tf.keras.Input(shape=input_shape)\n    x = tf.tile(inputs, (1, 1, 1, 3))\n    # Transfer Learning\n    x = base_model(x)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    # Dense\n    x = tf.keras.layers.Dropout(0.6)(x)\n    outputs = tf.keras.layers.Dense(output_shape, activation=\"sigmoid\", dtype='float32')(x)\n\n    model = tf.keras.Model(inputs, outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:55:58.381062Z","iopub.execute_input":"2022-07-12T02:55:58.381316Z","iopub.status.idle":"2022-07-12T02:55:58.393061Z","shell.execute_reply.started":"2022-07-12T02:55:58.381287Z","shell.execute_reply":"2022-07-12T02:55:58.392260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model(input_shape=(IMG_HEIGHT, IMG_WIDTH, 1), output_shape=num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:56:06.074827Z","iopub.execute_input":"2022-07-12T02:56:06.075077Z","iopub.status.idle":"2022-07-12T02:56:08.866965Z","shell.execute_reply.started":"2022-07-12T02:56:06.075049Z","shell.execute_reply":"2022-07-12T02:56:08.866273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps_per_epoch = len(y_train)//BATCH_SIZE\nvalidation_steps = len(y_valid)//BATCH_SIZE","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:56:58.477633Z","iopub.execute_input":"2022-07-12T02:56:58.477895Z","iopub.status.idle":"2022-07-12T02:56:58.482737Z","shell.execute_reply.started":"2022-07-12T02:56:58.477867Z","shell.execute_reply":"2022-07-12T02:56:58.481400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tfa.optimizers.RectifiedAdam(learning_rate=MAX_LR,\n                                                     total_steps=EPOCHS*steps_per_epoch,\n                                                     warmup_proportion=0.1,\n                                                     min_lr=MIN_LR), \n              loss=tf.losses.BinaryCrossentropy(),\n              metrics=['binary_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:56:59.885310Z","iopub.execute_input":"2022-07-12T02:56:59.886011Z","iopub.status.idle":"2022-07-12T02:56:59.907366Z","shell.execute_reply.started":"2022-07-12T02:56:59.885973Z","shell.execute_reply":"2022-07-12T02:56:59.906722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_callbacks = tf.keras.callbacks.EarlyStopping(patience=PATIENCE, \n                                                monitor='val_loss', \n                                                restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:57:03.445493Z","iopub.execute_input":"2022-07-12T02:57:03.446018Z","iopub.status.idle":"2022-07-12T02:57:03.450260Z","shell.execute_reply.started":"2022-07-12T02:57:03.445981Z","shell.execute_reply":"2022-07-12T02:57:03.449386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Model","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_ds, \n                    validation_data=valid_ds,\n                    epochs=EPOCHS,\n                    callbacks=[my_callbacks],\n                    steps_per_epoch=steps_per_epoch,\n                    validation_steps=validation_steps)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T02:57:06.168709Z","iopub.execute_input":"2022-07-12T02:57:06.168976Z","iopub.status.idle":"2022-07-12T03:40:44.995647Z","shell.execute_reply.started":"2022-07-12T02:57:06.168950Z","shell.execute_reply":"2022-07-12T03:40:44.994917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Validate the Model\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))\nax1.set_title('Accuracy')\nax1.plot(history.history['binary_accuracy'], 'r', marker=\"o\", label='accuracy')\nax1.plot(history.history['val_binary_accuracy'], 'g', marker=\"x\", label='val_accuracy')\nax1.legend()\n\nax2.set_title('Loss')\nax2.plot(history.history['loss'], 'r', marker=\"o\", label='loss')\nax2.plot(history.history['val_loss'], 'g', marker=\"x\", label='val_loss')\nax2.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:40:47.507849Z","iopub.execute_input":"2022-07-12T03:40:47.508104Z","iopub.status.idle":"2022-07-12T03:40:47.819840Z","shell.execute_reply.started":"2022-07-12T03:40:47.508074Z","shell.execute_reply":"2022-07-12T03:40:47.819173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"pred = model.predict(test_ds)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:44:55.775149Z","iopub.execute_input":"2022-07-12T03:44:55.775430Z","iopub.status.idle":"2022-07-12T03:44:56.743783Z","shell.execute_reply.started":"2022-07-12T03:44:55.775401Z","shell.execute_reply":"2022-07-12T03:44:56.742922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = []\nfor i in pred:\n    ans = np.where(i>0.9)[0]\n    if ans.shape[0] == 0:\n        temp.append(str(np.argmax(i)))\n    else:\n        temp.append(\" \".join(ans.astype(str)))","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:45:06.636609Z","iopub.execute_input":"2022-07-12T03:45:06.636890Z","iopub.status.idle":"2022-07-12T03:45:06.652273Z","shell.execute_reply.started":"2022-07-12T03:45:06.636859Z","shell.execute_reply":"2022-07-12T03:45:06.651336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(pred.flatten(), bins=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:45:16.309431Z","iopub.execute_input":"2022-07-12T03:45:16.309887Z","iopub.status.idle":"2022-07-12T03:45:16.503981Z","shell.execute_reply.started":"2022-07-12T03:45:16.309850Z","shell.execute_reply":"2022-07-12T03:45:16.503182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv(\"../input/unifesp-x-ray-body-part-classifier/sample_submission.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:45:38.259228Z","iopub.execute_input":"2022-07-12T03:45:38.259514Z","iopub.status.idle":"2022-07-12T03:45:38.270048Z","shell.execute_reply.started":"2022-07-12T03:45:38.259483Z","shell.execute_reply":"2022-07-12T03:45:38.269280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:45:46.103042Z","iopub.execute_input":"2022-07-12T03:45:46.103308Z","iopub.status.idle":"2022-07-12T03:45:46.114878Z","shell.execute_reply.started":"2022-07-12T03:45:46.103277Z","shell.execute_reply":"2022-07-12T03:45:46.114014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.Target = temp\ndf_test","metadata":{"execution":{"iopub.status.busy":"2022-07-12T03:46:24.897287Z","iopub.execute_input":"2022-07-12T03:46:24.897708Z","iopub.status.idle":"2022-07-12T03:46:24.912705Z","shell.execute_reply.started":"2022-07-12T03:46:24.897667Z","shell.execute_reply":"2022-07-12T03:46:24.911771Z"},"trusted":true},"execution_count":null,"outputs":[]}]}