{"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-05-06T08:45:02.785992Z","iopub.execute_input":"2022-05-06T08:45:02.786323Z","iopub.status.idle":"2022-05-06T08:45:08.962352Z","shell.execute_reply.started":"2022-05-06T08:45:02.786239Z","shell.execute_reply":"2022-05-06T08:45:08.961612Z"},"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-05-06T08:45:08.963906Z","iopub.execute_input":"2022-05-06T08:45:08.964146Z","iopub.status.idle":"2022-05-06T08:45:08.987437Z","shell.execute_reply.started":"2022-05-06T08:45:08.964102Z","shell.execute_reply":"2022-05-06T08:45:08.986811Z"},"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-05-06T08:45:08.990241Z","iopub.execute_input":"2022-05-06T08:45:08.990432Z","iopub.status.idle":"2022-05-06T08:45:08.995173Z","shell.execute_reply.started":"2022-05-06T08:45:08.990405Z","shell.execute_reply":"2022-05-06T08:45:08.994434Z"},"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-05-06T08:45:08.997266Z","iopub.execute_input":"2022-05-06T08:45:08.998152Z","iopub.status.idle":"2022-05-06T08:58:22.752777Z","shell.execute_reply.started":"2022-05-06T08:45:08.998108Z","shell.execute_reply":"2022-05-06T08:58:22.75149Z"},"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-05-06T08:58:22.754874Z","iopub.execute_input":"2022-05-06T08:58:22.755503Z","iopub.status.idle":"2022-05-06T09:00:56.538116Z","shell.execute_reply.started":"2022-05-06T08:58:22.75541Z","shell.execute_reply":"2022-05-06T09:00:56.537446Z"},"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-05-06T09:00:56.539502Z","iopub.execute_input":"2022-05-06T09:00:56.539961Z","iopub.status.idle":"2022-05-06T09:00:58.704507Z","shell.execute_reply.started":"2022-05-06T09:00:56.539923Z","shell.execute_reply":"2022-05-06T09:00:58.703684Z"},"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-05-06T09:00:58.705754Z","iopub.execute_input":"2022-05-06T09:00:58.706025Z","iopub.status.idle":"2022-05-06T09:01:02.275051Z","shell.execute_reply.started":"2022-05-06T09:00:58.705988Z","shell.execute_reply":"2022-05-06T09:01:02.274348Z"},"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-05-06T09:01:02.276407Z","iopub.execute_input":"2022-05-06T09:01:02.276848Z","iopub.status.idle":"2022-05-06T09:01:02.380807Z","shell.execute_reply.started":"2022-05-06T09:01:02.276809Z","shell.execute_reply":"2022-05-06T09:01:02.38015Z"},"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-05-06T09:01:02.382045Z","iopub.execute_input":"2022-05-06T09:01:02.382291Z","iopub.status.idle":"2022-05-06T09:01:02.398094Z","shell.execute_reply.started":"2022-05-06T09:01:02.382256Z","shell.execute_reply":"2022-05-06T09:01:02.397321Z"},"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-05-06T09:01:02.40077Z","iopub.execute_input":"2022-05-06T09:01:02.401083Z","iopub.status.idle":"2022-05-06T11:45:36.81344Z","shell.execute_reply.started":"2022-05-06T09:01:02.401046Z","shell.execute_reply":"2022-05-06T11:45:36.81269Z"},"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-05-06T11:45:36.814968Z","iopub.execute_input":"2022-05-06T11:45:36.815315Z","iopub.status.idle":"2022-05-06T11:45:36.819878Z","shell.execute_reply.started":"2022-05-06T11:45:36.815276Z","shell.execute_reply":"2022-05-06T11:45:36.819194Z"},"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-05-06T11:45:36.821045Z","iopub.execute_input":"2022-05-06T11:45:36.821738Z","iopub.status.idle":"2022-05-06T11:45:37.061222Z","shell.execute_reply.started":"2022-05-06T11:45:36.821702Z","shell.execute_reply":"2022-05-06T11:45:37.060446Z"},"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-05-06T11:45:37.063503Z","iopub.execute_input":"2022-05-06T11:45:37.063994Z","iopub.status.idle":"2022-05-06T11:45:48.546998Z","shell.execute_reply.started":"2022-05-06T11:45:37.063954Z","shell.execute_reply":"2022-05-06T11:45:48.546309Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-05-06T11:45:48.548179Z","iopub.execute_input":"2022-05-06T11:45:48.548513Z","iopub.status.idle":"2022-05-06T11:45:48.557578Z","shell.execute_reply.started":"2022-05-06T11:45:48.548463Z","shell.execute_reply":"2022-05-06T11:45:48.556879Z"},"trusted":true},"execution_count":null,"outputs":[]}]}