{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n\nimport sys\nimport random\nimport warnings\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as ticker\nfrom matplotlib import rcParams\nfrom IPython.display import IFrame\nfrom IPython.core.display import display, HTML\nimport imageio\n\nfrom mpl_toolkits import mplot3d\nimport seaborn as sns\nfrom tqdm import tqdm\nfrom itertools import chain\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\n\nimport matplotlib\nmatplotlib.rcParams['animation.html'] = 'jshtml'\nfrom PIL import Image\nimport cv2\nimport glob\nimport re\nimport random\nfrom scipy import ndimage, misc\n\nimport time\nimport cv2\nimport pydicom\nfrom matplotlib.animation import FuncAnimation\n\n\nimport matplotlib.pyplot as plt\nimport matplotlib.gridspec as gridspec\nimport matplotlib.pylab as pylab\n#-----------------------------------------------------------\n\n#-----------------------------------------------------------\ndef show_n_images(imgs, titles = None, enlarge = 20, cmap='jet'):\n    \n    plt.set_cmap(cmap)\n    n = len(imgs)\n    gs1 = gridspec.GridSpec(1, n)   \n    \n    fig1 = plt.figure(); # create a figure with the default size \n    fig1.set_size_inches(enlarge, 2*enlarge);\n            \n    for i in range(n):\n        ax1 = fig1.add_subplot(gs1[i]) \n        ax1.imshow(imgs[i], interpolation='none');\n        if (titles is not None):\n            ax1.set_title(titles[i])\n        ax1.set_ylim(ax1.get_ylim()[::-1])\n    plt.show();\n#--------------------------------------------------------------\n\n","metadata":{"execution":{"iopub.status.busy":"2021-10-16T09:43:01.506305Z","iopub.execute_input":"2021-10-16T09:43:01.507423Z","iopub.status.idle":"2021-10-16T09:43:02.940849Z","shell.execute_reply.started":"2021-10-16T09:43:01.507211Z","shell.execute_reply":"2021-10-16T09:43:02.940122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.insert(1, '/kaggle/input/rsnabratsprocessed')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.337998Z","iopub.execute_input":"2021-10-16T02:28:26.338298Z","iopub.status.idle":"2021-10-16T02:28:26.343498Z","shell.execute_reply.started":"2021-10-16T02:28:26.338262Z","shell.execute_reply":"2021-10-16T02:28:26.34218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datadir = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\"","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.345076Z","iopub.execute_input":"2021-10-16T02:28:26.345439Z","iopub.status.idle":"2021-10-16T02:28:26.352247Z","shell.execute_reply.started":"2021-10-16T02:28:26.345403Z","shell.execute_reply":"2021-10-16T02:28:26.351547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from process_data_pydicom import get_pat_data","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.355024Z","iopub.execute_input":"2021-10-16T02:28:26.355302Z","iopub.status.idle":"2021-10-16T02:28:26.378569Z","shell.execute_reply.started":"2021-10-16T02:28:26.355265Z","shell.execute_reply":"2021-10-16T02:28:26.377935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\ndata_dir = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification')\n#'../input/rsna-miccai-brain-tumor-radiogenomic-classification/')\n\nmri_types =  [\"T1w\",  \"T1wCE\",\"T2w\",\"FLAIR\"]\nexcluded_images = [109, 123, 709] # Bad images","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.3797Z","iopub.execute_input":"2021-10-16T02:28:26.379997Z","iopub.status.idle":"2021-10-16T02:28:26.384616Z","shell.execute_reply.started":"2021-10-16T02:28:26.37994Z","shell.execute_reply":"2021-10-16T02:28:26.383945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(data_dir / \"train_labels.csv\",\n#                        index='id',\n#                       nrows=100000\n                      )\ntest_df = pd.read_csv(data_dir / \"sample_submission.csv\")\nsample_submission = pd.read_csv(data_dir / \"sample_submission.csv\")\n\ntrain_df = train_df[~train_df.BraTS21ID.isin(excluded_images)]\n\nprint(f\"train data: Rows={train_df.shape[0]}, Columns={train_df.shape[1]}\")\n# print(f\"test data : Rows={test_df.shape[0]}, Columns={test_df.shape[1]}\")","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.385986Z","iopub.execute_input":"2021-10-16T02:28:26.386581Z","iopub.status.idle":"2021-10-16T02:28:26.432718Z","shell.execute_reply.started":"2021-10-16T02:28:26.386542Z","shell.execute_reply":"2021-10-16T02:28:26.432062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.433729Z","iopub.execute_input":"2021-10-16T02:28:26.434182Z","iopub.status.idle":"2021-10-16T02:28:26.447673Z","shell.execute_reply.started":"2021-10-16T02:28:26.434145Z","shell.execute_reply":"2021-10-16T02:28:26.446919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datadir + str(0).zfill(5)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.448774Z","iopub.execute_input":"2021-10-16T02:28:26.449067Z","iopub.status.idle":"2021-10-16T02:28:26.454954Z","shell.execute_reply.started":"2021-10-16T02:28:26.449033Z","shell.execute_reply":"2021-10-16T02:28:26.454025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d1 = get_pat_data(datadir + str(0).zfill(5), vsize=128)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:26.456496Z","iopub.execute_input":"2021-10-16T02:28:26.456843Z","iopub.status.idle":"2021-10-16T02:28:45.795142Z","shell.execute_reply.started":"2021-10-16T02:28:26.456807Z","shell.execute_reply":"2021-10-16T02:28:45.794384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d2 = get_pat_data(datadir + str(688).zfill(5), vsize=128)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:28:45.799645Z","iopub.execute_input":"2021-10-16T02:28:45.799873Z","iopub.status.idle":"2021-10-16T02:29:02.046985Z","shell.execute_reply.started":"2021-10-16T02:28:45.799846Z","shell.execute_reply":"2021-10-16T02:29:02.046109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind=87\nshow_n_images([d2[0,ind,:,:],d2[1,ind,:,:], d2[2,ind,:,:], d2[3,ind,:,:]])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:02.048192Z","iopub.execute_input":"2021-10-16T02:29:02.048979Z","iopub.status.idle":"2021-10-16T02:29:02.601058Z","shell.execute_reply.started":"2021-10-16T02:29:02.048937Z","shell.execute_reply":"2021-10-16T02:29:02.600192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mclahe as mc\n","metadata":{"execution":{"iopub.status.busy":"2021-10-16T08:43:36.535447Z","iopub.execute_input":"2021-10-16T08:43:36.536067Z","iopub.status.idle":"2021-10-16T08:43:36.613411Z","shell.execute_reply.started":"2021-10-16T08:43:36.53597Z","shell.execute_reply":"2021-10-16T08:43:36.612306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from: https://www.kaggle.com/sreevishnudamodaran/rsna-3d-clahe-voxels-tpu-3d-augmentations/notebook\ndef resize_voxel(voxel, sz=(64, 256, 256)):\n    output = np.zeros((sz[0], sz[1], sz[2]), dtype=np.uint8)\n    if np.argmax(voxel.shape) == 0:\n        for i, s in enumerate(np.linspace(0, voxel.shape[0] - 1, num=sz[0])):\n            sampled = voxel[int(s), :, :]\n            output[i, :, :] = cv2.resize(sampled, (sz[2], sz[1]), cv2.INTER_CUBIC)\n    elif np.argmax(voxel.shape) == 1:\n        for i, s in enumerate(np.linspace(0, voxel.shape[1] - 1, num=sz[1])):\n            sampled = voxel[:, int(s), :]\n            output[:, i, :] = cv2.resize(sampled, (sz[2], sz[0]), cv2.INTER_CUBIC)\n    elif np.argmax(voxel.shape) == 2:\n        for i, s in enumerate(np.linspace(0, voxel.shape[2] - 1, num=sz[2])):\n            sampled = voxel[:, :, int(s)]\n            output[:, :, i] = cv2.resize(sampled, (sz[1], sz[0]), cv2.INTER_CUBIC)\n    return output","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"224\nfor i, row in df.iterrows():\n    pid = int(row['BraTS21ID'])\n    d1 = get_pat_data(datadir + str(688).zfill(5), vsize=224)\n    #*** save d in real notebook\n    new_data_crop = torch.from_numpy(d1[None, :, :, :, :]).float()\n\n    #output = trainer.predict([[new_data_crop], ])\n    \n    ot = np.array(torch.Tensor.cpu(output[0]))\n    ot = ot[0]\n\n    im = ot.copy()\n    #im[im>=0.5]=1\n    #im[im<0.5]=0\n    imout = im[0]+im[1]+im[2]\n    imout[imout>=0.5]=1\n    imout[imout<0.5]=0\n    \n    tum_pix = np.where(imout>0)\n    \n    fname = os.path.join('train_tumor', 'tumor_'+str(pid).zfill(5))+'.npy'\n    np.save(fname, tum_pix)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_proc_train_img(pid):\n    im = np.load('../input/rsnabratsprocessed/rsna_processes/train/' + 'proc_'+str(pid).zfill(5)+'.npz' )\n    im = im['arr_0']\n    return im","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:02.602337Z","iopub.execute_input":"2021-10-16T02:29:02.602601Z","iopub.status.idle":"2021-10-16T02:29:02.608959Z","shell.execute_reply.started":"2021-10-16T02:29:02.602563Z","shell.execute_reply":"2021-10-16T02:29:02.607932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:02.610813Z","iopub.execute_input":"2021-10-16T02:29:02.611566Z","iopub.status.idle":"2021-10-16T02:29:02.624521Z","shell.execute_reply.started":"2021-10-16T02:29:02.611528Z","shell.execute_reply":"2021-10-16T02:29:02.623716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_proc_test_img(pid):\n    im = np.load('../input/rsnabratsprocessed/rsna_processes/test/' + 'proc_'+str(pid).zfill(5)+'.npz' )\n    im = im['arr_0']\n    return im","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:02.626088Z","iopub.execute_input":"2021-10-16T02:29:02.626346Z","iopub.status.idle":"2021-10-16T02:29:02.63285Z","shell.execute_reply.started":"2021-10-16T02:29:02.62631Z","shell.execute_reply":"2021-10-16T02:29:02.630667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d3 = get_proc_train_img(688)\nd3.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:02.633875Z","iopub.execute_input":"2021-10-16T02:29:02.634069Z","iopub.status.idle":"2021-10-16T02:29:02.805415Z","shell.execute_reply.started":"2021-10-16T02:29:02.634047Z","shell.execute_reply":"2021-10-16T02:29:02.804536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind=87\nshow_n_images([d3[0,ind,:,:],d3[1,ind,:,:], d3[2,ind,:,:], d3[3,ind,:,:]])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:02.806967Z","iopub.execute_input":"2021-10-16T02:29:02.807251Z","iopub.status.idle":"2021-10-16T02:29:03.336774Z","shell.execute_reply.started":"2021-10-16T02:29:02.807215Z","shell.execute_reply":"2021-10-16T02:29:03.336051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d4 = get_proc_test_img(1)\nind=87\nim=d4\nshow_n_images([im[0,ind,:,:],im[1,ind,:,:], im[2,ind,:,:], im[3,ind,:,:]])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:03.33805Z","iopub.execute_input":"2021-10-16T02:29:03.338287Z","iopub.status.idle":"2021-10-16T02:29:03.985828Z","shell.execute_reply.started":"2021-10-16T02:29:03.338252Z","shell.execute_reply":"2021-10-16T02:29:03.98513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.insert(1, '/kaggle/input/brats2019unet/')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:03.987248Z","iopub.execute_input":"2021-10-16T02:29:03.98749Z","iopub.status.idle":"2021-10-16T02:29:03.991639Z","shell.execute_reply.started":"2021-10-16T02:29:03.987456Z","shell.execute_reply":"2021-10-16T02:29:03.990844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from unet_pretrained_predict_brats import get_unet","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:03.993088Z","iopub.execute_input":"2021-10-16T02:29:03.993349Z","iopub.status.idle":"2021-10-16T02:29:05.671846Z","shell.execute_reply.started":"2021-10-16T02:29:03.993311Z","shell.execute_reply":"2021-10-16T02:29:05.671135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mdl = get_unet('../input/brats2019unet/brats2019_trained_state_dict.pt')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:05.673254Z","iopub.execute_input":"2021-10-16T02:29:05.673502Z","iopub.status.idle":"2021-10-16T02:29:09.140292Z","shell.execute_reply.started":"2021-10-16T02:29:05.673469Z","shell.execute_reply":"2021-10-16T02:29:09.139564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nnew_data_crop = torch.from_numpy(d3[None, :, :, :, :]).float()\n\nmdl.cuda()\n\nwith torch.no_grad():\n\n    data = [new_data_crop]\n\n\n    data = [d.cuda() for d in data]\n\n    output = mdl(data)\n\not = np.array(torch.Tensor.cpu(output[0]))\not = ot[0]\n\nim = ot.copy()\nimout = im[0]+im[1]+im[2]\nimout[imout>=0.5]=1\nimout[imout<0.5]=0\nim = imout","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:09.14165Z","iopub.execute_input":"2021-10-16T02:29:09.141922Z","iopub.status.idle":"2021-10-16T02:29:14.363723Z","shell.execute_reply.started":"2021-10-16T02:29:09.141886Z","shell.execute_reply":"2021-10-16T02:29:14.358715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind = 97\nim=ot[0]\nshow_n_images([im[ind-1,:,:],im[ind,:,:], im[ind+1,:,:], im[ind+2,:,:]])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:14.364935Z","iopub.execute_input":"2021-10-16T02:29:14.365194Z","iopub.status.idle":"2021-10-16T02:29:14.870425Z","shell.execute_reply.started":"2021-10-16T02:29:14.365156Z","shell.execute_reply":"2021-10-16T02:29:14.869757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind = 97\nim=imout\nshow_n_images([im[ind-1,:,:],im[ind,:,:], im[ind+1,:,:], im[ind+2,:,:]])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:14.871619Z","iopub.execute_input":"2021-10-16T02:29:14.871966Z","iopub.status.idle":"2021-10-16T02:29:15.357149Z","shell.execute_reply.started":"2021-10-16T02:29:14.871925Z","shell.execute_reply":"2021-10-16T02:29:15.356436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train_df.copy()\ndf=df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:15.358393Z","iopub.execute_input":"2021-10-16T02:29:15.358631Z","iopub.status.idle":"2021-10-16T02:29:15.363375Z","shell.execute_reply.started":"2021-10-16T02:29:15.358593Z","shell.execute_reply":"2021-10-16T02:29:15.362434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\n\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:15.36471Z","iopub.execute_input":"2021-10-16T02:29:15.365467Z","iopub.status.idle":"2021-10-16T02:29:20.438802Z","shell.execute_reply.started":"2021-10-16T02:29:15.365431Z","shell.execute_reply":"2021-10-16T02:29:20.437985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train,df_valid = train_test_split(df, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:20.44349Z","iopub.execute_input":"2021-10-16T02:29:20.444043Z","iopub.status.idle":"2021-10-16T02:29:20.455861Z","shell.execute_reply.started":"2021-10-16T02:29:20.44398Z","shell.execute_reply":"2021-10-16T02:29:20.454994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:20.461019Z","iopub.execute_input":"2021-10-16T02:29:20.463154Z","iopub.status.idle":"2021-10-16T02:29:20.477642Z","shell.execute_reply.started":"2021-10-16T02:29:20.463116Z","shell.execute_reply":"2021-10-16T02:29:20.477044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_for_label(lab=1, df=df_train):\n    \n    img_id= random.choice(df[df.MGMT_value ==lab].BraTS21ID.values)\n    im = get_proc_train_img(img_id)\n    return im","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:20.484693Z","iopub.execute_input":"2021-10-16T02:29:20.486885Z","iopub.status.idle":"2021-10-16T02:29:20.493601Z","shell.execute_reply.started":"2021-10-16T02:29:20.486823Z","shell.execute_reply":"2021-10-16T02:29:20.492675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from random import randrange\ndef get_tum_plane_for_label(lab=1, df=df_train, min_tum = 3):\n    \n    img_id= random.choice(df[df.MGMT_value ==lab].BraTS21ID.values)\n    \n    im = get_proc_train_img(img_id)\n    tum = np.load('../input/rsnatumorpredicted/train_tumor/'+'tumor_'+str(img_id).zfill(5)+'.npy')\n        \n    while tum.shape[1]==0:\n        img_id= random.choice(df[df.MGMT_value ==lab].BraTS21ID.values)\n        im = get_proc_train_img(img_id)\n        tum = np.load('../input/rsnatumorpredicted/train_tumor/'+'tumor_'+str(img_id).zfill(5)+'.npy')\n        \n    dirp = random.choice([0,1,2])\n    \n    ind = randrange(tum.shape[1])\n    if (dirp==0):    \n        return im[:,tum[0][ind],:,:]/255.\n    if (dirp==1):    \n        return im[:,:,tum[1][ind],:]/255.\n    if (dirp==2):    \n        return im[:,:,:,tum[2][ind]]/255.\n","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:20.498813Z","iopub.execute_input":"2021-10-16T02:29:20.50113Z","iopub.status.idle":"2021-10-16T02:29:20.514253Z","shell.execute_reply.started":"2021-10-16T02:29:20.501065Z","shell.execute_reply":"2021-10-16T02:29:20.513402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from random import randrange\ndef get_plane_for_label(lab=1, df=df_train):\n    \n    img_id= random.choice(df[df.MGMT_value ==lab].BraTS21ID.values)\n    \n    im = get_proc_train_img(img_id)\n    ind = randrange(128)\n    \n    dirp = random.choice([0,1,2])\n    \n    ind = randrange(5,118)\n    if (dirp==0):    \n        return im[:,ind,:,:]/255.\n    if (dirp==1):    \n        return im[:,:,ind,:]/255.\n    if (dirp==2):    \n        return im[:,:,:,ind]/255.\n\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:20.519908Z","iopub.execute_input":"2021-10-16T02:29:20.52228Z","iopub.status.idle":"2021-10-16T02:29:20.532276Z","shell.execute_reply.started":"2021-10-16T02:29:20.522243Z","shell.execute_reply":"2021-10-16T02:29:20.531186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pln = get_tum_plane_for_label(lab=0)\npln.shape, pln.min(),pln.max()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:20.537671Z","iopub.execute_input":"2021-10-16T02:29:20.540436Z","iopub.status.idle":"2021-10-16T02:29:20.722745Z","shell.execute_reply.started":"2021-10-16T02:29:20.540398Z","shell.execute_reply":"2021-10-16T02:29:20.721846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_n_images([pln[0],pln[1],pln[2],pln[3]])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:20.726171Z","iopub.execute_input":"2021-10-16T02:29:20.726408Z","iopub.status.idle":"2021-10-16T02:29:21.294289Z","shell.execute_reply.started":"2021-10-16T02:29:20.72638Z","shell.execute_reply":"2021-10-16T02:29:21.293634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pln = get_plane_for_label(lab=0)\nshow_n_images([pln[0],pln[1],pln[2],pln[3]])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:21.295496Z","iopub.execute_input":"2021-10-16T02:29:21.295892Z","iopub.status.idle":"2021-10-16T02:29:21.979049Z","shell.execute_reply.started":"2021-10-16T02:29:21.295856Z","shell.execute_reply":"2021-10-16T02:29:21.978335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def n_random_plane(n=6, lab=1, df=df_train):\n    \n    plns = np.zeros((n,4,128,128), np.float32)\n    for i in range(n-2):\n        \n        plns[i] = get_tum_plane_for_label(lab, df)\n        \n    plns[n-2] = get_plane_for_label(lab, df)\n    plns[n-1] = get_plane_for_label(lab, df)\n    return plns","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:21.980464Z","iopub.execute_input":"2021-10-16T02:29:21.980745Z","iopub.status.idle":"2021-10-16T02:29:21.986146Z","shell.execute_reply.started":"2021-10-16T02:29:21.980708Z","shell.execute_reply":"2021-10-16T02:29:21.985443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = n_random_plane()\nimgs.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:21.987487Z","iopub.execute_input":"2021-10-16T02:29:21.987957Z","iopub.status.idle":"2021-10-16T02:29:22.493747Z","shell.execute_reply.started":"2021-10-16T02:29:21.987919Z","shell.execute_reply":"2021-10-16T02:29:22.493001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_n_images([imgs[i,0] for i in range(6)])\nshow_n_images([imgs[i,1] for i in range(6)])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:29:22.495221Z","iopub.execute_input":"2021-10-16T02:29:22.495479Z","iopub.status.idle":"2021-10-16T02:29:23.771274Z","shell.execute_reply.started":"2021-10-16T02:29:22.495445Z","shell.execute_reply":"2021-10-16T02:29:23.770502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = n_random_plane(n=6,lab=0, df=df_valid)\nshow_n_images([imgs[i,0] for i in range(6)])\nshow_n_images([imgs[i,1] for i in range(6)])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:38:35.123784Z","iopub.execute_input":"2021-10-16T02:38:35.124345Z","iopub.status.idle":"2021-10-16T02:38:37.425106Z","shell.execute_reply.started":"2021-10-16T02:38:35.124307Z","shell.execute_reply":"2021-10-16T02:38:37.424408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def n_random_planes_for_each_lab(df=df_train, n_imgs=12):      \n\n    \n    images_1 = n_random_plane(n=n_imgs//2,lab=1, df=df)\n    images_0 = n_random_plane(n=n_imgs//2,lab=0, df=df)\n    \n    images = np.concatenate((images_1, images_0), axis=0)\n    \n    lbl = np.zeros(images.shape[0], dtype=np.uint8)\n     \n    lbl[0:n_imgs//2] = 1 \n    \n    return images, lbl","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:40:49.972113Z","iopub.execute_input":"2021-10-16T02:40:49.97257Z","iopub.status.idle":"2021-10-16T02:40:49.978536Z","shell.execute_reply.started":"2021-10-16T02:40:49.972531Z","shell.execute_reply":"2021-10-16T02:40:49.97762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im,lb = n_random_planes_for_each_lab()\nim.shape,lb.shape,lb","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:40:52.403798Z","iopub.execute_input":"2021-10-16T02:40:52.404637Z","iopub.status.idle":"2021-10-16T02:40:53.187806Z","shell.execute_reply.started":"2021-10-16T02:40:52.404588Z","shell.execute_reply":"2021-10-16T02:40:53.18691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_n_images([im[i,0] for i in range(12)])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:38:56.917538Z","iopub.execute_input":"2021-10-16T02:38:56.917836Z","iopub.status.idle":"2021-10-16T02:38:57.929357Z","shell.execute_reply.started":"2021-10-16T02:38:56.917804Z","shell.execute_reply":"2021-10-16T02:38:57.928541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IM_SIZE=128\nim_size=128\n","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:17:45.042825Z","iopub.execute_input":"2021-10-15T18:17:45.045132Z","iopub.status.idle":"2021-10-15T18:17:45.050671Z","shell.execute_reply.started":"2021-10-15T18:17:45.04506Z","shell.execute_reply":"2021-10-15T18:17:45.049872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\ndef generate_train_batch(df=df_train, batch_size = 12, shuffle_imgs = True):\n   \n    while 1:\n\n        fimages, lbls = n_random_planes_for_each_lab(df, batch_size)\n        \n        if (shuffle_imgs):\n            fimages, lbls = shuffle(fimages, lbls)\n        \n        yield fimages[:,0:1,:,:], to_categorical(lbls, num_classes=2)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:41:49.23301Z","iopub.execute_input":"2021-10-16T02:41:49.233808Z","iopub.status.idle":"2021-10-16T02:41:49.240215Z","shell.execute_reply.started":"2021-10-16T02:41:49.233762Z","shell.execute_reply":"2021-10-16T02:41:49.239312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen_train = generate_train_batch(df=df_train, batch_size = 16)\nbim,blb = next(gen_train)\nbim.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:41:51.531079Z","iopub.execute_input":"2021-10-16T02:41:51.531741Z","iopub.status.idle":"2021-10-16T02:41:52.725429Z","shell.execute_reply.started":"2021-10-16T02:41:51.531707Z","shell.execute_reply":"2021-10-16T02:41:52.724708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen_test = generate_train_batch(df=df_valid,batch_size = 64)\n                                 \nbim_test,blb_test = next(gen_test)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:41:55.661287Z","iopub.execute_input":"2021-10-16T02:41:55.661541Z","iopub.status.idle":"2021-10-16T02:41:57.521708Z","shell.execute_reply.started":"2021-10-16T02:41:55.661514Z","shell.execute_reply":"2021-10-16T02:41:57.521049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.backend.set_image_data_format('channels_first')\ndef get_vgg16():\n    \n    model = tf.keras.applications.VGG16(\n        include_top=True,\n        weights=None,\n        input_tensor=None,\n        input_shape=(4,128,128),\n        pooling=None,\n        classes=2\n    )\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:39:35.916107Z","iopub.execute_input":"2021-10-16T02:39:35.916819Z","iopub.status.idle":"2021-10-16T02:39:35.921545Z","shell.execute_reply.started":"2021-10-16T02:39:35.916775Z","shell.execute_reply":"2021-10-16T02:39:35.920736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_modemvg():\n    \n\n    inpt = keras.Input(shape=(1,128,128))\n\n    #h = keras.layers.experimental.preprocessing.Rescaling(1.0 / 255)(inpt)\n    h = keras.layers.Conv2D(32, kernel_size=(3,3), activation=\"relu\", name=\"Conv_1\")(inpt)\n    h = keras.layers.Conv2D(32, kernel_size=(3,3), activation=\"relu\", name=\"Conv_1_1\")(h)\n    h = keras.layers.MaxPool2D(pool_size=(2, 2))(h)\n\n    h = keras.layers.Conv2D(64, kernel_size=(3,3), activation=\"relu\", name=\"Conv_2\")(h)\n    h = keras.layers.Conv2D(64, kernel_size=(3,3), activation=\"relu\", name=\"Conv_21\")(h)\n    h = keras.layers.MaxPool2D(pool_size=(2,2))(h)\n\n    h = keras.layers.Conv2D(128, kernel_size=(3,3), activation=\"relu\", name=\"Conv_31\")(h)\n    h = keras.layers.Conv2D(128, kernel_size=(3,3), activation=\"relu\", name=\"Conv_32\")(h)\n    h = keras.layers.Conv2D(128, kernel_size=(3,3), activation=\"relu\", name=\"Conv_33\")(h)\n    h = keras.layers.MaxPool2D(pool_size=(2,2))(h)\n    \n    h = keras.layers.Conv2D(512, kernel_size=(3,3), activation=\"relu\", name=\"Conv_41\")(h)\n    h = keras.layers.Conv2D(512, kernel_size=(3,3), activation=\"relu\", name=\"Conv_42\")(h)\n    h = keras.layers.Conv2D(512, kernel_size=(3,3), activation=\"relu\", name=\"Conv_43\")(h)\n    h = keras.layers.MaxPool2D(pool_size=(2,2))(h)\n    \n    h = keras.layers.Dropout(0.4)(h)\n\n    h = keras.layers.Flatten()(h)\n    h = keras.layers.Dense(1024, activation=\"relu\")(h)\n    h = keras.layers.Dense(128, activation=\"relu\")(h)\n    \n    output = keras.layers.Dense(2, activation=\"softmax\")(h)\n\n    model = keras.Model(inpt, output)\n    model.summary()\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:42:07.309288Z","iopub.execute_input":"2021-10-16T02:42:07.310208Z","iopub.status.idle":"2021-10-16T02:42:07.325734Z","shell.execute_reply.started":"2021-10-16T02:42:07.310147Z","shell.execute_reply":"2021-10-16T02:42:07.324524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model03():\n    \n    inpt = keras.Input(shape=(1,128,128))\n\n    #h = keras.layers.experimental.preprocessing.Rescaling(1.0 / 255)(inpt)\n\n    h = keras.layers.Conv2D(64, kernel_size=(4, 4), activation=\"relu\", name=\"Conv_1\")(inpt)\n    h = keras.layers.MaxPool2D(pool_size=(2, 2))(h)\n\n    h = keras.layers.Conv2D(32, kernel_size=(2, 2), activation=\"relu\", name=\"Conv_2\")(h)\n    h = keras.layers.MaxPool2D(pool_size=(1, 1))(h)\n\n    h = keras.layers.Dropout(0.1)(h)\n\n    h = keras.layers.Flatten()(h)\n    h = keras.layers.Dense(32, activation=\"relu\")(h)\n\n    output = keras.layers.Dense(2, activation=\"softmax\")(h)\n\n    model = keras.Model(inpt, output)\n    model.summary()\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:42:12.066112Z","iopub.execute_input":"2021-10-16T02:42:12.066834Z","iopub.status.idle":"2021-10-16T02:42:12.075007Z","shell.execute_reply.started":"2021-10-16T02:42:12.066785Z","shell.execute_reply":"2021-10-16T02:42:12.074204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model03()\nroc_auc = tf.keras.metrics.AUC(name='roc_auc', curve='ROC')\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:42:14.742739Z","iopub.execute_input":"2021-10-16T02:42:14.743474Z","iopub.status.idle":"2021-10-16T02:42:16.693677Z","shell.execute_reply.started":"2021-10-16T02:42:14.743436Z","shell.execute_reply":"2021-10-16T02:42:16.692994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:42:27.429916Z","iopub.execute_input":"2021-10-16T02:42:27.43045Z","iopub.status.idle":"2021-10-16T02:42:27.435861Z","shell.execute_reply.started":"2021-10-16T02:42:27.430412Z","shell.execute_reply":"2021-10-16T02:42:27.435049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = tf.config.list_physical_devices('GPU')\ngpus","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:42:29.851076Z","iopub.execute_input":"2021-10-16T02:42:29.851692Z","iopub.status.idle":"2021-10-16T02:42:29.857312Z","shell.execute_reply.started":"2021-10-16T02:42:29.851653Z","shell.execute_reply":"2021-10-16T02:42:29.856619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    tf.config.set_visible_devices(gpus[0], 'GPU')\n    logical_gpus = tf.config.list_logical_devices('GPU')\n    print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPU\")","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:42:35.465223Z","iopub.execute_input":"2021-10-16T02:42:35.465679Z","iopub.status.idle":"2021-10-16T02:42:35.471572Z","shell.execute_reply.started":"2021-10-16T02:42:35.465635Z","shell.execute_reply":"2021-10-16T02:42:35.470825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get_model03\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nepochs=100\nsteps_per_epoch=256\n\nearlystopper = EarlyStopping(patience=8, verbose=1)\ncheckpointer = ModelCheckpoint(filepath = 'best_model103.hdf5',\n                               verbose=1,\n                               save_best_only=True, save_weights_only = True)\nhistory = model.fit_generator(gen_train,verbose=1,\n                                        validation_data = gen_test, validation_steps = 24, \n                                              steps_per_epoch=steps_per_epoch,\n                              epochs=epochs,\n                    callbacks=[earlystopper, checkpointer]\n                                       )","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:42:49.005568Z","iopub.execute_input":"2021-10-16T02:42:49.005871Z","iopub.status.idle":"2021-10-16T02:55:35.454871Z","shell.execute_reply.started":"2021-10-16T02:42:49.00584Z","shell.execute_reply":"2021-10-16T02:55:35.451945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device_name = tf.test.gpu_device_name()\nif \"GPU\" not in device_name:\n    print(\"GPU device not found\")\nprint('Found GPU at: {}'.format(device_name))","metadata":{"execution":{"iopub.status.busy":"2021-10-16T02:27:59.428475Z","iopub.execute_input":"2021-10-16T02:27:59.428825Z","iopub.status.idle":"2021-10-16T02:27:59.506675Z","shell.execute_reply.started":"2021-10-16T02:27:59.428713Z","shell.execute_reply":"2021-10-16T02:27:59.505129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc = tf.keras.metrics.AUC(name='roc_auc', curve='ROC')\nmodel.load_weights('best_model03.hdf5')\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),\n              loss='categorical_crossentropy', metrics=['accuracy', roc_auc])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:55:07.957566Z","iopub.execute_input":"2021-10-15T18:55:07.95778Z","iopub.status.idle":"2021-10-15T18:55:08.025894Z","shell.execute_reply.started":"2021-10-15T18:55:07.957755Z","shell.execute_reply":"2021-10-15T18:55:08.024688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nepochs=100\nsteps_per_epoch=256\n\nearlystopper = EarlyStopping(patience=8, verbose=1)\ncheckpointer = ModelCheckpoint(filepath = 'best_model03_0001.hdf5',\n                               verbose=1,\n                               save_best_only=True, save_weights_only = True)\nhistory = model.fit_generator(gen_train,verbose=1,\n                                        validation_data = gen_test, validation_steps = 24, \n                                              steps_per_epoch=steps_per_epoch,\n                              epochs=epochs,\n                    callbacks=[earlystopper, checkpointer]\n                                       )","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:55:08.026897Z","iopub.status.idle":"2021-10-15T18:55:08.027832Z","shell.execute_reply.started":"2021-10-15T18:55:08.027603Z","shell.execute_reply":"2021-10-15T18:55:08.027627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_modemvg()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.711256Z","iopub.status.idle":"2021-10-15T18:14:37.711675Z","shell.execute_reply.started":"2021-10-15T18:14:37.711448Z","shell.execute_reply":"2021-10-15T18:14:37.71147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model03()\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n              loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.713044Z","iopub.status.idle":"2021-10-15T18:14:37.713493Z","shell.execute_reply.started":"2021-10-15T18:14:37.713257Z","shell.execute_reply":"2021-10-15T18:14:37.713281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get_modemvg\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nepochs=100\nsteps_per_epoch=256\n\nearlystopper = EarlyStopping(patience=8, verbose=1)\ncheckpointer = ModelCheckpoint(filepath = 'best_modemvg.hdf5',\n                               verbose=1,\n                               save_best_only=True, save_weights_only = True)\nhistory = model.fit_generator(gen_train,verbose=1,\n                                        validation_data = gen_test, validation_steps = 24, \n                                              steps_per_epoch=steps_per_epoch,\n                              epochs=epochs,\n                    callbacks=[earlystopper, checkpointer]\n                                       )","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.714865Z","iopub.status.idle":"2021-10-15T18:14:37.715326Z","shell.execute_reply.started":"2021-10-15T18:14:37.715095Z","shell.execute_reply":"2021-10-15T18:14:37.715119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('best_modemvg.hdf5')\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),\n              loss='categorical_crossentropy', metrics=['accuracy', 'roc_auc'])","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.716844Z","iopub.status.idle":"2021-10-15T18:14:37.717303Z","shell.execute_reply.started":"2021-10-15T18:14:37.717066Z","shell.execute_reply":"2021-10-15T18:14:37.717097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nepochs=100\nsteps_per_epoch=256\n\nearlystopper = EarlyStopping(patience=8, verbose=1)\ncheckpointer = ModelCheckpoint(filepath = 'best_modemvg_0001.hdf5',\n                               verbose=1,\n                               save_best_only=True, save_weights_only = True)\nhistory = model.fit_generator(gen_train,verbose=1,\n                                        validation_data = gen_test, validation_steps = 24, \n                                              steps_per_epoch=steps_per_epoch,\n                              epochs=epochs,\n                    callbacks=[earlystopper, checkpointer]\n                                       )","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.718787Z","iopub.status.idle":"2021-10-15T18:14:37.71923Z","shell.execute_reply.started":"2021-10-15T18:14:37.718991Z","shell.execute_reply":"2021-10-15T18:14:37.719013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# only planesz","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.720596Z","iopub.status.idle":"2021-10-15T18:14:37.721036Z","shell.execute_reply.started":"2021-10-15T18:14:37.720793Z","shell.execute_reply":"2021-10-15T18:14:37.720815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nepochs=100\nsteps_per_epoch=128\n\nearlystopper = EarlyStopping(patience=8, verbose=1)\ncheckpointer = ModelCheckpoint(filepath = 'best_model.hdf5',\n                               verbose=1,\n                               save_best_only=True, save_weights_only = True)\nhistory = model.fit_generator(gen_train,verbose=1,\n                                        validation_data = gen_test, validation_steps = 34, \n                                              steps_per_epoch=steps_per_epoch,\n                              epochs=epochs,\n                    callbacks=[earlystopper, checkpointer]\n                                       )","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.722406Z","iopub.status.idle":"2021-10-15T18:14:37.722828Z","shell.execute_reply.started":"2021-10-15T18:14:37.722598Z","shell.execute_reply":"2021-10-15T18:14:37.722622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('best_model.hdf5')","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.724196Z","iopub.status.idle":"2021-10-15T18:14:37.724617Z","shell.execute_reply.started":"2021-10-15T18:14:37.724388Z","shell.execute_reply":"2021-10-15T18:14:37.72441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict on test\n!mkdir proc_test\n!mkdir tum_test","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.726099Z","iopub.status.idle":"2021-10-15T18:14:37.726515Z","shell.execute_reply.started":"2021-10-15T18:14:37.7263Z","shell.execute_reply":"2021-10-15T18:14:37.726322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdir = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/test/'","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.727945Z","iopub.status.idle":"2021-10-15T18:14:37.728389Z","shell.execute_reply.started":"2021-10-15T18:14:37.728161Z","shell.execute_reply":"2021-10-15T18:14:37.728184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, row in test_df.iterrows():\n    \n    pid = int(row['BraTS21ID'])\n    d1 = get_pat_data(testdir + str(pid).zfill(5))\n    \n    new_data_crop = torch.from_numpy(d1[None, :, :, :, :]).float()\n\n    with torch.no_grad():\n\n        data = [new_data_crop]\n\n\n        #data = [d.cuda() for d in data]\n\n        output = mdl(data)\n        \n    \n    ot = np.array(torch.Tensor.cpu(output[0]))\n    ot = ot[0]\n\n    im = ot.copy()\n    imout = im[0]+im[1]+im[2]\n    imout[imout>=0.5]=1\n    imout[imout<0.5]=0\n    \n    tum_pix = np.where(imout>0)\n    \n    predt = []\n    for k in tum_pix.shape[1]:\n        pred = model.predict(im[:,tum_pix[0][k]])\n        predt.appendnd(pred)\n    \n    test_df.at[i,'MGMT_value'] = np.mean(predt)\n    '''\n    fname = os.path.join('tum_test', 'tumor_'+str(pid).zfill(5)+'.npy')\n    np.save(fname, tum_pix)\n    \n    fname = os.path.join('proc_test', 'proc_'+str(pid).zfill(5)+'.npz')\n    np.savez(fname, d1)'''\n    ","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.729857Z","iopub.status.idle":"2021-10-15T18:14:37.730322Z","shell.execute_reply.started":"2021-10-15T18:14:37.730091Z","shell.execute_reply":"2021-10-15T18:14:37.730115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:14:37.731763Z","iopub.status.idle":"2021-10-15T18:14:37.732189Z","shell.execute_reply.started":"2021-10-15T18:14:37.731945Z","shell.execute_reply":"2021-10-15T18:14:37.731982Z"},"trusted":true},"execution_count":null,"outputs":[]}]}