{"cells":[{"metadata":{"_uuid":"323f22eb85744ef15a54946a58a017e52942133e"},"cell_type":"markdown","source":"Reference :\n\nhttps://www.kaggle.com/kmader/lung-opacity-classification-transfer-learning\n\n\nI just used DenseNet 121"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b148e50a8ba9440c2b4ee582496dbf63608cb92c"},"cell_type":"code","source":"# params we will probably want to do some hyperparameter optimization later\nBASE_MODEL= 'DenseNet121'\nIMG_SIZE = (512, 512) # [(224, 224), (384, 384), (512, 512), (640, 640)]\nBATCH_SIZE = 128 # [1, 8, 16, 24]\nDENSE_COUNT = 128 # [32, 64, 128, 256]\nDROPOUT = 0.5 # [0, 0.25, 0.5]\nLEARN_RATE = 0.01 # [1e-4, 1e-3, 4e-3]\nTRAIN_SAMPLES = 3000 # [3000, 6000, 15000]\nTEST_SAMPLES = 600\nUSE_ATTN = False # [True, False]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"image_bbox_df = pd.read_csv('../input/lung-opacity-overview/image_bbox_full.csv')\nimage_bbox_df['path'] = image_bbox_df['path'].map(lambda x: \n                                                  x.replace('input', \n                                                            'input/rsna-pneumonia-detection-challenge'))\nprint(image_bbox_df.shape[0], 'images')\nimage_bbox_df.sample(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4925492adb4f55f01794709cb751a42ca5c2177"},"cell_type":"code","source":"# get the labels in the right format\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder\nclass_enc = LabelEncoder()\nimage_bbox_df['class_idx'] = class_enc.fit_transform(image_bbox_df['class'])\noh_enc = OneHotEncoder(sparse=False)\nimage_bbox_df['class_vec'] = oh_enc.fit_transform(\n    image_bbox_df['class_idx'].values.reshape(-1, 1)).tolist() \nimage_bbox_df.sample(3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b562636a2a48a0557fb16d98841fcbf650245641"},"cell_type":"markdown","source":"# Split into Training and Validation\nThis will give us some feedback on how well our model is doing and if we are overfitting"},{"metadata":{"trusted":true,"_uuid":"c12bfd5115610163c2b63ece8fa8845bb7792327"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimage_df = image_bbox_df.groupby('patientId').apply(lambda x: x.sample(1))\nraw_train_df, valid_df = train_test_split(image_df, test_size=0.15, random_state=2018,\n                                    stratify=image_df['class'])\nprint(raw_train_df.shape, 'training data')\nprint(valid_df.shape, 'validation data')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"71c22947c5007a28627b41af14ee8a7f2e990174"},"cell_type":"markdown","source":"## Balance Training Set\nAnd reduce the total image count"},{"metadata":{"trusted":true,"_uuid":"48b8d60f4435d3ca50d11e12d4eee518c6972ab5"},"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (20, 10))\nraw_train_df.groupby('class').size().plot.bar(ax=ax1)\ntrain_df = raw_train_df.groupby('class').\\\n    apply(lambda x: x.sample(TRAIN_SAMPLES//1)).\\\n    reset_index(drop=True)\ntrain_df.groupby('class').size().plot.bar(ax=ax2) \nprint(train_df.shape[0], 'new training size')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e9b274bfc403678cd6428b8d245bcb1a08fa0808"},"cell_type":"markdown","source":"## Keras Image Transplantation\nSince Keras is design for color jpeg images we need to hack a bit to make it dicom friendly"},{"metadata":{"trusted":true,"_uuid":"9548edfc1a318edfcd2a387468a5b7950a376cc5"},"cell_type":"code","source":"import keras.preprocessing.image as KPImage\nfrom PIL import Image\nimport pydicom\ndef read_dicom_image(in_path):\n    img_arr = pydicom.read_file(in_path).pixel_array\n    return img_arr/img_arr.max()\n    \nclass medical_pil():\n    @staticmethod\n    def open(in_path):\n        if '.dcm' in in_path:\n            c_slice = read_dicom_image(in_path)\n            int_slice =  (255*c_slice).clip(0, 255).astype(np.uint8) # 8bit images are more friendly\n            return Image.fromarray(int_slice)\n        else:\n            return Image.open(in_path)\n    fromarray = Image.fromarray\nKPImage.pil_image = medical_pil","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a0d2ca01b3719e94212234b9d3e4ed43520262eb"},"cell_type":"markdown","source":"# Data Augmentation\nHere we can perform simple augmentation (the `imgaug` and `Augmentation` packages offer much more flexiblity). In order to setup the augmentation we need to know which model we are using"},{"metadata":{"trusted":true,"_uuid":"b655a151895a67cb66b41ccf0bf97c5cd80cc0f2"},"cell_type":"code","source":"try:\n    # keras 2.2\n    from keras_preprocessing.image import ImageDataGenerator\nexcept:\n    from keras.preprocessing.image import ImageDataGenerator\nif BASE_MODEL=='VGG16':\n    from keras.applications.vgg16 import VGG16 as PTModel, preprocess_input\nelif BASE_MODEL=='RESNET52':\n    from keras.applications.resnet50 import ResNet50 as PTModel, preprocess_input\nelif BASE_MODEL=='InceptionV3':\n    from keras.applications.inception_v3 import InceptionV3 as PTModel, preprocess_input\nelif BASE_MODEL=='Xception':\n    from keras.applications.xception import Xception as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet169': \n    from keras.applications.densenet import DenseNet169 as PTModel, preprocess_input\nelif BASE_MODEL=='DenseNet121':\n    from keras.applications.densenet import DenseNet121 as PTModel, preprocess_input\nelse:\n    raise ValueError('Unknown model: {}'.format(BASE_MODEL))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a068b664c8bb465938fa3974c7b6e6120bf0860e"},"cell_type":"code","source":"img_gen_args = dict(samplewise_center=False, \n                              samplewise_std_normalization=False, \n                              horizontal_flip = True, \n                              vertical_flip = False, \n                              height_shift_range = 0.05, \n                              width_shift_range = 0.02, \n                              rotation_range = 3, \n                              shear_range = 0.01,\n                              fill_mode = 'nearest',\n                              zoom_range = 0.05,\n                               preprocessing_function=preprocess_input)\nimg_gen = ImageDataGenerator(**img_gen_args)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a3983f1be91084ba8c04441280efb18290814f2"},"cell_type":"code","source":"def flow_from_dataframe(img_data_gen, in_df, path_col, y_col, seed = None, **dflow_args):\n    base_dir = os.path.dirname(in_df[path_col].values[0])\n    print('## Ignore next message from keras, values are replaced anyways: seed: {}'.format(seed))\n    df_gen = img_data_gen.flow_from_directory(base_dir, \n                                     class_mode = 'sparse',\n                                              seed = seed,\n                                    **dflow_args)\n    df_gen.filenames = in_df[path_col].values\n    df_gen.classes = np.stack(in_df[y_col].values,0)\n    df_gen.samples = in_df.shape[0]\n    df_gen.n = in_df.shape[0]\n    df_gen._set_index_array()\n    df_gen.directory = '' # since we have the full path\n    print('Reinserting dataframe: {} images'.format(in_df.shape[0]))\n    return df_gen","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"720a67aaa3a8f4c9d5d50752c3f18f4e54dc3af0"},"cell_type":"code","source":"train_gen = flow_from_dataframe(img_gen, train_df, \n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = BATCH_SIZE)\n\nvalid_gen = flow_from_dataframe(img_gen, valid_df, \n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = 256) # we can use much larger batches for evaluation\n# used a fixed dataset for evaluating the algorithm\nvalid_X, valid_Y = next(flow_from_dataframe(img_gen, \n                               valid_df, \n                             path_col = 'path',\n                            y_col = 'class_vec', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = TEST_SAMPLES)) # one big batch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"26f5733df5b2c77a0c7e1a75b495744b0f8b34a4"},"cell_type":"code","source":"t_x, t_y = next(train_gen)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f273320778cca1188e3bf7800248ebf06533c61c"},"cell_type":"markdown","source":"# Build our pretrained model\nHere we build the pretrained model and download the weights"},{"metadata":{"trusted":true,"_uuid":"7ec309826c33787294dd1ba2cac5e4e65bab1755"},"cell_type":"code","source":"base_pretrained_model = PTModel(input_shape =  t_x.shape[1:], \n                              include_top = False, weights = 'imagenet')\nbase_pretrained_model.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c0af654a22993505bd8dc48355c4bd347b89947a"},"cell_type":"markdown","source":"## Model Supplements\nHere we add a few other layers to the model to make it better suited for the classification problem. "},{"metadata":{"trusted":true,"_uuid":"b0f2055383236d17f0d5455ecc2af5de922fc3b8"},"cell_type":"code","source":"from keras.layers import GlobalAveragePooling2D, Dense, Dropout, Flatten, Input, Conv2D, multiply, LocallyConnected2D, Lambda, AvgPool2D\nfrom keras.models import Model\nfrom keras.optimizers import Adam\npt_features = Input(base_pretrained_model.get_output_shape_at(0)[1:], name = 'feature_input')\npt_depth = base_pretrained_model.get_output_shape_at(0)[-1]\nfrom keras.layers import BatchNormalization\nbn_features = BatchNormalization()(pt_features)\ngap = GlobalAveragePooling2D()(bn_features)\n\ngap_dr = Dropout(DROPOUT)(gap)\ndr_steps = Dropout(DROPOUT)(Dense(DENSE_COUNT, activation = 'relu')(gap_dr))\nout_layer = Dense(t_y.shape[1], activation = 'softmax')(dr_steps)\n\nattn_model = Model(inputs = [pt_features], \n                   outputs = [out_layer], name = 'trained_model')\n\nattn_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74103ef71c30a9725949fbeed864174cbe62c69d"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.optimizers import Adam\npneu_model = Sequential(name = 'combined_model')\nbase_pretrained_model.trainable = False\npneu_model.add(base_pretrained_model)\npneu_model.add(attn_model)\npneu_model.compile(optimizer = Adam(lr = LEARN_RATE), loss = 'categorical_crossentropy',\n                           metrics = ['categorical_accuracy'])\npneu_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def decay_schedule(epoch, lr):\n    if (epoch % 10 == 0) and (epoch != 0):\n        lr = lr * 0.1\n    return lr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f6ff110ed2db877c1daee57de63e7dc16593c0e"},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau\nweight_path=\"{}_weights.best.hdf5\".format('lung_opacity')\n\ncheckpoint = ModelCheckpoint(weight_path, monitor='val_loss',\n                             save_best_only=True, mode='min', save_weights_only = True)\n\nlr_scheduler = LearningRateScheduler(decay_schedule)\n\nearly = EarlyStopping(monitor=\"val_loss\", \n                      mode=\"min\", \n                      patience=20) # probably needs to be more patient, but kaggle time is limited\ncallbacks_list = [checkpoint, early, lr_scheduler]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"EPOCH = 30","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58347af9669fed1e5308ac90a6ce06b3579761c5"},"cell_type":"code","source":"%%time\ntrain_gen.batch_size = BATCH_SIZE\nhistory = pneu_model.fit_generator(train_gen, \n                         validation_data = (valid_X, valid_Y), \n                         epochs=EPOCH, \n                         callbacks=callbacks_list,\n                         workers=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(10,5))\n# Plot history: Train loss\nplt.plot(history.history['loss'], label='Training loss')\nplt.xticks(np.arange(0, EPOCH, step=3))\nplt.legend(loc=\"upper right\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot history: validation accuracy\nplt.figure(figsize=(10,5))\nplt.plot(history.history['val_categorical_accuracy'], label='Validation accuracy')\nplt.xticks(np.arange(0, EPOCH, step=3))\nplt.legend(loc=\"lower right\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08e50876364886ef4a39723055136d741597348a"},"cell_type":"code","source":"pneu_model.load_weights(weight_path)\npneu_model.save('full_model.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f67dd5726e24517401ff0724f2aa07b1787cf791"},"cell_type":"code","source":"pred_Y = pneu_model.predict(valid_X, \n                          batch_size = BATCH_SIZE, \n                          verbose = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40f86cabdbea5e8dfc736882c4e7f5036297ec0a"},"cell_type":"code","source":"from sklearn.metrics import classification_report, confusion_matrix, accuracy_score\n\nplt.matshow(confusion_matrix(np.argmax(valid_Y, -1), np.argmax(pred_Y,-1)))\nprint(classification_report(np.argmax(valid_Y, -1), \n                            np.argmax(pred_Y,-1), target_names = class_enc.classes_))\nprint(confusion_matrix(np.argmax(valid_Y, -1), np.argmax(pred_Y,-1)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(accuracy_score(np.argmax(valid_Y, -1), np.argmax(pred_Y,-1)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = [0,1,2]\ncm = confusion_matrix(np.argmax(valid_Y, -1), np.argmax(pred_Y,-1), labels)\nprint(cm)\nfig = plt.figure()\nax = fig.add_subplot(111)\nax.xaxis.set_label_position('top')\ncax = ax.matshow(cm)\nfig.colorbar(cax)\nax.set_xticklabels([''] + labels)\nax.set_yticklabels([''] + labels)\n\nplt.xlabel('True')\nplt.ylabel('Predicted')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c217311f0f08d5473f493244cc4c4e17c0b6e9d"},"cell_type":"code","source":"from sklearn.metrics import roc_curve, roc_auc_score\nfpr, tpr, _ = roc_curve(np.argmax(valid_Y,-1)==0, pred_Y[:,0])\nfig, ax1 = plt.subplots(1,1, figsize = (5, 5), dpi = 250)\nax1.plot(fpr, tpr, 'b.-', label = 'DenseNet121-Model (AUC:%2.2f)' % roc_auc_score(np.argmax(valid_Y,-1)==0, pred_Y[:,0]))\nax1.plot(fpr, fpr, 'k-', label = 'Random Guessing')\nax1.legend(loc = 4)\nax1.set_xlabel('False Positive Rate')\nax1.set_ylabel('True Positive Rate');\nax1.set_title('Lung Opacity ROC Curve')\nfig.savefig('roc_valid.pdf')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}