{"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 random\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow_io as tfio\nimport tensorflow_addons as tfa\nimport seaborn as sns\n\nfrom PIL import Image\nfrom PIL import ImageEnhance\nfrom PIL import ImageFilter as Filter\nfrom matplotlib import pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score, confusion_matrix\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.layers import InputLayer, Conv2D, MaxPooling2D, Input, Activation, Add, ReLU\nfrom tensorflow.keras.layers import BatchNormalization, Dropout, AveragePooling2D, Dense, Flatten, Concatenate\nfrom tensorflow.keras.activations import relu, softmax\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:27.015705Z","iopub.execute_input":"2022-07-25T04:21:27.016203Z","iopub.status.idle":"2022-07-25T04:21:33.639418Z","shell.execute_reply.started":"2022-07-25T04:21:27.016116Z","shell.execute_reply":"2022-07-25T04:21:33.638443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## config","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"train_meta_data = '../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train.csv'\ntrain_data_dir = '../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train'\nepochs = 200\nlr = 1e-3\nvalid_split = 0.2\ninput_size = 224\nbatch_size = 32\nclasses = 22\ninitializer = tf.keras.initializers.HeUniform()\noptimizer = tf.keras.optimizers.Nadam(learning_rate=lr)\nloss = tf.keras.losses.categorical_crossentropy\nweight_decay = 5e-4","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:33.641737Z","iopub.execute_input":"2022-07-25T04:21:33.642568Z","iopub.status.idle":"2022-07-25T04:21:33.659642Z","shell.execute_reply.started":"2022-07-25T04:21:33.642512Z","shell.execute_reply":"2022-07-25T04:21:33.658667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Pre-processing pipeline","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"test_image = Image.open('../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train/10093718510761734264315711288650185574.jpg')\ntest_arr = np.expand_dims(np.asarray(test_image),2)\ntest_arr_rgb = tf.image.grayscale_to_rgb(tf.convert_to_tensor(test_arr))","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:33.661176Z","iopub.execute_input":"2022-07-25T04:21:33.661498Z","iopub.status.idle":"2022-07-25T04:21:36.512231Z","shell.execute_reply.started":"2022-07-25T04:21:33.661466Z","shell.execute_reply":"2022-07-25T04:21:36.511255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[8,8], dpi=100)\nplt.imshow(test_arr,cmap=plt.cm.gray)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:36.513500Z","iopub.execute_input":"2022-07-25T04:21:36.514765Z","iopub.status.idle":"2022-07-25T04:21:36.834838Z","shell.execute_reply.started":"2022-07-25T04:21:36.514727Z","shell.execute_reply":"2022-07-25T04:21:36.833197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## augmentations apply:\n\n* edge enhance\n* de-texturization\n* filp right-left\n* flip up-down\n* random cropping","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"ada_thold = cv2.adaptiveThreshold(test_arr, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2.01)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:36.838514Z","iopub.execute_input":"2022-07-25T04:21:36.838814Z","iopub.status.idle":"2022-07-25T04:21:36.857578Z","shell.execute_reply.started":"2022-07-25T04:21:36.838789Z","shell.execute_reply":"2022-07-25T04:21:36.856700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\naxes[0].imshow(test_arr, cmap=plt.cm.gray)\naxes[1].imshow(ada_thold,cmap=plt.cm.gray)\n\naxes[0].axis('off')\naxes[1].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:36.858940Z","iopub.execute_input":"2022-07-25T04:21:36.859290Z","iopub.status.idle":"2022-07-25T04:21:37.387683Z","shell.execute_reply.started":"2022-07-25T04:21:36.859257Z","shell.execute_reply":"2022-07-25T04:21:37.386794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"laplacian = cv2.Laplacian(test_arr,cv2.CV_64F, ksize=5)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:37.388968Z","iopub.execute_input":"2022-07-25T04:21:37.390780Z","iopub.status.idle":"2022-07-25T04:21:37.418742Z","shell.execute_reply.started":"2022-07-25T04:21:37.390741Z","shell.execute_reply":"2022-07-25T04:21:37.417625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\naxes[0].imshow(test_arr, cmap=plt.cm.gray)\naxes[1].imshow(laplacian, cmap=plt.cm.gray)\n\naxes[0].axis('off')\naxes[1].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:37.420437Z","iopub.execute_input":"2022-07-25T04:21:37.421088Z","iopub.status.idle":"2022-07-25T04:21:38.026512Z","shell.execute_reply.started":"2022-07-25T04:21:37.421051Z","shell.execute_reply":"2022-07-25T04:21:38.025448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"edgeEnahnced = test_image.filter(Filter.EDGE_ENHANCE_MORE)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:38.028030Z","iopub.execute_input":"2022-07-25T04:21:38.028652Z","iopub.status.idle":"2022-07-25T04:21:38.037645Z","shell.execute_reply.started":"2022-07-25T04:21:38.028617Z","shell.execute_reply":"2022-07-25T04:21:38.036662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\naxes[0].imshow(test_arr, cmap=plt.cm.gray)\naxes[1].imshow(edgeEnahnced, cmap=plt.cm.gray)\n\naxes[0].axis('off')\naxes[1].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:38.039342Z","iopub.execute_input":"2022-07-25T04:21:38.040047Z","iopub.status.idle":"2022-07-25T04:21:38.629159Z","shell.execute_reply.started":"2022-07-25T04:21:38.040009Z","shell.execute_reply":"2022-07-25T04:21:38.628072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"de_texturize = cv2.bilateralFilter(test_arr,9,50,50)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:38.630872Z","iopub.execute_input":"2022-07-25T04:21:38.631219Z","iopub.status.idle":"2022-07-25T04:21:38.660147Z","shell.execute_reply.started":"2022-07-25T04:21:38.631187Z","shell.execute_reply":"2022-07-25T04:21:38.659298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\naxes[0].imshow(test_arr, cmap=plt.cm.gray)\naxes[1].imshow(de_texturize, cmap=plt.cm.gray)\n\naxes[0].axis('off')\naxes[1].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:38.662039Z","iopub.execute_input":"2022-07-25T04:21:38.662360Z","iopub.status.idle":"2022-07-25T04:21:39.200315Z","shell.execute_reply.started":"2022-07-25T04:21:38.662326Z","shell.execute_reply":"2022-07-25T04:21:39.199412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=1, ncols=3, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\naxes[0].imshow(test_arr, cmap=plt.cm.gray)\naxes[1].imshow(tf.image.adjust_saturation(test_arr_rgb, 1.4), cmap=plt.cm.gray)\naxes[2].imshow(tf.image.adjust_contrast(test_arr, 0.3), cmap=plt.cm.gray)\n\naxes[0].axis('off')\naxes[1].axis('off')\naxes[2].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:39.201860Z","iopub.execute_input":"2022-07-25T04:21:39.202544Z","iopub.status.idle":"2022-07-25T04:21:39.697260Z","shell.execute_reply.started":"2022-07-25T04:21:39.202488Z","shell.execute_reply":"2022-07-25T04:21:39.696440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = Image.open('../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train/10166555243811009418614649510306269973.jpg')\nimage_arr = np.expand_dims(np.asarray(image),2) \nv_center = image_arr.shape[1]//2\nh_center = image_arr.shape[0]//2\n\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\naxes[0].imshow(image_arr[:h_center,:,:], cmap=plt.cm.gray)\naxes[1].imshow(image_arr[h_center:,:,:], cmap=plt.cm.gray)\naxes[2].imshow(image_arr[:,:v_center,:], cmap=plt.cm.gray)\naxes[3].imshow(image_arr[:,v_center:,:], cmap=plt.cm.gray)\n\naxes[0].axis('off')\naxes[1].axis('off')\naxes[2].axis('off')\naxes[3].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:39.702832Z","iopub.execute_input":"2022-07-25T04:21:39.703559Z","iopub.status.idle":"2022-07-25T04:21:40.254280Z","shell.execute_reply.started":"2022-07-25T04:21:39.703509Z","shell.execute_reply":"2022-07-25T04:21:40.253441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ##### resize croped images without distroying aspect ratio","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"img = Image.fromarray(np.squeeze(image_arr[:h_center,:,:], axis=2)).convert('L')\nimg.thumbnail((400, 400))\n\nplt.imshow(img, cmap=plt.cm.gray)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:40.255652Z","iopub.execute_input":"2022-07-25T04:21:40.256544Z","iopub.status.idle":"2022-07-25T04:21:40.452426Z","shell.execute_reply.started":"2022-07-25T04:21:40.256493Z","shell.execute_reply":"2022-07-25T04:21:40.451549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def edge_enhancing(array):\n    method = np.random.choice(['ada_thold', 'laplacian', 'edge_enahnced'])\n    \n    if method=='ada_thold':     \n        return np.expand_dims(cv2.adaptiveThreshold(array, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 1), 2)\n    \n    elif method=='laplacian':\n        return np.expand_dims(cv2.Laplacian(array,cv2.CV_64F, ksize=5), 2)\n    \n    else:\n        image = Image.fromarray(np.squeeze(array, axis=2)).convert('L')\n        return np.expand_dims(np.asarray(image.filter(Filter.EDGE_ENHANCE_MORE)), 2)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:40.453805Z","iopub.execute_input":"2022-07-25T04:21:40.454197Z","iopub.status.idle":"2022-07-25T04:21:40.461440Z","shell.execute_reply.started":"2022-07-25T04:21:40.454162Z","shell.execute_reply":"2022-07-25T04:21:40.460576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def de_texturization(array):\n    n = np.random.choice([5, 9, 13, 15])\n    sigma = np.random.choice([50, 65, 75])\n    \n    return np.expand_dims(cv2.bilateralFilter(array, n, sigma, sigma), 2)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:40.462938Z","iopub.execute_input":"2022-07-25T04:21:40.463563Z","iopub.status.idle":"2022-07-25T04:21:40.476142Z","shell.execute_reply.started":"2022-07-25T04:21:40.463529Z","shell.execute_reply":"2022-07-25T04:21:40.475275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_flip(array):    \n    if random.choice([True, False]):\n        return tf.image.random_flip_left_right(array).numpy()\n    else:\n        return tf.image.random_flip_up_down(array).numpy()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:40.477743Z","iopub.execute_input":"2022-07-25T04:21:40.478081Z","iopub.status.idle":"2022-07-25T04:21:40.486224Z","shell.execute_reply.started":"2022-07-25T04:21:40.478049Z","shell.execute_reply":"2022-07-25T04:21:40.485347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tumbnail(array, shape=(512,512)):\n    return cv2.resize(array, shape) ","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:40.487353Z","iopub.execute_input":"2022-07-25T04:21:40.487656Z","iopub.status.idle":"2022-07-25T04:21:40.497569Z","shell.execute_reply.started":"2022-07-25T04:21:40.487622Z","shell.execute_reply":"2022-07-25T04:21:40.496451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_crop(array):\n    method = np.random.choice(['left', 'right', 'top', 'down'])\n    v_center = array.shape[1]//2\n    h_center = array.shape[0]//2\n    \n    if method == 'left':\n        return array[:,:v_center,:]\n    elif method == 'right':\n        return array[:,v_center:,:]\n    elif method == 'top':\n        return array[:h_center,:,:]\n    elif method == 'down':\n        return array[h_center:,:,:]\n    else:\n        return array","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:40.499038Z","iopub.execute_input":"2022-07-25T04:21:40.499439Z","iopub.status.idle":"2022-07-25T04:21:40.508219Z","shell.execute_reply.started":"2022-07-25T04:21:40.499404Z","shell.execute_reply":"2022-07-25T04:21:40.507377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = Image.open('../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train/10166555243811009418614649510306269973.jpg')\nimage_arr = np.expand_dims(np.asarray(image), axis=2)\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\nfor i in range(len(axes)):\n    axes[i].imshow(edge_enhancing(image_arr), cmap=plt.cm.gray)\n    axes[i].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:40.509783Z","iopub.execute_input":"2022-07-25T04:21:40.510224Z","iopub.status.idle":"2022-07-25T04:21:41.231510Z","shell.execute_reply.started":"2022-07-25T04:21:40.510192Z","shell.execute_reply":"2022-07-25T04:21:41.230574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_arr = np.expand_dims(np.asarray(image), axis=2)\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\nfor i in range(len(axes)):\n    axes[i].imshow(de_texturization(image_arr), cmap=plt.cm.gray)\n    axes[i].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:41.233157Z","iopub.execute_input":"2022-07-25T04:21:41.235817Z","iopub.status.idle":"2022-07-25T04:21:42.305743Z","shell.execute_reply.started":"2022-07-25T04:21:41.235772Z","shell.execute_reply":"2022-07-25T04:21:42.304775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_arr = np.expand_dims(np.asarray(image), axis=2)\nfig, axes = plt.subplots(nrows=1, ncols=4, figsize=[12,6], dpi=200)\naxes = axes.ravel()\n\nfor i in range(len(axes)):\n    img = tumbnail(random_crop(image_arr))\n    axes[i].imshow(img, cmap=plt.cm.gray)\n    axes[i].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:42.306783Z","iopub.execute_input":"2022-07-25T04:21:42.307118Z","iopub.status.idle":"2022-07-25T04:21:42.921293Z","shell.execute_reply.started":"2022-07-25T04:21:42.307086Z","shell.execute_reply":"2022-07-25T04:21:42.920377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pre_procrssing(image):\n    temp = np.asarray(image)\n    \n    if np.random.choice([True, False], p=[0.45, 0.55]):\n        # other augmentaions\n        temp = de_texturization(temp)\n        \n        # crop\n        if random.choice([True, False]):\n            temp = tf.image.random_crop(temp, (128,128,1)).numpy()\n        else:\n            temp = random_crop(temp)\n        \n        temp = tumbnail(temp, (input_size, input_size))\n\n        return np.expand_dims(temp, 2)\n    \n    else:\n        return temp","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:42.922714Z","iopub.execute_input":"2022-07-25T04:21:42.923798Z","iopub.status.idle":"2022-07-25T04:21:42.931579Z","shell.execute_reply.started":"2022-07-25T04:21:42.923760Z","shell.execute_reply":"2022-07-25T04:21:42.930466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Pre-processing Pipeline","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"files = os.listdir('../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train')\nrand_files = random.choices(files, k=24)\n\nfig, axes = plt.subplots(nrows=4, ncols=6, figsize=[12,8], dpi=200)\naxes = axes.ravel()\n\nfor i in range(len(axes)):\n    img = Image.open(os.path.join('../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train',rand_files[i]))\n    img = pre_procrssing(img)\n    axes[i].imshow(img, cmap=plt.cm.gray)\n    axes[i].axis('off')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:42.934410Z","iopub.execute_input":"2022-07-25T04:21:42.935362Z","iopub.status.idle":"2022-07-25T04:21:45.427387Z","shell.execute_reply.started":"2022-07-25T04:21:42.935303Z","shell.execute_reply":"2022-07-25T04:21:45.426417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/xray-body-images-in-png-unifesp-competion/train_df.csv', usecols=['image_path', 'Target'])\n# train_df['file_name'] = train_df['file_name'] + '.jpg'\ntrain_df","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:45.428768Z","iopub.execute_input":"2022-07-25T04:21:45.430014Z","iopub.status.idle":"2022-07-25T04:21:45.480536Z","shell.execute_reply.started":"2022-07-25T04:21:45.429879Z","shell.execute_reply":"2022-07-25T04:21:45.479500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df['file_name'] = '../input/unifesp-xray-body-part-classifier-dataset-jpeg/data/train/' + train_df['file_name']\ntrain_df['Target'] = train_df['Target'].str.strip()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:45.481798Z","iopub.execute_input":"2022-07-25T04:21:45.483229Z","iopub.status.idle":"2022-07-25T04:21:45.492186Z","shell.execute_reply.started":"2022-07-25T04:21:45.483187Z","shell.execute_reply":"2022-07-25T04:21:45.491330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multi_labels = [i for i, target in enumerate(train_df['Target']) if len(target) > 2]\nlen(multi_labels)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:45.493958Z","iopub.execute_input":"2022-07-25T04:21:45.494339Z","iopub.status.idle":"2022-07-25T04:21:45.506043Z","shell.execute_reply.started":"2022-07-25T04:21:45.494287Z","shell.execute_reply":"2022-07-25T04:21:45.505079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corrected_labels = []\n\nfor ml in multi_labels:    \n    corrected_labels.append([train_df.loc[ml, 'image_path'], train_df.loc[ml, 'Target'].split(' ')[0]])","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:45.507861Z","iopub.execute_input":"2022-07-25T04:21:45.508339Z","iopub.status.idle":"2022-07-25T04:21:45.521717Z","shell.execute_reply.started":"2022-07-25T04:21:45.508304Z","shell.execute_reply":"2022-07-25T04:21:45.520854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.concat([train_df.drop(train_df.loc[multi_labels].index),\n                     pd.DataFrame(corrected_labels, columns=['image_path', 'Target'])], ignore_index=True)\ntrain_df","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:21:45.522818Z","iopub.execute_input":"2022-07-25T04:21:45.523913Z","iopub.status.idle":"2022-07-25T04:21:45.546838Z","shell.execute_reply.started":"2022-07-25T04:21:45.523867Z","shell.execute_reply":"2022-07-25T04:21:45.545869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:21:45.548217Z","iopub.execute_input":"2022-07-25T04:21:45.548641Z","iopub.status.idle":"2022-07-25T04:21:45.562648Z","shell.execute_reply.started":"2022-07-25T04:21:45.548608Z","shell.execute_reply":"2022-07-25T04:21:45.560676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_path'] = train_df['image_path'].str.replace('./images','../input/xray-body-images-in-png-unifesp-competion/images')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:21:45.564106Z","iopub.execute_input":"2022-07-25T04:21:45.564708Z","iopub.status.idle":"2022-07-25T04:21:45.577472Z","shell.execute_reply.started":"2022-07-25T04:21:45.564673Z","shell.execute_reply":"2022-07-25T04:21:45.576431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create more training samples","metadata":{}},{"cell_type":"code","source":"new_train_data = []\n\nif not os.path.isdir('./train'):\n    os.mkdir('./train')\n    \nfor j, (tar, file) in enumerate(train_df.values):\n    img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)\n    img = np.expand_dims(img, 2)\n    \n    for i, scale in enumerate([0.5, 0.65, 0.8, 0.95]):\n        new_file_path = f\"./train/c-{i}-{file.split('/')[-1]}\"\n        temp = tf.image.central_crop(img, scale).numpy()\n        temp = edge_enhancing(temp)\n        cv2.imwrite(new_file_path, temp)\n        new_train_data.append([tar, new_file_path])\n        \n        print(f'{j}/{train_df.shape[0]} -- {i+1}', end='\\r')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:21:45.579128Z","iopub.execute_input":"2022-07-25T04:21:45.579834Z","iopub.status.idle":"2022-07-25T04:22:29.686870Z","shell.execute_reply.started":"2022-07-25T04:21:45.579801Z","shell.execute_reply":"2022-07-25T04:22:29.685817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = os.listdir('./train/')\nrand_files = random.choices(files, k=24)\n\nfig, axes = plt.subplots(nrows=4, ncols=6, figsize=[12,8], dpi=200)\naxes = axes.ravel()\n\nfor i in range(len(axes)):\n    img = Image.open(os.path.join('./train',rand_files[i]))\n    axes[i].imshow(img, cmap=plt.cm.gray)\n    axes[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:22:29.688471Z","iopub.execute_input":"2022-07-25T04:22:29.689776Z","iopub.status.idle":"2022-07-25T04:22:31.569693Z","shell.execute_reply.started":"2022-07-25T04:22:29.689737Z","shell.execute_reply":"2022-07-25T04:22:31.568496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_data = pd.DataFrame(new_train_data, columns=['Target', 'image_path'])\nnew_train_data","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:22:31.571094Z","iopub.execute_input":"2022-07-25T04:22:31.571730Z","iopub.status.idle":"2022-07-25T04:22:31.589748Z","shell.execute_reply.started":"2022-07-25T04:22:31.571688Z","shell.execute_reply":"2022-07-25T04:22:31.588799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.concat([train_df, new_train_data], ignore_index=True)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:22:31.591145Z","iopub.execute_input":"2022-07-25T04:22:31.592040Z","iopub.status.idle":"2022-07-25T04:22:31.606392Z","shell.execute_reply.started":"2022-07-25T04:22:31.592000Z","shell.execute_reply":"2022-07-25T04:22:31.605471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, test = train_test_split(train_df[['image_path', 'Target']], test_size=0.1)\ntrain.shape, test.shape","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:22:31.607795Z","iopub.execute_input":"2022-07-25T04:22:31.608760Z","iopub.status.idle":"2022-07-25T04:22:31.619670Z","shell.execute_reply.started":"2022-07-25T04:22:31.608719Z","shell.execute_reply":"2022-07-25T04:22:31.618770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build Model","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"markdown","source":"### Config Data Loders","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"generator = ImageDataGenerator(rescale=1 / 255,\n                               rotation_range=10,\n                               width_shift_range=0.25,\n                               height_shift_range=0.25,\n                               shear_range=0.2,\n                               horizontal_flip=True,\n                               vertical_flip=True,\n                               samplewise_center=True,\n                               samplewise_std_normalization=True,\n                               validation_split=valid_split,\n                               preprocessing_function=pre_procrssing\n                              )\n\ntest_generator = ImageDataGenerator(rescale=1 / 255,\n                                    samplewise_center=True,\n                                    samplewise_std_normalization=True)\n\ntrain_datagen = generator.flow_from_dataframe(dataframe=train,\n                                              x_col='image_path',\n                                              y_col='Target',\n                                              target_size=(input_size, input_size),\n                                              batch_size=batch_size,\n                                              color_mode='grayscale',\n                                              subset='training',\n                                              seed=48)\n\nvalid_datagen = generator.flow_from_dataframe(dataframe=train,\n                                              x_col='image_path',\n                                              y_col='Target',\n                                              target_size=(input_size, input_size),\n                                              batch_size=batch_size,\n                                              color_mode='grayscale',\n                                              subset='validation',\n                                              shuffle=False,\n                                              seed=48)\n\ntest_datagen = test_generator.flow_from_dataframe(dataframe=test,\n                                                  x_col='image_path',\n                                                  y_col='Target',\n                                                  color_mode='grayscale',\n                                                  batch_size=batch_size,\n                                                  target_size=(input_size, input_size),\n                                                  shuffle=False,\n                                                  seed=48)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:22:31.621370Z","iopub.execute_input":"2022-07-25T04:22:31.621764Z","iopub.status.idle":"2022-07-25T04:22:32.532898Z","shell.execute_reply.started":"2022-07-25T04:22:31.621732Z","shell.execute_reply":"2022-07-25T04:22:32.532007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train mini batch","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=4, ncols=8, figsize=[32, 10], dpi=200)\naxes = axes.ravel()\n\nfor i, arr in enumerate(train_datagen.next()[0]):\n    img = tf.keras.utils.array_to_img(arr)\n    axes[i].imshow(img, cmap=plt.cm.gray)\n    \nplt.show()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:22:32.534493Z","iopub.execute_input":"2022-07-25T04:22:32.534841Z","iopub.status.idle":"2022-07-25T04:22:36.894615Z","shell.execute_reply.started":"2022-07-25T04:22:32.534807Z","shell.execute_reply":"2022-07-25T04:22:36.893538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Validation mini batch","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=4, ncols=8, figsize=[32, 10], dpi=200)\naxes = axes.ravel()\n\nfor i, arr in enumerate(valid_datagen.next()[0]):\n    img = tf.keras.utils.array_to_img(arr)\n    axes[i].imshow(img, cmap=plt.cm.gray)\n    \nplt.show()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:22:36.896153Z","iopub.execute_input":"2022-07-25T04:22:36.900140Z","iopub.status.idle":"2022-07-25T04:22:41.674743Z","shell.execute_reply.started":"2022-07-25T04:22:36.900102Z","shell.execute_reply":"2022-07-25T04:22:41.669604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test mini batch","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=4, ncols=8, figsize=[32, 10], dpi=200)\naxes = axes.ravel()\n\nfor i, arr in enumerate(test_datagen.next()[0]):\n    img = tf.keras.utils.array_to_img(arr)\n    axes[i].imshow(img, cmap=plt.cm.gray)\n    \nplt.show()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:22:41.683660Z","iopub.execute_input":"2022-07-25T04:22:41.684025Z","iopub.status.idle":"2022-07-25T04:22:46.193166Z","shell.execute_reply.started":"2022-07-25T04:22:41.683993Z","shell.execute_reply":"2022-07-25T04:22:46.188603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Base Estimator Sequential Model","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"early_stop = tf.keras.callbacks.EarlyStopping(patience=25,\n                                              monitor='val_loss',\n                                              restore_best_weights=True,\n                                              verbose=1)\n\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(patience=5,\n                                                 monitor='val_loss',\n                                                 factor=0.5,\n                                                 verbose=1)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:22:46.194888Z","iopub.execute_input":"2022-07-25T04:22:46.195755Z","iopub.status.idle":"2022-07-25T04:22:46.202257Z","shell.execute_reply.started":"2022-07-25T04:22:46.195714Z","shell.execute_reply":"2022-07-25T04:22:46.201390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_input = Input((input_size, input_size, 1))\nfeat_exc_input = Concatenate()([model_input, model_input, model_input])\nfeature_extractor = tf.keras.applications.Xception(include_top=False,\n                                                   weights=\"imagenet\",\n                                                   input_tensor=feat_exc_input)\n\nmodel = Sequential([feature_extractor,\n                    AveragePooling2D((3,3)),\n                    Flatten(),\n                    Dense(units=classes, activation=softmax)])\n\nmodel.compile(optimizer=optimizer,\n                  loss=loss,\n                  metrics=['accuracy', tfa.metrics.F1Score(num_classes=classes, threshold=0.5)])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:22:46.203673Z","iopub.execute_input":"2022-07-25T04:22:46.204250Z","iopub.status.idle":"2022-07-25T04:22:48.277793Z","shell.execute_reply.started":"2022-07-25T04:22:46.204215Z","shell.execute_reply":"2022-07-25T04:22:48.276865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T04:22:48.279069Z","iopub.execute_input":"2022-07-25T04:22:48.279425Z","iopub.status.idle":"2022-07-25T04:22:48.295379Z","shell.execute_reply.started":"2022-07-25T04:22:48.279381Z","shell.execute_reply":"2022-07-25T04:22:48.294376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training and Evaluation","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"history = model.fit(train_datagen,\n                    validation_data=valid_datagen,\n                    batch_size=batch_size,\n                    epochs=epochs,\n                    callbacks=[early_stop,reduce_lr])","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T04:22:48.296879Z","iopub.execute_input":"2022-07-25T04:22:48.297677Z","iopub.status.idle":"2022-07-25T07:33:05.596968Z","shell.execute_reply.started":"2022-07-25T04:22:48.297640Z","shell.execute_reply":"2022-07-25T07:33:05.596013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_datagen)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:05.598218Z","iopub.execute_input":"2022-07-25T07:33:05.598577Z","iopub.status.idle":"2022-07-25T07:33:10.833385Z","shell.execute_reply.started":"2022-07-25T07:33:05.598542Z","shell.execute_reply":"2022-07-25T07:33:10.832474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = np.argmax(model.predict(test_datagen), axis=1)\npred_y = [{v:k for k,v in train_datagen.class_indices.items()}[k] for k in test_preds]\ntrue_y = test.Target.values","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:10.834748Z","iopub.execute_input":"2022-07-25T07:33:10.835187Z","iopub.status.idle":"2022-07-25T07:33:13.662602Z","shell.execute_reply.started":"2022-07-25T07:33:10.835148Z","shell.execute_reply":"2022-07-25T07:33:13.661638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_score_test = accuracy_score(true_y, pred_y)\nconf_metric = confusion_matrix(true_y, pred_y, labels=list(train_datagen.class_indices.keys()))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:13.664057Z","iopub.execute_input":"2022-07-25T07:33:13.664389Z","iopub.status.idle":"2022-07-25T07:33:13.678622Z","shell.execute_reply.started":"2022-07-25T07:33:13.664355Z","shell.execute_reply":"2022-07-25T07:33:13.677589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[12,12], dpi=100)\nsns.heatmap(np.round(conf_metric/np.sum(conf_metric, axis=1), 2),\n            cbar=False,\n            annot=True,\n            annot_kws={\"size\": 9},\n            cmap=plt.cm.Blues)\nplt.xlabel('True labels')\nplt.ylabel('Predicted labels')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:13.680261Z","iopub.execute_input":"2022-07-25T07:33:13.680945Z","iopub.status.idle":"2022-07-25T07:33:15.096360Z","shell.execute_reply.started":"2022-07-25T07:33:13.680896Z","shell.execute_reply":"2022-07-25T07:33:15.095463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[12,6], dpi=300)\nsns.lineplot(x=list(range(len(history.history['accuracy']))),\n             y=history.history['accuracy'],\n             label='train')\nsns.lineplot(x=list(range(len(history.history['val_accuracy']))),\n             y=history.history['val_accuracy'],\n             label='validation')\nplt.show()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:15.097821Z","iopub.execute_input":"2022-07-25T07:33:15.099794Z","iopub.status.idle":"2022-07-25T07:33:15.592642Z","shell.execute_reply.started":"2022-07-25T07:33:15.099753Z","shell.execute_reply":"2022-07-25T07:33:15.591687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[12,6], dpi=300)\nsns.lineplot(x=list(range(len(np.mean(history.history['f1_score'], axis=1)))),\n             y=np.mean(history.history['f1_score'], axis=1),\n             label='train')\nsns.lineplot(x=list(range(len(np.mean(history.history['val_f1_score'], axis=1)))),\n             y=np.mean(history.history['val_f1_score'], axis=1),\n             label='validation')\nplt.show()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:15.593862Z","iopub.execute_input":"2022-07-25T07:33:15.594807Z","iopub.status.idle":"2022-07-25T07:33:16.083349Z","shell.execute_reply.started":"2022-07-25T07:33:15.594772Z","shell.execute_reply":"2022-07-25T07:33:16.082443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[12,6], dpi=300)\nsns.lineplot(x=list(range(len(history.history['loss']))),\n             y=history.history['loss'],\n             label='train')\nsns.lineplot(x=list(range(len(history.history['val_loss']))),\n             y=history.history['val_loss'],\n             label='validation')\nplt.show()","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:16.084768Z","iopub.execute_input":"2022-07-25T07:33:16.085338Z","iopub.status.idle":"2022-07-25T07:33:17.100056Z","shell.execute_reply.started":"2022-07-25T07:33:16.085299Z","shell.execute_reply":"2022-07-25T07:33:17.097598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Saving files","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"temp = pd.DataFrame(history.history)\ntemp.to_csv('model_Xception.csv', index=False)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:17.103419Z","iopub.execute_input":"2022-07-25T07:33:17.106421Z","iopub.status.idle":"2022-07-25T07:33:17.187068Z","shell.execute_reply.started":"2022-07-25T07:33:17.106374Z","shell.execute_reply":"2022-07-25T07:33:17.186019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model_Xception.hdf5')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:17.188396Z","iopub.execute_input":"2022-07-25T07:33:17.188922Z","iopub.status.idle":"2022-07-25T07:33:18.006833Z","shell.execute_reply.started":"2022-07-25T07:33:17.188881Z","shell.execute_reply":"2022-07-25T07:33:18.005801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('model_Xception_weight.hdf5')","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:18.008455Z","iopub.execute_input":"2022-07-25T07:33:18.008814Z","iopub.status.idle":"2022-07-25T07:33:18.248974Z","shell.execute_reply.started":"2022-07-25T07:33:18.008777Z","shell.execute_reply":"2022-07-25T07:33:18.248007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Inference","metadata":{"pycharm":{"name":"#%% md\n"}}},{"cell_type":"code","source":"test_loc = '../input/xray-body-images-in-png-unifesp-competion/images/test'\n\ntest_data = ImageDataGenerator(rescale=1.0/255,\n                               samplewise_center=True,\n                               samplewise_std_normalization=True).flow_from_directory(directory=test_loc,\n                                                                                      target_size=(input_size, input_size),\n                                                                                      batch_size=batch_size,\n                                                                                      classes=['.'],\n                                                                                      color_mode='grayscale',\n                                                                                      shuffle=False)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:18.250578Z","iopub.execute_input":"2022-07-25T07:33:18.251137Z","iopub.status.idle":"2022-07-25T07:33:18.753788Z","shell.execute_reply.started":"2022-07-25T07:33:18.251101Z","shell.execute_reply":"2022-07-25T07:33:18.752861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_p = model.predict(test_data, verbose=1)","metadata":{"pycharm":{"name":"#%%\n"},"execution":{"iopub.status.busy":"2022-07-25T07:33:18.755608Z","iopub.execute_input":"2022-07-25T07:33:18.755874Z","iopub.status.idle":"2022-07-25T07:33:29.240462Z","shell.execute_reply.started":"2022-07-25T07:33:18.755850Z","shell.execute_reply":"2022-07-25T07:33:29.239271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inverse_map = {v:k for k,v in train_datagen.class_indices.items()}\ninverse_map","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:29.242367Z","iopub.execute_input":"2022-07-25T07:33:29.242715Z","iopub.status.idle":"2022-07-25T07:33:29.250777Z","shell.execute_reply.started":"2022-07-25T07:33:29.242682Z","shell.execute_reply":"2022-07-25T07:33:29.249865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = []\n\nfor i in range(len(preds_p)):\n    multi_lab = np.where(preds_p[i]>0.5)[0].tolist()\n    \n    if len(multi_lab) > 1:\n        test_preds.append([test_data.filenames[i], ' '.join([inverse_map[m] for m in multi_lab])])\n    else:\n        test_preds.append([test_data.filenames[i], inverse_map[np.argmax(preds_p[i])]])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:29.252512Z","iopub.execute_input":"2022-07-25T07:33:29.253203Z","iopub.status.idle":"2022-07-25T07:33:29.295260Z","shell.execute_reply.started":"2022-07-25T07:33:29.253165Z","shell.execute_reply":"2022-07-25T07:33:29.294310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = pd.DataFrame(test_preds, columns=['SOPInstanceUID', 'Target'])\ntest_preds","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:29.296994Z","iopub.execute_input":"2022-07-25T07:33:29.297363Z","iopub.status.idle":"2022-07-25T07:33:29.318859Z","shell.execute_reply.started":"2022-07-25T07:33:29.297327Z","shell.execute_reply":"2022-07-25T07:33:29.317874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds['SOPInstanceUID'] = test_preds['SOPInstanceUID'].str.replace('-c.png', '')\ntest_preds['SOPInstanceUID'] = test_preds['SOPInstanceUID'].str.replace('./', '')\ntest_preds","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:29.320447Z","iopub.execute_input":"2022-07-25T07:33:29.320977Z","iopub.status.idle":"2022-07-25T07:33:29.341841Z","shell.execute_reply.started":"2022-07-25T07:33:29.320935Z","shell.execute_reply":"2022-07-25T07:33:29.340859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds.to_csv('model_submission_v5.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:29.343281Z","iopub.execute_input":"2022-07-25T07:33:29.343936Z","iopub.status.idle":"2022-07-25T07:33:29.354565Z","shell.execute_reply.started":"2022-07-25T07:33:29.343879Z","shell.execute_reply":"2022-07-25T07:33:29.353577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_rows = 150\ntest_preds.Target.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T07:33:29.356078Z","iopub.execute_input":"2022-07-25T07:33:29.356870Z","iopub.status.idle":"2022-07-25T07:33:29.366642Z","shell.execute_reply.started":"2022-07-25T07:33:29.356835Z","shell.execute_reply":"2022-07-25T07:33:29.365583Z"},"trusted":true},"execution_count":null,"outputs":[]}]}