{"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\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' ","metadata":{"execution":{"iopub.status.busy":"2022-08-04T01:55:30.523838Z","iopub.execute_input":"2022-08-04T01:55:30.524149Z","iopub.status.idle":"2022-08-04T01:55:30.551349Z","shell.execute_reply.started":"2022-08-04T01:55:30.524069Z","shell.execute_reply":"2022-08-04T01:55:30.550672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T01:55:31.979233Z","iopub.execute_input":"2022-08-04T01:55:31.979856Z","iopub.status.idle":"2022-08-04T01:55:38.800734Z","shell.execute_reply.started":"2022-08-04T01:55:31.979820Z","shell.execute_reply":"2022-08-04T01:55:38.799920Z"},"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-4\nMIN_LR = 1e-6","metadata":{"execution":{"iopub.status.busy":"2022-08-04T02:51:49.899815Z","iopub.execute_input":"2022-08-04T02:51:49.900102Z","iopub.status.idle":"2022-08-04T02:51:49.905082Z","shell.execute_reply.started":"2022-08-04T02:51:49.900069Z","shell.execute_reply":"2022-08-04T02:51:49.904340Z"},"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-08-04T01:55:38.810480Z","iopub.execute_input":"2022-08-04T01:55:38.810772Z","iopub.status.idle":"2022-08-04T01:55:38.825987Z","shell.execute_reply.started":"2022-08-04T01:55:38.810731Z","shell.execute_reply":"2022-08-04T01:55:38.825161Z"},"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-08-04T01:55:40.307582Z","iopub.execute_input":"2022-08-04T01:55:40.309898Z","iopub.status.idle":"2022-08-04T01:55:40.367094Z","shell.execute_reply.started":"2022-08-04T01:55:40.309857Z","shell.execute_reply":"2022-08-04T01:55:40.366347Z"},"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-08-04T01:55:44.056029Z","iopub.execute_input":"2022-08-04T01:55:44.056331Z","iopub.status.idle":"2022-08-04T01:55:44.065813Z","shell.execute_reply.started":"2022-08-04T01:55:44.056293Z","shell.execute_reply":"2022-08-04T01:55:44.064961Z"},"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-08-04T01:55:44.647638Z","iopub.execute_input":"2022-08-04T01:55:44.648100Z","iopub.status.idle":"2022-08-04T01:55:44.655215Z","shell.execute_reply.started":"2022-08-04T01:55:44.648065Z","shell.execute_reply":"2022-08-04T01:55:44.654022Z"},"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-08-04T01:55:45.299400Z","iopub.execute_input":"2022-08-04T01:55:45.299877Z","iopub.status.idle":"2022-08-04T01:55:45.321221Z","shell.execute_reply.started":"2022-08-04T01:55:45.299844Z","shell.execute_reply":"2022-08-04T01:55:45.320465Z"},"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-08-04T01:55:47.020441Z","iopub.execute_input":"2022-08-04T01:55:47.021234Z","iopub.status.idle":"2022-08-04T01:55:47.321738Z","shell.execute_reply.started":"2022-08-04T01:55:47.021178Z","shell.execute_reply":"2022-08-04T01:55:47.321015Z"},"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-08-04T01:55:50.499586Z","iopub.execute_input":"2022-08-04T01:55:50.500063Z","iopub.status.idle":"2022-08-04T01:55:51.519961Z","shell.execute_reply.started":"2022-08-04T01:55:50.500027Z","shell.execute_reply":"2022-08-04T01:55:51.519328Z"},"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-08-04T01:55:55.963450Z","iopub.execute_input":"2022-08-04T01:55:55.964105Z","iopub.status.idle":"2022-08-04T01:55:55.985545Z","shell.execute_reply.started":"2022-08-04T01:55:55.964069Z","shell.execute_reply":"2022-08-04T01:55:55.984714Z"},"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-08-04T01:55:57.471708Z","iopub.execute_input":"2022-08-04T01:55:57.471968Z","iopub.status.idle":"2022-08-04T01:55:57.478762Z","shell.execute_reply.started":"2022-08-04T01:55:57.471939Z","shell.execute_reply":"2022-08-04T01:55:57.477947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T01:55:58.036793Z","iopub.execute_input":"2022-08-04T01:55:58.037070Z","iopub.status.idle":"2022-08-04T01:55:58.046269Z","shell.execute_reply.started":"2022-08-04T01:55:58.037038Z","shell.execute_reply":"2022-08-04T01:55:58.045542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = df_test.SOPInstanceUID.to_numpy()\n# test has no y label\n# y_test = df_test.iloc[:, 2:].to_numpy() ","metadata":{"execution":{"iopub.status.busy":"2022-08-04T01:55:59.743269Z","iopub.execute_input":"2022-08-04T01:55:59.743874Z","iopub.status.idle":"2022-08-04T01:55:59.748119Z","shell.execute_reply.started":"2022-08-04T01:55:59.743838Z","shell.execute_reply":"2022-08-04T01:55:59.747273Z"},"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-08-04T01:56:01.427789Z","iopub.execute_input":"2022-08-04T01:56:01.428069Z","iopub.status.idle":"2022-08-04T01:56:01.433341Z","shell.execute_reply.started":"2022-08-04T01:56:01.428033Z","shell.execute_reply":"2022-08-04T01:56:01.432539Z"},"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-08-04T01:56:12.252908Z","iopub.execute_input":"2022-08-04T01:56:12.253199Z","iopub.status.idle":"2022-08-04T01:56:12.269664Z","shell.execute_reply.started":"2022-08-04T01:56:12.253162Z","shell.execute_reply":"2022-08-04T01:56:12.268917Z"},"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-08-04T01:56:17.805222Z","iopub.execute_input":"2022-08-04T01:56:17.805864Z","iopub.status.idle":"2022-08-04T01:56:17.815792Z","shell.execute_reply.started":"2022-08-04T01:56:17.805826Z","shell.execute_reply":"2022-08-04T01:56:17.814822Z"},"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-08-04T01:56:20.251801Z","iopub.execute_input":"2022-08-04T01:56:20.252326Z","iopub.status.idle":"2022-08-04T01:56:20.256080Z","shell.execute_reply.started":"2022-08-04T01:56:20.252268Z","shell.execute_reply":"2022-08-04T01:56:20.255168Z"},"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-08-04T01:56:24.116338Z","iopub.execute_input":"2022-08-04T01:56:24.116666Z","iopub.status.idle":"2022-08-04T01:56:24.123462Z","shell.execute_reply.started":"2022-08-04T01:56:24.116628Z","shell.execute_reply":"2022-08-04T01:56:24.122653Z"},"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-08-04T01:56:43.115719Z","iopub.execute_input":"2022-08-04T01:56:43.116026Z","iopub.status.idle":"2022-08-04T01:56:46.554883Z","shell.execute_reply.started":"2022-08-04T01:56:43.115992Z","shell.execute_reply":"2022-08-04T01:56:46.553879Z"},"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-08-04T01:56:51.559295Z","iopub.execute_input":"2022-08-04T01:56:51.559790Z","iopub.status.idle":"2022-08-04T01:56:51.675654Z","shell.execute_reply.started":"2022-08-04T01:56:51.559753Z","shell.execute_reply":"2022-08-04T01:56:51.674946Z"},"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-08-04T01:58:00.639958Z","iopub.execute_input":"2022-08-04T01:58:00.640816Z","iopub.status.idle":"2022-08-04T01:58:00.684967Z","shell.execute_reply.started":"2022-08-04T01:58:00.640780Z","shell.execute_reply":"2022-08-04T01:58:00.684322Z"},"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-08-04T01:58:01.643247Z","iopub.execute_input":"2022-08-04T01:58:01.643746Z","iopub.status.idle":"2022-08-04T01:58:01.652051Z","shell.execute_reply.started":"2022-08-04T01:58:01.643709Z","shell.execute_reply":"2022-08-04T01:58:01.651190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, label_batch = next(iter(train_ds))","metadata":{"execution":{"iopub.status.busy":"2022-08-04T01:58:02.235636Z","iopub.execute_input":"2022-08-04T01:58:02.236423Z","iopub.status.idle":"2022-08-04T01:58:02.431956Z","shell.execute_reply.started":"2022-08-04T01:58:02.236375Z","shell.execute_reply":"2022-08-04T01:58:02.431226Z"},"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-08-04T01:58:06.495596Z","iopub.execute_input":"2022-08-04T01:58:06.496382Z","iopub.status.idle":"2022-08-04T01:58:06.848808Z","shell.execute_reply.started":"2022-08-04T01:58:06.496344Z","shell.execute_reply":"2022-08-04T01:58:06.848136Z"},"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-08-04T01:58:06.923657Z","iopub.execute_input":"2022-08-04T01:58:06.924161Z","iopub.status.idle":"2022-08-04T01:58:06.934436Z","shell.execute_reply.started":"2022-08-04T01:58:06.924125Z","shell.execute_reply":"2022-08-04T01:58:06.933597Z"},"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_Dense(input_shape, output_shape):\n    base_model = tf.keras.applications.densenet.DenseNet201(input_shape=input_shape,\n                                                            include_top=False)\n    flat1 = tf.keras.layers.Flatten()(base_model.layers[-1].output)\n    class1 = tf.keras.layers.Dense(1024, activation='relu')(flat1)\n    output = tf.keras.layers.Dense(output_shape, activation='softmax')(class1)\n    # define new model\n    model = tf.keras.Model(inputs=base_model.inputs, outputs=output)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-04T02:34:05.741075Z","iopub.execute_input":"2022-08-04T02:34:05.741371Z","iopub.status.idle":"2022-08-04T02:34:05.749121Z","shell.execute_reply.started":"2022-08-04T02:34:05.741338Z","shell.execute_reply":"2022-08-04T02:34:05.748332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.densenet.DenseNet201(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    x = tf.keras.layers.Dense(1024)(x)\n    outputs = tf.keras.layers.Dense(output_shape, activation=\"softmax\", dtype='float32')(x)\n\n    model = tf.keras.Model(inputs, outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-04T02:41:59.678150Z","iopub.execute_input":"2022-08-04T02:41:59.678902Z","iopub.status.idle":"2022-08-04T02:41:59.686049Z","shell.execute_reply.started":"2022-08-04T02:41:59.678864Z","shell.execute_reply":"2022-08-04T02:41:59.685172Z"},"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-08-04T02:52:31.513366Z","iopub.execute_input":"2022-08-04T02:52:31.513739Z","iopub.status.idle":"2022-08-04T02:52:37.310915Z","shell.execute_reply.started":"2022-08-04T02:52:31.513704Z","shell.execute_reply":"2022-08-04T02:52:37.310176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T02:34:15.468492Z","iopub.execute_input":"2022-08-04T02:34:15.469157Z","iopub.status.idle":"2022-08-04T02:34:15.805745Z","shell.execute_reply.started":"2022-08-04T02:34:15.469091Z","shell.execute_reply":"2022-08-04T02:34:15.804941Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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-08-04T02:36:36.553071Z","iopub.execute_input":"2022-08-04T02:36:36.553370Z","iopub.status.idle":"2022-08-04T02:36:36.557456Z","shell.execute_reply.started":"2022-08-04T02:36:36.553338Z","shell.execute_reply":"2022-08-04T02:36:36.556599Z"},"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=[tf.keras.metrics.BinaryAccuracy(name='bin_acc'),\n                      tfa.metrics.F1Score(name = 'f1',\n                          num_classes=22,\n                          average='micro',\n                          threshold=0.5)])","metadata":{"execution":{"iopub.status.busy":"2022-08-04T02:52:37.312678Z","iopub.execute_input":"2022-08-04T02:52:37.312946Z","iopub.status.idle":"2022-08-04T02:52:37.343433Z","shell.execute_reply.started":"2022-08-04T02:52:37.312911Z","shell.execute_reply":"2022-08-04T02:52:37.342786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_callbacks = tf.keras.callbacks.EarlyStopping(patience=PATIENCE, \n                                                monitor='val_f1',\n                                                mode = 'max',\n                                                min_delta=0.0005,\n                                                restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T03:52:36.476297Z","iopub.execute_input":"2022-08-04T03:52:36.476798Z","iopub.status.idle":"2022-08-04T03:52:36.482566Z","shell.execute_reply.started":"2022-08-04T03:52:36.476763Z","shell.execute_reply":"2022-08-04T03:52:36.481680Z"},"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-08-04T03:52:41.263200Z","iopub.execute_input":"2022-08-04T03:52:41.263799Z","iopub.status.idle":"2022-08-04T04:17:34.459044Z","shell.execute_reply.started":"2022-08-04T03:52:41.263765Z","shell.execute_reply":"2022-08-04T04:17:34.458210Z"},"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['bin_acc'], 'r', marker=\"o\", label='accuracy')\nax1.plot(history.history['val_bin_acc'], '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-17T16:29:12.672029Z","iopub.execute_input":"2022-07-17T16:29:12.672743Z","iopub.status.idle":"2022-07-17T16:30:07.383728Z","shell.execute_reply.started":"2022-07-17T16:29:12.672709Z","shell.execute_reply":"2022-07-17T16:30:07.381575Z"},"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-17T15:06:47.084062Z","iopub.status.idle":"2022-07-17T15:06:47.084480Z","shell.execute_reply.started":"2022-07-17T15:06:47.084261Z","shell.execute_reply":"2022-07-17T15:06:47.084283Z"},"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-17T15:06:47.086658Z","iopub.status.idle":"2022-07-17T15:06:47.088929Z","shell.execute_reply.started":"2022-07-17T15:06:47.087095Z","shell.execute_reply":"2022-07-17T15:06:47.087120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ds","metadata":{"execution":{"iopub.status.busy":"2022-07-17T15:06:47.090862Z","iopub.status.idle":"2022-07-17T15:06:47.091275Z","shell.execute_reply.started":"2022-07-17T15:06:47.091044Z","shell.execute_reply":"2022-07-17T15:06:47.091063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2022-07-17T15:06:47.092190Z","iopub.status.idle":"2022-07-17T15:06:47.092910Z","shell.execute_reply.started":"2022-07-17T15:06:47.092666Z","shell.execute_reply":"2022-07-17T15:06:47.092691Z"},"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-17T15:06:47.094874Z","iopub.status.idle":"2022-07-17T15:06:47.095511Z","shell.execute_reply.started":"2022-07-17T15:06:47.095262Z","shell.execute_reply":"2022-07-17T15:06:47.095287Z"},"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-17T15:06:47.096783Z","iopub.status.idle":"2022-07-17T15:06:47.097333Z","shell.execute_reply.started":"2022-07-17T15:06:47.097109Z","shell.execute_reply":"2022-07-17T15:06:47.097132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2022-07-17T15:06:47.098701Z","iopub.status.idle":"2022-07-17T15:06:47.099249Z","shell.execute_reply.started":"2022-07-17T15:06:47.099002Z","shell.execute_reply":"2022-07-17T15:06:47.099027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.Target = temp\ndf_test","metadata":{"execution":{"iopub.status.busy":"2022-07-17T15:06:47.100598Z","iopub.status.idle":"2022-07-17T15:06:47.101441Z","shell.execute_reply.started":"2022-07-17T15:06:47.101206Z","shell.execute_reply":"2022-07-17T15:06:47.101230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}