{"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 os\nimport sys \nimport gc\nimport glob\nimport random \nimport cv2\nimport numpy as np \nimport pandas as pd \nfrom sklearn import metrics\nimport torch\nfrom torch import nn\nimport torch.nn.functional as F\nfrom tqdm import tqdm_notebook as tqdm\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\ngc.enable()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 48 \nNUM_FOLDS = 5\nNUM_EPOCHS = 3\nDEVICE = 'cuda'\nLEARNING_RATE = 1e-3\n\nsubmission_df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"package_path = \"../input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master/\"\nsys.path.append(package_path)\n\nimport efficientnet_pytorch","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset:\n    def __init__(self, paths, targets=None, inference_only=False):\n        self.paths = paths\n        self.targets = targets\n        self.inference_only = inference_only\n    \n    def __len__(self):\n        return len(self.paths)\n    \n    def __getitem__(self, index):\n        _id = self.paths[index]\n        patient_path = f\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/test/{str(_id).zfill(5)}/\"\n        channels = []\n        for t in (\"FLAIR\", \"T1w\", \"T1wCE\"): # \"T2w\"\n            t_paths = sorted(\n                glob.glob(os.path.join(patient_path, t, \"*\")), \n                key=lambda x: int(x[:-4].split(\"-\")[-1]),\n            )\n            # start, end = int(len(t_paths) * 0.475), int(len(t_paths) * 0.525)\n            x = len(t_paths)\n            if x < 10:\n                r = range(x)\n            else:\n                d = x // 10\n                r = range(d, x - d, d)\n                \n            channel = []\n            # for i in range(start, end + 1):\n            for i in r:\n                channel.append(cv2.resize(load_dicom(t_paths[i]), (256, 256)) / 255)\n            channel = np.mean(channel, axis=0)\n            channels.append(channel)\n        \n        if self.inference_only:\n            return {\n                'X': torch.tensor(channels).float()\n            }\n        \n        return {\n            \"X\": torch.tensor(channels).float(), \n            \"y\": torch.tensor(self.targets[index], dtype=torch.float),\n        }","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.net = efficientnet_pytorch.EfficientNet.from_name(\"efficientnet-b0\")\n        checkpoint = torch.load(\"../input/efficientnet-pytorch/efficientnet-b0-08094119.pth\")\n        self.net.load_state_dict(checkpoint)\n        n_features = self.net._fc.in_features\n        self.net._fc = nn.Linear(in_features=n_features, out_features=1, bias=True)\n    \n    def forward(self, x):\n        out = self.net(x)\n        return out","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, data_loader):\n    model.eval()\n\n    result = np.zeros(len(data_loader.dataset))    \n    index = 0\n    \n    final_predictions = []\n    with torch.no_grad():\n        for data in data_loader:\n            features = data['X']\n            features = features.to(DEVICE, dtype=torch.float)\n            \n            predictions = model(features).squeeze()\n            predictions = torch.sigmoid(predictions).cpu().detach().numpy().tolist()\n            final_predictions.extend(predictions)\n            \n        \n        return final_predictions","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_paths = [\n    '../input/rsna-brain-train-baseline/model_0.pth',\n    '../input/rsna-brain-train-baseline/model_1.pth',\n    '../input/rsna-brain-train-baseline/model_2.pth',\n    '../input/rsna-brain-train-baseline/model_3.pth',\n    '../input/rsna-brain-train-baseline/model_4.pth',\n]\n\ntest_dataset = Dataset(\n    submission_df.BraTS21ID.values,\n    inference_only=True\n)\ntest_loader = torch.utils.data.DataLoader(\n    test_dataset,\n    batch_size=BATCH_SIZE\n)  ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_predictions = np.zeros((NUM_FOLDS, len(submission_df)))\n\nfor i, path in enumerate(model_paths):\n    print(f'using {path}...')\n    \n    model = Model()\n    model.load_state_dict(torch.load(path))\n    model.to(DEVICE)\n    \n    all_predictions[i] = predict(model, test_loader)\n    \n    del model\n    gc.collect()\n\nsub_preds = all_predictions.mean(axis=0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df['MGMT_value'] = sub_preds\nsubmission_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}