{"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 matplotlib.pyplot as plt \nimport cv2 as cv\nfrom path import Path\nimport os \nimport glob\nimport tensorflow_hub as hub\nimport os \nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers\nfrom tqdm import tqdm\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import applications\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-13T04:41:41.011395Z","iopub.execute_input":"2022-06-13T04:41:41.011991Z","iopub.status.idle":"2022-06-13T04:41:41.020006Z","shell.execute_reply.started":"2022-06-13T04:41:41.011948Z","shell.execute_reply":"2022-06-13T04:41:41.017970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df= pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\nsample_df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:41:41.031482Z","iopub.execute_input":"2022-06-13T04:41:41.031968Z","iopub.status.idle":"2022-06-13T04:41:41.043941Z","shell.execute_reply.started":"2022-06-13T04:41:41.031929Z","shell.execute_reply":"2022-06-13T04:41:41.042922Z"},"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":"2022-06-13T04:41:41.049030Z","iopub.execute_input":"2022-06-13T04:41:41.049756Z","iopub.status.idle":"2022-06-13T04:41:41.055888Z","shell.execute_reply.started":"2022-06-13T04:41:41.049691Z","shell.execute_reply":"2022-06-13T04:41:41.054991Z"},"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(train_df))):\n    idt=train_df.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=cv.resize(img,(64,64)) \n        image=img_to_array(img)\n        image=image/255.0\n        trainset+=[image]\n        trainlabel+=[train_df.loc[i,'MGMT_value']]\n        trainidt+=[idt]","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:41:41.063705Z","iopub.execute_input":"2022-06-13T04:41:41.065670Z","iopub.status.idle":"2022-06-13T04:57:19.731593Z","shell.execute_reply.started":"2022-06-13T04:41:41.065625Z","shell.execute_reply":"2022-06-13T04:57:19.730762Z"},"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_df))):\n    idt=sample_df.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=cv.resize(img,(64,64)) \n        image=img_to_array(img)\n        image=image/255.0\n        testset+=[image]\n        testidt+=[idt]","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:57:19.733356Z","iopub.execute_input":"2022-06-13T04:57:19.734171Z","iopub.status.idle":"2022-06-13T04:59:49.276741Z","shell.execute_reply.started":"2022-06-13T04:57:19.734126Z","shell.execute_reply":"2022-06-13T04:59:49.275890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=np.array(trainlabel)\nY_train=to_categorical(y)\nX_train=np.array(trainset)\nX_test=np.array(testset)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:59:49.278423Z","iopub.execute_input":"2022-06-13T04:59:49.278742Z","iopub.status.idle":"2022-06-13T04:59:51.567523Z","shell.execute_reply.started":"2022-06-13T04:59:49.278700Z","shell.execute_reply":"2022-06-13T04:59:51.566462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_height,img_width = 64,64 \nnum_classes = 2\nbase_model = applications.efficientnet.EfficientNetB0(weights= None, include_top=False, input_shape= (img_height,img_width,1))\n# base_model = applications.resnet.ResNet50(weights= None, include_top=False, input_shape= (img_height,img_width,1))","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:59:51.569806Z","iopub.execute_input":"2022-06-13T04:59:51.570123Z","iopub.status.idle":"2022-06-13T04:59:56.295576Z","shell.execute_reply.started":"2022-06-13T04:59:51.570091Z","shell.execute_reply":"2022-06-13T04:59:56.294765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base_model.output\nx = keras.layers.GlobalAveragePooling2D()(x)\nx = keras.layers.Dropout(0.7)(x)\npredictions = keras.layers.Dense(num_classes, activation= 'softmax')(x)\nmodel = keras.models.Model(inputs = base_model.input, outputs = predictions)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:59:56.297189Z","iopub.execute_input":"2022-06-13T04:59:56.297457Z","iopub.status.idle":"2022-06-13T04:59:56.468253Z","shell.execute_reply.started":"2022-06-13T04:59:56.297420Z","shell.execute_reply":"2022-06-13T04:59:56.467171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:59:56.470141Z","iopub.execute_input":"2022-06-13T04:59:56.470461Z","iopub.status.idle":"2022-06-13T04:59:56.493127Z","shell.execute_reply.started":"2022-06-13T04:59:56.470422Z","shell.execute_reply":"2022-06-13T04:59:56.492215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, epochs = 100, batch_size = 64)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T04:59:56.494709Z","iopub.execute_input":"2022-06-13T04:59:56.494986Z","iopub.status.idle":"2022-06-13T07:54:36.979735Z","shell.execute_reply.started":"2022-06-13T04:59:56.494946Z","shell.execute_reply":"2022-06-13T07:54:36.978838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_ac = history.history['accuracy']\nget_los = history.history['loss']","metadata":{"execution":{"iopub.status.busy":"2022-06-13T07:54:36.981661Z","iopub.execute_input":"2022-06-13T07:54:36.981940Z","iopub.status.idle":"2022-06-13T07:54:36.986732Z","shell.execute_reply.started":"2022-06-13T07:54:36.981903Z","shell.execute_reply":"2022-06-13T07:54:36.985886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = range(len(get_ac))\nplt.plot(epochs, get_ac, 'g', label='Accuracy of Training data')\nplt.plot(epochs, get_los, 'r', label='Loss of Training data')\nplt.title('Training data accuracy and loss')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-13T07:54:36.988322Z","iopub.execute_input":"2022-06-13T07:54:36.988596Z","iopub.status.idle":"2022-06-13T07:54:37.257158Z","shell.execute_reply.started":"2022-06-13T07:54:36.988546Z","shell.execute_reply":"2022-06-13T07:54:37.256416Z"},"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":{"execution":{"iopub.status.busy":"2022-06-13T07:54:37.260269Z","iopub.execute_input":"2022-06-13T07:54:37.260521Z","iopub.status.idle":"2022-06-13T07:54:49.236277Z","shell.execute_reply.started":"2022-06-13T07:54:37.260493Z","shell.execute_reply":"2022-06-13T07:54:49.235492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result2['BraTS21ID']=sample_df['BraTS21ID']\nresult2['MGMT_value']=result2['MGMT_value'].apply(lambda x: 1 if x>=0.5 else 0)\n#result2['MGMT_value']=result2['MGMT_value'].apply(lambda x:round(x*10)/10)\nresult2.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-13T07:54:49.237509Z","iopub.execute_input":"2022-06-13T07:54:49.237780Z","iopub.status.idle":"2022-06-13T07:54:49.248364Z","shell.execute_reply.started":"2022-06-13T07:54:49.237740Z","shell.execute_reply":"2022-06-13T07:54:49.247525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}