{"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 keras import applications\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-06T11:03:46.402318Z","iopub.execute_input":"2022-04-06T11:03:46.4031Z","iopub.status.idle":"2022-04-06T11:03:53.467654Z","shell.execute_reply.started":"2022-04-06T11:03:46.403009Z","shell.execute_reply":"2022-04-06T11:03:53.466554Z"},"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-04-06T11:03:53.469918Z","iopub.execute_input":"2022-04-06T11:03:53.470575Z","iopub.status.idle":"2022-04-06T11:03:53.507578Z","shell.execute_reply.started":"2022-04-06T11:03:53.470527Z","shell.execute_reply":"2022-04-06T11:03:53.506699Z"},"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-04-06T11:03:53.508982Z","iopub.execute_input":"2022-04-06T11:03:53.509432Z","iopub.status.idle":"2022-04-06T11:03:53.516903Z","shell.execute_reply.started":"2022-04-06T11:03:53.509384Z","shell.execute_reply":"2022-04-06T11:03:53.515792Z"},"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-04-06T11:03:53.52019Z","iopub.execute_input":"2022-04-06T11:03:53.521347Z","iopub.status.idle":"2022-04-06T11:18:38.24515Z","shell.execute_reply.started":"2022-04-06T11:03:53.521314Z","shell.execute_reply":"2022-04-06T11:18:38.244207Z"},"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-04-06T11:18:38.247017Z","iopub.execute_input":"2022-04-06T11:18:38.247555Z","iopub.status.idle":"2022-04-06T11:22:04.649346Z","shell.execute_reply.started":"2022-04-06T11:18:38.247511Z","shell.execute_reply":"2022-04-06T11:22:04.648335Z"},"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-04-06T11:22:04.651231Z","iopub.execute_input":"2022-04-06T11:22:04.651812Z","iopub.status.idle":"2022-04-06T11:22:07.257923Z","shell.execute_reply.started":"2022-04-06T11:22:04.651761Z","shell.execute_reply":"2022-04-06T11:22:07.256706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_height,img_width = 64,64 \nnum_classes = 2\nbase_model = applications.resnet.ResNet50(weights= None, include_top=False, input_shape= (img_height,img_width,1))","metadata":{"execution":{"iopub.status.busy":"2022-04-06T11:22:07.259847Z","iopub.execute_input":"2022-04-06T11:22:07.260151Z","iopub.status.idle":"2022-04-06T11:22:11.71291Z","shell.execute_reply.started":"2022-04-06T11:22:07.2601Z","shell.execute_reply":"2022-04-06T11:22:11.71187Z"},"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-04-06T11:22:11.714454Z","iopub.execute_input":"2022-04-06T11:22:11.714766Z","iopub.status.idle":"2022-04-06T11:22:11.878791Z","shell.execute_reply.started":"2022-04-06T11:22:11.714723Z","shell.execute_reply":"2022-04-06T11:22:11.877764Z"},"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-04-06T11:22:11.882082Z","iopub.execute_input":"2022-04-06T11:22:11.882578Z","iopub.status.idle":"2022-04-06T11:22:11.903894Z","shell.execute_reply.started":"2022-04-06T11:22:11.882531Z","shell.execute_reply":"2022-04-06T11:22:11.902727Z"},"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-04-06T11:22:11.907187Z","iopub.execute_input":"2022-04-06T11:22:11.907483Z","iopub.status.idle":"2022-04-06T14:13:35.283682Z","shell.execute_reply.started":"2022-04-06T11:22:11.907442Z","shell.execute_reply":"2022-04-06T14:13:35.282706Z"},"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-04-06T14:13:35.285392Z","iopub.execute_input":"2022-04-06T14:13:35.285736Z","iopub.status.idle":"2022-04-06T14:13:35.292008Z","shell.execute_reply.started":"2022-04-06T14:13:35.285694Z","shell.execute_reply":"2022-04-06T14:13:35.290976Z"},"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-04-06T14:13:35.293889Z","iopub.execute_input":"2022-04-06T14:13:35.295036Z","iopub.status.idle":"2022-04-06T14:13:35.582027Z","shell.execute_reply.started":"2022-04-06T14:13:35.294996Z","shell.execute_reply":"2022-04-06T14:13:35.581109Z"},"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-04-06T14:13:35.583432Z","iopub.execute_input":"2022-04-06T14:13:35.584863Z","iopub.status.idle":"2022-04-06T14:13:42.762544Z","shell.execute_reply.started":"2022-04-06T14:13:35.584792Z","shell.execute_reply":"2022-04-06T14:13:42.761625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result2['BraTS21ID']=sample_df['BraTS21ID']\nresult2['MGMT_value']=result2['MGMT_value'].apply(lambda x:round(x*10)/10)\nresult2.to_csv('submission.csv',index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-06T14:13:42.764209Z","iopub.execute_input":"2022-04-06T14:13:42.76472Z","iopub.status.idle":"2022-04-06T14:13:42.777143Z","shell.execute_reply.started":"2022-04-06T14:13:42.764663Z","shell.execute_reply":"2022-04-06T14:13:42.776011Z"},"trusted":true},"execution_count":null,"outputs":[]}]}