{"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 pandas as pd\nimport numpy as np\nfrom glob import glob\nimport os\nimport cv2\nfrom PIL import Image","metadata":{"papermill":{"duration":0.227148,"end_time":"2021-09-05T06:59:19.765816","exception":false,"start_time":"2021-09-05T06:59:19.538668","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:17:52.922050Z","iopub.execute_input":"2021-09-05T10:17:52.922671Z","iopub.status.idle":"2021-09-05T10:17:53.063118Z","shell.execute_reply.started":"2021-09-05T10:17:52.922627Z","shell.execute_reply":"2021-09-05T10:17:53.062243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\ndf","metadata":{"papermill":{"duration":0.042679,"end_time":"2021-09-05T06:59:19.819166","exception":false,"start_time":"2021-09-05T06:59:19.776487","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:17:53.064684Z","iopub.execute_input":"2021-09-05T10:17:53.065068Z","iopub.status.idle":"2021-09-05T10:17:53.095994Z","shell.execute_reply.started":"2021-09-05T10:17:53.065027Z","shell.execute_reply":"2021-09-05T10:17:53.095103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '../input/rsna-miccai-png/train'\npatients = sorted(os.listdir(data_dir))\npatients[:5]","metadata":{"papermill":{"duration":0.091459,"end_time":"2021-09-05T06:59:19.921410","exception":false,"start_time":"2021-09-05T06:59:19.829951","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:17:53.099820Z","iopub.execute_input":"2021-09-05T10:17:53.100088Z","iopub.status.idle":"2021-09-05T10:17:53.148099Z","shell.execute_reply.started":"2021-09-05T10:17:53.100062Z","shell.execute_reply":"2021-09-05T10:17:53.147265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '../input/rsna-miccai-png/test'\npatients_test = sorted(os.listdir(data_dir))\npatients_test[:5]","metadata":{"papermill":{"duration":0.034471,"end_time":"2021-09-05T06:59:19.966862","exception":false,"start_time":"2021-09-05T06:59:19.932391","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:17:53.149773Z","iopub.execute_input":"2021-09-05T10:17:53.150138Z","iopub.status.idle":"2021-09-05T10:17:53.168279Z","shell.execute_reply.started":"2021-09-05T10:17:53.150101Z","shell.execute_reply":"2021-09-05T10:17:53.167349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id = []\nfor i in patients:\n    id.append(int(i))\nid[:5]","metadata":{"papermill":{"duration":0.02055,"end_time":"2021-09-05T06:59:19.998932","exception":false,"start_time":"2021-09-05T06:59:19.978382","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:17:53.169755Z","iopub.execute_input":"2021-09-05T10:17:53.170174Z","iopub.status.idle":"2021-09-05T10:17:53.177487Z","shell.execute_reply.started":"2021-09-05T10:17:53.170139Z","shell.execute_reply":"2021-09-05T10:17:53.176066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.DataFrame({'path' : glob('../input/rsna-miccai-png/train/00000/FLAIR/*.png'),\n                              'label' : 1})\n\nfor i, patient in enumerate(patients[1:]):\n    data_slice = pd.DataFrame({'path' : glob('../input/rsna-miccai-png/train/' + patient +'/FLAIR/*.png'),\n                               'label' : df['MGMT_value'][i+1]})\n    train_dataset = pd.concat([train_dataset, data_slice])\n    \ntrain_dataset = train_dataset.reset_index(drop = True)\ntrain_dataset['label'] = train_dataset['label'].astype(str)\ntrain_dataset","metadata":{"papermill":{"duration":6.820495,"end_time":"2021-09-05T06:59:26.830529","exception":false,"start_time":"2021-09-05T06:59:20.010034","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:17:53.179653Z","iopub.execute_input":"2021-09-05T10:17:53.180066Z","iopub.status.idle":"2021-09-05T10:18:03.343904Z","shell.execute_reply.started":"2021-09-05T10:17:53.180027Z","shell.execute_reply":"2021-09-05T10:18:03.343068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = pd.DataFrame({'path' : glob('../input/rsna-miccai-png/test/00001/FLAIR/*.png')})\n\nfor i, patient in enumerate(patients_test[1:]):\n    data_slice = pd.DataFrame({'path' : glob('../input/rsna-miccai-png/test/' + patient +'/FLAIR/*.png')})\n    test_dataset = pd.concat([test_dataset, data_slice])\n    \ntest_dataset = test_dataset.reset_index(drop = True)\n\ntest_dataset","metadata":{"papermill":{"duration":1.147329,"end_time":"2021-09-05T06:59:27.990369","exception":false,"start_time":"2021-09-05T06:59:26.843040","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:18:03.345227Z","iopub.execute_input":"2021-09-05T10:18:03.345593Z","iopub.status.idle":"2021-09-05T10:18:05.495656Z","shell.execute_reply.started":"2021-09-05T10:18:03.345556Z","shell.execute_reply":"2021-09-05T10:18:05.494807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_valid = train_test_split(train_dataset, stratify = train_dataset['label'], random_state = 42, test_size = 0.2)","metadata":{"papermill":{"duration":0.89613,"end_time":"2021-09-05T06:59:28.898945","exception":false,"start_time":"2021-09-05T06:59:28.002815","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:18:05.497975Z","iopub.execute_input":"2021-09-05T10:18:05.498395Z","iopub.status.idle":"2021-09-05T10:18:06.280137Z","shell.execute_reply.started":"2021-09-05T10:18:05.498353Z","shell.execute_reply":"2021-09-05T10:18:06.279215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\nfrom keras import *\nfrom keras.layers import *\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom keras.callbacks import *\n\nidg = ImageDataGenerator(horizontal_flip = True)\nidg2 = ImageDataGenerator()\n\ntrain_dataset = idg.flow_from_dataframe(x_train, x_col = 'path', y_col = 'label', target_size=(150, 150), batch_size = 64)\nvalid_dataset = idg2.flow_from_dataframe(x_valid, x_col = 'path', y_col = 'label', target_size=(150, 150), batch_size = 64)\n\nefn = EfficientNetB0(include_top = False, pooling = 'avg', input_shape=(150, 150, 3), weights='../input/keras-pretrained-models/EfficientNetB0_NoTop_ImageNet.h5',)\nes = EarlyStopping(patience = 2, restore_best_weights = True)\nrl = ReduceLROnPlateau(patience = 1, factor = 0.2, verbose = 1)\n\nmodel = Sequential()\nmodel.add(efn)\nmodel.add(Dense(2, activation = 'softmax'))\n\nmodel.compile(metrics = ['acc'], loss = 'categorical_crossentropy', optimizer = 'adam')\n\nmodel.fit(train_dataset, validation_data = valid_dataset, epochs = 1, callbacks = [es, rl])\n\ntest_generator = idg2.flow_from_dataframe(test_dataset, x_col = 'path', y_col = None, target_size = (150, 150), batch_size = 64, class_mode = None, shuffle = False)\n\nresult = model.predict(test_generator, verbose = True, workers = 2)","metadata":{"papermill":{"duration":518.304114,"end_time":"2021-09-05T07:08:07.215610","exception":false,"start_time":"2021-09-05T06:59:28.911496","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:18:06.281726Z","iopub.execute_input":"2021-09-05T10:18:06.282053Z","iopub.status.idle":"2021-09-05T10:25:26.643356Z","shell.execute_reply.started":"2021-09-05T10:18:06.282015Z","shell.execute_reply":"2021-09-05T10:25:26.642240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"papermill":{"duration":0.677873,"end_time":"2021-09-05T07:08:08.495733","exception":false,"start_time":"2021-09-05T07:08:07.817860","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:25:26.645223Z","iopub.execute_input":"2021-09-05T10:25:26.645882Z","iopub.status.idle":"2021-09-05T10:25:26.662470Z","shell.execute_reply.started":"2021-09-05T10:25:26.645833Z","shell.execute_reply":"2021-09-05T10:25:26.660846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_result = []\ni = 0\nfor patient in patients_test:\n    length = len(glob('../input/rsna-miccai-png/test/' + patient +'/FLAIR/*.png'))\n    final_result.append(result[i:i+length, 1].sum()/length)\n    i += length\n    \nfinal_result","metadata":{"papermill":{"duration":0.504398,"end_time":"2021-09-05T07:08:09.460284","exception":false,"start_time":"2021-09-05T07:08:08.955886","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:25:26.664291Z","iopub.execute_input":"2021-09-05T10:25:26.665668Z","iopub.status.idle":"2021-09-05T10:25:26.815104Z","shell.execute_reply.started":"2021-09-05T10:25:26.665612Z","shell.execute_reply":"2021-09-05T10:25:26.811828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\")\ntest","metadata":{"papermill":{"duration":0.418429,"end_time":"2021-09-05T07:08:10.279932","exception":false,"start_time":"2021-09-05T07:08:09.861503","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:33:33.771812Z","iopub.execute_input":"2021-09-05T10:33:33.772162Z","iopub.status.idle":"2021-09-05T10:33:33.790921Z","shell.execute_reply.started":"2021-09-05T10:33:33.772123Z","shell.execute_reply":"2021-09-05T10:33:33.789845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['MGMT_value'] = final_result","metadata":{"execution":{"iopub.status.busy":"2021-09-05T10:33:36.193581Z","iopub.execute_input":"2021-09-05T10:33:36.193932Z","iopub.status.idle":"2021-09-05T10:33:36.203214Z","shell.execute_reply.started":"2021-09-05T10:33:36.193900Z","shell.execute_reply":"2021-09-05T10:33:36.202059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T10:35:08.845495Z","iopub.execute_input":"2021-09-05T10:35:08.845894Z","iopub.status.idle":"2021-09-05T10:35:08.865975Z","shell.execute_reply.started":"2021-09-05T10:35:08.845859Z","shell.execute_reply":"2021-09-05T10:35:08.864818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.to_csv('submission.csv', index = 0)","metadata":{"papermill":{"duration":0.402749,"end_time":"2021-09-05T07:08:12.197724","exception":false,"start_time":"2021-09-05T07:08:11.794975","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-09-05T10:33:59.315051Z","iopub.execute_input":"2021-09-05T10:33:59.315436Z","iopub.status.idle":"2021-09-05T10:33:59.323534Z","shell.execute_reply.started":"2021-09-05T10:33:59.315388Z","shell.execute_reply":"2021-09-05T10:33:59.322207Z"},"trusted":true},"execution_count":null,"outputs":[]}]}