{"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":"## Imports","metadata":{}},{"cell_type":"code","source":"!pip install segmentation_models_pytorch                        ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:13:47.169788Z","iopub.execute_input":"2022-09-17T13:13:47.170386Z","iopub.status.idle":"2022-09-17T13:14:05.223192Z","shell.execute_reply.started":"2022-09-17T13:13:47.170273Z","shell.execute_reply":"2022-09-17T13:14:05.222040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import albumentations as A  ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:05.225489Z","iopub.execute_input":"2022-09-17T13:14:05.225889Z","iopub.status.idle":"2022-09-17T13:14:06.607799Z","shell.execute_reply.started":"2022-09-17T13:14:05.225842Z","shell.execute_reply":"2022-09-17T13:14:06.606825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport os\nimport cv2  \nimport zipfile   \nimport rasterio\nimport numpy as np \nimport math  \nimport pandas as pd \nfrom PIL import Image\nimport tifffile as tiff\nimport seaborn as sns\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom rasterio.windows import Window\nfrom torch.utils.data import Dataset\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import DataLoader \nfrom torchvision import transforms\nfrom sklearn.model_selection import StratifiedKFold\nfrom collections import Counter\nimport pytorch_lightning as pl \nimport segmentation_models_pytorch as smp\nfrom torch.cuda.amp import autocast, GradScaler\nfrom torchvision import models\nimport torchvision\nfrom torch.utils.checkpoint import checkpoint_sequential\nfrom torch.utils.checkpoint import checkpoint\nfrom albumentations.pytorch import ToTensorV2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:06.609281Z","iopub.execute_input":"2022-09-17T13:14:06.609664Z","iopub.status.idle":"2022-09-17T13:14:12.103079Z","shell.execute_reply.started":"2022-09-17T13:14:06.609627Z","shell.execute_reply":"2022-09-17T13:14:12.102012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Config","metadata":{}},{"cell_type":"code","source":"class cfg:\n    epoch = 15\n    train_batch_size = 16\n    val_batch_size = 16 \n    num_workers = os.cpu_count()       \n    pin_memory = True \n    lr = 5e-4\n    use_dataset = 0\n    size = (384,384)\n    loss_func = 'bce'\n    device = 'cuda'","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.105890Z","iopub.execute_input":"2022-09-17T13:14:12.106653Z","iopub.status.idle":"2022-09-17T13:14:12.112158Z","shell.execute_reply.started":"2022-09-17T13:14:12.106606Z","shell.execute_reply":"2022-09-17T13:14:12.110951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 42\ntorch.manual_seed(seed)\nnp.random.seed = seed    ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = '../input/hubmap-organ-segmentation/train_images/'  \ntrain_df = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.113594Z","iopub.execute_input":"2022-09-17T13:14:12.114161Z","iopub.status.idle":"2022-09-17T13:14:12.421894Z","shell.execute_reply.started":"2022-09-17T13:14:12.114126Z","shell.execute_reply":"2022-09-17T13:14:12.420913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Helper functions","metadata":{}},{"cell_type":"code","source":"def mask2rle(img): # encoder\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels= img.T.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n \ndef rle2mask(mask_rle, shape): # decoder \n    '''\n    mask_rle: run-length as string formated (start length) \n    shape: (width,height) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape).T","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.423243Z","iopub.execute_input":"2022-09-17T13:14:12.423621Z","iopub.status.idle":"2022-09-17T13:14:12.432570Z","shell.execute_reply.started":"2022-09-17T13:14:12.423585Z","shell.execute_reply":"2022-09-17T13:14:12.431598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale(img, mask, size=cfg.size): \n    scaled_img = cv2.resize(img, size)\n    scaled_mask = cv2.resize(mask, size, interpolation=cv2.INTER_NEAREST)\n    return scaled_img, scaled_mask","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.433910Z","iopub.execute_input":"2022-09-17T13:14:12.434882Z","iopub.status.idle":"2022-09-17T13:14:12.444430Z","shell.execute_reply.started":"2022-09-17T13:14:12.434846Z","shell.execute_reply":"2022-09-17T13:14:12.443510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=5)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.446316Z","iopub.execute_input":"2022-09-17T13:14:12.447142Z","iopub.status.idle":"2022-09-17T13:14:12.452889Z","shell.execute_reply.started":"2022-09-17T13:14:12.447108Z","shell.execute_reply":"2022-09-17T13:14:12.451970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_bins(age):\n    age_ = ''\n    if(age <= 20.0):\n        age_ = '0'\n    elif(age <= 40.0):\n        age_ = '1'\n    elif(age <= 50.0):\n        age_ = '2'\n    elif(age <= 60.0):\n        age_ = '3'\n    else:\n        age_ = '4'\n    return age_","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.454199Z","iopub.execute_input":"2022-09-17T13:14:12.454634Z","iopub.status.idle":"2022-09-17T13:14:12.462305Z","shell.execute_reply.started":"2022-09-17T13:14:12.454596Z","shell.execute_reply":"2022-09-17T13:14:12.461111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c_sum = []\nfor index, row in train_df.iterrows():\n    c_sum.append(row['organ'] + to_bins(row['age']) + row['sex'])","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.468061Z","iopub.execute_input":"2022-09-17T13:14:12.468795Z","iopub.status.idle":"2022-09-17T13:14:12.497489Z","shell.execute_reply.started":"2022-09-17T13:14:12.468740Z","shell.execute_reply":"2022-09-17T13:14:12.496651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['c_sum'] = c_sum    ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.498676Z","iopub.execute_input":"2022-09-17T13:14:12.499108Z","iopub.status.idle":"2022-09-17T13:14:12.505546Z","shell.execute_reply.started":"2022-09-17T13:14:12.499071Z","shell.execute_reply":"2022-09-17T13:14:12.504514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold_ = [0]*train_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.506929Z","iopub.execute_input":"2022-09-17T13:14:12.507416Z","iopub.status.idle":"2022-09-17T13:14:12.512848Z","shell.execute_reply.started":"2022-09-17T13:14:12.507381Z","shell.execute_reply":"2022-09-17T13:14:12.511712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k,(train_index, test_index) in enumerate(skf.split(train_df['id'], train_df['c_sum'])):\n    for i in test_index:\n        fold_[i] = k","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.514438Z","iopub.execute_input":"2022-09-17T13:14:12.514842Z","iopub.status.idle":"2022-09-17T13:14:12.528807Z","shell.execute_reply.started":"2022-09-17T13:14:12.514808Z","shell.execute_reply":"2022-09-17T13:14:12.527804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['fold'] = fold_","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.531068Z","iopub.execute_input":"2022-09-17T13:14:12.532041Z","iopub.status.idle":"2022-09-17T13:14:12.537884Z","shell.execute_reply.started":"2022-09-17T13:14:12.532006Z","shell.execute_reply":"2022-09-17T13:14:12.537102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()        ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.539072Z","iopub.execute_input":"2022-09-17T13:14:12.539896Z","iopub.status.idle":"2022-09-17T13:14:12.563695Z","shell.execute_reply.started":"2022-09-17T13:14:12.539855Z","shell.execute_reply":"2022-09-17T13:14:12.562677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Scaling + Visualization","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image(_id):\n    return torch.load('../input/rescale-images/rescale_image/image_' + str(_id) + '.pt')\ndef get_mask(_id):\n    return torch.load('../input/rescale-images/rescale_image/mask_' + str(_id) + '.pt')","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.565211Z","iopub.execute_input":"2022-09-17T13:14:12.565571Z","iopub.status.idle":"2022-09-17T13:14:12.571236Z","shell.execute_reply.started":"2022-09-17T13:14:12.565533Z","shell.execute_reply":"2022-09-17T13:14:12.569688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"       ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = A.Compose(\n    [\n        A.Resize(cfg.size[0],cfg.size[1]),\n        #A.Normalize(m,s),\n        #ToTensorV2()\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.572945Z","iopub.execute_input":"2022-09-17T13:14:12.573688Z","iopub.status.idle":"2022-09-17T13:14:12.581120Z","shell.execute_reply.started":"2022-09-17T13:14:12.573655Z","shell.execute_reply":"2022-09-17T13:14:12.580060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class HuBMAPDataset(Dataset):\n    def __init__(self,df,transform):\n        self.ids = df['id'].values  \n        self.rle = df['rle'].values\n        self.width = df['img_width'].values\n        self.height = df['img_height'].values\n        self.transform = transform \n    def __len__(self):  \n        return len(self.ids)\n    def __getitem__(self,idx):\n        img  = tiff.imread(TRAIN_PATH + str(self.ids[idx]) +'.tiff')   \n        mask = rle2mask(self.rle[idx], (self.height[idx],self.width[idx]))\n        transformed = self.transform(image=img, mask=mask)\n        image = transformed[\"image\"]/255\n        mask = transformed[\"mask\"] \n        image = torch.tensor(image,dtype = torch.float32)\n        mask = torch.tensor(mask)\n        image = torch.permute(image,(2,0,1))\n        return image,mask ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.582759Z","iopub.execute_input":"2022-09-17T13:14:12.583566Z","iopub.status.idle":"2022-09-17T13:14:12.593632Z","shell.execute_reply.started":"2022-09-17T13:14:12.583532Z","shell.execute_reply":"2022-09-17T13:14:12.592735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = HuBMAPDataset(train_df,transform)    ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.595158Z","iopub.execute_input":"2022-09-17T13:14:12.596191Z","iopub.status.idle":"2022-09-17T13:14:12.602861Z","shell.execute_reply.started":"2022-09-17T13:14:12.596112Z","shell.execute_reply":"2022-09-17T13:14:12.601517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.604649Z","iopub.execute_input":"2022-09-17T13:14:12.605066Z","iopub.status.idle":"2022-09-17T13:14:12.623476Z","shell.execute_reply.started":"2022-09-17T13:14:12.605025Z","shell.execute_reply":"2022-09-17T13:14:12.622483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img  = tiff.imread(TRAIN_PATH + str(10044) +'.tiff')","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.624889Z","iopub.execute_input":"2022-09-17T13:14:12.625230Z","iopub.status.idle":"2022-09-17T13:14:12.967705Z","shell.execute_reply.started":"2022-09-17T13:14:12.625196Z","shell.execute_reply":"2022-09-17T13:14:12.966730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def stats(l):  \n    return torch.tensor(l).float().mean(),torch.tensor(l).float().std()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.969263Z","iopub.execute_input":"2022-09-17T13:14:12.969662Z","iopub.status.idle":"2022-09-17T13:14:12.975426Z","shell.execute_reply.started":"2022-09-17T13:14:12.969625Z","shell.execute_reply":"2022-09-17T13:14:12.974235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Dice_metric(preds,labels): \n    t1 = preds * labels  \n    t1 = 2 * torch.sum(t1,(1,2))\n    t2 = preds + labels \n    t2 = torch.sum(t2,(1,2))\n    t = t1 / t2\n    return t.mean()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.977012Z","iopub.execute_input":"2022-09-17T13:14:12.977722Z","iopub.status.idle":"2022-09-17T13:14:12.985051Z","shell.execute_reply.started":"2022-09-17T13:14:12.977685Z","shell.execute_reply":"2022-09-17T13:14:12.984031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Dice_loss(preds,labels):\n    return 1 - Dice_metric(preds,labels) ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.987514Z","iopub.execute_input":"2022-09-17T13:14:12.988205Z","iopub.status.idle":"2022-09-17T13:14:12.993039Z","shell.execute_reply.started":"2022-09-17T13:14:12.988169Z","shell.execute_reply":"2022-09-17T13:14:12.992150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomLoss(nn.Module):\n    def __init__(self):\n        super(CustomLoss,self).__init__()\n        self.diceloss = smp.losses.DiceLoss(mode='binary')\n        self.binloss = smp.losses.SoftBCEWithLogitsLoss(reduction = 'mean' , smooth_factor = 0.1)\n\n    def forward(self, output, mask):\n        dice = self.diceloss(output,mask)\n        bce = self.binloss(output.squeeze() , mask)\n        loss = dice * 0.7 + bce * 0.3\n        return loss","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:12.994984Z","iopub.execute_input":"2022-09-17T13:14:12.995353Z","iopub.status.idle":"2022-09-17T13:14:13.004013Z","shell.execute_reply.started":"2022-09-17T13:14:12.995319Z","shell.execute_reply":"2022-09-17T13:14:13.002406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_modelfp16(model,dataloader,optimizer,scheduler = None):\n    avgloss = 0.0\n    s = nn.Sigmoid() \n    scaler = GradScaler()\n    model.train()\n    #loss_func = smp.losses.SoftBCEWithLogitsLoss(reduction = 'mean' , smooth_factor = 0.1)\n    loss_func = CustomLoss()\n    for image,mask in tqdm(dataloader):  \n        optimizer.zero_grad() \n        image = image.to(device = cfg.device)\n        mask = mask.to(device = cfg.device)\n        with autocast(enabled = True):\n            output = model(image)\n            mask = mask.float()\n            output = s(output)   \n            #dice_loss = Dice_loss(output, mask).requires_grad_(True)\n            dice_loss = loss_func(output,mask)\n        avgloss += dice_loss\n        scaler.scale(dice_loss).backward()  \n        scaler.unscale_(optimizer)     \n        #torch.nn.utils.clip_grad_norm_(model.parameters(),1)\n        scaler.step(optimizer)  \n        scaler.update()\n        if(scheduler is not None):\n            scheduler.step()\n        avgloss += dice_loss\n    return avgloss/len(dataloader)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.005126Z","iopub.execute_input":"2022-09-17T13:14:13.006730Z","iopub.status.idle":"2022-09-17T13:14:13.015432Z","shell.execute_reply.started":"2022-09-17T13:14:13.006695Z","shell.execute_reply":"2022-09-17T13:14:13.014510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def params_grad(model):\n    grads = []\n    for param in model.parameters(): \n        if(param.grad is not None):\n            grads.append(torch.norm(param.grad).item())\n    plt.hist(grads) \n    plt.show()\n    print(np.mean(np.array(grads)))","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.016661Z","iopub.execute_input":"2022-09-17T13:14:13.017198Z","iopub.status.idle":"2022-09-17T13:14:13.027150Z","shell.execute_reply.started":"2022-09-17T13:14:13.017164Z","shell.execute_reply":"2022-09-17T13:14:13.026131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(model,dataloader,optimizer,scheduler = None):\n    avgloss = 0.0\n    s = nn.Sigmoid() \n    #loss_func = smp.losses.SoftBCEWithLogitsLoss(reduction = 'mean' , smooth_factor = 0.1)\n    loss_func = CustomLoss()\n    step = 0 \n    for image,mask in tqdm(dataloader):  \n        optimizer.zero_grad() \n        image = image.to(device = cfg.device)\n        mask = mask.to(device = cfg.device)\n        output = model(image)\n        #print('count nan: ',torch.count_nonzero(torch.isnan(output)).item()/np.prod(output.shape))\n        #plt.hist(torch.flatten(output).tolist())\n        #plt.show() \n        mask = mask.float()     \n        #output = s(output)   \n        dice_loss = loss_func(output,mask)\n        avgloss += dice_loss\n        dice_loss.backward() \n        #params_grad(model)\n        #torch.nn.utils.clip_grad_norm_(model.parameters(),1)\n        optimizer.step() \n        if(scheduler is not None):\n            scheduler.step()\n        avgloss += dice_loss\n        if(step != 0):\n            print(avgloss/step)\n        step += 1\n    return avgloss/len(dataloader)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.034164Z","iopub.execute_input":"2022-09-17T13:14:13.034716Z","iopub.status.idle":"2022-09-17T13:14:13.041497Z","shell.execute_reply.started":"2022-09-17T13:14:13.034690Z","shell.execute_reply":"2022-09-17T13:14:13.040377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval_model(model,dataloader):\n    avgloss = 0.0 \n    avgmetric = 0.0 \n    s = nn.Sigmoid()\n    for image,mask in tqdm(dataloader): \n        with torch.no_grad():\n            image = image.to(device = cfg.device)\n            mask = mask.to(device = cfg.device)\n            output = model(image)\n            mask = mask.float()  \n            output = output.detach()\n            mask = mask.detach()     \n            output = s(output)\n            #print('count nan: ',torch.count_nonzero(torch.isnan(output)).item()/np.prod(output.shape))\n            #output[output != output] = 0.0\n            output = output.round() \n            #print(output,output.mean(),output.shape,mask.shape)\n            dice = Dice_metric(output.squeeze(), mask)\n            avgmetric += dice\n            avgloss += (1 - dice)\n    loss = avgloss/len(dataloader)\n    metric = avgmetric/len(dataloader) \n    return loss,metric ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.043010Z","iopub.execute_input":"2022-09-17T13:14:13.043627Z","iopub.status.idle":"2022-09-17T13:14:13.053257Z","shell.execute_reply.started":"2022-09-17T13:14:13.043591Z","shell.execute_reply":"2022-09-17T13:14:13.052284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"      ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"                                 ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tyfiyuijop","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.055009Z","iopub.execute_input":"2022-09-17T13:14:13.055669Z","iopub.status.idle":"2022-09-17T13:14:13.331531Z","shell.execute_reply.started":"2022-09-17T13:14:13.055635Z","shell.execute_reply":"2022-09-17T13:14:13.329424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nfold = 5        \ncv_res = []            \nfor k in range(nfold): \n    train = train_df[train_df['fold'] != k] \n    test = train_df[train_df['fold'] == k]     \n    model = smp.Unet(        \n        encoder_name=\"efficientnet-b7\",        \n        encoder_weights=\"imagenet\",         \n        in_channels=3,                            \n        classes=1     \n    )        \n    model = model.to(device = cfg.device)\n    optimizer = torch.optim.AdamW(filter(lambda x: x.requires_grad == True, model.parameters()),lr = cfg.lr)\n    train_dataset = HuBMAPDataset(train,transform)\n    test_dataset = HuBMAPDataset(test,transform)\n    train_loader = DataLoader(train_dataset,batch_size = cfg.train_batch_size,num_workers = os.cpu_count(),\n                              pin_memory = cfg.pin_memory,shuffle=True)\n    test_loader = DataLoader(test_dataset,batch_size = cfg.val_batch_size,num_workers = os.cpu_count(),\n                             pin_memory = cfg.pin_memory,shuffle=False)\n    #scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer,T_max = len(train_loader))\n    #scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=1e3, \n    #                                          max_lr=1e-3, epochs=cfg.epoch, steps_per_epoch=len(train_loader))\n    scheduler = None\n    '''\n        Early stopping: saving the best model in every fold\n    ''' \n    current_loss = 1.0   \n    for epoch in range(cfg.epoch):\n        print('epoch {}'.format(epoch))\n        loss = train_modelfp16(model,train_loader,optimizer,scheduler = scheduler)\n        torch.cuda.empty_cache()                         \n        gc.collect()\n        loss_test,metric_test = eval_model(model,test_loader) \n        loss_train,metric_train = eval_model(model,train_loader)\n        print('test loss: ',loss_test.item(),' test metric: ',metric_test.item())\n        print('train loss: ',loss_train.item(),' train metric: ',metric_train.item())\n        if(loss_test.item() < current_loss):\n            '''torch.save({             \n                'epoch': epoch,         \n                'model_state_dict': model.state_dict(),\n                'optimizer_state_dict': optimizer.state_dict(),\n                'scheduler': scheduler.state_dict(),\n                'loss': loss_test, \n            }, './models/checkpoint' + str(k) + '.pth')\n            print('Saving model with loss: ',loss_test.item())'''\n            current_loss = loss_test.item()\n    cv_res.append(metric_test.item())                          \nstat = stats(cv_res)                                             \nprint(cv_res,' mean: ',stat[0],' std: ',stat[1])                                                                                                                           ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:20.313010Z","iopub.execute_input":"2022-09-17T13:14:20.313430Z","iopub.status.idle":"2022-09-17T13:25:55.795483Z","shell.execute_reply.started":"2022-09-17T13:14:20.313392Z","shell.execute_reply":"2022-09-17T13:25:55.793068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = torch.tensor([1e90,2e90,3e90])                                                           ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.334790Z","iopub.status.idle":"2022-09-17T13:14:13.335441Z","shell.execute_reply.started":"2022-09-17T13:14:13.335192Z","shell.execute_reply":"2022-09-17T13:14:13.335217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = torch.Tensor([float('NaN'), 1, float('NaN'), 2, 3])\na = a.isnan()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.337029Z","iopub.status.idle":"2022-09-17T13:14:13.337881Z","shell.execute_reply.started":"2022-09-17T13:14:13.337608Z","shell.execute_reply":"2022-09-17T13:14:13.337632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.count_nonzero(a).item()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.339243Z","iopub.status.idle":"2022-09-17T13:14:13.340092Z","shell.execute_reply.started":"2022-09-17T13:14:13.339849Z","shell.execute_reply":"2022-09-17T13:14:13.339873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image,mask = train_dataset[5]                                                                             ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.342423Z","iopub.status.idle":"2022-09-17T13:14:13.343148Z","shell.execute_reply.started":"2022-09-17T13:14:13.342895Z","shell.execute_reply":"2022-09-17T13:14:13.342920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(torch.permute(image,(1,2,0)).int())","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.344411Z","iopub.status.idle":"2022-09-17T13:14:13.345134Z","shell.execute_reply.started":"2022-09-17T13:14:13.344883Z","shell.execute_reply":"2022-09-17T13:14:13.344907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = image.unsqueeze(dim = 0)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.346482Z","iopub.status.idle":"2022-09-17T13:14:13.347194Z","shell.execute_reply.started":"2022-09-17T13:14:13.346946Z","shell.execute_reply":"2022-09-17T13:14:13.346970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_ = model(image.to(device = 'cuda'))","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.348456Z","iopub.status.idle":"2022-09-17T13:14:13.349151Z","shell.execute_reply.started":"2022-09-17T13:14:13.348914Z","shell.execute_reply":"2022-09-17T13:14:13.348936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = nn.Sigmoid()\nprint(s(mask_)) ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.350412Z","iopub.status.idle":"2022-09-17T13:14:13.351111Z","shell.execute_reply.started":"2022-09-17T13:14:13.350871Z","shell.execute_reply":"2022-09-17T13:14:13.350894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_ = s(mask_)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.352731Z","iopub.status.idle":"2022-09-17T13:14:13.353453Z","shell.execute_reply.started":"2022-09-17T13:14:13.353186Z","shell.execute_reply":"2022-09-17T13:14:13.353210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_ = mask_.detach().round().int()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.354741Z","iopub.status.idle":"2022-09-17T13:14:13.355437Z","shell.execute_reply.started":"2022-09-17T13:14:13.355187Z","shell.execute_reply":"2022-09-17T13:14:13.355211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask_.squeeze().cpu())","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.356718Z","iopub.status.idle":"2022-09-17T13:14:13.357412Z","shell.execute_reply.started":"2022-09-17T13:14:13.357168Z","shell.execute_reply":"2022-09-17T13:14:13.357191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask) ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:14:13.358686Z","iopub.status.idle":"2022-09-17T13:14:13.359391Z","shell.execute_reply.started":"2022-09-17T13:14:13.359137Z","shell.execute_reply":"2022-09-17T13:14:13.359161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preds(num):\n    image,mask = train_dataset[num]\n    image = image.unsqueeze(dim = 0) \n    mask_ = model(image.to(device = 'cuda'))\n    s = nn.Sigmoid()   \n    mask_ = s(mask_)\n    mask_ = mask_.detach().round().int()\n    f, axarr = plt.subplots(1,2,figsize = (12,12))\n    axarr[0].imshow(mask_.squeeze().cpu())\n    axarr[1].imshow(mask) ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:29:50.519981Z","iopub.execute_input":"2022-09-17T13:29:50.520361Z","iopub.status.idle":"2022-09-17T13:29:50.528836Z","shell.execute_reply.started":"2022-09-17T13:29:50.520327Z","shell.execute_reply":"2022-09-17T13:29:50.527550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds(32) ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T13:30:17.470470Z","iopub.execute_input":"2022-09-17T13:30:17.471186Z","iopub.status.idle":"2022-09-17T13:30:17.881522Z","shell.execute_reply.started":"2022-09-17T13:30:17.471150Z","shell.execute_reply":"2022-09-17T13:30:17.880513Z"},"trusted":true},"execution_count":null,"outputs":[]}]}