{"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 shutil\nimport cv2\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.applications.vgg16 import preprocess_input, VGG16\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\nfrom sklearn.model_selection import train_test_split\nimport yaml\n\nfrom kaggle_secrets import UserSecretsClient\nimport pydicom\n\nfrom pathlib import Path\nfrom tqdm.auto import tqdm\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport skimage.io\nimport tqdm\nimport glob\nimport tensorflow \n\nfrom tqdm import tqdm\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\n\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nfrom skimage.color import grey2rgb\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D, Conv2D,GlobalAveragePooling2D\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras.applications.nasnet import NASNetLarge\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:41:48.466766Z","iopub.execute_input":"2021-06-22T10:41:48.467127Z","iopub.status.idle":"2021-06-22T10:41:48.475831Z","shell.execute_reply.started":"2021-06-22T10:41:48.467094Z","shell.execute_reply":"2021-06-22T10:41:48.474890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SIIM_COVID19_DETECTION_DIR = '/kaggle/input/siim-covid19-detection/'\nPART0_RESIZED_DIR = '../input/siim-covid19-resized-to-512px-jpg'\n\n\nTEMP_DIR = '/kaggle/temp/'\n\nINPUT_DIR = PART0_RESIZED_DIR+'/train/'\n\nOUTPUT_DIR = DATASET_DIR = TEMP_DIR+'/train/'\nTRAIN_DIR = DATASET_DIR + 'train/'\nTA_DIR = TRAIN_DIR+'ta/'\nIA_DIR = TRAIN_DIR+'ia/'\nAA_DIR = TRAIN_DIR+'aa/'\nNP_DIR = TRAIN_DIR+'np/'\n\nWORKING_DIR = '/kaggle/working/'\n\nWANDB_PROJECT_NAME = 'project8-kaggle-covid19'\nWANDB_ENTITY_NAME = ''\n\nTRAIN_IMAGE_LEVEL_PATH = SIIM_COVID19_DETECTION_DIR+'train_image_level.csv'\nTRAIN_STUDY_LEVEL_PATH = SIIM_COVID19_DETECTION_DIR+'train_study_level.csv'\nMETA_PATH = PART0_RESIZED_DIR+'meta.csv'\n\nBATCH_SIZE = 64\nEPOCHS = 25\nIMG_SIZE = WIDTH = HEIGHT = 224\nLEARNING_RATE = 0.00008\n\nINTERPOLATION = cv2.INTER_LANCZOS4","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:30:04.200593Z","iopub.execute_input":"2021-06-22T10:30:04.201021Z","iopub.status.idle":"2021-06-22T10:30:04.207901Z","shell.execute_reply.started":"2021-06-22T10:30:04.200981Z","shell.execute_reply":"2021-06-22T10:30:04.206511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_image_level = pd.read_csv(TRAIN_IMAGE_LEVEL_PATH)\ndf_train_study_level = pd.read_csv(TRAIN_STUDY_LEVEL_PATH)\n\ndf_train_image_level['id'] = df_train_image_level.apply(lambda row: row.id.split('_')[0], axis=1)\ndf_train_image_level['path'] = df_train_image_level.apply(lambda row: INPUT_DIR+row.id+'.jpg', axis=1)\ndf_train_image_level['image_level'] = df_train_image_level.apply(lambda row: row.label.split(' ')[0], axis=1)\n\ndf_train_study_level['id'] = df_train_study_level.apply(lambda row: row.id.split('_')[0], axis=1)\ndf_train_study_level.columns = ['StudyInstanceUID', 'Negative for Pneumonia', 'Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance']","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:30:04.209661Z","iopub.execute_input":"2021-06-22T10:30:04.210104Z","iopub.status.idle":"2021-06-22T10:30:04.605678Z","shell.execute_reply.started":"2021-06-22T10:30:04.210064Z","shell.execute_reply":"2021-06-22T10:30:04.604826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_image_level = df_train_image_level.merge(df_train_study_level, on='StudyInstanceUID',how=\"left\")\ndf_train_image_level = df_train_image_level[['id','StudyInstanceUID','path','Negative for Pneumonia','Typical Appearance','Indeterminate Appearance','Atypical Appearance']]\ndf_train_image_level = df_train_image_level.dropna()\ndf_train_image_level = df_train_image_level[~df_train_image_level.duplicated(subset=['StudyInstanceUID'], keep='first')]\ndf_train_image_level = df_train_image_level.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:30:04.607332Z","iopub.execute_input":"2021-06-22T10:30:04.607692Z","iopub.status.idle":"2021-06-22T10:30:04.634296Z","shell.execute_reply.started":"2021-06-22T10:30:04.607653Z","shell.execute_reply":"2021-06-22T10:30:04.633587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[os.makedirs(dir, exist_ok=True) for dir in [TA_DIR,IA_DIR,AA_DIR,NP_DIR]]\nfor i in tqdm(range(len(df_train_image_level))):\n    row = df_train_image_level.loc[i]\n    if row['Typical Appearance']:\n        shutil.copy(row.path, f'{TA_DIR}{row.id}.jpg')\n    elif row['Indeterminate Appearance']:\n        shutil.copy(row.path, f'{IA_DIR}{row.id}.jpg')\n    elif row['Atypical Appearance']:\n        shutil.copy(row.path, f'{AA_DIR}{row.id}.jpg')\n    elif row['Negative for Pneumonia']:\n        shutil.copy(row.path, f'{NP_DIR}{row.id}.jpg')\n    else:\n        print('Error: check df_train_image_level')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:30:04.637307Z","iopub.execute_input":"2021-06-22T10:30:04.637627Z","iopub.status.idle":"2021-06-22T10:30:16.587966Z","shell.execute_reply.started":"2021-06-22T10:30:04.637593Z","shell.execute_reply":"2021-06-22T10:30:16.587061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen_kwargs = dict(validation_split=.20,\n                      preprocessing_function=preprocess_input\n                     )\ndataflow_kwargs = dict(target_size=(IMG_SIZE, IMG_SIZE),\n                       batch_size=BATCH_SIZE,\n                       interpolation=\"lanczos\"\n                      )\n\nvalid_datagen = tf.keras.preprocessing.image.ImageDataGenerator(**datagen_kwargs)\nvalid_generator = valid_datagen.flow_from_directory(TRAIN_DIR,\n                                                    subset=\"validation\",\n                                                    shuffle=False,\n                                                    **dataflow_kwargs)\n\ntrain_datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=40,\n    horizontal_flip=True,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    **datagen_kwargs)\ntrain_generator = train_datagen.flow_from_directory(TRAIN_DIR,\n                                                    subset=\"training\",\n                                                    shuffle=True,\n                                                    **dataflow_kwargs)\n\nprint('classes :', train_generator.class_indices)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:30:16.589722Z","iopub.execute_input":"2021-06-22T10:30:16.590254Z","iopub.status.idle":"2021-06-22T10:30:16.917946Z","shell.execute_reply.started":"2021-06-22T10:30:16.590214Z","shell.execute_reply":"2021-06-22T10:30:16.916714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Initialization\n\nbase_model = NASNetLarge(input_shape =  (331, 331, 3), \n                              include_top = False, weights = 'imagenet')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:32:10.065281Z","iopub.execute_input":"2021-06-22T10:32:10.065646Z","iopub.status.idle":"2021-06-22T10:32:18.267304Z","shell.execute_reply.started":"2021-06-22T10:32:10.065608Z","shell.execute_reply":"2021-06-22T10:32:18.266473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freezing Layers\n\nfor layer in base_model.layers:\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:32:22.583535Z","iopub.execute_input":"2021-06-22T10:32:22.583862Z","iopub.status.idle":"2021-06-22T10:32:22.617945Z","shell.execute_reply.started":"2021-06-22T10:32:22.583830Z","shell.execute_reply":"2021-06-22T10:32:22.617275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building Model\n\nmodel=Sequential()\nmodel.add(base_model)\nmodel.add(BatchNormalization())\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dropout(0.5))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(4,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:41:53.826264Z","iopub.execute_input":"2021-06-22T10:41:53.826616Z","iopub.status.idle":"2021-06-22T10:41:56.427927Z","shell.execute_reply.started":"2021-06-22T10:41:53.826584Z","shell.execute_reply":"2021-06-22T10:41:56.427150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summary\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:41:58.651555Z","iopub.execute_input":"2021-06-22T10:41:58.651910Z","iopub.status.idle":"2021-06-22T10:41:58.712262Z","shell.execute_reply.started":"2021-06-22T10:41:58.651878Z","shell.execute_reply":"2021-06-22T10:41:58.711211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nfrom IPython.display import SVG, Image\nplot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)\nImage('model.png',width=400, height=200)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:42:12.616190Z","iopub.execute_input":"2021-06-22T10:42:12.616590Z","iopub.status.idle":"2021-06-22T10:42:12.773229Z","shell.execute_reply.started":"2021-06-22T10:42:12.616560Z","shell.execute_reply":"2021-06-22T10:42:12.772217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Compile \n\nOPT= tensorflow.keras.optimizers.RMSprop(lr=0.001)\n\nmodel.compile(loss='categorical_crossentropy',\n              metrics=[tensorflow.keras.metrics.AUC(name = 'auc')],\n              optimizer=OPT)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:42:23.985756Z","iopub.execute_input":"2021-06-22T10:42:23.986145Z","iopub.status.idle":"2021-06-22T10:42:24.021038Z","shell.execute_reply.started":"2021-06-22T10:42:23.986083Z","shell.execute_reply":"2021-06-22T10:42:24.020349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining Callbacks\n\nfilepath = './best_weights.hdf5'\n\nearlystopping = EarlyStopping(monitor = 'val_auc', \n                              mode = 'max' , \n                              patience = 10,\n                              verbose = 1)\n\ncheckpoint    = ModelCheckpoint(filepath, \n                                monitor = 'val_auc', \n                                mode='max', \n                                save_best_only=True, \n                                verbose = 1)\n\n\ncallback_list = [earlystopping, checkpoint]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T10:42:28.244761Z","iopub.execute_input":"2021-06-22T10:42:28.245132Z","iopub.status.idle":"2021-06-22T10:42:28.250558Z","shell.execute_reply.started":"2021-06-22T10:42:28.245090Z","shell.execute_reply":"2021-06-22T10:42:28.249602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history=model.fit(train_generator,\n                        validation_data=valid_generator,\n                        epochs = 30,\n                        callbacks = callback_list,\n                        verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:07:33.863109Z","iopub.execute_input":"2021-06-22T11:07:33.863488Z","iopub.status.idle":"2021-06-22T11:15:52.569776Z","shell.execute_reply.started":"2021-06-22T11:07:33.863456Z","shell.execute_reply":"2021-06-22T11:15:52.568800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nmodel.save('./best_weights.hdf5')\n#model = keras.models.load_model('./best_weights.hdf5')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:16:01.537937Z","iopub.execute_input":"2021-06-22T11:16:01.538372Z","iopub.status.idle":"2021-06-22T11:16:03.424940Z","shell.execute_reply.started":"2021-06-22T11:16:01.538334Z","shell.execute_reply":"2021-06-22T11:16:03.423728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summarize history for loss\n\nplt.plot(model_history.history['loss'])\nplt.plot(model_history.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left', bbox_to_anchor=(1,1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:16:06.062864Z","iopub.execute_input":"2021-06-22T11:16:06.063224Z","iopub.status.idle":"2021-06-22T11:16:06.242652Z","shell.execute_reply.started":"2021-06-22T11:16:06.063189Z","shell.execute_reply":"2021-06-22T11:16:06.241667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summarize history for auc\n\nplt.plot(model_history.history['auc'])\nplt.plot(model_history.history['val_auc'])\nplt.title('Model AUC')\nplt.ylabel('AUC')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left', bbox_to_anchor=(1,1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T11:16:19.932373Z","iopub.execute_input":"2021-06-22T11:16:19.932705Z","iopub.status.idle":"2021-06-22T11:16:20.088122Z","shell.execute_reply.started":"2021-06-22T11:16:19.932673Z","shell.execute_reply":"2021-06-22T11:16:20.087168Z"},"trusted":true},"execution_count":null,"outputs":[]}]}