{"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-05-06T09:48:17.779549Z","iopub.execute_input":"2022-05-06T09:48:17.780303Z","iopub.status.idle":"2022-05-06T09:48:24.851451Z","shell.execute_reply.started":"2022-05-06T09:48:17.78017Z","shell.execute_reply":"2022-05-06T09:48:24.850382Z"},"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-05-06T09:48:24.853441Z","iopub.execute_input":"2022-05-06T09:48:24.853878Z","iopub.status.idle":"2022-05-06T09:48:24.858807Z","shell.execute_reply.started":"2022-05-06T09:48:24.853834Z","shell.execute_reply":"2022-05-06T09:48:24.858215Z"},"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-05-06T09:48:24.859884Z","iopub.execute_input":"2022-05-06T09:48:24.861251Z","iopub.status.idle":"2022-05-06T09:48:24.877213Z","shell.execute_reply.started":"2022-05-06T09:48:24.86115Z","shell.execute_reply":"2022-05-06T09:48:24.876502Z"},"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-05-06T09:48:24.879873Z","iopub.execute_input":"2022-05-06T09:48:24.880347Z","iopub.status.idle":"2022-05-06T09:48:24.912766Z","shell.execute_reply.started":"2022-05-06T09:48:24.880306Z","shell.execute_reply":"2022-05-06T09:48:24.912058Z"},"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-05-06T09:48:24.914049Z","iopub.execute_input":"2022-05-06T09:48:24.914495Z","iopub.status.idle":"2022-05-06T09:48:24.922939Z","shell.execute_reply.started":"2022-05-06T09:48:24.914459Z","shell.execute_reply":"2022-05-06T09:48:24.922198Z"},"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-05-06T09:48:24.924588Z","iopub.execute_input":"2022-05-06T09:48:24.924944Z","iopub.status.idle":"2022-05-06T09:48:24.934949Z","shell.execute_reply.started":"2022-05-06T09:48:24.924905Z","shell.execute_reply":"2022-05-06T09:48:24.93418Z"},"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-05-06T09:48:24.936607Z","iopub.execute_input":"2022-05-06T09:48:24.936973Z","iopub.status.idle":"2022-05-06T09:48:24.961054Z","shell.execute_reply.started":"2022-05-06T09:48:24.936934Z","shell.execute_reply":"2022-05-06T09:48:24.960238Z"},"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-05-06T09:48:24.962724Z","iopub.execute_input":"2022-05-06T09:48:24.963005Z","iopub.status.idle":"2022-05-06T09:48:25.24623Z","shell.execute_reply.started":"2022-05-06T09:48:24.962969Z","shell.execute_reply":"2022-05-06T09:48:25.244386Z"},"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-05-06T09:48:25.247594Z","iopub.execute_input":"2022-05-06T09:48:25.247838Z","iopub.status.idle":"2022-05-06T09:48:26.05435Z","shell.execute_reply.started":"2022-05-06T09:48:25.247805Z","shell.execute_reply":"2022-05-06T09:48:26.053701Z"},"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-05-06T09:48:26.056423Z","iopub.execute_input":"2022-05-06T09:48:26.057333Z","iopub.status.idle":"2022-05-06T09:48:26.077763Z","shell.execute_reply.started":"2022-05-06T09:48:26.057291Z","shell.execute_reply":"2022-05-06T09:48:26.077016Z"},"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-05-06T09:48:26.079011Z","iopub.execute_input":"2022-05-06T09:48:26.079263Z","iopub.status.idle":"2022-05-06T09:48:26.084782Z","shell.execute_reply.started":"2022-05-06T09:48:26.079231Z","shell.execute_reply":"2022-05-06T09:48:26.083775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T09:48:26.08633Z","iopub.execute_input":"2022-05-06T09:48:26.08723Z","iopub.status.idle":"2022-05-06T09:48:26.099508Z","shell.execute_reply.started":"2022-05-06T09:48:26.087158Z","shell.execute_reply":"2022-05-06T09:48:26.098705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_test.SOPInstanceUID.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T09:48:26.10089Z","iopub.execute_input":"2022-05-06T09:48:26.101239Z","iopub.status.idle":"2022-05-06T09:48:26.105391Z","shell.execute_reply.started":"2022-05-06T09:48:26.101203Z","shell.execute_reply":"2022-05-06T09:48:26.104538Z"},"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-05-06T09:48:26.106844Z","iopub.execute_input":"2022-05-06T09:48:26.107417Z","iopub.status.idle":"2022-05-06T09:48:26.114305Z","shell.execute_reply.started":"2022-05-06T09:48:26.107263Z","shell.execute_reply":"2022-05-06T09:48:26.113534Z"},"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-05-06T09:48:26.140219Z","iopub.execute_input":"2022-05-06T09:48:26.140422Z","iopub.status.idle":"2022-05-06T09:48:26.152293Z","shell.execute_reply.started":"2022-05-06T09:48:26.1404Z","shell.execute_reply":"2022-05-06T09:48:26.151485Z"},"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-05-06T09:48:26.651936Z","iopub.execute_input":"2022-05-06T09:48:26.652288Z","iopub.status.idle":"2022-05-06T09:48:26.662321Z","shell.execute_reply.started":"2022-05-06T09:48:26.652257Z","shell.execute_reply":"2022-05-06T09:48:26.661632Z"},"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-05-06T09:48:27.129963Z","iopub.execute_input":"2022-05-06T09:48:27.130522Z","iopub.status.idle":"2022-05-06T09:48:27.134519Z","shell.execute_reply.started":"2022-05-06T09:48:27.130483Z","shell.execute_reply":"2022-05-06T09:48:27.133673Z"},"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-05-06T09:48:27.585502Z","iopub.execute_input":"2022-05-06T09:48:27.585778Z","iopub.status.idle":"2022-05-06T09:48:27.591948Z","shell.execute_reply.started":"2022-05-06T09:48:27.585748Z","shell.execute_reply":"2022-05-06T09:48:27.59078Z"},"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-05-06T09:48:28.016167Z","iopub.execute_input":"2022-05-06T09:48:28.016953Z","iopub.status.idle":"2022-05-06T09:48:30.447562Z","shell.execute_reply.started":"2022-05-06T09:48:28.016916Z","shell.execute_reply":"2022-05-06T09:48:30.446846Z"},"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-05-06T09:48:30.449464Z","iopub.execute_input":"2022-05-06T09:48:30.449976Z","iopub.status.idle":"2022-05-06T09:48:30.568083Z","shell.execute_reply.started":"2022-05-06T09:48:30.449935Z","shell.execute_reply":"2022-05-06T09:48:30.567401Z"},"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-05-06T09:48:30.569789Z","iopub.execute_input":"2022-05-06T09:48:30.569974Z","iopub.status.idle":"2022-05-06T09:48:30.61622Z","shell.execute_reply.started":"2022-05-06T09:48:30.56995Z","shell.execute_reply":"2022-05-06T09:48:30.615571Z"},"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-05-06T09:48:30.619168Z","iopub.execute_input":"2022-05-06T09:48:30.619371Z","iopub.status.idle":"2022-05-06T09:48:30.626455Z","shell.execute_reply.started":"2022-05-06T09:48:30.619348Z","shell.execute_reply":"2022-05-06T09:48:30.625668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, label_batch = next(iter(train_ds))","metadata":{"execution":{"iopub.status.busy":"2022-05-06T09:48:30.627765Z","iopub.execute_input":"2022-05-06T09:48:30.628061Z","iopub.status.idle":"2022-05-06T09:48:30.795814Z","shell.execute_reply.started":"2022-05-06T09:48:30.628025Z","shell.execute_reply":"2022-05-06T09:48:30.795084Z"},"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-05-06T09:48:30.796795Z","iopub.execute_input":"2022-05-06T09:48:30.796976Z","iopub.status.idle":"2022-05-06T09:48:31.148853Z","shell.execute_reply.started":"2022-05-06T09:48:30.796953Z","shell.execute_reply":"2022-05-06T09:48:31.148221Z"},"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-05-06T09:48:31.150056Z","iopub.execute_input":"2022-05-06T09:48:31.150445Z","iopub.status.idle":"2022-05-06T09:48:31.160565Z","shell.execute_reply.started":"2022-05-06T09:48:31.150409Z","shell.execute_reply":"2022-05-06T09:48:31.159761Z"},"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.5)(x)\n    x = tf.keras.layers.Dense(128, activation=\"relu\")(x)\n    x = tf.keras.layers.Dropout(0.5)(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-05-06T09:48:31.162666Z","iopub.execute_input":"2022-05-06T09:48:31.163689Z","iopub.status.idle":"2022-05-06T09:48:31.174821Z","shell.execute_reply.started":"2022-05-06T09:48:31.163649Z","shell.execute_reply":"2022-05-06T09:48:31.173882Z"},"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-05-06T09:48:31.544292Z","iopub.execute_input":"2022-05-06T09:48:31.544716Z","iopub.status.idle":"2022-05-06T09:48:34.18151Z","shell.execute_reply.started":"2022-05-06T09:48:31.544687Z","shell.execute_reply":"2022-05-06T09:48:34.180727Z"},"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-05-06T09:48:34.183132Z","iopub.execute_input":"2022-05-06T09:48:34.183411Z","iopub.status.idle":"2022-05-06T09:48:34.190029Z","shell.execute_reply.started":"2022-05-06T09:48:34.183379Z","shell.execute_reply":"2022-05-06T09:48:34.189236Z"},"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-05-06T09:48:34.193213Z","iopub.execute_input":"2022-05-06T09:48:34.193439Z","iopub.status.idle":"2022-05-06T09:48:34.214608Z","shell.execute_reply.started":"2022-05-06T09:48:34.193409Z","shell.execute_reply":"2022-05-06T09:48:34.213989Z"},"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-05-06T09:48:34.217637Z","iopub.execute_input":"2022-05-06T09:48:34.217821Z","iopub.status.idle":"2022-05-06T09:48:34.222498Z","shell.execute_reply.started":"2022-05-06T09:48:34.217799Z","shell.execute_reply":"2022-05-06T09:48:34.221194Z"},"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-05-06T09:48:34.224874Z","iopub.execute_input":"2022-05-06T09:48:34.225591Z","iopub.status.idle":"2022-05-06T10:32:46.039686Z","shell.execute_reply.started":"2022-05-06T09:48:34.225553Z","shell.execute_reply":"2022-05-06T10:32:46.038957Z"},"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-05-06T10:32:46.041212Z","iopub.execute_input":"2022-05-06T10:32:46.041548Z","iopub.status.idle":"2022-05-06T10:32:46.360536Z","shell.execute_reply.started":"2022-05-06T10:32:46.04151Z","shell.execute_reply":"2022-05-06T10:32:46.359846Z"},"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-05-06T10:32:46.361599Z","iopub.execute_input":"2022-05-06T10:32:46.363335Z","iopub.status.idle":"2022-05-06T10:32:50.529224Z","shell.execute_reply.started":"2022-05-06T10:32:46.363295Z","shell.execute_reply":"2022-05-06T10:32:50.528558Z"},"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-05-06T10:46:15.963224Z","iopub.execute_input":"2022-05-06T10:46:15.963754Z","iopub.status.idle":"2022-05-06T10:46:15.978977Z","shell.execute_reply.started":"2022-05-06T10:46:15.963717Z","shell.execute_reply":"2022-05-06T10:46:15.978274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(pred.flatten(), bins=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-06T10:45:39.886837Z","iopub.execute_input":"2022-05-06T10:45:39.887136Z","iopub.status.idle":"2022-05-06T10:45:40.088579Z","shell.execute_reply.started":"2022-05-06T10:45:39.887102Z","shell.execute_reply":"2022-05-06T10:45:40.087806Z"},"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.Target = temp\ndf_test.to_csv(\"./submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T10:46:20.292215Z","iopub.execute_input":"2022-05-06T10:46:20.292753Z","iopub.status.idle":"2022-05-06T10:46:20.310069Z","shell.execute_reply.started":"2022-05-06T10:46:20.292716Z","shell.execute_reply":"2022-05-06T10:46:20.309398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}