{"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 cv2\nimport pydicom\n\nfrom pathlib import Path\nfrom tqdm.auto import tqdm\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport skimage.io\nimport tqdm\nimport glob\nimport tensorflow\n\n# graphing\nimport matplotlib.image as mpimage\nimport matplotlib.pyplot as plt\n\n%matplotlib inline\n\nfrom PIL import Image\nimport random\nimport gc\nimport re\nimport cv2\nfrom tqdm import tqdm\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\n\n# TF model stuff\nimport tensorflow as tf\nfrom tensorflow.keras import backend as K\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Conv2D, Activation, MaxPooling2D, Dropout, GlobalAveragePooling1D, GlobalAveragePooling2D, Flatten, BatchNormalization, Dense\nfrom tensorflow.keras.optimizers import Adam, RMSprop, SGD\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom sklearn.utils.class_weight import compute_class_weight\nfrom sklearn.metrics import accuracy_score, roc_auc_score, classification_report","metadata":{"execution":{"iopub.status.busy":"2021-06-20T13:45:10.416121Z","iopub.execute_input":"2021-06-20T13:45:10.416637Z","iopub.status.idle":"2021-06-20T13:45:12.922920Z","shell.execute_reply.started":"2021-06-20T13:45:10.416553Z","shell.execute_reply":"2021-06-20T13:45:12.921917Z"},"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 = 32\nEPOCHS = 25\nIMG_SIZE = WIDTH = HEIGHT = 224\nLEARNING_RATE = 0.00008\n\nINTERPOLATION = cv2.INTER_LANCZOS4","metadata":{"execution":{"iopub.status.busy":"2021-06-20T13:45:12.925315Z","iopub.execute_input":"2021-06-20T13:45:12.925749Z","iopub.status.idle":"2021-06-20T13:45:12.935988Z","shell.execute_reply.started":"2021-06-20T13:45:12.925712Z","shell.execute_reply":"2021-06-20T13:45:12.934368Z"},"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-20T13:45:12.938319Z","iopub.execute_input":"2021-06-20T13:45:12.939753Z","iopub.status.idle":"2021-06-20T13:45:13.432022Z","shell.execute_reply.started":"2021-06-20T13:45:12.939478Z","shell.execute_reply":"2021-06-20T13:45:13.430659Z"},"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-20T13:45:13.434334Z","iopub.execute_input":"2021-06-20T13:45:13.435155Z","iopub.status.idle":"2021-06-20T13:45:13.468690Z","shell.execute_reply.started":"2021-06-20T13:45:13.435107Z","shell.execute_reply":"2021-06-20T13:45:13.467623Z"},"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-20T13:45:13.473270Z","iopub.execute_input":"2021-06-20T13:45:13.473716Z","iopub.status.idle":"2021-06-20T13:45:43.190104Z","shell.execute_reply.started":"2021-06-20T13:45:13.473671Z","shell.execute_reply":"2021-06-20T13:45:43.187283Z"},"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-20T13:45:43.192606Z","iopub.execute_input":"2021-06-20T13:45:43.193019Z","iopub.status.idle":"2021-06-20T13:45:43.530597Z","shell.execute_reply.started":"2021-06-20T13:45:43.192974Z","shell.execute_reply":"2021-06-20T13:45:43.527999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_CLASSES = 4\ndef create_model(input_shape):\n    K.clear_session()\n    base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape)\n    x = base_model.output\n    x = GlobalAveragePooling2D(name='avg_pool')(x)\n\n\n    x = Dense(512, activation='relu')(x)\n    x = Dropout(0.1)(x)\n    x = Dense(256, activation='relu')(x)\n    x = Dropout(0.1)(x)\n    x = Dense(128, activation='relu')(x)\n    x = Dropout(0.1)(x)\n    x = BatchNormalization()(x)\n    for layer in base_model.layers:\n        layer.trainable = False\n        \n    predictions = Dense(NUM_CLASSES, activation='sigmoid')(x)\n    model = Model(inputs=base_model.inputs, outputs=predictions)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-06-20T13:45:43.533531Z","iopub.execute_input":"2021-06-20T13:45:43.533978Z","iopub.status.idle":"2021-06-20T13:45:43.544389Z","shell.execute_reply.started":"2021-06-20T13:45:43.533932Z","shell.execute_reply":"2021-06-20T13:45:43.542679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dimension_x = 224\ndimension_y = 224\nmodel = create_model((dimension_x, dimension_y, 3))","metadata":{"execution":{"iopub.status.busy":"2021-06-20T13:45:43.546333Z","iopub.execute_input":"2021-06-20T13:45:43.547147Z","iopub.status.idle":"2021-06-20T13:45:50.950500Z","shell.execute_reply.started":"2021-06-20T13:45:43.547098Z","shell.execute_reply":"2021-06-20T13:45:50.949165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_loss = tf.keras.metrics.Mean('training_loss', dtype=tf.float32)\ntraining_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('training_accuracy', dtype=tf.float32)\ntest_loss = tf.keras.metrics.Mean('test_loss', dtype=tf.float32)\ntest_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('test_accuracy', dtype=tf.float32)\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2021-06-20T13:45:50.952449Z","iopub.execute_input":"2021-06-20T13:45:50.952896Z","iopub.status.idle":"2021-06-20T13:45:51.103496Z","shell.execute_reply.started":"2021-06-20T13:45:50.952837Z","shell.execute_reply":"2021-06-20T13:45:51.102470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(lr=0.015)\n\nmodel.compile(loss='categorical_crossentropy',\n             optimizer=optimizer,\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-20T13:45:51.104967Z","iopub.execute_input":"2021-06-20T13:45:51.105309Z","iopub.status.idle":"2021-06-20T13:45:51.139943Z","shell.execute_reply.started":"2021-06-20T13:45:51.105265Z","shell.execute_reply":"2021-06-20T13:45:51.138962Z"},"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-20T13:46:17.438070Z","iopub.execute_input":"2021-06-20T13:46:17.438520Z","iopub.status.idle":"2021-06-20T13:46:22.518189Z","shell.execute_reply.started":"2021-06-20T13:46:17.438474Z","shell.execute_reply":"2021-06-20T13:46:22.516508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Defining Callbacks\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\n\nfilepath = './best_weights.hdf5'\n\nearlystopping = EarlyStopping(monitor = 'val_auc', \n                              mode = 'max' , \n                              patience = 15,\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-20T13:47:10.314290Z","iopub.execute_input":"2021-06-20T13:47:10.314683Z","iopub.status.idle":"2021-06-20T13:47:10.323193Z","shell.execute_reply.started":"2021-06-20T13:47:10.314653Z","shell.execute_reply":"2021-06-20T13:47:10.321890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history=model.fit(train_generator,\n                        validation_data=valid_generator,\n                        epochs = 20,\n                        callbacks = callback_list,\n                        verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2021-06-20T13:47:15.010284Z","iopub.execute_input":"2021-06-20T13:47:15.010715Z","iopub.status.idle":"2021-06-20T14:20:36.021031Z","shell.execute_reply.started":"2021-06-20T13:47:15.010651Z","shell.execute_reply":"2021-06-20T14:20:36.019755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\n#model.save('./best_weights.hdf5')\nmodel = keras.models.load_model('./best_weights.hdf5')","metadata":{},"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-20T14:22:54.400145Z","iopub.execute_input":"2021-06-20T14:22:54.400825Z","iopub.status.idle":"2021-06-20T14:22:54.611168Z","shell.execute_reply.started":"2021-06-20T14:22:54.400795Z","shell.execute_reply":"2021-06-20T14:22:54.609579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summarize history for loss\n\nplt.plot(model_history.history['accuracy'])\nplt.plot(model_history.history['val_accuracy'])\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-20T14:23:52.695095Z","iopub.execute_input":"2021-06-20T14:23:52.695568Z","iopub.status.idle":"2021-06-20T14:23:52.889222Z","shell.execute_reply.started":"2021-06-20T14:23:52.695529Z","shell.execute_reply":"2021-06-20T14:23:52.887782Z"},"trusted":true},"execution_count":null,"outputs":[]}]}