{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":29653,"databundleVersionId":2420395,"sourceType":"competition"}],"dockerImageVersionId":30177,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-04-28T05:02:55.274069Z","iopub.execute_input":"2025-04-28T05:02:55.274684Z","iopub.status.idle":"2025-04-28T05:03:01.889583Z","shell.execute_reply.started":"2025-04-28T05:02:55.274575Z","shell.execute_reply":"2025-04-28T05:03:01.888975Z"}},"outputs":[],"execution_count":null},{"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":"2025-04-28T05:03:01.890790Z","iopub.execute_input":"2025-04-28T05:03:01.891002Z","iopub.status.idle":"2025-04-28T05:03:01.915369Z","shell.execute_reply.started":"2025-04-28T05:03:01.890977Z","shell.execute_reply":"2025-04-28T05:03:01.914556Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2025-04-28T05:03:01.916522Z","iopub.execute_input":"2025-04-28T05:03:01.916812Z","iopub.status.idle":"2025-04-28T05:03:01.922570Z","shell.execute_reply.started":"2025-04-28T05:03:01.916776Z","shell.execute_reply":"2025-04-28T05:03:01.921881Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2025-04-28T05:03:01.924603Z","iopub.execute_input":"2025-04-28T05:03:01.925299Z","iopub.status.idle":"2025-04-28T05:29:03.854204Z","shell.execute_reply.started":"2025-04-28T05:03:01.925259Z","shell.execute_reply":"2025-04-28T05:29:03.853393Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T05:29:03.855198Z","iopub.execute_input":"2025-04-28T05:29:03.855377Z","iopub.status.idle":"2025-04-28T05:32:33.294401Z","shell.execute_reply.started":"2025-04-28T05:29:03.855355Z","shell.execute_reply":"2025-04-28T05:32:33.293710Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y=np.array(trainlabel)\nY_train=to_categorical(y)\nX_train=np.array(trainset)\nX_test=np.array(testset)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T05:32:33.295540Z","iopub.execute_input":"2025-04-28T05:32:33.295767Z","iopub.status.idle":"2025-04-28T05:32:35.604812Z","shell.execute_reply.started":"2025-04-28T05:32:33.295740Z","shell.execute_reply":"2025-04-28T05:32:35.603892Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T05:32:35.605935Z","iopub.execute_input":"2025-04-28T05:32:35.606156Z","iopub.status.idle":"2025-04-28T05:32:39.416041Z","shell.execute_reply.started":"2025-04-28T05:32:35.606130Z","shell.execute_reply":"2025-04-28T05:32:39.415408Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T05:32:39.416976Z","iopub.execute_input":"2025-04-28T05:32:39.417178Z","iopub.status.idle":"2025-04-28T05:32:39.518910Z","shell.execute_reply.started":"2025-04-28T05:32:39.417144Z","shell.execute_reply":"2025-04-28T05:32:39.518133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T05:32:39.519970Z","iopub.execute_input":"2025-04-28T05:32:39.520179Z","iopub.status.idle":"2025-04-28T05:32:39.535603Z","shell.execute_reply.started":"2025-04-28T05:32:39.520153Z","shell.execute_reply":"2025-04-28T05:32:39.534801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, epochs = 50, batch_size = 64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T05:32:39.538089Z","iopub.execute_input":"2025-04-28T05:32:39.538381Z","iopub.status.idle":"2025-04-28T06:54:52.794701Z","shell.execute_reply.started":"2025-04-28T05:32:39.538337Z","shell.execute_reply":"2025-04-28T06:54:52.793867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"get_ac = history.history['accuracy']\nget_los = history.history['loss']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T06:54:52.796318Z","iopub.execute_input":"2025-04-28T06:54:52.796541Z","iopub.status.idle":"2025-04-28T06:54:52.800951Z","shell.execute_reply.started":"2025-04-28T06:54:52.796516Z","shell.execute_reply":"2025-04-28T06:54:52.800160Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T06:54:52.802023Z","iopub.execute_input":"2025-04-28T06:54:52.802248Z","iopub.status.idle":"2025-04-28T06:54:53.079871Z","shell.execute_reply.started":"2025-04-28T06:54:52.802224Z","shell.execute_reply":"2025-04-28T06:54:53.079162Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T06:54:53.080890Z","iopub.execute_input":"2025-04-28T06:54:53.081136Z","iopub.status.idle":"2025-04-28T06:54:59.824956Z","shell.execute_reply.started":"2025-04-28T06:54:53.081108Z","shell.execute_reply":"2025-04-28T06:54:59.824334Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-28T06:54:59.826262Z","iopub.execute_input":"2025-04-28T06:54:59.826813Z","iopub.status.idle":"2025-04-28T06:54:59.835441Z","shell.execute_reply.started":"2025-04-28T06:54:59.826775Z","shell.execute_reply":"2025-04-28T06:54:59.834843Z"}},"outputs":[],"execution_count":null}]}