{"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":"In this notebook, we will use [TorchIO](http://torchio.rtfd.io/) and its new [`RSNAMICCAI`](https://torchio.readthedocs.io/datasets.html#rsnamiccai) dataset class to load, preprocess and write the challenge dataset.","metadata":{}},{"cell_type":"code","source":"# Import torchio via this notebook \n# https://www.kaggle.com/ohbewise/pip-download-torchio\n\n!pip install --quiet --no-index --find-links ../input/pip-download-torchio/ --requirement ../input/pip-download-torchio/requirements.txt","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:30:37.944212Z","iopub.execute_input":"2021-09-27T01:30:37.945080Z","iopub.status.idle":"2021-09-27T01:30:51.245922Z","shell.execute_reply.started":"2021-09-27T01:30:37.944917Z","shell.execute_reply":"2021-09-27T01:30:51.244679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nimport torch\nimport torchio as tio\nimport matplotlib.pyplot as plt\n\nplt.rcParams[\"figure.figsize\"] = (12, 10)","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:30:53.516087Z","iopub.execute_input":"2021-09-27T01:30:53.516461Z","iopub.status.idle":"2021-09-27T01:30:55.784052Z","shell.execute_reply.started":"2021-09-27T01:30:53.516427Z","shell.execute_reply":"2021-09-27T01:30:55.783046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# debug: pass\npreprocessing_transforms = (\n    tio.ToCanonical(),\n    tio.Resample(1, image_interpolation='bspline'),\n    tio.Resample('T1w', image_interpolation='nearest'),\n    tio.CropOrPad((251,251,150)),\n)\npreprocess = tio.Compose(preprocessing_transforms)","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:40:40.293762Z","iopub.execute_input":"2021-09-27T01:40:40.294218Z","iopub.status.idle":"2021-09-27T01:40:40.300406Z","shell.execute_reply.started":"2021-09-27T01:40:40.294152Z","shell.execute_reply":"2021-09-27T01:40:40.298998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification'\n\ntest_set = tio.datasets.RSNAMICCAI(root_dir, train=False, transform=preprocess)","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:40:40.820176Z","iopub.execute_input":"2021-09-27T01:40:40.820521Z","iopub.status.idle":"2021-09-27T01:40:41.254453Z","shell.execute_reply.started":"2021-09-27T01:40:40.820492Z","shell.execute_reply":"2021-09-27T01:40:41.253382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generate dataloaders\nbatch_size = 1\n\ntest_loader = torch.utils.data.DataLoader(test_set,\n                                          batch_size=batch_size,\n                                          shuffle=False)\n\nprint(len(test_loader))","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:40:44.979226Z","iopub.execute_input":"2021-09-27T01:40:44.979694Z","iopub.status.idle":"2021-09-27T01:40:44.986949Z","shell.execute_reply.started":"2021-09-27T01:40:44.979651Z","shell.execute_reply":"2021-09-27T01:40:44.985396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate model and import weights","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.append('../input/efficientnetpyttorch3d/EfficientNet-PyTorch-3D')","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:32:15.046969Z","iopub.execute_input":"2021-09-27T01:32:15.047387Z","iopub.status.idle":"2021-09-27T01:32:15.053383Z","shell.execute_reply.started":"2021-09-27T01:32:15.047344Z","shell.execute_reply":"2021-09-27T01:32:15.051722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from efficientnet_pytorch_3d import EfficientNet3D\n\nmodel = EfficientNet3D.from_name(\"efficientnet-b0\", override_params={'num_classes': 1}, in_channels=1)\nmodel.cuda()\n\ndevice = torch.device('cuda') if torch.cuda.is_available() else 'cpu'","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:32:36.506167Z","iopub.execute_input":"2021-09-27T01:32:36.506645Z","iopub.status.idle":"2021-09-27T01:32:41.710933Z","shell.execute_reply.started":"2021-09-27T01:32:36.506602Z","shell.execute_reply":"2021-09-27T01:32:41.709731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load model weights\nPATH = '../input/rsna-model-baseline/model_baseline.pt'\n\nmodel.load_state_dict(torch.load(PATH))\nmodel.eval()","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:33:29.869451Z","iopub.execute_input":"2021-09-27T01:33:29.869843Z","iopub.status.idle":"2021-09-27T01:33:30.648701Z","shell.execute_reply.started":"2021-09-27T01:33:29.869812Z","shell.execute_reply":"2021-09-27T01:33:30.647500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\n\nsigmoid = torch.nn.Sigmoid()\nls_pred = []\n\nwith torch.no_grad():\n    for batch in tqdm(test_loader):\n    \n        # Get input array\n        input_flair = batch['FLAIR']['data'].float().to(device)\n        #input_t1w = batch['T1w']['data'].float().to(device)\n        #input_t1wce = batch['T1wCE']['data'].float().to(device)\n        #input_t2w = batch['T2w']['data'].float().to(device)\n        \n        # Concat input together\n        #inputs = torch.cat([input_flair, input_t1w, input_t1wce, input_t2w], dim=0)\n\n        # Feed to the model and get logit\n        logit = model(input_flair)\n        pred = sigmoid(logit).cpu().detach().numpy().item()\n        ls_pred.append(pred)","metadata":{"execution":{"iopub.status.busy":"2021-09-27T01:47:39.615635Z","iopub.execute_input":"2021-09-27T01:47:39.616043Z","iopub.status.idle":"2021-09-27T02:42:03.464566Z","shell.execute_reply.started":"2021-09-27T01:47:39.615995Z","shell.execute_reply":"2021-09-27T02:42:03.461748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load sample submission\ncsv_path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv'\n\nimport pandas as pd\nsubmission = pd.read_csv(csv_path)\nsubmission['MGMT_value'] = ls_pred\n\n# Export submission\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-27T02:44:24.306313Z","iopub.execute_input":"2021-09-27T02:44:24.306731Z","iopub.status.idle":"2021-09-27T02:44:24.369973Z","shell.execute_reply.started":"2021-09-27T02:44:24.306693Z","shell.execute_reply":"2021-09-27T02:44:24.368907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}