{"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":"# Predictions: Brain Tumor Classif. with PyTorch⚡Lightning\n\nThe goal of this challenge is to Predict the status of a genetic biomarker important for brain cancer treatment.\n\nAll the code is refered from public repository: https://github.com/Borda/kaggle_brain-tumor-3D\nAny nice contribution is welcome!","metadata":{}},{"cell_type":"code","source":"! cp -r /kaggle/input/brain-tumor-classif-submissions/package_freeze package_freeze\n\nimport glob, os, shutil\n# finad all packages\npkgs = glob.glob(\"package_freeze/*.xyz\")\n# rename them back to correct name format\n[shutil.move(p, p.replace(\".xyz\", \".tar.gz\")) for p in pkgs]\n\n! pip install -q \"kaggle-brain3D\" --no-index --find-link /kaggle/working/package_freeze\n\n# ! pip install -q https://github.com/Borda/kaggle_brain-tumor-3D/archive/refs/heads/main.zip\n\n! pip uninstall -q -y wandb\n! pip list | grep torch","metadata":{"execution":{"iopub.status.busy":"2021-10-15T23:41:04.813217Z","iopub.execute_input":"2021-10-15T23:41:04.814347Z","iopub.status.idle":"2021-10-15T23:41:47.382599Z","shell.execute_reply.started":"2021-10-15T23:41:04.814271Z","shell.execute_reply":"2021-10-15T23:41:47.381465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! ls -l /kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification\n! nvidia-smi\n# ! mkdir /kaggle/temp\n\n%matplotlib inline\n%reload_ext autoreload\n%autoreload 2\n\nimport kaggle_brain3d\nprint(kaggle_brain3d.__version__)","metadata":{"tags":[],"execution":{"iopub.status.busy":"2021-10-15T23:41:47.385360Z","iopub.execute_input":"2021-10-15T23:41:47.385629Z","iopub.status.idle":"2021-10-15T23:41:49.002754Z","shell.execute_reply.started":"2021-10-15T23:41:47.385598Z","shell.execute_reply":"2021-10-15T23:41:49.001568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare dataset","metadata":{}},{"cell_type":"code","source":"import os\nimport glob\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nPATH_DATASET = \"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification\"\nPATH_MODELS = \"/kaggle/input/brain-tumor-classif-submissions\"\n# PATH_TEMP = \"/kaggle/temp\"\nSCAN_TYPES = (\"FLAIR\", \"T1w\", \"T1CE\", \"T2w\")\n\ndf_sample = pd.read_csv(os.path.join(PATH_DATASET, \"sample_submission.csv\"))\ndisplay(df_sample.head())","metadata":{"tags":[],"execution":{"iopub.status.busy":"2021-10-15T23:41:49.005118Z","iopub.execute_input":"2021-10-15T23:41:49.005505Z","iopub.status.idle":"2021-10-15T23:41:49.059528Z","shell.execute_reply.started":"2021-10-15T23:41:49.005453Z","shell.execute_reply":"2021-10-15T23:41:49.058562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lightning DataModule\n\nIt is constric to wrap all data-related peaces and define Pytoch dataloder for Training / Validation / Testing phase.\n\nAt the end we show a few sample images from the fost training batch.","metadata":{}},{"cell_type":"code","source":"from functools import partial\nimport rising.transforms as rtr\nfrom rising.loading import DataLoader, default_transform_call\nfrom rising.random import DiscreteParameter, UniformParameter\n\nfrom kaggle_brain3d.utils import show_volume\nfrom kaggle_brain3d.data import BrainScansDM, TRAIN_TRANSFORMS, VAL_TRANSFORMS\nfrom kaggle_brain3d.transforms import RandomAffine, rising_zero_mean\n\n# ==============================\n\ndm = BrainScansDM(\n    data_dir=PATH_DATASET,\n    scan_types=[\"T2w\"],\n    vol_size=224,\n    crop_thr=None,\n    # crop_thr=1e-6,  # experimental crop threshold\n    # batch_size=4,  # for full model training\n    batch_size=2,  # for finetune head\n    cache_dir=None,\n    # in_memory=True,\n    num_workers=2,\n    train_transforms=rtr.Compose(TRAIN_TRANSFORMS, transform_call=default_transform_call),\n    valid_transforms=rtr.Compose(VAL_TRANSFORMS, transform_call=default_transform_call),\n)\ndm.setup()\n# dm.prepare_data(num_proc=3, dataset=dm.test_dataset)\nprint(f\"Training batches: {len(dm.train_dataloader())}\")\nprint(f\"Validation batches: {len(dm.val_dataloader())}\")\nprint(f\"Test batches: {len(dm.test_dataloader())}\")\n\n# Quick view\nfor batch in dm.test_dataloader():\n    for i in range(2):\n        show_volume(batch[\"data\"][i][0], fig_size=(6, 4), v_min_max=(-1., 3.))\n    break","metadata":{"tags":[],"execution":{"iopub.status.busy":"2021-10-15T23:41:49.060899Z","iopub.execute_input":"2021-10-15T23:41:49.061150Z","iopub.status.idle":"2021-10-15T23:42:26.120929Z","shell.execute_reply.started":"2021-10-15T23:41:49.061122Z","shell.execute_reply":"2021-10-15T23:42:26.118967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare 3D model\n\nLightningModule is the core of PL, it wrappes all model related peaces, mainly:\n\n- the model/architecture/weights\n- evaluation metrics\n- configs for optimizer and LR scheduler","metadata":{}},{"cell_type":"code","source":"import torch\nfrom torchsummary import summary\nfrom kaggle_brain3d.models import LitBrainMRI, create_pretrained_medical_resnet\nfrom monai.networks.nets import resnet10, resnet18, resnet34, resnet50, SEResNet50\n\n# ==============================\n\n\nPATH_CKPT = os.path.join(PATH_MODELS, \"v10.ckpt\")\nnet = resnet34(pretrained=False, spatial_dims=3, n_input_channels=1, num_classes=2)\n\nmodel = LitBrainMRI(net=net)\n# summary(model, input_size=(1, 128, 128, 128))\nckpt = torch.load(PATH_CKPT, map_location=torch.device('cpu'))\nmodel.load_state_dict(ckpt['state_dict'])","metadata":{"tags":[],"execution":{"iopub.status.busy":"2021-10-15T23:42:26.127880Z","iopub.execute_input":"2021-10-15T23:42:26.128588Z","iopub.status.idle":"2021-10-15T23:42:36.888819Z","shell.execute_reply.started":"2021-10-15T23:42:26.128504Z","shell.execute_reply":"2021-10-15T23:42:36.887846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predictions...","metadata":{}},{"cell_type":"code","source":"model.eval()\nmodel.cpu()\nsubmission = []\n\nfor batch in dm.test_dataloader():\n    print(batch.keys())\n    print(batch.get(\"label\"))\n    imgs = batch.get(\"data\")\n    print(imgs.shape)\n    with torch.no_grad():\n        preds = model(imgs)\n    print(preds)\n    probs = torch.nn.functional.softmax(preds)\n    print(probs)\n    break","metadata":{"execution":{"iopub.status.busy":"2021-10-15T23:42:36.890537Z","iopub.execute_input":"2021-10-15T23:42:36.890863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_brain3d.models import make_submission\n\ndf_submission = make_submission(model, dm.test_dataloader(), \"cuda\" if torch.cuda.is_available() else \"cpu\")\ndisplay(df_submission)\n# df_submission.set_index(\"BraTS21ID\", inplace=True)\ndf_submission[[\"MGMT_value\"]].to_csv(\"submission.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission[[\"MGMT_value\"]].hist(bins=25)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! cat submission.csv","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}