{"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 json\nimport glob\nimport random\nimport collections\nimport time\nimport re\n\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport torch\nfrom torch import nn\nfrom torch.utils import data as torch_data\nfrom sklearn import model_selection as sk_model_selection\nfrom torch.nn import functional as torch_functional\nimport torch.nn.functional as F\n\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.metrics import accuracy_score\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-18T06:17:09.579863Z","iopub.execute_input":"2023-01-18T06:17:09.580234Z","iopub.status.idle":"2023-01-18T06:17:09.587967Z","shell.execute_reply.started":"2023-01-18T06:17:09.580204Z","shell.execute_reply":"2023-01-18T06:17:09.586677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = load_dicom_image(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00005/FLAIR/Image-10.dcm\")","metadata":{"execution":{"iopub.status.busy":"2023-01-18T06:13:38.770314Z","iopub.execute_input":"2023-01-18T06:13:38.77095Z","iopub.status.idle":"2023-01-18T06:13:38.783449Z","shell.execute_reply.started":"2023-01-18T06:13:38.770916Z","shell.execute_reply":"2023-01-18T06:13:38.78236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_directory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification'\npytorch3dpath = \"../input/efficientnetpyttorch3d/EfficientNet-PyTorch-3D\"\nmri_types = ['FLAIR','T1w','T1wCE','T2w']\nSIZE = 256\nNUM_IMAGES = 64\n\nsys.path.append(pytorch3dpath)\nfrom efficientnet_pytorch_3d import EfficientNet3D","metadata":{"execution":{"iopub.status.busy":"2023-01-18T06:12:13.59074Z","iopub.execute_input":"2023-01-18T06:12:13.591261Z","iopub.status.idle":"2023-01-18T06:12:13.643563Z","shell.execute_reply.started":"2023-01-18T06:12:13.591229Z","shell.execute_reply":"2023-01-18T06:12:13.642636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_image(path, img_size=SIZE, voi_lut=True, rotate=0):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n        \n    if rotate > 0:\n        rot_choices = [0, cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE, cv2.ROTATE_180]\n        data = cv2.rotate(data, rot_choices[rotate])\n        \n    data = cv2.resize(data, (img_size, img_size))\n    return data\n\n\ndef load_dicom_images_3d(scan_id, num_imgs=NUM_IMAGES, img_size=SIZE, mri_type=\"FLAIR\", split=\"train\", rotate=0):\n\n    files = sorted(glob.glob(f\"{data_directory}/{split}/{scan_id}/{mri_type}/*.dcm\"), \n               key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)])\n    middle = len(files)//2\n    num_imgs2 = num_imgs//2\n    p1 = max(0, middle - num_imgs2)\n    p2 = min(len(files), middle + num_imgs2)\n    img3d = np.stack([load_dicom_image(f, rotate=rotate) for f in files[p1:p2]]).T \n    if img3d.shape[-1] < num_imgs:\n        n_zero = np.zeros((img_size, img_size, num_imgs - img3d.shape[-1]))\n        img3d = np.concatenate((img3d,  n_zero), axis = -1)\n        \n    if np.min(img3d) < np.max(img3d):\n        img3d = img3d - np.min(img3d)\n        img3d = img3d / np.max(img3d)\n            \n    return np.expand_dims(img3d,0)\n\na = load_dicom_images_3d(\"00002\")\nprint(a.shape)\nprint(np.min(a), np.max(a), np.mean(a), np.median(a))","metadata":{"execution":{"iopub.status.busy":"2023-01-18T06:12:15.542526Z","iopub.execute_input":"2023-01-18T06:12:15.542888Z","iopub.status.idle":"2023-01-18T06:12:16.763799Z","shell.execute_reply.started":"2023-01-18T06:12:15.542859Z","shell.execute_reply":"2023-01-18T06:12:16.760327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = load_dicom_images_3d(\"00002\")\nprint(a.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T19:26:41.614528Z","iopub.execute_input":"2023-01-15T19:26:41.614892Z","iopub.status.idle":"2023-01-15T19:26:41.960161Z","shell.execute_reply.started":"2023-01-15T19:26:41.614859Z","shell.execute_reply":"2023-01-15T19:26:41.959063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed):\n    random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    if torch.cuda.is_available():\n        torch.cuda.manual_seed_all(seed)\n        torch.backends.cudnn.deterministic = True\n\nset_seed(12)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T19:26:43.318835Z","iopub.execute_input":"2023-01-15T19:26:43.319557Z","iopub.status.idle":"2023-01-15T19:26:43.384359Z","shell.execute_reply.started":"2023-01-15T19:26:43.319518Z","shell.execute_reply":"2023-01-15T19:26:43.383458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df[\"MGMT_value\"] == 0]","metadata":{"execution":{"iopub.status.busy":"2023-01-15T19:33:32.755577Z","iopub.execute_input":"2023-01-15T19:33:32.755968Z","iopub.status.idle":"2023-01-15T19:33:32.768544Z","shell.execute_reply.started":"2023-01-15T19:33:32.755913Z","shell.execute_reply":"2023-01-15T19:33:32.767596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(f\"{data_directory}/train_labels.csv\")\ndisplay(train_df)\n\ndf_train, df_valid = sk_model_selection.train_test_split(\n    train_df, \n    test_size=0.2, \n    random_state=12, \n    stratify=train_df[\"MGMT_value\"],\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-15T19:26:44.628898Z","iopub.execute_input":"2023-01-15T19:26:44.629584Z","iopub.status.idle":"2023-01-15T19:26:44.66507Z","shell.execute_reply.started":"2023-01-15T19:26:44.629548Z","shell.execute_reply":"2023-01-15T19:26:44.664103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.net = EfficientNet3D.from_name(\"efficientnet-b0\", override_params={'num_classes': 2}, in_channels=1)\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\n","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:57:36.737966Z","iopub.execute_input":"2023-01-14T16:57:36.738321Z","iopub.status.idle":"2023-01-14T16:57:36.745682Z","shell.execute_reply.started":"2023-01-14T16:57:36.738292Z","shell.execute_reply":"2023-01-14T16:57:36.744444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(torch_data.Dataset):\n    def __init__(self, paths, targets=None, mri_type=None, label_smoothing=0.01, split=\"train\", augment=False):\n        self.paths = paths\n        self.targets = targets\n        self.mri_type = mri_type\n        self.label_smoothing = label_smoothing\n        self.split = split\n        self.augment = augment\n          \n    def __len__(self):\n        return len(self.paths)\n    \n    def __getitem__(self, index):\n        scan_id = self.paths[index]\n        if self.targets is None:\n            data = load_dicom_images_3d(str(scan_id).zfill(5), mri_type=self.mri_type[index], split=self.split)\n        else:\n            if self.augment:\n                rotation = np.random.randint(0,4)\n            else:\n                rotation = 0\n\n            data = load_dicom_images_3d(str(scan_id).zfill(5), mri_type=self.mri_type[index], split=\"train\", rotate=rotation)\n#         print(\"SS\",data.shape)\n        if self.targets is None:\n            return {\"X\": torch.tensor(data).float(), \"id\": scan_id}\n        else:\n            y = torch.tensor(abs(self.targets[index]-self.label_smoothing), dtype=torch.float)\n            return {\"X\": torch.tensor(data).float(), \"y\": y}\n","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:57:37.622982Z","iopub.execute_input":"2023-01-14T16:57:37.623389Z","iopub.status.idle":"2023-01-14T16:57:37.633208Z","shell.execute_reply.started":"2023-01-14T16:57:37.623355Z","shell.execute_reply":"2023-01-14T16:57:37.632037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(modelfile, df, mri_type, split):\n    print(\"Predict:\", modelfile, mri_type, df.shape)\n    df.loc[:,\"MRI_Type\"] = mri_type\n    data_retriever = Dataset(\n        df[\"BraTS21ID\"].values, \n        mri_type=df[\"MRI_Type\"].values,\n        split=split\n    )\n    print(data_retriever)\n    data_loader = torch_data.DataLoader(\n        data_retriever,\n        batch_size=4,\n        shuffle=False,\n        num_workers=8,\n    )\n   \n    model = Model()\n    model.to(device)\n    \n    checkpoint = torch.load(modelfile)\n    model.load_state_dict(checkpoint[\"model_state_dict\"])\n    model.eval()\n    \n    y_pred = []\n    ids = []\n\n    for e, batch in enumerate(data_loader,1):\n        print(f\"{e}/{len(data_loader)}\", end=\"\\r\")\n#         print(\"shape:\",batch[\"X\"].shape)\n        with torch.no_grad():\n            tmp_pred = torch.sigmoid(model(batch[\"X\"].to(device))).cpu().numpy().squeeze()\n            if tmp_pred.size == 1:\n                y_pred.append(tmp_pred)\n            else:\n                y_pred.extend(tmp_pred.tolist())\n            ids.extend(batch[\"id\"].numpy().tolist())\n            \n    preddf = pd.DataFrame({\"BraTS21ID\": ids, \"MGMT_value\": y_pred}) \n    preddf = preddf.set_index(\"BraTS21ID\")\n    return preddf","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:57:43.90351Z","iopub.execute_input":"2023-01-14T16:57:43.903966Z","iopub.status.idle":"2023-01-14T16:57:43.921971Z","shell.execute_reply.started":"2023-01-14T16:57:43.903927Z","shell.execute_reply":"2023-01-14T16:57:43.921028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\npred = predict(\"/kaggle/input/mri-models-specific/FLAIR-e10-loss0.680-auc0.624.pth\", df_valid, \"FLAIR\", \"train\")\ny_pred= pred[\"MGMT_value\"].tolist()\n# y_pred = [int(np.round(i)) for i in y_pred]\ndf_valid[\"MGMT_pred\"] =y_pred\ndisplay(df_valid)\nauc = roc_auc_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\naccuracy = accuracy_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\nprint(\"Accuracy\",accuracy)\nsns.displot(df_valid[\"MGMT_pred\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:57:52.418037Z","iopub.execute_input":"2023-01-14T16:57:52.418466Z","iopub.status.idle":"2023-01-14T16:58:34.632674Z","shell.execute_reply.started":"2023-01-14T16:57:52.418428Z","shell.execute_reply":"2023-01-14T16:58:34.631151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\npred = predict(\"/kaggle/input/mri-models-specific/T1w-e6-loss0.691-auc0.581.pth\", df_valid, \"T1w\", \"train\")\ny_pred= pred[\"MGMT_value\"].tolist()\ny_pred = [int(np.round(i)) for i in y_pred]\ndf_valid[\"MGMT_pred\"] =y_pred\ndisplay(df_valid)\nauc = roc_auc_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\naccuracy = accuracy_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\nprint(\"Accuracy\",accuracy)\nsns.displot(df_valid[\"MGMT_pred\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-02T10:35:16.416345Z","iopub.execute_input":"2023-01-02T10:35:16.41663Z","iopub.status.idle":"2023-01-02T10:35:49.5314Z","shell.execute_reply.started":"2023-01-02T10:35:16.416601Z","shell.execute_reply":"2023-01-02T10:35:49.530348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\npred = predict(\"/kaggle/input/mri-models-specific/T1wCE-e4-loss0.688-auc0.561.pth\", df_valid, \"T1wCE\", \"train\")\ny_pred= pred[\"MGMT_value\"].tolist()\ny_pred = [int(np.round(i)) for i in y_pred]\ndf_valid[\"MGMT_pred\"] =y_pred\ndisplay(df_valid)\nauc = roc_auc_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\naccuracy = accuracy_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\nprint(\"Accuracy\",accuracy)\nsns.displot(df_valid[\"MGMT_pred\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-02T10:35:49.532949Z","iopub.execute_input":"2023-01-02T10:35:49.534063Z","iopub.status.idle":"2023-01-02T10:36:25.496888Z","shell.execute_reply.started":"2023-01-02T10:35:49.53402Z","shell.execute_reply":"2023-01-02T10:36:25.4958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\npred = predict(\"/kaggle/input/mri-models-specific/T2w-e10-loss0.690-auc0.577.pth\", df_valid, \"T2w\", \"train\")\ny_pred= pred[\"MGMT_value\"].tolist()\ny_pred = [int(np.round(i)) for i in y_pred]\ndf_valid[\"MGMT_pred\"] =y_pred\ndisplay(df_valid)\nauc = roc_auc_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\naccuracy = accuracy_score(df_valid[\"MGMT_value\"], df_valid[\"MGMT_pred\"])\nprint(\"Accuracy\",accuracy)\nsns.displot(df_valid[\"MGMT_pred\"])","metadata":{"execution":{"iopub.status.busy":"2023-01-02T10:36:25.498498Z","iopub.execute_input":"2023-01-02T10:36:25.499643Z","iopub.status.idle":"2023-01-02T10:37:04.189482Z","shell.execute_reply.started":"2023-01-02T10:36:25.499594Z","shell.execute_reply":"2023-01-02T10:37:04.188459Z"},"trusted":true},"execution_count":null,"outputs":[]}]}