{"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 numpy as np\nimport pandas as pd\nimport os\nimport cv2\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.layers import Dense,Flatten,Dropout,Conv2D,MaxPooling2D,BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nimport keras\nfrom keras.models import Sequential\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":4.372049,"end_time":"2021-08-15T01:48:08.773037","exception":false,"start_time":"2021-08-15T01:48:04.400988","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T13:48:37.483652Z","iopub.execute_input":"2021-08-17T13:48:37.484061Z","iopub.status.idle":"2021-08-17T13:48:42.047092Z","shell.execute_reply.started":"2021-08-17T13:48:37.483983Z","shell.execute_reply":"2021-08-17T13:48:42.046248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_name=os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train')\ntest_name=os.listdir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/test')","metadata":{"papermill":{"duration":0.073265,"end_time":"2021-08-15T01:48:08.859865","exception":false,"start_time":"2021-08-15T01:48:08.7866","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T13:48:42.048529Z","iopub.execute_input":"2021-08-17T13:48:42.048853Z","iopub.status.idle":"2021-08-17T13:48:42.096595Z","shell.execute_reply.started":"2021-08-17T13:48:42.048820Z","shell.execute_reply":"2021-08-17T13:48:42.095914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\nlabels","metadata":{"papermill":{"duration":0.045008,"end_time":"2021-08-15T01:48:08.965429","exception":false,"start_time":"2021-08-15T01:48:08.920421","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T13:48:42.098133Z","iopub.execute_input":"2021-08-17T13:48:42.098375Z","iopub.status.idle":"2021-08-17T13:48:42.126352Z","shell.execute_reply.started":"2021-08-17T13:48:42.098351Z","shell.execute_reply":"2021-08-17T13:48:42.125470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\nsample","metadata":{"papermill":{"duration":0.032644,"end_time":"2021-08-15T01:48:09.012478","exception":false,"start_time":"2021-08-15T01:48:08.979834","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T13:48:42.127919Z","iopub.execute_input":"2021-08-17T13:48:42.128274Z","iopub.status.idle":"2021-08-17T13:48:42.147472Z","shell.execute_reply.started":"2021-08-17T13:48:42.128241Z","shell.execute_reply":"2021-08-17T13:48:42.146728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    dicom=pydicom.read_file(path)\n    data=dicom.pixel_array\n    data=data-np.min(data)\n    if np.max(data) != 0:\n        data=data/np.max(data)\n    data=(data*255).astype(np.uint8)\n    return data","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:48:42.148584Z","iopub.execute_input":"2021-08-17T13:48:42.148965Z","iopub.status.idle":"2021-08-17T13:48:42.155460Z","shell.execute_reply.started":"2021-08-17T13:48:42.148908Z","shell.execute_reply":"2021-08-17T13:48:42.154388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path0='../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T1wCE/Image-1.dcm'\nimg1=load_dicom(path0)\nimg2=cv2.resize(img1,(9,9)) \nprint(img1.shape)\nprint(img2.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:48:42.157102Z","iopub.execute_input":"2021-08-17T13:48:42.157578Z","iopub.status.idle":"2021-08-17T13:48:42.189128Z","shell.execute_reply.started":"2021-08-17T13:48:42.157534Z","shell.execute_reply":"2021-08-17T13:48:42.188286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='../input/rsna-miccai-brain-tumor-radiogenomic-classification/train'\ntrainset=[]\ntrainlabel=[]\ntrainidt=[]\nfor i in tqdm(range(len(labels))):\n    idt=labels.loc[i,'BraTS21ID']\n    idt2=('00000'+str(idt))[-5:]\n    path=os.path.join(train_dir,idt2,'T1wCE')              \n    for im in os.listdir(path):\n        img=load_dicom(os.path.join(path,im))\n        img=cv2.resize(img,(9,9)) \n        image=img_to_array(img)\n        image=image/255.0\n        trainset+=[image]\n        trainlabel+=[labels.loc[i,'MGMT_value']]\n        trainidt+=[idt]","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:48:42.190403Z","iopub.execute_input":"2021-08-17T13:48:42.190786Z","iopub.status.idle":"2021-08-17T13:59:35.689429Z","shell.execute_reply.started":"2021-08-17T13:48:42.190752Z","shell.execute_reply":"2021-08-17T13:59:35.688647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir='../input/rsna-miccai-brain-tumor-radiogenomic-classification/test'\ntestset=[]\ntestidt=[]\nfor i in tqdm(range(len(sample))):\n    idt=sample.loc[i,'BraTS21ID']\n    idt2=('00000'+str(idt))[-5:]\n    path=os.path.join(test_dir,idt2,'T1wCE')               \n    for im in os.listdir(path):   \n        img=load_dicom(os.path.join(path,im))\n        img=cv2.resize(img,(9,9)) \n        image=img_to_array(img)\n        image=image/255.0\n        testset+=[image]\n        testidt+=[idt]","metadata":{"execution":{"iopub.status.busy":"2021-08-17T13:59:35.692023Z","iopub.execute_input":"2021-08-17T13:59:35.692356Z","iopub.status.idle":"2021-08-17T14:01:28.504688Z","shell.execute_reply.started":"2021-08-17T13:59:35.692328Z","shell.execute_reply":"2021-08-17T14:01:28.503865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\ny0=np.array(trainlabel)\nY_train=to_categorical(y0)\nX_train=np.array(trainset)\nX_test=np.array(testset)","metadata":{"papermill":{"duration":0.11132,"end_time":"2021-08-15T01:55:57.901314","exception":false,"start_time":"2021-08-15T01:55:57.789994","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:01:28.506294Z","iopub.execute_input":"2021-08-17T14:01:28.506794Z","iopub.status.idle":"2021-08-17T14:01:28.616076Z","shell.execute_reply.started":"2021-08-17T14:01:28.506752Z","shell.execute_reply":"2021-08-17T14:01:28.615205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(64,(4,4),input_shape = (9,9,1),activation = 'relu'))\nmodel.add(Conv2D(32,(2,2),activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dense(8, activation='relu'))\nmodel.add(Dense(2, activation='softmax'))","metadata":{"papermill":{"duration":1.681756,"end_time":"2021-08-15T01:55:59.5995","exception":false,"start_time":"2021-08-15T01:55:57.917744","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:01:28.617355Z","iopub.execute_input":"2021-08-17T14:01:28.617734Z","iopub.status.idle":"2021-08-17T14:01:30.737233Z","shell.execute_reply.started":"2021-08-17T14:01:28.617697Z","shell.execute_reply":"2021-08-17T14:01:30.736396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])","metadata":{"papermill":{"duration":0.031642,"end_time":"2021-08-15T01:55:59.647331","exception":false,"start_time":"2021-08-15T01:55:59.615689","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:01:30.738397Z","iopub.execute_input":"2021-08-17T14:01:30.738730Z","iopub.status.idle":"2021-08-17T14:01:30.764709Z","shell.execute_reply.started":"2021-08-17T14:01:30.738697Z","shell.execute_reply":"2021-08-17T14:01:30.763614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"papermill":{"duration":0.027444,"end_time":"2021-08-15T01:55:59.690933","exception":false,"start_time":"2021-08-15T01:55:59.663489","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:01:30.766536Z","iopub.execute_input":"2021-08-17T14:01:30.766999Z","iopub.status.idle":"2021-08-17T14:01:30.787762Z","shell.execute_reply.started":"2021-08-17T14:01:30.766958Z","shell.execute_reply":"2021-08-17T14:01:30.781624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"his = model.fit(X_train, Y_train, validation_split=0.2, epochs=100, batch_size=64, verbose=2)","metadata":{"papermill":{"duration":252.54866,"end_time":"2021-08-15T02:00:12.256908","exception":false,"start_time":"2021-08-15T01:55:59.708248","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:01:30.789058Z","iopub.execute_input":"2021-08-17T14:01:30.789467Z","iopub.status.idle":"2021-08-17T14:07:09.026785Z","shell.execute_reply.started":"2021-08-17T14:01:30.789428Z","shell.execute_reply":"2021-08-17T14:07:09.025973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_acc = his.history['accuracy']\nvalue_acc = his.history['val_accuracy']\nget_loss = his.history['loss']\nvalidation_loss = his.history['val_loss']\n\nepochs = range(len(get_acc))\nplt.plot(epochs, get_acc, 'r', label='Accuracy of Training data')\nplt.plot(epochs, value_acc, 'b', label='Accuracy of Validation data')\nplt.title('Training vs validation accuracy')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.258723,"end_time":"2021-08-15T02:00:12.584549","exception":false,"start_time":"2021-08-15T02:00:12.325826","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:07:09.028267Z","iopub.execute_input":"2021-08-17T14:07:09.028620Z","iopub.status.idle":"2021-08-17T14:07:09.221672Z","shell.execute_reply.started":"2021-08-17T14:07:09.028584Z","shell.execute_reply":"2021-08-17T14:07:09.220699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = range(len(get_loss))\nplt.plot(epochs, get_loss, 'r', label='Loss of Training data')\nplt.plot(epochs, validation_loss, 'b', label='Loss of Validation data')\nplt.title('Training vs validation loss')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.238648,"end_time":"2021-08-15T02:00:12.895467","exception":false,"start_time":"2021-08-15T02:00:12.656819","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:07:09.223117Z","iopub.execute_input":"2021-08-17T14:07:09.223513Z","iopub.status.idle":"2021-08-17T14:07:09.385542Z","shell.execute_reply.started":"2021-08-17T14:07:09.223474Z","shell.execute_reply":"2021-08-17T14:07:09.384542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"papermill":{"duration":0.089939,"end_time":"2021-08-15T02:00:13.061334","exception":false,"start_time":"2021-08-15T02:00:12.971395","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:07:09.386775Z","iopub.execute_input":"2021-08-17T14:07:09.387154Z","iopub.status.idle":"2021-08-17T14:07:09.401161Z","shell.execute_reply.started":"2021-08-17T14:07:09.387100Z","shell.execute_reply":"2021-08-17T14:07:09.400069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_test)\npred=np.argmax(y_pred,axis=1)\nresult=pd.DataFrame(testidt)\nresult[1]=pred\nresult.columns=['BraTS21ID','MGMT_value']\nresult2=result.groupby('BraTS21ID',as_index=False).mean()\nresult2","metadata":{"papermill":{"duration":0.51761,"end_time":"2021-08-15T02:00:13.649865","exception":false,"start_time":"2021-08-15T02:00:13.132255","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:07:09.402837Z","iopub.execute_input":"2021-08-17T14:07:09.403256Z","iopub.status.idle":"2021-08-17T14:07:09.978609Z","shell.execute_reply.started":"2021-08-17T14:07:09.403201Z","shell.execute_reply":"2021-08-17T14:07:09.977747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result2['BraTS21ID']=sample['BraTS21ID']\nresult2['MGMT_value']=result2['MGMT_value'].apply(lambda x:round(x*10)/10)\nresult2.to_csv('submission.csv',index=False)\nresult2","metadata":{"papermill":{"duration":0.094658,"end_time":"2021-08-15T02:00:13.825252","exception":false,"start_time":"2021-08-15T02:00:13.730594","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-08-17T14:07:09.980101Z","iopub.execute_input":"2021-08-17T14:07:09.980474Z","iopub.status.idle":"2021-08-17T14:07:10.000496Z","shell.execute_reply.started":"2021-08-17T14:07:09.980435Z","shell.execute_reply":"2021-08-17T14:07:09.999665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.073373,"end_time":"2021-08-15T02:00:13.973048","exception":false,"start_time":"2021-08-15T02:00:13.899675","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}