{"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":"markdown","source":"### Importing Libraries","metadata":{"papermill":{"duration":0.019853,"end_time":"2021-07-14T06:36:49.082307","exception":false,"start_time":"2021-07-14T06:36:49.062454","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import os\nimport torch\nimport torch.nn as nn\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport pydicom as dicom\nimport cv2\nimport ast\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"papermill":{"duration":0.495913,"end_time":"2021-07-14T06:36:49.597437","exception":false,"start_time":"2021-07-14T06:36:49.101524","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:10.271008Z","iopub.execute_input":"2021-07-14T08:33:10.271359Z","iopub.status.idle":"2021-07-14T08:33:10.27648Z","shell.execute_reply.started":"2021-07-14T08:33:10.271327Z","shell.execute_reply":"2021-07-14T08:33:10.275695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Path of dataset","metadata":{"papermill":{"duration":0.017935,"end_time":"2021-07-14T06:36:49.634317","exception":false,"start_time":"2021-07-14T06:36:49.616382","status":"completed"},"tags":[]}},{"cell_type":"code","source":"path = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/'\nos.listdir(path)","metadata":{"papermill":{"duration":0.031764,"end_time":"2021-07-14T06:36:49.685308","exception":false,"start_time":"2021-07-14T06:36:49.653544","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:13.844395Z","iopub.execute_input":"2021-07-14T08:33:13.844921Z","iopub.status.idle":"2021-07-14T08:33:13.855117Z","shell.execute_reply.started":"2021-07-14T08:33:13.84488Z","shell.execute_reply":"2021-07-14T08:33:13.853552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Loading dataset","metadata":{"papermill":{"duration":0.018413,"end_time":"2021-07-14T06:36:49.722929","exception":false,"start_time":"2021-07-14T06:36:49.704516","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train_labels.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"papermill":{"duration":0.049836,"end_time":"2021-07-14T06:36:49.791666","exception":false,"start_time":"2021-07-14T06:36:49.74183","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:15.111603Z","iopub.execute_input":"2021-07-14T08:33:15.112122Z","iopub.status.idle":"2021-07-14T08:33:15.12594Z","shell.execute_reply.started":"2021-07-14T08:33:15.112079Z","shell.execute_reply":"2021-07-14T08:33:15.125059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Overview of dataset","metadata":{"papermill":{"duration":0.01833,"end_time":"2021-07-14T06:36:49.829713","exception":false,"start_time":"2021-07-14T06:36:49.811383","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print('Samples train:', len(train_data))\nprint('Samples test:', len(samp_subm))","metadata":{"papermill":{"duration":0.029432,"end_time":"2021-07-14T06:36:49.877726","exception":false,"start_time":"2021-07-14T06:36:49.848294","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:15.830874Z","iopub.execute_input":"2021-07-14T08:33:15.831479Z","iopub.status.idle":"2021-07-14T08:33:15.83755Z","shell.execute_reply.started":"2021-07-14T08:33:15.83143Z","shell.execute_reply":"2021-07-14T08:33:15.836721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"papermill":{"duration":0.04769,"end_time":"2021-07-14T06:36:49.94474","exception":false,"start_time":"2021-07-14T06:36:49.89705","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:16.422975Z","iopub.execute_input":"2021-07-14T08:33:16.423516Z","iopub.status.idle":"2021-07-14T08:33:16.432435Z","shell.execute_reply.started":"2021-07-14T08:33:16.423468Z","shell.execute_reply":"2021-07-14T08:33:16.431736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MGMT_value counts ","metadata":{}},{"cell_type":"code","source":"train_data[\"MGMT_value\"].value_counts().head(2).plot(kind = 'pie', autopct='%1.1f%%', figsize=(8, 8)).legend()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:17.01258Z","iopub.execute_input":"2021-07-14T08:33:17.013111Z","iopub.status.idle":"2021-07-14T08:33:17.352678Z","shell.execute_reply.started":"2021-07-14T08:33:17.013061Z","shell.execute_reply":"2021-07-14T08:33:17.351314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"MGMT_value\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:17.354542Z","iopub.execute_input":"2021-07-14T08:33:17.354833Z","iopub.status.idle":"2021-07-14T08:33:17.36389Z","shell.execute_reply.started":"2021-07-14T08:33:17.354806Z","shell.execute_reply":"2021-07-14T08:33:17.362862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.head()","metadata":{"papermill":{"duration":0.034572,"end_time":"2021-07-14T06:36:49.998935","exception":false,"start_time":"2021-07-14T06:36:49.964363","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:17.554103Z","iopub.execute_input":"2021-07-14T08:33:17.554572Z","iopub.status.idle":"2021-07-14T08:33:17.568756Z","shell.execute_reply.started":"2021-07-14T08:33:17.554536Z","shell.execute_reply":"2021-07-14T08:33:17.567265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Extract folder id of the first train sample","metadata":{"papermill":{"duration":0.020498,"end_time":"2021-07-14T06:36:50.081102","exception":false,"start_time":"2021-07-14T06:36:50.060604","status":"completed"},"tags":[]}},{"cell_type":"code","source":"folder = str(train_data.loc[0, 'BraTS21ID']).zfill(5)\nfolder","metadata":{"papermill":{"duration":0.034038,"end_time":"2021-07-14T06:36:50.135247","exception":false,"start_time":"2021-07-14T06:36:50.101209","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:18.363756Z","iopub.execute_input":"2021-07-14T08:33:18.364106Z","iopub.status.idle":"2021-07-14T08:33:18.373078Z","shell.execute_reply.started":"2021-07-14T08:33:18.364076Z","shell.execute_reply":"2021-07-14T08:33:18.371698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Folders content","metadata":{"papermill":{"duration":0.020596,"end_time":"2021-07-14T06:36:50.176531","exception":false,"start_time":"2021-07-14T06:36:50.155935","status":"completed"},"tags":[]}},{"cell_type":"code","source":"os.listdir(path+'train/'+folder)","metadata":{"papermill":{"duration":0.036186,"end_time":"2021-07-14T06:36:50.233083","exception":false,"start_time":"2021-07-14T06:36:50.196897","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:19.100124Z","iopub.execute_input":"2021-07-14T08:33:19.100517Z","iopub.status.idle":"2021-07-14T08:33:19.109883Z","shell.execute_reply.started":"2021-07-14T08:33:19.100484Z","shell.execute_reply":"2021-07-14T08:33:19.108586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of FLAIR images:', len(os.listdir(path+'train/'+folder+'/'+'FLAIR')))\nprint('Number of T1w images:', len(os.listdir(path+'train/'+folder+'/'+'T1w')))\nprint('Number of T1wCE images:', len(os.listdir(path+'train/'+folder+'/'+'T1wCE')))\nprint('Number of T2w images:', len(os.listdir(path+'train/'+folder+'/'+'T2w')))","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.102662,"end_time":"2021-07-14T06:36:50.358103","exception":false,"start_time":"2021-07-14T06:36:50.255441","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:19.436486Z","iopub.execute_input":"2021-07-14T08:33:19.436903Z","iopub.status.idle":"2021-07-14T08:33:19.448054Z","shell.execute_reply.started":"2021-07-14T08:33:19.436859Z","shell.execute_reply":"2021-07-14T08:33:19.447105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Read image","metadata":{"execution":{"iopub.execute_input":"2021-07-14T06:29:52.486712Z","iopub.status.busy":"2021-07-14T06:29:52.486362Z","iopub.status.idle":"2021-07-14T06:29:52.492418Z","shell.execute_reply":"2021-07-14T06:29:52.491077Z","shell.execute_reply.started":"2021-07-14T06:29:52.486664Z"},"papermill":{"duration":0.020451,"end_time":"2021-07-14T06:36:50.401305","exception":false,"start_time":"2021-07-14T06:36:50.380854","status":"completed"},"tags":[]}},{"cell_type":"code","source":"path_file = ''.join([path, 'train/', folder, '/', 'FLAIR/'])\nimage = os.listdir(path_file)[0]\ndata_file = dicom.dcmread(path_file+image)\nimg = data_file.pixel_array","metadata":{"papermill":{"duration":0.044499,"end_time":"2021-07-14T06:36:50.466717","exception":false,"start_time":"2021-07-14T06:36:50.422218","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:19.944864Z","iopub.execute_input":"2021-07-14T08:33:19.945203Z","iopub.status.idle":"2021-07-14T08:33:19.955763Z","shell.execute_reply.started":"2021-07-14T08:33:19.945174Z","shell.execute_reply":"2021-07-14T08:33:19.954062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Image shape","metadata":{"papermill":{"duration":0.020498,"end_time":"2021-07-14T06:36:50.508205","exception":false,"start_time":"2021-07-14T06:36:50.487707","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print('Image shape:', img.shape)","metadata":{"papermill":{"duration":0.030781,"end_time":"2021-07-14T06:36:50.560096","exception":false,"start_time":"2021-07-14T06:36:50.529315","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-07-14T08:33:20.511243Z","iopub.execute_input":"2021-07-14T08:33:20.511609Z","iopub.status.idle":"2021-07-14T08:33:20.518151Z","shell.execute_reply.started":"2021-07-14T08:33:20.511579Z","shell.execute_reply":"2021-07-14T08:33:20.516581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # Flair Images ","metadata":{}},{"cell_type":"code","source":"def plot_examples(row = 0, cat = 'FLAIR'): \n    folder = str(train_data.loc[row, 'BraTS21ID']).zfill(5)\n    path_file = ''.join([path, 'train/', folder, '/', cat, '/'])\n    images = os.listdir(path_file)\n    \n    fig, axs = plt.subplots(1, 5, figsize=(30, 30))\n    fig.subplots_adjust(hspace = .2, wspace=.2)\n    axs = axs.ravel()\n    \n    for num in range(5):\n        data_file = dicom.dcmread(path_file+images[num])\n        img = data_file.pixel_array\n        axs[num].imshow(img, cmap='gray')\n        axs[num].set_title(cat+' '+images[num])\n        axs[num].set_xticklabels([])\n        axs[num].set_yticklabels([])\n        \nrow = 0\nplot_examples(row = row, cat = 'FLAIR')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:21.058773Z","iopub.execute_input":"2021-07-14T08:33:21.059123Z","iopub.status.idle":"2021-07-14T08:33:21.980896Z","shell.execute_reply.started":"2021-07-14T08:33:21.059094Z","shell.execute_reply":"2021-07-14T08:33:21.979804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # T1w Images ","metadata":{}},{"cell_type":"code","source":"plot_examples(row = row, cat = 'T1w')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:21.982402Z","iopub.execute_input":"2021-07-14T08:33:21.982749Z","iopub.status.idle":"2021-07-14T08:33:22.862814Z","shell.execute_reply.started":"2021-07-14T08:33:21.982719Z","shell.execute_reply":"2021-07-14T08:33:22.861524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # T1wCE Images ","metadata":{}},{"cell_type":"code","source":"plot_examples(row = row, cat = 'T1wCE')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:22.864789Z","iopub.execute_input":"2021-07-14T08:33:22.86518Z","iopub.status.idle":"2021-07-14T08:33:23.699866Z","shell.execute_reply.started":"2021-07-14T08:33:22.865147Z","shell.execute_reply":"2021-07-14T08:33:23.698297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # T2w Images ","metadata":{}},{"cell_type":"code","source":"plot_examples(row = row, cat = 'T2w')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:23.702368Z","iopub.execute_input":"2021-07-14T08:33:23.702988Z","iopub.status.idle":"2021-07-14T08:33:24.537803Z","shell.execute_reply.started":"2021-07-14T08:33:23.702934Z","shell.execute_reply":"2021-07-14T08:33:24.536321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning for Brain Tumor Radiogenomic Classification¶","metadata":{}},{"cell_type":"code","source":"VGG_types = {\n    'VGG11' : [64, 'M', 128, 'M', 256, 256, 'M', 512,512, 'M',512,512,'M'],\n    'VGG13' : [64,64, 'M', 128, 128, 'M', 256, 256, 'M', 512,512, 'M', 512,512,'M'],\n    'VGG16' : [64,64, 'M', 128, 128, 'M', 256, 256,256, 'M', 512,512,512, 'M',512,512,512,'M'],\n    'VGG19' : [64,64, 'M', 128, 128, 'M', 256, 256,256,256, 'M', 512,512,512,512, 'M',512,512,512,512,'M']\n}\nclass VGGnet(nn.Module):\n    def __init__(self, model, in_channels=3, num_classes=10, init_weights=True):\n        super(VGGnet,self).__init__()\n        self.in_channels = in_channels\n\n        # create conv_layers corresponding to VGG type\n        self.conv_layers = self.create_conv_laters(VGG_types[model])\n\n        self.fcs = nn.Sequential(\n            nn.Linear(1536, 1536//2),\n            nn.ReLU(),\n            nn.Dropout(),\n            nn.Linear(1536//2, 1536//2),\n            nn.ReLU(),\n            nn.Dropout(),\n            nn.Linear(1536//2, num_classes),\n        )\n\n        # weight initialization\n        if init_weights:\n            self._initialize_weights()\n\n    def forward(self, x):\n        x = self.conv_layers(x)\n        x = x.view(x.size(0), -1)\n        x = self.fcs(x)\n        return x\n\n    # defint weight initialization function\n    def _initialize_weights(self):\n        for m in self.modules():\n            if isinstance(m, nn.Conv3d):\n                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')\n                if m.bias is not None:\n                    nn.init.constant_(m.bias, 0)\n            elif isinstance(m, nn.BatchNorm3d):\n                nn.init.constant_(m.weight, 1)\n                nn.init.constant_(m.bias, 0)\n            elif isinstance(m, nn.Linear):\n                nn.init.normal_(m.weight, 0, 0.01)\n                nn.init.constant_(m.bias, 0)\n    \n    # define a function to create conv layer taken the key of VGG_type dict \n    def create_conv_laters(self, architecture):\n        layers = []\n        in_channels = self.in_channels # 3\n\n        for x in architecture:\n            if type(x) == int: # int means conv layer\n                out_channels = x\n\n                layers += [nn.Conv3d(in_channels=in_channels, out_channels=out_channels,\n                                     kernel_size=(3,2,3), stride=(1,1,1), padding=(1,1,1)),\n                           nn.BatchNorm3d(x),\n                           nn.ReLU()]\n                in_channels = x\n            elif x == 'M':\n                layers += [nn.MaxPool3d(kernel_size=(2,2,2), stride=(2,2,2))]\n        \n        return nn.Sequential(*layers)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:24.53945Z","iopub.execute_input":"2021-07-14T08:33:24.539797Z","iopub.status.idle":"2021-07-14T08:33:24.559514Z","shell.execute_reply.started":"2021-07-14T08:33:24.539757Z","shell.execute_reply":"2021-07-14T08:33:24.558227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define device\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n# creat VGGnet object\n# Choose between 'VGG11', 'VGG13', 'VGG16', 'VGG19'\nmodel = VGGnet('VGG16', in_channels=1, num_classes=1, init_weights=True).to(device)\nprint(model)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:24.561339Z","iopub.execute_input":"2021-07-14T08:33:24.561673Z","iopub.status.idle":"2021-07-14T08:33:25.22417Z","shell.execute_reply.started":"2021-07-14T08:33:24.561645Z","shell.execute_reply":"2021-07-14T08:33:25.222516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:29.209004Z","iopub.execute_input":"2021-07-14T08:33:29.209365Z","iopub.status.idle":"2021-07-14T08:33:29.222989Z","shell.execute_reply.started":"2021-07-14T08:33:29.209334Z","shell.execute_reply":"2021-07-14T08:33:29.222139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T08:33:32.191842Z","iopub.execute_input":"2021-07-14T08:33:32.192412Z","iopub.status.idle":"2021-07-14T08:33:32.199815Z","shell.execute_reply.started":"2021-07-14T08:33:32.192377Z","shell.execute_reply":"2021-07-14T08:33:32.198343Z"},"trusted":true},"execution_count":null,"outputs":[]}]}