{"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":"from fastai.vision.all import *\nimport numpy as np\nimport sys\nimport os\nnp.set_printoptions(threshold=sys.maxsize)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:51.289793Z","iopub.execute_input":"2021-08-30T15:10:51.290174Z","iopub.status.idle":"2021-08-30T15:10:53.632321Z","shell.execute_reply.started":"2021-08-30T15:10:51.290092Z","shell.execute_reply":"2021-08-30T15:10:53.631404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fastai\nprint(fastai.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.633656Z","iopub.execute_input":"2021-08-30T15:10:53.633987Z","iopub.status.idle":"2021-08-30T15:10:53.642043Z","shell.execute_reply.started":"2021-08-30T15:10:53.633945Z","shell.execute_reply":"2021-08-30T15:10:53.641131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append(\"../input/effnet-pytorch/EfficientNet-PyTorch-master\")","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.643997Z","iopub.execute_input":"2021-08-30T15:10:53.644339Z","iopub.status.idle":"2021-08-30T15:10:53.649643Z","shell.execute_reply.started":"2021-08-30T15:10:53.644302Z","shell.execute_reply":"2021-08-30T15:10:53.648581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cp -rf ../input/effnet-pytorch/EfficientNet-PyTorch-master /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.651719Z","iopub.execute_input":"2021-08-30T15:10:53.652321Z","iopub.status.idle":"2021-08-30T15:10:53.658185Z","shell.execute_reply.started":"2021-08-30T15:10:53.652283Z","shell.execute_reply":"2021-08-30T15:10:53.657359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#os.chdir(\"/kaggle/working/EfficientNet-PyTorch-master\")","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.659467Z","iopub.execute_input":"2021-08-30T15:10:53.659966Z","iopub.status.idle":"2021-08-30T15:10:53.666347Z","shell.execute_reply.started":"2021-08-30T15:10:53.659924Z","shell.execute_reply":"2021-08-30T15:10:53.665566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \n#!pip install -e .","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.667631Z","iopub.execute_input":"2021-08-30T15:10:53.668246Z","iopub.status.idle":"2021-08-30T15:10:53.674481Z","shell.execute_reply.started":"2021-08-30T15:10:53.668210Z","shell.execute_reply":"2021-08-30T15:10:53.673608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.675841Z","iopub.execute_input":"2021-08-30T15:10:53.676259Z","iopub.status.idle":"2021-08-30T15:10:53.682883Z","shell.execute_reply.started":"2021-08-30T15:10:53.676223Z","shell.execute_reply":"2021-08-30T15:10:53.682056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nm = resnet34()\nm = nn.Sequential(*list(m.children())[:-2])\ntst = DynamicUnet(m, 2, (128,128), norm_type=None)\nx = cast(torch.randn(2, 3, 128, 128), TensorImage)\ny = tst(x)\n'''","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.686154Z","iopub.execute_input":"2021-08-30T15:10:53.686413Z","iopub.status.idle":"2021-08-30T15:10:53.695743Z","shell.execute_reply.started":"2021-08-30T15:10:53.686371Z","shell.execute_reply":"2021-08-30T15:10:53.694756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print( y.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.697819Z","iopub.execute_input":"2021-08-30T15:10:53.698557Z","iopub.status.idle":"2021-08-30T15:10:53.702675Z","shell.execute_reply.started":"2021-08-30T15:10:53.698519Z","shell.execute_reply":"2021-08-30T15:10:53.701825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport random\ndf = pd.read_csv('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv', header=0, names=['id','value'], dtype=object)\ndf = df[~df.id.isin([\"00109\", \"00123\", \"00709\"])]","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.703941Z","iopub.execute_input":"2021-08-30T15:10:53.704518Z","iopub.status.idle":"2021-08-30T15:10:53.726573Z","shell.execute_reply.started":"2021-08-30T15:10:53.704482Z","shell.execute_reply":"2021-08-30T15:10:53.725871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.727730Z","iopub.execute_input":"2021-08-30T15:10:53.728076Z","iopub.status.idle":"2021-08-30T15:10:53.733439Z","shell.execute_reply.started":"2021-08-30T15:10:53.728044Z","shell.execute_reply":"2021-08-30T15:10:53.732619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.734839Z","iopub.execute_input":"2021-08-30T15:10:53.735520Z","iopub.status.idle":"2021-08-30T15:10:53.751578Z","shell.execute_reply.started":"2021-08-30T15:10:53.735407Z","shell.execute_reply":"2021-08-30T15:10:53.750723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://stackoverflow.com/a/4836734/8245487\ndef natural_sort(l): \n    convert = lambda text: int(text) if text.isdigit() else text.lower()\n    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]\n    return sorted(l, key=alphanum_key)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.752848Z","iopub.execute_input":"2021-08-30T15:10:53.753209Z","iopub.status.idle":"2021-08-30T15:10:53.758597Z","shell.execute_reply.started":"2021-08-30T15:10:53.753175Z","shell.execute_reply":"2021-08-30T15:10:53.757474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nnp.seterr(divide='ignore',invalid='ignore')","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.760112Z","iopub.execute_input":"2021-08-30T15:10:53.760501Z","iopub.status.idle":"2021-08-30T15:10:53.770128Z","shell.execute_reply.started":"2021-08-30T15:10:53.760466Z","shell.execute_reply":"2021-08-30T15:10:53.769224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pydicom\nimport pandas as pd\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom tqdm import tqdm\nimport binascii\nfrom PIL import Image\n\nINPUT = '/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification'\n\nif not os.path.exists('./train'):\n    os.makedirs('./train')\n    \n\nif not os.path.exists('./test'):\n    os.makedirs('./test')\n\n\ndef get_dicom_files(input_dir, dataset='train'):\n    for subdir, dirs, files in os.walk(f\"{input_dir}/{dataset}\"):\n        if len(files) == 0:\n            continue\n        filename = natural_sort(files)[len(files)//2] #take middle most image -- FLAIR DCM file per training item.\n        filepath = os.path.join(subdir, filename)\n        \n        if filepath.endswith(\".dcm\") and \"FLAIR\" in filepath:\n            cur_id = subdir.split('/')[-2]\n            outpath = os.path.join(f'./{dataset}',f'{cur_id}.png')\n            \n            process_dicom(filepath, outpath)\n\ndef process_dicom(path, outpath):\n    dicom = pydicom.read_file(path)\n    data = apply_voi_lut(dicom.pixel_array, dicom)\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    \n    height = len(data)\n    width = len(data[0])\n    \n    pixels_out = []\n    for row in data:\n        pixels_out.extend(row)\n    assert(len(pixels_out) == height * width)\n    \n    image_out = Image.new('L', (width, height))\n    image_out.putdata(pixels_out)\n    image_out.save(outpath)\n\nget_dicom_files(INPUT, 'train')\nget_dicom_files(INPUT, 'test')\n\n\n#     final = pd.DataFrame(final)\n#     final.to_csv(f\"{args['output']}/dicom_meta_{args['dataset']}.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:10:53.771581Z","iopub.execute_input":"2021-08-30T15:10:53.772024Z","iopub.status.idle":"2021-08-30T15:12:35.853492Z","shell.execute_reply.started":"2021-08-30T15:10:53.771982Z","shell.execute_reply":"2021-08-30T15:12:35.852638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id_num in df.id:\n    full_path = './train/{}.png'.format(id_num)\n    df.loc[df.id == id_num, 'file'] = full_path\n    ","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:35.856759Z","iopub.execute_input":"2021-08-30T15:12:35.857024Z","iopub.status.idle":"2021-08-30T15:12:36.256364Z","shell.execute_reply.started":"2021-08-30T15:12:35.856997Z","shell.execute_reply":"2021-08-30T15:12:36.255507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:36.263163Z","iopub.execute_input":"2021-08-30T15:12:36.263515Z","iopub.status.idle":"2021-08-30T15:12:36.287105Z","shell.execute_reply.started":"2021-08-30T15:12:36.263479Z","shell.execute_reply":"2021-08-30T15:12:36.286199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dls = ImageDataLoaders.from_df(df, item_tfms=Resize(224), bs=64, label_col =1, fn_col=2, path='')","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:36.288480Z","iopub.execute_input":"2021-08-30T15:12:36.288799Z","iopub.status.idle":"2021-08-30T15:12:36.298917Z","shell.execute_reply.started":"2021-08-30T15:12:36.288769Z","shell.execute_reply":"2021-08-30T15:12:36.298234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dls = ImageDataLoaders.from_df(df, item_tfms=Resize(224), bs=16, label_col =1, fn_col=2, path='')","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:36.300960Z","iopub.execute_input":"2021-08-30T15:12:36.301612Z","iopub.status.idle":"2021-08-30T15:12:36.309719Z","shell.execute_reply.started":"2021-08-30T15:12:36.301549Z","shell.execute_reply":"2021-08-30T15:12:36.308960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(df, item_tfms=Resize(224), bs=8, label_col =1, fn_col=2, path='')","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:36.311078Z","iopub.execute_input":"2021-08-30T15:12:36.311457Z","iopub.status.idle":"2021-08-30T15:12:40.703722Z","shell.execute_reply.started":"2021-08-30T15:12:36.311422Z","shell.execute_reply":"2021-08-30T15:12:40.702911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:40.704969Z","iopub.execute_input":"2021-08-30T15:12:40.705293Z","iopub.status.idle":"2021-08-30T15:12:41.182762Z","shell.execute_reply.started":"2021-08-30T15:12:40.705258Z","shell.execute_reply":"2021-08-30T15:12:41.181956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nimport torch.nn as nn\nimport torch.nn.functional as F\n\n\nclass Net(nn.Module):\n    def __init__(self, pretrained=False):\n        super().__init__()\n        self.conv1 = nn.Conv2d(3, 6, 5)\n        self.pool = nn.MaxPool2d(2, 2)\n        self.conv2 = nn.Conv2d(6, 16, 5)\n        self.fc1 = nn.Linear(16 * 5 * 5, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 10)\n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = torch.flatten(x, 1) # flatten all dimensions except batch\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.relu(self.fc3(x))\n        return x","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:41.183887Z","iopub.execute_input":"2021-08-30T15:12:41.184225Z","iopub.status.idle":"2021-08-30T15:12:41.193147Z","shell.execute_reply.started":"2021-08-30T15:12:41.184192Z","shell.execute_reply":"2021-08-30T15:12:41.191971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net2(nn.Module):\n    def __init__(self, pretrained=False):\n        super().__init__()\n        self.conv1 = nn.Conv2d(3, 6, 5)\n        self.pool = nn.MaxPool2d(2, 2)\n        self.conv2 = nn.Conv2d(6, 16, 5)\n        self.fc1 = nn.Linear(16 * 5 * 5, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 10)\n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        x = torch.flatten(x, 1) # flatten all dimensions except batch\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.relu(self.fc3(x))\n        return x","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:41.198456Z","iopub.execute_input":"2021-08-30T15:12:41.199091Z","iopub.status.idle":"2021-08-30T15:12:41.207317Z","shell.execute_reply.started":"2021-08-30T15:12:41.199055Z","shell.execute_reply":"2021-08-30T15:12:41.206574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import libraries\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nimport seaborn as sns\n\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense, Dropout, InputLayer\nfrom tensorflow.keras.layers import Input, Activation, BatchNormalization, LeakyReLU\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import losses, optimizers","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:41.209433Z","iopub.execute_input":"2021-08-30T15:12:41.209778Z","iopub.status.idle":"2021-08-30T15:12:45.058388Z","shell.execute_reply.started":"2021-08-30T15:12:41.209743Z","shell.execute_reply":"2021-08-30T15:12:45.057507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#learn = cnn_learner(dls, Net, metrics=[error_rate, accuracy], model_dir=\"/tmp/model/\").to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:45.060418Z","iopub.execute_input":"2021-08-30T15:12:45.060680Z","iopub.status.idle":"2021-08-30T15:12:45.068242Z","shell.execute_reply.started":"2021-08-30T15:12:45.060655Z","shell.execute_reply":"2021-08-30T15:12:45.067169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append(\"../input/pretrained-model-weights-pytorch\")","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:45.071643Z","iopub.execute_input":"2021-08-30T15:12:45.071922Z","iopub.status.idle":"2021-08-30T15:12:45.078642Z","shell.execute_reply.started":"2021-08-30T15:12:45.071872Z","shell.execute_reply":"2021-08-30T15:12:45.077883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!mkdir -p /tmp/model","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:45.079925Z","iopub.execute_input":"2021-08-30T15:12:45.080331Z","iopub.status.idle":"2021-08-30T15:12:45.086501Z","shell.execute_reply.started":"2021-08-30T15:12:45.080295Z","shell.execute_reply":"2021-08-30T15:12:45.085696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p /root/.cache/torch/hub/checkpoints/","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:45.087726Z","iopub.execute_input":"2021-08-30T15:12:45.088149Z","iopub.status.idle":"2021-08-30T15:12:45.752272Z","shell.execute_reply.started":"2021-08-30T15:12:45.088114Z","shell.execute_reply":"2021-08-30T15:12:45.751198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!mkdir -p /tmp/model","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:45.753703Z","iopub.execute_input":"2021-08-30T15:12:45.754069Z","iopub.status.idle":"2021-08-30T15:12:45.760603Z","shell.execute_reply.started":"2021-08-30T15:12:45.754028Z","shell.execute_reply":"2021-08-30T15:12:45.759500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle/input/pytorch-pretrained-models/vgg16_bn-6c64b313.pth  /root/.cache/torch/hub/checkpoints/vgg16_bn-6c64b313.pth","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:45.762277Z","iopub.execute_input":"2021-08-30T15:12:45.762677Z","iopub.status.idle":"2021-08-30T15:12:53.303461Z","shell.execute_reply.started":"2021-08-30T15:12:45.762641Z","shell.execute_reply":"2021-08-30T15:12:53.302462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle/input/resnet50-pytorch-pretrained/resnet50-19c8e357.pth  /root/.cache/torch/hub/checkpoints/resnet50-19c8e357.pth","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:53.305050Z","iopub.execute_input":"2021-08-30T15:12:53.305385Z","iopub.status.idle":"2021-08-30T15:12:55.782796Z","shell.execute_reply.started":"2021-08-30T15:12:53.305347Z","shell.execute_reply":"2021-08-30T15:12:55.781726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle/input/pytorch-pretrained-models/resnet101-5d3b4d8f.pth  /root/.cache/torch/hub/checkpoints/resnet101-5d3b4d8f.pth","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:55.786540Z","iopub.execute_input":"2021-08-30T15:12:55.786819Z","iopub.status.idle":"2021-08-30T15:12:58.066599Z","shell.execute_reply.started":"2021-08-30T15:12:55.786789Z","shell.execute_reply":"2021-08-30T15:12:58.065578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle//input/pytorch-pretrained-models/vgg19_bn-c79401a0.pth  /root/.cache/torch/hub/checkpoints/vgg19_bn-c79401a0.pth","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:12:58.068168Z","iopub.execute_input":"2021-08-30T15:12:58.068508Z","iopub.status.idle":"2021-08-30T15:13:03.509954Z","shell.execute_reply.started":"2021-08-30T15:12:58.068468Z","shell.execute_reply":"2021-08-30T15:13:03.508791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle/input/densenet121/densenet121-a639ec97.pth  /root/.cache/torch/hub/checkpoints/densenet121-a639ec97.pth","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:03.511830Z","iopub.execute_input":"2021-08-30T15:13:03.512253Z","iopub.status.idle":"2021-08-30T15:13:04.982510Z","shell.execute_reply.started":"2021-08-30T15:13:03.512210Z","shell.execute_reply":"2021-08-30T15:13:04.981392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle/input/pytorch-pretrained-models/resnet152-b121ed2d.pth  /root/.cache/torch/hub/checkpoints/resnet152-b121ed2d.pth","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:04.984118Z","iopub.execute_input":"2021-08-30T15:13:04.984459Z","iopub.status.idle":"2021-08-30T15:13:07.673663Z","shell.execute_reply.started":"2021-08-30T15:13:04.984419Z","shell.execute_reply":"2021-08-30T15:13:07.672520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learn = cnn_learner(dls, vgg16_bn, metrics=[error_rate, accuracy]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:07.675387Z","iopub.execute_input":"2021-08-30T15:13:07.676017Z","iopub.status.idle":"2021-08-30T15:13:07.685154Z","shell.execute_reply.started":"2021-08-30T15:13:07.675973Z","shell.execute_reply":"2021-08-30T15:13:07.684145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learn = cnn_learner(dls, resnet101, metrics=[error_rate, accuracy]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:07.689558Z","iopub.execute_input":"2021-08-30T15:13:07.691145Z","iopub.status.idle":"2021-08-30T15:13:09.856309Z","shell.execute_reply.started":"2021-08-30T15:13:07.691115Z","shell.execute_reply":"2021-08-30T15:13:09.855233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learn =cnn_learner(dls,xresnet50 , metrics=[error_rate, accuracy] ).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:09.860244Z","iopub.execute_input":"2021-08-30T15:13:09.863345Z","iopub.status.idle":"2021-08-30T15:13:09.950930Z","shell.execute_reply.started":"2021-08-30T15:13:09.863300Z","shell.execute_reply":"2021-08-30T15:13:09.944318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learn = cnn_learner(dls, densenet121, metrics=[error_rate, accuracy]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:09.952755Z","iopub.execute_input":"2021-08-30T15:13:09.955120Z","iopub.status.idle":"2021-08-30T15:13:10.069602Z","shell.execute_reply.started":"2021-08-30T15:13:09.955075Z","shell.execute_reply":"2021-08-30T15:13:10.068692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"''' \nfrom efficientnet_pytorch import EfficientNet\nmodel_name = 'efficientnet-b2'\ndef getModel():\n    #model = EfficientNet.from_pretrained(model_name)\n    model = EfficientNet.from_name(model_name)\n    #model._bn1 = nn.Identity()\n    #model._fc = nn.Linear(1408,data.c)\n    model._fc = nn.Linear(1408,2)\n    return model\nmodel=getModel()\n\n!cp '/kaggle/input/effnetb2-4d6/efficientnet-b2-8bb594d6.pth' '/root/.cache/torch/hub/checkpoints/efficientnet-b2-8bb594d6.pth'\n '''","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:10.085536Z","iopub.execute_input":"2021-08-30T15:13:10.086713Z","iopub.status.idle":"2021-08-30T15:13:10.196269Z","shell.execute_reply.started":"2021-08-30T15:13:10.086664Z","shell.execute_reply":"2021-08-30T15:13:10.188548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp '/kaggle/input/fastai-xresnet50/xrn50_940.pth' '/root/.cache/torch/hub/checkpoints/xrn50_940.pth'\n \n ","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:10.197744Z","iopub.execute_input":"2021-08-30T15:13:10.198348Z","iopub.status.idle":"2021-08-30T15:13:15.737380Z","shell.execute_reply.started":"2021-08-30T15:13:10.198306Z","shell.execute_reply":"2021-08-30T15:13:15.736340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn =cnn_learner(dls,xresnet152 , metrics=[error_rate, accuracy] ).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:15.738845Z","iopub.execute_input":"2021-08-30T15:13:15.739211Z","iopub.status.idle":"2021-08-30T15:13:18.893523Z","shell.execute_reply.started":"2021-08-30T15:13:15.739168Z","shell.execute_reply":"2021-08-30T15:13:18.892627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:18.894872Z","iopub.execute_input":"2021-08-30T15:13:18.895283Z","iopub.status.idle":"2021-08-30T15:13:40.861354Z","shell.execute_reply.started":"2021-08-30T15:13:18.895243Z","shell.execute_reply":"2021-08-30T15:13:40.860519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#learn.fit_one_cycle(100, lr_max=1e-2, cbs=ShortEpochCallback())","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:40.862692Z","iopub.execute_input":"2021-08-30T15:13:40.863064Z","iopub.status.idle":"2021-08-30T15:13:40.867218Z","shell.execute_reply.started":"2021-08-30T15:13:40.863024Z","shell.execute_reply":"2021-08-30T15:13:40.866110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(20, lr_max=1e-2)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:13:40.868533Z","iopub.execute_input":"2021-08-30T15:13:40.868953Z","iopub.status.idle":"2021-08-30T15:18:10.657243Z","shell.execute_reply.started":"2021-08-30T15:13:40.868893Z","shell.execute_reply":"2021-08-30T15:18:10.656330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit(10)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:18:10.658739Z","iopub.execute_input":"2021-08-30T15:18:10.659111Z","iopub.status.idle":"2021-08-30T15:20:26.089994Z","shell.execute_reply.started":"2021-08-30T15:18:10.659068Z","shell.execute_reply":"2021-08-30T15:20:26.089092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:20:26.091546Z","iopub.execute_input":"2021-08-30T15:20:26.091901Z","iopub.status.idle":"2021-08-30T15:20:26.691787Z","shell.execute_reply.started":"2021-08-30T15:20:26.091858Z","shell.execute_reply":"2021-08-30T15:20:26.690777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.DataFrame(columns=['id', 'value'])\ndf_test.id = os.listdir(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test/\")","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:20:26.693287Z","iopub.execute_input":"2021-08-30T15:20:26.693634Z","iopub.status.idle":"2021-08-30T15:20:26.709556Z","shell.execute_reply.started":"2021-08-30T15:20:26.693597Z","shell.execute_reply":"2021-08-30T15:20:26.708650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for id_num in df_test.id:\n    full_path = './test/{}.png'.format(id_num)\n    prediction = learn.predict(full_path)\n    print(prediction)\n    probability = prediction[2][1].item()\n    print(probability)\n    df_test.loc[df_test.id==id_num, 'value'] = probability","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:20:26.711525Z","iopub.execute_input":"2021-08-30T15:20:26.711821Z","iopub.status.idle":"2021-08-30T15:20:34.810863Z","shell.execute_reply.started":"2021-08-30T15:20:26.711795Z","shell.execute_reply":"2021-08-30T15:20:34.809858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:20:34.812272Z","iopub.execute_input":"2021-08-30T15:20:34.812622Z","iopub.status.idle":"2021-08-30T15:20:34.823500Z","shell.execute_reply.started":"2021-08-30T15:20:34.812583Z","shell.execute_reply":"2021-08-30T15:20:34.822548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.value.min()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:20:34.825019Z","iopub.execute_input":"2021-08-30T15:20:34.825691Z","iopub.status.idle":"2021-08-30T15:20:34.834833Z","shell.execute_reply.started":"2021-08-30T15:20:34.825606Z","shell.execute_reply":"2021-08-30T15:20:34.833856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.value.max()","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:20:34.836292Z","iopub.execute_input":"2021-08-30T15:20:34.836732Z","iopub.status.idle":"2021-08-30T15:20:34.845590Z","shell.execute_reply.started":"2021-08-30T15:20:34.836695Z","shell.execute_reply":"2021-08-30T15:20:34.844646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.rename(columns={'id':'BraTS21ID','value':'MGMT_value'}).to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-30T15:20:34.846880Z","iopub.execute_input":"2021-08-30T15:20:34.847232Z","iopub.status.idle":"2021-08-30T15:20:34.858405Z","shell.execute_reply.started":"2021-08-30T15:20:34.847196Z","shell.execute_reply":"2021-08-30T15:20:34.857499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}