{"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 tensorflow.keras import applications\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-04-27T19:30:12.301972Z","iopub.execute_input":"2025-04-27T19:30:12.302189Z","iopub.status.idle":"2025-04-27T19:30:18.897298Z","shell.execute_reply.started":"2025-04-27T19:30:12.302128Z","shell.execute_reply":"2025-04-27T19:30:18.896724Z"},"trusted":true},"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-27T19:30:18.898534Z","iopub.execute_input":"2025-04-27T19:30:18.898751Z","iopub.status.idle":"2025-04-27T19:30:18.916085Z","shell.execute_reply.started":"2025-04-27T19:30:18.898727Z","shell.execute_reply":"2025-04-27T19:30:18.915521Z"},"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-27T19:30:18.916957Z","iopub.execute_input":"2025-04-27T19:30:18.917149Z","iopub.status.idle":"2025-04-27T19:30:18.923657Z","shell.execute_reply.started":"2025-04-27T19:30:18.917126Z","shell.execute_reply":"2025-04-27T19:30:18.922969Z"},"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-27T19:30:18.924638Z","iopub.execute_input":"2025-04-27T19:30:18.924889Z","iopub.status.idle":"2025-04-27T19:45:25.167277Z","shell.execute_reply.started":"2025-04-27T19:30:18.924856Z","shell.execute_reply":"2025-04-27T19:45:25.166573Z"},"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":{"execution":{"iopub.status.busy":"2025-04-27T19:45:25.169124Z","iopub.execute_input":"2025-04-27T19:45:25.169324Z","iopub.status.idle":"2025-04-27T19:47:42.534533Z","shell.execute_reply.started":"2025-04-27T19:45:25.169299Z","shell.execute_reply":"2025-04-27T19:47:42.533868Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2025-04-27T19:47:42.535474Z","iopub.execute_input":"2025-04-27T19:47:42.535659Z","iopub.status.idle":"2025-04-27T19:47:44.095114Z","shell.execute_reply.started":"2025-04-27T19:47:42.535636Z","shell.execute_reply":"2025-04-27T19:47:44.09433Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2025-04-27T19:47:44.096117Z","iopub.execute_input":"2025-04-27T19:47:44.096318Z","iopub.status.idle":"2025-04-27T19:47:48.120546Z","shell.execute_reply.started":"2025-04-27T19:47:44.096295Z","shell.execute_reply":"2025-04-27T19:47:48.119966Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2025-04-27T19:47:48.121522Z","iopub.execute_input":"2025-04-27T19:47:48.121765Z","iopub.status.idle":"2025-04-27T19:47:48.247136Z","shell.execute_reply.started":"2025-04-27T19:47:48.121731Z","shell.execute_reply":"2025-04-27T19:47:48.246437Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2025-04-27T19:47:48.248125Z","iopub.execute_input":"2025-04-27T19:47:48.248322Z","iopub.status.idle":"2025-04-27T19:47:48.262349Z","shell.execute_reply.started":"2025-04-27T19:47:48.248298Z","shell.execute_reply":"2025-04-27T19:47:48.261743Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, epochs = 100, batch_size = 64)","metadata":{"execution":{"iopub.status.busy":"2025-04-27T19:47:48.263412Z","iopub.execute_input":"2025-04-27T19:47:48.263649Z","iopub.status.idle":"2025-04-27T22:19:19.403987Z","shell.execute_reply.started":"2025-04-27T19:47:48.263616Z","shell.execute_reply":"2025-04-27T22:19:19.40343Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"get_ac = history.history['accuracy']\nget_los = history.history['loss']","metadata":{"execution":{"iopub.status.busy":"2025-04-27T22:19:19.405109Z","iopub.execute_input":"2025-04-27T22:19:19.405309Z","iopub.status.idle":"2025-04-27T22:19:19.409192Z","shell.execute_reply.started":"2025-04-27T22:19:19.405283Z","shell.execute_reply":"2025-04-27T22:19:19.408477Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2025-04-27T22:19:19.410311Z","iopub.execute_input":"2025-04-27T22:19:19.410785Z","iopub.status.idle":"2025-04-27T22:19:19.591354Z","shell.execute_reply.started":"2025-04-27T22:19:19.410748Z","shell.execute_reply":"2025-04-27T22:19:19.590663Z"},"trusted":true},"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":{"execution":{"iopub.status.busy":"2025-04-27T22:19:19.592697Z","iopub.execute_input":"2025-04-27T22:19:19.593304Z","iopub.status.idle":"2025-04-27T22:19:26.245035Z","shell.execute_reply.started":"2025-04-27T22:19:19.593265Z","shell.execute_reply":"2025-04-27T22:19:26.244286Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2025-04-27T22:19:26.246781Z","iopub.execute_input":"2025-04-27T22:19:26.246944Z","iopub.status.idle":"2025-04-27T22:19:26.254686Z","shell.execute_reply.started":"2025-04-27T22:19:26.246924Z","shell.execute_reply":"2025-04-27T22:19:26.254073Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}