{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Intro\n\n**CheXNet** [[1]](https://arxiv.org/pdf/1711.05225.pdf) is a 121 layer **DenseNet** developed by Stanford researchers that can detect pneumonia from chest X-rays at a level exceeding practicing radiologists. \n\nThe weights of the model are uploaded into this notebook and used to train on our data to classify normal vs opacity (typical, atypical, indeterminate) cases. \n\nContrast Limited Adaptive Histogram Equalization (**CLAHE**) is used for preprocessing and some augmentation techniques are applied. \nFor interpretability, **GRAD-CAM** is used to see if the model is paying attention to the opacities (comparing to the groundtruth bounding boxes).","metadata":{}},{"cell_type":"markdown","source":"### nb work done on top of:\n\nhttps://www.kaggle.com/sinamhd9/chexnet-fine-tuned-model-interpretation","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### get data - \n\nstart from the eda notebook","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:35:55.366677Z","iopub.execute_input":"2021-07-17T18:35:55.366995Z","iopub.status.idle":"2021-07-17T18:35:55.371114Z","shell.execute_reply.started":"2021-07-17T18:35:55.366944Z","shell.execute_reply":"2021-07-17T18:35:55.369999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df = pd.read_csv(\"../input/siim-covid19-detection/train_study_level.csv\")\nstudy_df[\"id\"] = study_df[\"id\"].str.replace(\"_study\", \"\")\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:35:55.372881Z","iopub.execute_input":"2021-07-17T18:35:55.373470Z","iopub.status.idle":"2021-07-17T18:35:55.405792Z","shell.execute_reply.started":"2021-07-17T18:35:55.373430Z","shell.execute_reply":"2021-07-17T18:35:55.405102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:35:55.408576Z","iopub.execute_input":"2021-07-17T18:35:55.408817Z","iopub.status.idle":"2021-07-17T18:35:55.425137Z","shell.execute_reply.started":"2021-07-17T18:35:55.408794Z","shell.execute_reply":"2021-07-17T18:35:55.424281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:35:55.426602Z","iopub.execute_input":"2021-07-17T18:35:55.426967Z","iopub.status.idle":"2021-07-17T18:35:55.430833Z","shell.execute_reply.started":"2021-07-17T18:35:55.426919Z","shell.execute_reply":"2021-07-17T18:35:55.429854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_absolute_file_paths(directory):\n    all_abs_file_paths = []\n    for dirpath,_,filenames in os.walk(directory):\n        for f in filenames:\n            all_abs_file_paths.append(os.path.abspath(os.path.join(dirpath, f)))\n    return all_abs_file_paths","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:35:55.432173Z","iopub.execute_input":"2021-07-17T18:35:55.432663Z","iopub.status.idle":"2021-07-17T18:35:55.439219Z","shell.execute_reply.started":"2021-07-17T18:35:55.432627Z","shell.execute_reply":"2021-07-17T18:35:55.438221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm; tqdm.pandas();\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:35:55.441114Z","iopub.execute_input":"2021-07-17T18:35:55.441571Z","iopub.status.idle":"2021-07-17T18:35:55.447261Z","shell.execute_reply.started":"2021-07-17T18:35:55.441537Z","shell.execute_reply":"2021-07-17T18:35:55.446310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    study_df[\"study_dir\"] = \"/kaggle/input/siim-covid19-detection/train/\"+study_df[\"id\"]\n    study_df[\"images_per_study\"] = study_df.study_dir.progress_apply(lambda x: len(get_absolute_file_paths(x)))\n#     study_df[\"images_per_study\"] = study_df.study_dir.apply(lambda x: len(get_absolute_file_paths(x)))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:35:55.465835Z","iopub.execute_input":"2021-07-17T18:35:55.466091Z","iopub.status.idle":"2021-07-17T18:36:18.940335Z","shell.execute_reply.started":"2021-07-17T18:35:55.466069Z","shell.execute_reply":"2021-07-17T18:36:18.939410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:18.942023Z","iopub.execute_input":"2021-07-17T18:36:18.942408Z","iopub.status.idle":"2021-07-17T18:36:18.957249Z","shell.execute_reply.started":"2021-07-17T18:36:18.942368Z","shell.execute_reply":"2021-07-17T18:36:18.955979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df = pd.read_csv(\"/kaggle/input/siim-covid19-detection/train_image_level.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:18.959707Z","iopub.execute_input":"2021-07-17T18:36:18.960133Z","iopub.status.idle":"2021-07-17T18:36:19.015246Z","shell.execute_reply.started":"2021-07-17T18:36:18.960091Z","shell.execute_reply":"2021-07-17T18:36:19.014398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.016653Z","iopub.execute_input":"2021-07-17T18:36:19.017026Z","iopub.status.idle":"2021-07-17T18:36:19.031888Z","shell.execute_reply.started":"2021-07-17T18:36:19.016994Z","shell.execute_reply":"2021-07-17T18:36:19.031087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df = pd.merge(image_df, \n                          study_df, \n                          left_on='StudyInstanceUID',\n                          right_on='id')","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.034740Z","iopub.execute_input":"2021-07-17T18:36:19.035017Z","iopub.status.idle":"2021-07-17T18:36:19.049773Z","shell.execute_reply.started":"2021-07-17T18:36:19.034990Z","shell.execute_reply":"2021-07-17T18:36:19.049036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.050876Z","iopub.execute_input":"2021-07-17T18:36:19.051224Z","iopub.status.idle":"2021-07-17T18:36:19.066076Z","shell.execute_reply.started":"2021-07-17T18:36:19.051190Z","shell.execute_reply":"2021-07-17T18:36:19.065125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.067652Z","iopub.execute_input":"2021-07-17T18:36:19.068120Z","iopub.status.idle":"2021-07-17T18:36:19.079921Z","shell.execute_reply.started":"2021-07-17T18:36:19.068084Z","shell.execute_reply":"2021-07-17T18:36:19.078662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_image = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\n# df_study['id'] = df_study['id'].str.replace('_study',\"\")\n# df_study.rename({'id': 'StudyInstanceUID'},axis=1, inplace=True)\n# df_train = df_image.merge(df_study, on='StudyInstanceUID')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.083849Z","iopub.execute_input":"2021-07-17T18:36:19.084273Z","iopub.status.idle":"2021-07-17T18:36:19.088289Z","shell.execute_reply.started":"2021-07-17T18:36:19.084229Z","shell.execute_reply":"2021-07-17T18:36:19.086994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### in this nb, we use the jpg variant of data","metadata":{}},{"cell_type":"markdown","source":"### attempt1: image binary classifier - opacity vs. none","metadata":{}},{"cell_type":"code","source":"image_study_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.090917Z","iopub.execute_input":"2021-07-17T18:36:19.091325Z","iopub.status.idle":"2021-07-17T18:36:19.111673Z","shell.execute_reply.started":"2021-07-17T18:36:19.091286Z","shell.execute_reply":"2021-07-17T18:36:19.110736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df.loc[image_study_df['Negative for Pneumonia']==1, 'study_label'] = 'negative'\nimage_study_df.loc[image_study_df['Typical Appearance']==1, 'study_label'] = 'typical'\nimage_study_df.loc[image_study_df['Indeterminate Appearance']==1, 'study_label'] = 'indeterminate'\nimage_study_df.loc[image_study_df['Atypical Appearance']==1, 'study_label'] = 'atypical'\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.113877Z","iopub.execute_input":"2021-07-17T18:36:19.114214Z","iopub.status.idle":"2021-07-17T18:36:19.132059Z","shell.execute_reply.started":"2021-07-17T18:36:19.114187Z","shell.execute_reply":"2021-07-17T18:36:19.131190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df.drop(['Negative for Pneumonia','Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance'], axis=1, inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.134053Z","iopub.execute_input":"2021-07-17T18:36:19.134404Z","iopub.status.idle":"2021-07-17T18:36:19.142111Z","shell.execute_reply.started":"2021-07-17T18:36:19.134353Z","shell.execute_reply":"2021-07-17T18:36:19.141099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.143337Z","iopub.execute_input":"2021-07-17T18:36:19.143698Z","iopub.status.idle":"2021-07-17T18:36:19.166436Z","shell.execute_reply.started":"2021-07-17T18:36:19.143655Z","shell.execute_reply":"2021-07-17T18:36:19.165673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df['id_jpg'] = image_study_df['id_x'].str.replace('_image', '.jpg')\nimage_study_df['image_label'] = image_study_df['label'].str.split().apply(lambda x : x[0])\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.167765Z","iopub.execute_input":"2021-07-17T18:36:19.168222Z","iopub.status.idle":"2021-07-17T18:36:19.192121Z","shell.execute_reply.started":"2021-07-17T18:36:19.168187Z","shell.execute_reply":"2021-07-17T18:36:19.191394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df.head(20)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.192945Z","iopub.execute_input":"2021-07-17T18:36:19.193310Z","iopub.status.idle":"2021-07-17T18:36:19.215190Z","shell.execute_reply.started":"2021-07-17T18:36:19.193275Z","shell.execute_reply":"2021-07-17T18:36:19.214092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_study_df.image_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.216822Z","iopub.execute_input":"2021-07-17T18:36:19.217295Z","iopub.status.idle":"2021-07-17T18:36:19.229193Z","shell.execute_reply.started":"2021-07-17T18:36:19.217257Z","shell.execute_reply":"2021-07-17T18:36:19.228075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_size = pd.read_csv('../input/covid-jpg-512/size.csv')\n# df_train = df_train.merge(df_size, on='id')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.230656Z","iopub.execute_input":"2021-07-17T18:36:19.231320Z","iopub.status.idle":"2021-07-17T18:36:19.236807Z","shell.execute_reply.started":"2021-07-17T18:36:19.231277Z","shell.execute_reply":"2021-07-17T18:36:19.235363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_size.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.238336Z","iopub.execute_input":"2021-07-17T18:36:19.238871Z","iopub.status.idle":"2021-07-17T18:36:19.245446Z","shell.execute_reply.started":"2021-07-17T18:36:19.238831Z","shell.execute_reply":"2021-07-17T18:36:19.244364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_size.split.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.247195Z","iopub.execute_input":"2021-07-17T18:36:19.247639Z","iopub.status.idle":"2021-07-17T18:36:19.254543Z","shell.execute_reply.started":"2021-07-17T18:36:19.247601Z","shell.execute_reply":"2021-07-17T18:36:19.253583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import numpy as np\n# import os\n# from skimage import exposure\n# import matplotlib\n# matplotlib.rcParams.update({'font.size': 16})\n# import warnings\n# warnings.filterwarnings('ignore')\n# import tensorflow.keras.backend as K\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.256240Z","iopub.execute_input":"2021-07-17T18:36:19.256636Z","iopub.status.idle":"2021-07-17T18:36:19.263805Z","shell.execute_reply.started":"2021-07-17T18:36:19.256596Z","shell.execute_reply":"2021-07-17T18:36:19.262710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# def preprocess_image(img):\n#     equ_img = exposure.equalize_adapthist(img/255, clip_limit=0.05, kernel_size=24)\n#     return equ_img\n\n# df_opa = df_train[df_train['image_label']=='opacity'].reset_index()\n# fig, axs = plt.subplots(5, 2, figsize=(10,20))\n# fig.subplots_adjust(hspace=.2, wspace=.2)\n# n=5\n# for i in range(n):\n#     img = cv2.imread(os.path.join(train_dir, df_opa['id'][i]))\n#     img_proc = preprocess_image(img)\n#     axs[i, 0].imshow(img)\n#     axs[i, 1].imshow(img_proc)\n#     axs[i, 0].axis('off')\n#     axs[i, 1].axis('off')\n#     boxes = literal_eval(df_opa['boxes'][i])\n#     for box in boxes:\n#         axs[i, 0].add_patch(Rectangle((box['x']*(512/df_opa['dim1'][i]), box['y']*(512/df_opa['dim0'][i])), box['width']*(512/df_opa['dim1'][i]), box['height']*(512/df_opa['dim0'][i]), fill=0, color='y', linewidth=3))\n#         axs[i, 0].set_title(df_opa['study_label'][i])\n#         axs[i, 1].add_patch(Rectangle((box['x']*(512/df_opa['dim1'][i]), box['y']*(512/df_opa['dim0'][i])), box['width']*(512/df_opa['dim1'][i]), box['height']*(512/df_opa['dim0'][i]), fill=0, color='r', linewidth=3))\n#         axs[i, 1].set_title('After CLAHE')\n    \n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.265257Z","iopub.execute_input":"2021-07-17T18:36:19.265643Z","iopub.status.idle":"2021-07-17T18:36:19.272977Z","shell.execute_reply.started":"2021-07-17T18:36:19.265603Z","shell.execute_reply":"2021-07-17T18:36:19.271621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ImageGenerators and Augmentations","metadata":{}},{"cell_type":"code","source":"# batch_size = 32\nbatch_size = 16\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.274481Z","iopub.execute_input":"2021-07-17T18:36:19.274873Z","iopub.status.idle":"2021-07-17T18:36:19.283436Z","shell.execute_reply.started":"2021-07-17T18:36:19.274834Z","shell.execute_reply":"2021-07-17T18:36:19.282591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/covid-jpg-512/train'\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.286279Z","iopub.execute_input":"2021-07-17T18:36:19.286522Z","iopub.status.idle":"2021-07-17T18:36:19.292564Z","shell.execute_reply.started":"2021-07-17T18:36:19.286500Z","shell.execute_reply":"2021-07-17T18:36:19.291870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! ls '../input/covid-jpg-512/train' | wc -l","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:19.296394Z","iopub.execute_input":"2021-07-17T18:36:19.296658Z","iopub.status.idle":"2021-07-17T18:36:20.736501Z","shell.execute_reply.started":"2021-07-17T18:36:19.296632Z","shell.execute_reply":"2021-07-17T18:36:20.735380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! ls -ahl '../input/covid-jpg-512/train' | grep 000c3a3f293f","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:20.740608Z","iopub.execute_input":"2021-07-17T18:36:20.740933Z","iopub.status.idle":"2021-07-17T18:36:25.001924Z","shell.execute_reply.started":"2021-07-17T18:36:20.740901Z","shell.execute_reply":"2021-07-17T18:36:25.000816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n# img = cv2.imread('../input/covid-jpg-512/train/000a312787f2.jpg')\nimg = cv2.imread('../input/covid-jpg-512/train/000c3a3f293f.jpg')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:25.004863Z","iopub.execute_input":"2021-07-17T18:36:25.007862Z","iopub.status.idle":"2021-07-17T18:36:25.166406Z","shell.execute_reply.started":"2021-07-17T18:36:25.007818Z","shell.execute_reply":"2021-07-17T18:36:25.165384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:25.170849Z","iopub.execute_input":"2021-07-17T18:36:25.172914Z","iopub.status.idle":"2021-07-17T18:36:25.183013Z","shell.execute_reply.started":"2021-07-17T18:36:25.172873Z","shell.execute_reply":"2021-07-17T18:36:25.182058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img_size = 150\nimg_size = 512\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:25.187548Z","iopub.execute_input":"2021-07-17T18:36:25.189578Z","iopub.status.idle":"2021-07-17T18:36:25.194864Z","shell.execute_reply.started":"2021-07-17T18:36:25.189540Z","shell.execute_reply":"2021-07-17T18:36:25.194030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:25.200016Z","iopub.execute_input":"2021-07-17T18:36:25.200803Z","iopub.status.idle":"2021-07-17T18:36:29.774166Z","shell.execute_reply.started":"2021-07-17T18:36:25.200765Z","shell.execute_reply":"2021-07-17T18:36:29.773304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_generator = ImageDataGenerator(\n        rescale = 1./255,\n        validation_split=0.25,\n        rotation_range=10,\n        width_shift_range=0.1,\n        height_shift_range=0.1,\n        shear_range=0.1,\n        zoom_range=0.1,\n        horizontal_flip=False,\n        fill_mode='nearest',\n)\n\nimage_generator_valid = ImageDataGenerator(validation_split=0.25,\n                                           rescale = 1./255,  )\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:29.775572Z","iopub.execute_input":"2021-07-17T18:36:29.775908Z","iopub.status.idle":"2021-07-17T18:36:29.781928Z","shell.execute_reply.started":"2021-07-17T18:36:29.775871Z","shell.execute_reply":"2021-07-17T18:36:29.780965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = image_generator.flow_from_dataframe(\n        dataframe = image_study_df,\n        directory='../input/covid-jpg-512/train',\n        x_col = 'id_jpg',\n        y_col =  'image_label',  \n        target_size=(img_size, img_size),\n        batch_size=batch_size,\n#         class_mode='binary',\n        subset='training', \n        seed = 23) \n\nvalid_generator=image_generator_valid.flow_from_dataframe(\n    dataframe = image_study_df,\n    directory='../input/covid-jpg-512/train',\n    x_col = 'id_jpg',\n    y_col = 'image_label',\n    target_size=(img_size, img_size),\n    batch_size=batch_size,\n#     class_mode='binary',\n    subset='validation', \n    shuffle=False, \n    seed=23) \n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:29.783412Z","iopub.execute_input":"2021-07-17T18:36:29.783881Z","iopub.status.idle":"2021-07-17T18:36:31.800321Z","shell.execute_reply.started":"2021-07-17T18:36:29.783843Z","shell.execute_reply":"2021-07-17T18:36:31.799414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(valid_generator.filenames)/valid_generator.batch_size","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:38.317288Z","iopub.execute_input":"2021-07-17T18:36:38.317610Z","iopub.status.idle":"2021-07-17T18:36:38.323254Z","shell.execute_reply.started":"2021-07-17T18:36:38.317580Z","shell.execute_reply":"2021-07-17T18:36:38.322178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_generator.filenames)/train_generator.batch_size","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:38.631604Z","iopub.execute_input":"2021-07-17T18:36:38.631936Z","iopub.status.idle":"2021-07-17T18:36:38.637877Z","shell.execute_reply.started":"2021-07-17T18:36:38.631905Z","shell.execute_reply":"2021-07-17T18:36:38.636606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator.batch_size, valid_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:38.890317Z","iopub.execute_input":"2021-07-17T18:36:38.890652Z","iopub.status.idle":"2021-07-17T18:36:38.896046Z","shell.execute_reply.started":"2021-07-17T18:36:38.890623Z","shell.execute_reply":"2021-07-17T18:36:38.895089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# valid_generator.allowed_class_modes\n# valid_generator.class_mode\n# valid_generator.classes\n# valid_generator.color_mode\n# valid_generator.directory\n# valid_generator.data_format\n# valid_generator.dtype\nvalid_generator.filenames[:5]","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:44.198357Z","iopub.execute_input":"2021-07-17T18:36:44.198687Z","iopub.status.idle":"2021-07-17T18:36:44.204524Z","shell.execute_reply.started":"2021-07-17T18:36:44.198657Z","shell.execute_reply":"2021-07-17T18:36:44.203422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# valid_generator.filepaths[:5]\n# valid_generator.image_shape\n# valid_generator.labels[:5]\nvalid_generator.target_size\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:44.243341Z","iopub.execute_input":"2021-07-17T18:36:44.243589Z","iopub.status.idle":"2021-07-17T18:36:44.250016Z","shell.execute_reply.started":"2021-07-17T18:36:44.243564Z","shell.execute_reply":"2021-07-17T18:36:44.249144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:47.165625Z","iopub.execute_input":"2021-07-17T18:36:47.165978Z","iopub.status.idle":"2021-07-17T18:36:47.171823Z","shell.execute_reply.started":"2021-07-17T18:36:47.165929Z","shell.execute_reply":"2021-07-17T18:36:47.170940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for j in range(4):\n#     aug_images = [train_generator[0][0][j] for i in range(5)]\n#     fig, axes = plt.subplots(1, 5, figsize=(24,24))\n#     axes = axes.flatten()\n#     for img, ax in zip(aug_images, axes):\n#         ax.imshow(img)\n#         ax.axis('off')\n# plt.tight_layout()\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:47.293854Z","iopub.execute_input":"2021-07-17T18:36:47.294210Z","iopub.status.idle":"2021-07-17T18:36:47.299510Z","shell.execute_reply.started":"2021-07-17T18:36:47.294179Z","shell.execute_reply":"2021-07-17T18:36:47.296907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Architecture","metadata":{}},{"cell_type":"code","source":"chex_weights_path = '../input/chexnet-weights/brucechou1983_CheXNet_Keras_0.3.0_weights.h5'\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:48.952581Z","iopub.execute_input":"2021-07-17T18:36:48.952910Z","iopub.status.idle":"2021-07-17T18:36:48.956584Z","shell.execute_reply.started":"2021-07-17T18:36:48.952877Z","shell.execute_reply":"2021-07-17T18:36:48.955628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, GlobalAveragePooling2D\n# from tensorflow.keras import models\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:49.245916Z","iopub.execute_input":"2021-07-17T18:36:49.246282Z","iopub.status.idle":"2021-07-17T18:36:49.250927Z","shell.execute_reply.started":"2021-07-17T18:36:49.246249Z","shell.execute_reply":"2021-07-17T18:36:49.249871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:50.660698Z","iopub.execute_input":"2021-07-17T18:36:50.661049Z","iopub.status.idle":"2021-07-17T18:36:50.666026Z","shell.execute_reply.started":"2021-07-17T18:36:50.661017Z","shell.execute_reply":"2021-07-17T18:36:50.665107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_model = DenseNet121(weights=None,\n                        include_top=False,\n                        input_shape=(img_size,img_size,3)\n                        )\nout = Dense(14, activation='sigmoid')(pre_model.output)\npre_model = Model(inputs=pre_model.input, outputs=out) \n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:51.264395Z","iopub.execute_input":"2021-07-17T18:36:51.264724Z","iopub.status.idle":"2021-07-17T18:36:55.171004Z","shell.execute_reply.started":"2021-07-17T18:36:51.264694Z","shell.execute_reply":"2021-07-17T18:36:55.170167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_model.load_weights(chex_weights_path)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:57.288425Z","iopub.execute_input":"2021-07-17T18:36:57.288760Z","iopub.status.idle":"2021-07-17T18:36:58.806381Z","shell.execute_reply.started":"2021-07-17T18:36:57.288722Z","shell.execute_reply":"2021-07-17T18:36:58.805478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pre_model.trainable = False\npre_model.trainable = True\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:36:58.810050Z","iopub.execute_input":"2021-07-17T18:36:58.810324Z","iopub.status.idle":"2021-07-17T18:36:58.825767Z","shell.execute_reply.started":"2021-07-17T18:36:58.810298Z","shell.execute_reply":"2021-07-17T18:36:58.824941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:01.424609Z","iopub.execute_input":"2021-07-17T18:37:01.424942Z","iopub.status.idle":"2021-07-17T18:37:01.612904Z","shell.execute_reply.started":"2021-07-17T18:37:01.424910Z","shell.execute_reply":"2021-07-17T18:37:01.612025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pre_model.layers)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:01.662238Z","iopub.execute_input":"2021-07-17T18:37:01.662502Z","iopub.status.idle":"2021-07-17T18:37:01.668345Z","shell.execute_reply.started":"2021-07-17T18:37:01.662475Z","shell.execute_reply":"2021-07-17T18:37:01.667405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_model.layers[420:]","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:01.868313Z","iopub.execute_input":"2021-07-17T18:37:01.868633Z","iopub.status.idle":"2021-07-17T18:37:01.875983Z","shell.execute_reply.started":"2021-07-17T18:37:01.868604Z","shell.execute_reply":"2021-07-17T18:37:01.875054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_model.layers[-2]","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:02.068609Z","iopub.execute_input":"2021-07-17T18:37:02.068927Z","iopub.status.idle":"2021-07-17T18:37:02.074922Z","shell.execute_reply.started":"2021-07-17T18:37:02.068897Z","shell.execute_reply":"2021-07-17T18:37:02.074015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# last_layer = pre_model.get_layer('conv5_block16_concat')\nlast_layer = pre_model.layers[-2]\n\nprint('last layer output shape: ', last_layer.output_shape)\nlast_output = last_layer.output","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:02.444455Z","iopub.execute_input":"2021-07-17T18:37:02.444799Z","iopub.status.idle":"2021-07-17T18:37:02.450300Z","shell.execute_reply.started":"2021-07-17T18:37:02.444766Z","shell.execute_reply":"2021-07-17T18:37:02.449244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:02.784917Z","iopub.execute_input":"2021-07-17T18:37:02.785316Z","iopub.status.idle":"2021-07-17T18:37:02.791542Z","shell.execute_reply.started":"2021-07-17T18:37:02.785282Z","shell.execute_reply":"2021-07-17T18:37:02.790413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Flatten the output layer to 1 dimension\nx = Flatten()(last_output)\n# Add a fully connected layer with 512 hidden units and ReLU activation\nx = Dense(512, activation='relu')(x)\n# Add a dropout rate of 0.2\nx = Dropout(0.2)(x)                  \n# # Add a fully connected layer with 128 hidden units and ReLU activation\n# x = Dense(128, activation='relu')(x)\n\n\n# Add final classification layer\n# x = Dense(1, activation='sigmoid')(x)  \nx = Dense(2, activation='softmax')(x)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:07.914651Z","iopub.execute_input":"2021-07-17T18:37:07.915173Z","iopub.status.idle":"2021-07-17T18:37:07.954816Z","shell.execute_reply.started":"2021-07-17T18:37:07.915108Z","shell.execute_reply":"2021-07-17T18:37:07.953931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam, RMSprop\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:08.173364Z","iopub.execute_input":"2021-07-17T18:37:08.173697Z","iopub.status.idle":"2021-07-17T18:37:08.177383Z","shell.execute_reply.started":"2021-07-17T18:37:08.173665Z","shell.execute_reply":"2021-07-17T18:37:08.176379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model( pre_model.input, x) \n\n# model.compile(optimizer = RMSprop(lr=0.01), \n#               loss='binary_crossentropy', \n#               metrics=['accuracy'])\n\nmodel.compile(Adam(lr=1e-3),\n              loss='binary_crossentropy',\n              metrics='accuracy')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:08.996563Z","iopub.execute_input":"2021-07-17T18:37:08.996909Z","iopub.status.idle":"2021-07-17T18:37:09.237817Z","shell.execute_reply.started":"2021-07-17T18:37:08.996880Z","shell.execute_reply":"2021-07-17T18:37:09.236905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:10.395358Z","iopub.execute_input":"2021-07-17T18:37:10.395686Z","iopub.status.idle":"2021-07-17T18:37:10.402174Z","shell.execute_reply.started":"2021-07-17T18:37:10.395656Z","shell.execute_reply":"2021-07-17T18:37:10.401266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:10.439428Z","iopub.execute_input":"2021-07-17T18:37:10.439708Z","iopub.status.idle":"2021-07-17T18:37:10.444999Z","shell.execute_reply.started":"2021-07-17T18:37:10.439681Z","shell.execute_reply":"2021-07-17T18:37:10.443938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:13.533089Z","iopub.execute_input":"2021-07-17T18:37:13.533457Z","iopub.status.idle":"2021-07-17T18:37:13.538913Z","shell.execute_reply.started":"2021-07-17T18:37:13.533424Z","shell.execute_reply":"2021-07-17T18:37:13.537673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rlr = ReduceLROnPlateau(monitor = 'val_accuracy', \n                        factor = 0.2, \n                        patience = 2, \n                        verbose = 1, \n                        min_delta = 1e-4, \n                        min_lr = 1e-4, \n                        mode = 'max')","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:13.861032Z","iopub.execute_input":"2021-07-17T18:37:13.861397Z","iopub.status.idle":"2021-07-17T18:37:13.868285Z","shell.execute_reply.started":"2021-07-17T18:37:13.861364Z","shell.execute_reply":"2021-07-17T18:37:13.867080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(monitor = 'val_accuracy', \n                   min_delta = 1e-4, \n                   patience = 3, \n                   mode = 'max', \n                   restore_best_weights = True, \n                   verbose = 1)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:14.834884Z","iopub.execute_input":"2021-07-17T18:37:14.835294Z","iopub.status.idle":"2021-07-17T18:37:14.840273Z","shell.execute_reply.started":"2021-07-17T18:37:14.835261Z","shell.execute_reply":"2021-07-17T18:37:14.839075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ckp = ModelCheckpoint('model.h5',\n                      monitor = 'val_accuracy',\n                      verbose = 0, \n                      save_best_only = True, \n                      mode = 'max')","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:15.536630Z","iopub.execute_input":"2021-07-17T18:37:15.536987Z","iopub.status.idle":"2021-07-17T18:37:15.541483Z","shell.execute_reply.started":"2021-07-17T18:37:15.536937Z","shell.execute_reply":"2021-07-17T18:37:15.540290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n      train_generator,\n      validation_data=valid_generator,\n#       steps_per_epoch = 296,\n      epochs=10,\n      callbacks=[es, ckp, rlr],\n#       validation_steps=98,\n#       verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-17T18:37:16.111712Z","iopub.execute_input":"2021-07-17T18:37:16.112106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### if acc does not change, try these ???:\n\nhttps://stackoverflow.com/questions/37213388/keras-accuracy-does-not-change\n\nhttps://datascience.stackexchange.com/questions/30930/accuracy-and-loss-dont-change-in-cnn-is-it-over-fitting\n\nhttps://www.kaggle.com/questions-and-answers/56171\n\nhttps://github.com/keras-team/keras/issues/2711\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Things to consider from https://arxiv.org/pdf/1711.05225.pdf:\n\noptimize the weighted binary cross entropy loss\n\nminibatches of size 16. \n\npick the model with the lowest validation loss - done.\n\nLearning rate changes - done\n\nbetter test-train split based on Y balance\n\nnormalize based on the mean and standard deviation of images\n\naugment the training data with random horizontal flipping.\n\n\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model performance","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend(loc=0)\nplt.figure()\n\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:26:09.49321Z","iopub.execute_input":"2021-06-27T10:26:09.493558Z","iopub.status.idle":"2021-06-27T10:26:09.669246Z","shell.execute_reply.started":"2021-06-27T10:26:09.493524Z","shell.execute_reply":"2021-06-27T10:26:09.668257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:26:16.615119Z","iopub.execute_input":"2021-06-27T10:26:16.615448Z","iopub.status.idle":"2021-06-27T10:26:16.619405Z","shell.execute_reply.started":"2021-06-27T10:26:16.615418Z","shell.execute_reply":"2021-06-27T10:26:16.618466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.math import confusion_matrix\nfrom sklearn.metrics import accuracy_score, confusion_matrix\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:26:16.821443Z","iopub.execute_input":"2021-06-27T10:26:16.821714Z","iopub.status.idle":"2021-06-27T10:26:17.394592Z","shell.execute_reply.started":"2021-06-27T10:26:16.821689Z","shell.execute_reply":"2021-06-27T10:26:17.393551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual =  valid_generator.labels\npreds = np.argmax(model.predict(valid_generator), axis=1)\ncfmx = confusion_matrix(actual, preds)\nacc = accuracy_score(actual, preds)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:26:20.299856Z","iopub.execute_input":"2021-06-27T10:26:20.300217Z","iopub.status.idle":"2021-06-27T10:26:36.403984Z","shell.execute_reply.started":"2021-06-27T10:26:20.300184Z","shell.execute_reply":"2021-06-27T10:26:36.403037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_generator)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:27:01.887101Z","iopub.execute_input":"2021-06-27T10:27:01.887451Z","iopub.status.idle":"2021-06-27T10:27:18.042713Z","shell.execute_reply.started":"2021-06-27T10:27:01.887419Z","shell.execute_reply":"2021-06-27T10:27:18.041938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual[:10]","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:27:18.044233Z","iopub.execute_input":"2021-06-27T10:27:18.044592Z","iopub.status.idle":"2021-06-27T10:27:18.050228Z","shell.execute_reply.started":"2021-06-27T10:27:18.044555Z","shell.execute_reply":"2021-06-27T10:27:18.049269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[:10]","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:27:18.052304Z","iopub.execute_input":"2021-06-27T10:27:18.053082Z","iopub.status.idle":"2021-06-27T10:27:18.061458Z","shell.execute_reply.started":"2021-06-27T10:27:18.053022Z","shell.execute_reply":"2021-06-27T10:27:18.060425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:27:27.164726Z","iopub.execute_input":"2021-06-27T10:27:27.165048Z","iopub.status.idle":"2021-06-27T10:27:27.172589Z","shell.execute_reply.started":"2021-06-27T10:27:27.165014Z","shell.execute_reply":"2021-06-27T10:27:27.171448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfmx","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:27:28.393678Z","iopub.execute_input":"2021-06-27T10:27:28.393995Z","iopub.status.idle":"2021-06-27T10:27:28.398905Z","shell.execute_reply.started":"2021-06-27T10:27:28.393961Z","shell.execute_reply":"2021-06-27T10:27:28.398118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:28:00.350968Z","iopub.execute_input":"2021-06-27T10:28:00.351335Z","iopub.status.idle":"2021-06-27T10:28:00.357253Z","shell.execute_reply.started":"2021-06-27T10:28:00.351303Z","shell.execute_reply":"2021-06-27T10:28:00.356394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from seaborn import heatmap\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T14:16:15.214763Z","iopub.status.idle":"2021-06-26T14:16:15.217673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print ('Test Accuracy:', acc )\n# heatmap(cfmx, annot=True, cmap='plasma',\n#         xticklabels=['Normal','Opacity'],fmt='.0f', yticklabels=['Normal', 'Opacity'])\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T14:16:15.218843Z","iopub.status.idle":"2021-06-26T14:16:15.219358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hist = pd.DataFrame(history.history)\n# fig, (ax1, ax2) = plt.subplots(figsize=(12,12),nrows=2, ncols=1)\n# hist['loss'].plot(ax=ax1,c='k',label='training loss')\n# hist['val_loss'].plot(ax=ax1,c='r',linestyle='--', label='validation loss')\n# ax1.legend()\n# hist['accuracy'].plot(ax=ax2,c='k',label='training accuracy')\n# hist['val_accuracy'].plot(ax=ax2,c='r',linestyle='--',label='validation accuracy')\n# ax2.legend()\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T14:16:15.225226Z","iopub.status.idle":"2021-06-26T14:16:15.225744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Interpretation ","metadata":{}},{"cell_type":"code","source":"def grad_cam(input_image, model, layer_name):\n\n    desired_layer = model.get_layer(layer_name)\n    grad_model = Model(model.inputs, [desired_layer.output, model.output])\n\n    with tf.GradientTape() as tape:\n        layer_output, preds = grad_model(input_image)\n        ix = (np.argsort(preds, axis=1)[:, -1]).item()\n        output_idx = preds[:, ix]\n\n    gradient = tape.gradient(output_idx, layer_output)\n    alpha_kc = np.mean(gradient, axis=(0,1,2))\n    L_gradCam = tf.nn.relu(np.dot(layer_output, alpha_kc)[0])\n    L_gradCam = (L_gradCam - np.min(L_gradCam)) / (np.max(L_gradCam) - np.min(L_gradCam)) \n    return L_gradCam.numpy()","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:28:26.02269Z","iopub.execute_input":"2021-06-27T10:28:26.023032Z","iopub.status.idle":"2021-06-27T10:28:26.030859Z","shell.execute_reply.started":"2021-06-27T10:28:26.022999Z","shell.execute_reply":"2021-06-27T10:28:26.029596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:28:30.633134Z","iopub.execute_input":"2021-06-27T10:28:30.633474Z","iopub.status.idle":"2021-06-27T10:28:30.636733Z","shell.execute_reply.started":"2021-06-27T10:28:30.633444Z","shell.execute_reply":"2021-06-27T10:28:30.635903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def blend(img_path, gradCam_img, alpha, colormap = cv2.COLORMAP_JET):\n    origin_img = img_to_array(load_img(img_path))\n    gradCam_resized = cv2.resize(gradCam_img, (origin_img.shape[1], origin_img.shape[0]), interpolation = cv2.INTER_LINEAR)\n    heatmap  = cv2.applyColorMap(np.uint8(gradCam_resized*255), colormap)\n    superimposed_image = cv2.cvtColor(origin_img.astype('uint8'), cv2.COLOR_RGB2BGR) + heatmap * alpha\n    return heatmap, superimposed_image","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:28:34.050227Z","iopub.execute_input":"2021-06-27T10:28:34.050561Z","iopub.status.idle":"2021-06-27T10:28:34.056612Z","shell.execute_reply.started":"2021-06-27T10:28:34.05053Z","shell.execute_reply":"2021-06-27T10:28:34.055397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_results(model, gen, label=0):\n    n = 50\n    fig, axs = plt.subplots(10, 5, figsize=(20,60))\n    fig.subplots_adjust(hspace=.5, wspace=.1)\n    axs = axs.ravel()\n    gen.next()\n    classes = list(gen.class_indices.keys()) \n    if label==0:\n        idx = np.array(np.where(np.array(gen.labels) ==0)).ravel()\n    else:\n        idx = np.array(np.where(np.array(gen.labels) ==1)).ravel()\n   \n    layer_name = 'bn'\n    for i in range(n):\n        sample_img_path = os.path.join(train_dir, df_train['id_jpg'][idx[i]])\n        img = load_process(sample_img_path, img_size)\n        pred = model.predict(img)\n        grad_cam_img = grad_cam(img, model, layer_name)\n        heatmap_img, result_img = blend(sample_img_path, grad_cam_img, 0.5)\n        axs[i].imshow(result_img[:,:,::-1]/255)\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])\n        if type(df_train['boxes'][idx[i]])==str:\n            boxes = literal_eval(df_train['boxes'][idx[i]])\n            for box in boxes:\n#                 axs[i].add_patch(Rectangle((box['x']*(512/df_train['dim1'][idx[i]]), box['y']*(512/df_train['dim0'][idx[i]])), box['width']*(512/df_train['dim1'][idx[i]]), box['height']*(512/df_train['dim0'][idx[i]]), fill=0, color='y', linewidth=2))\n                axs[i].set_title(f\"{df_train['study_label'][idx[i]]}, {df_train['image_label'][idx[i]]}\")\n        else:\n            axs[i].set_title(df_train['study_label'][idx[i]])\n        \n        axs[i].set_xlabel(f\"{classes[np.argmax(pred)]}, {round(pred[0][np.argmax(pred)]*100, 2)}%\")","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:33:48.634202Z","iopub.execute_input":"2021-06-27T10:33:48.634514Z","iopub.status.idle":"2021-06-27T10:33:48.646984Z","shell.execute_reply.started":"2021-06-27T10:33:48.634486Z","shell.execute_reply":"2021-06-27T10:33:48.645091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = image_study_df","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:28:50.62281Z","iopub.execute_input":"2021-06-27T10:28:50.623146Z","iopub.status.idle":"2021-06-27T10:28:50.627314Z","shell.execute_reply.started":"2021-06-27T10:28:50.623111Z","shell.execute_reply":"2021-06-27T10:28:50.626087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img, img_to_array\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:28:53.725912Z","iopub.execute_input":"2021-06-27T10:28:53.726254Z","iopub.status.idle":"2021-06-27T10:28:53.732272Z","shell.execute_reply.started":"2021-06-27T10:28:53.726223Z","shell.execute_reply":"2021-06-27T10:28:53.731427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_process(img, img_size):\n    img = load_img(img, target_size = (img_size, img_size))\n    img = img_to_array(img)\n    img = img.reshape((1, img.shape[0], img.shape[1], img.shape[2]))\n#     img = preprocess_image(img)\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:28:57.076684Z","iopub.execute_input":"2021-06-27T10:28:57.077014Z","iopub.status.idle":"2021-06-27T10:28:57.081909Z","shell.execute_reply.started":"2021-06-27T10:28:57.076984Z","shell.execute_reply":"2021-06-27T10:28:57.080897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:29:01.575885Z","iopub.execute_input":"2021-06-27T10:29:01.576221Z","iopub.status.idle":"2021-06-27T10:29:01.580711Z","shell.execute_reply.started":"2021-06-27T10:29:01.576187Z","shell.execute_reply":"2021-06-27T10:29:01.579855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ast import literal_eval\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:29:04.032318Z","iopub.execute_input":"2021-06-27T10:29:04.032682Z","iopub.status.idle":"2021-06-27T10:29:04.038045Z","shell.execute_reply.started":"2021-06-27T10:29:04.032648Z","shell.execute_reply":"2021-06-27T10:29:04.035482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.patches import Rectangle","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:29:06.933385Z","iopub.execute_input":"2021-06-27T10:29:06.933703Z","iopub.status.idle":"2021-06-27T10:29:06.937572Z","shell.execute_reply.started":"2021-06-27T10:29:06.933671Z","shell.execute_reply":"2021-06-27T10:29:06.93664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_results(model, valid_generator,label=0)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:30:07.760029Z","iopub.execute_input":"2021-06-27T10:30:07.7604Z","iopub.status.idle":"2021-06-27T10:30:07.763945Z","shell.execute_reply.started":"2021-06-27T10:30:07.760366Z","shell.execute_reply":"2021-06-27T10:30:07.762929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot_results(model, valid_generator,label=1)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T10:35:13.341809Z","iopub.execute_input":"2021-06-27T10:35:13.342166Z","iopub.status.idle":"2021-06-27T10:35:13.346154Z","shell.execute_reply.started":"2021-06-27T10:35:13.342133Z","shell.execute_reply":"2021-06-27T10:35:13.345004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}