{"cells":[{"metadata":{"_uuid":"08df5b0763fd140a63162c37160d2f8aec9da3aa"},"cell_type":"markdown","source":"# Data Loading Params"},{"metadata":{"trusted":true,"_uuid":"eec7f139c3cc1378ffb7883388ace63a7f1869b4"},"cell_type":"code","source":"# params we will probably want to do some hyperparameter optimization later\nIMG_SIZE = (384, 384) # [(224, 224), (384, 384), (512, 512), (640, 640)]\nBATCH_SIZE = 24 # [1, 8, 16, 24]\nTRAIN_SAMPLES = 8000 # [3000, 6000, 15000]\nTEST_SAMPLES = 800","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a182fc2caef9926f969df306c41d6fa790b2aaea"},"cell_type":"markdown","source":"# Setup, Don't Modify"},{"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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# Add input path to file path\n\nimage_bbox_df = pd.read_csv('../input/lung-opacity-overview/image_bbox_full.csv')\nimage_bbox_df['path'] = image_bbox_df['path'].map(lambda x: 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\n# Turn classes into numerical encodings\nclass_enc = LabelEncoder()\nimage_bbox_df['class_idx'] = class_enc.fit_transform(image_bbox_df['class'])\n# One hot encode 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":{"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.25, 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":{"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').apply(lambda x: x.sample(TRAIN_SAMPLES//3)).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":{"trusted":true,"_uuid":"4eb53f7c9a2eba3d9f734e860a289f13487b4e87"},"cell_type":"code","source":"import keras_preprocessing.image as KPImage\nfrom PIL import Image\nimport pydicom\n\ndef read_dicom_image(in_path):\n    img_arr = pydicom.read_file(in_path).pixel_array\n    return img_arr/img_arr.max()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9548edfc1a318edfcd2a387468a5b7950a376cc5"},"cell_type":"code","source":"class 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\n# Overwrite image loading\nKPImage.pil_image = medical_pil","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"48c34eedadc20ea36e2018159fcb95a0867edb02"},"cell_type":"markdown","source":"# Modify this to Change base model"},{"metadata":{"trusted":true,"_uuid":"b655a151895a67cb66b41ccf0bf97c5cd80cc0f2"},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.vgg16 import VGG16, preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cde7e9ad7ca7edeb1703157a2a50491e51652410"},"cell_type":"markdown","source":"## MODIFY THIS TO ADD IMAGE AUGMENTATION"},{"metadata":{"trusted":true,"_uuid":"a068b664c8bb465938fa3974c7b6e6120bf0860e"},"cell_type":"code","source":"# MODIFY THIS TO ADD IMAGE AUGMENTATION\nimg_gen_args = dict(preprocessing_function=preprocess_input)\nimg_gen = ImageDataGenerator(**img_gen_args)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2f79a363715b6eb85e65b50cfd1875b54c70a4d"},"cell_type":"markdown","source":"# Data Flow, Don't Modify"},{"metadata":{"trusted":true,"_uuid":"9a3983f1be91084ba8c04441280efb18290814f2"},"cell_type":"code","source":"'''\nClever hacking of the standard generator to load it from a dataframe. \nReplaces the file paths keras would from with the paths from the dataframe.\n\n'''\ndef 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,class_mode = 'sparse',seed = seed,**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,path_col = 'path',y_col = 'class_vec',\n                                target_size = IMG_SIZE,color_mode = 'rgb',batch_size = BATCH_SIZE)\n\nvalid_gen = flow_from_dataframe(img_gen, valid_df,path_col = 'path',y_col = 'class_vec',\n                                target_size = IMG_SIZE,color_mode = 'rgb',batch_size = 256) \n# used a fixed dataset for evaluating the algorithm\nvalid_X, valid_Y = next(flow_from_dataframe(img_gen,valid_df,path_col = 'path',y_col = 'class_vec',\n                                            target_size = IMG_SIZE,color_mode = 'rgb',\n                                            batch_size = TEST_SAMPLES))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"085cf677a4704dce34c679958674a84469e2ef4f"},"cell_type":"markdown","source":"# Show a batch\nHere we see what the augmentation actually looks like on a few sample images"},{"metadata":{"trusted":true,"_uuid":"37c9b66822c5dd59162813909c31d384db881026"},"cell_type":"code","source":"t_x, t_y = next(train_gen)\nprint(t_x.shape, t_y.shape)\nfig, m_axs = plt.subplots(2, 4, figsize = (16, 8))\nfor (c_x, c_y, c_ax) in zip(t_x, t_y, m_axs.flatten()):\n    c_ax.imshow(c_x[:,:,0], cmap = 'bone')\n    c_ax.set_title('%s' % class_enc.classes_[np.argmax(c_y)])\n    c_ax.axis('off')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f273320778cca1188e3bf7800248ebf06533c61c"},"cell_type":"markdown","source":"# Build our pretrained model, Modify This"},{"metadata":{"trusted":true,"_uuid":"7ec309826c33787294dd1ba2cac5e4e65bab1755"},"cell_type":"code","source":"base_pretrained_model = VGG16(input_shape =  t_x.shape[1:],include_top = False, weights = 'imagenet')\nbase_pretrained_model.trainable = True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e10c8bf8e252c4d7e855715742b90cca19d28c4"},"cell_type":"code","source":"# Make the last n conv layer trainable\nn = 2\nfor layer in base_pretrained_model.layers[:-n]:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"976d575439329c6adb7754ac51c77a6d27d8c65e"},"cell_type":"code","source":"base_pretrained_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c0af654a22993505bd8dc48355c4bd347b89947a"},"cell_type":"markdown","source":"# Model Supplements, Modify This\nHere we add a few other layers to the model to make it better suited for the classification problem. "},{"metadata":{"trusted":true,"_uuid":"5deb0ac4719837667f9cf72be4e115a007bc9f49"},"cell_type":"code","source":"# MODEL HYPER PARAMS\n# MODIFY THESE\nDENSE_COUNT = 128 # [32, 64, 128, 256]\nDROPOUT = 0.25 # [0, 0.25, 0.5]\nLEARN_RATE = 1e-4 # [1e-4, 1e-3, 4e-3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f285cb11ba397985a1354aff14362770cc94aec"},"cell_type":"code","source":"from keras.layers import GlobalAveragePooling2D, Dense, Dropout, LeakyReLU\nfrom keras.layers import Flatten, Input, Conv2D, multiply, LocallyConnected2D, Lambda\nfrom keras.layers import AvgPool2D,BatchNormalization\nfrom keras.models import Model\nfrom keras.optimizers import Adam\nfrom keras import layers","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')\n#base_pretrained_model.trainable = False\npneu_model.add(base_pretrained_model)\npneu_model.add(BatchNormalization())\npneu_model.add(GlobalAveragePooling2D())\npneu_model.add(Dense(DENSE_COUNT, activation = 'linear', use_bias=False))\npneu_model.add(Dropout(DROPOUT))\npneu_model.add(BatchNormalization())\npneu_model.add(LeakyReLU(0.1))\npneu_model.add(Dense(t_y.shape[1], activation = 'softmax'))\n\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,"_uuid":"58347af9669fed1e5308ac90a6ce06b3579761c5"},"cell_type":"code","source":"train_gen.batch_size = BATCH_SIZE\npneu_model.fit_generator(train_gen, \n                         steps_per_epoch=train_gen.n//BATCH_SIZE,\n                         validation_data=(valid_X, valid_Y), \n                         epochs=20,\n                         workers=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08e50876364886ef4a39723055136d741597348a"},"cell_type":"code","source":"pneu_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\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_))","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 = 'VGG-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":{"_kg_hide-output":true,"_kg_hide-input":true,"_uuid":"cf0f87166720c24ced3b2eae4b2afe77e6529225"},"cell_type":"markdown","source":"# Make a submission, Don't Modify\nWe load in the test images and make a submission using those images and a guess for $x, y$ and the width and height for all values where the model is more than 50% convinced there is something suspicious going on."},{"metadata":{"trusted":true,"_uuid":"2c8c27edd01ad87653e2c55284d64fa029afc472"},"cell_type":"code","source":"from glob import glob\nsub_img_df = pd.DataFrame({'path': \n              glob('../input/rsna-pneumonia-detection-challenge/stage_2_test_images/*.dcm')})\nsub_img_df['patientId'] = sub_img_df['path'].map(lambda x: os.path.splitext(os.path.basename(x))[0])\nsub_img_df.sample(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96510ebcc831ef0e9839b4050ba8294c5732fbb3"},"cell_type":"code","source":"submission_gen = flow_from_dataframe(img_gen, \n                                     sub_img_df, \n                             path_col = 'path',\n                            y_col = 'patientId', \n                            target_size = IMG_SIZE,\n                             color_mode = 'rgb',\n                            batch_size = BATCH_SIZE,\n                                    shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd564f016278a31bee570f7d6aedc953ca2ff432"},"cell_type":"code","source":"from tqdm import tqdm\nsub_steps = 2*sub_img_df.shape[0]//BATCH_SIZE\nout_ids, out_vec = [], []\nfor _, (t_x, t_y) in zip(tqdm(range(sub_steps)), submission_gen):\n    out_vec += [pneu_model.predict(t_x)]\n    out_ids += [t_y]\nout_vec = np.concatenate(out_vec, 0)\nout_ids = np.concatenate(out_ids, 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"527e6c29c66f2de2ec03b53f8de2406b6d051608"},"cell_type":"code","source":"pred_df = pd.DataFrame(out_vec, columns=class_enc.classes_)\npred_df['patientId'] = out_ids\npred_avg_df = pred_df.groupby('patientId').agg('mean').reset_index()\npred_avg_df['Lung Opacity'].hist()\npred_avg_df.to_csv('image_level_class_probs.csv', index=False) # not hte submission file\npred_avg_df.sample(2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5c7e5670184b1b9e7de9ad842de6ca671731b057"},"cell_type":"markdown","source":"### Simple Strategy\nWe use the `Lung Opacity` as our confidence and predict the image image. It will hopefully do a little bit better than a trivial baseline, and can be massively improved."},{"metadata":{"trusted":true,"_uuid":"68ee79e068892d310cc39209517ec20172db8889"},"cell_type":"code","source":"pred_avg_df['PredictionString'] = pred_avg_df['Lung Opacity'].map(lambda x: ('%2.2f 0 0 1024 1024' % x) if x>0.5 else '')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d746f6e21788945f5dc19843811a4e9068302255"},"cell_type":"code","source":"pred_avg_df[['patientId', 'PredictionString']].to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fdd135ae0a38d86d8782e5ec4f936c38869dcf8"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}