{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Description\nThis kernel performs inference for [PANDA concat tile pooling starter](https://www.kaggle.com/iafoss/panda-concat-fast-ai-starter) kernel with use of multiple models and 8 fold TTA. Check it for more training details. The image preprocessing pipline is provided [here](https://www.kaggle.com/iafoss/panda-16x128x128-tiles).","execution_count":null},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import cv2\nfrom tqdm import tqdm_notebook as tqdm\nimport fastai\nfrom fastai.vision import *\nimport os\n#from mish_activation import *\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport skimage.io\nimport numpy as np\nimport pandas as pd\nsys.path.insert(0, '../input/semisupervised-imagenet-models/semi-supervised-ImageNet1K-models-master/')\nfrom hubconf import *\nimport os\nimport sys\nsys.path = [\n    '../input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master',\n] + sys.path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nimport random\nimport numpy as np\n\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_everything(49)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! ls ../input/prostate-cancer-grade-assessment\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#DATA = '../input/prostate-cancer-grade-assessment/test_images/'\ntest='Y'\nalbu_iter='Y'\nif test=='Y':\n    DATA = '../input/prostate-cancer-grade-assessment/test_images'\n    TEST = '../input/prostate-cancer-grade-assessment/test.csv'\n    val='N'\nelse:\n    \n    DATA = '../input/prostate-cancer-grade-assessment/train_images'\n    val='Y'\n\n    TEST = '../input/prostate-cancer-grade-assessment/train.csv'\n#TEST = '../input/prostate-cancer-grade-assessment/train.csv'\nSAMPLE = '../input/prostate-cancer-grade-assessment/sample_submission.csv'\n#MODELS = [f'../input/panda-starter-models/RNXT50_{i}.pth' for i in range(4)]\npath='../input/prostate-cancer-grade-assessment'\n\nsz = 200\nbs = 2\n#N = 24\nnworkers = 2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class MishFunction(torch.autograd.Function):\n    @staticmethod\n    def forward(ctx, x):\n        ctx.save_for_backward(x)\n        return x * torch.tanh(F.softplus(x))   # x * tanh(ln(1 + exp(x)))\n\n    @staticmethod\n    def backward(ctx, grad_output):\n        x = ctx.saved_variables[0]\n        sigmoid = torch.sigmoid(x)\n        tanh_sp = torch.tanh(F.softplus(x)) \n        return grad_output * (tanh_sp + x * sigmoid * (1 - tanh_sp * tanh_sp))\n\nclass Mish(nn.Module):\n    def forward(self, x):\n        return MishFunction.apply(x)\n\ndef to_Mish(model):\n    for child_name, child in model.named_children():\n        if isinstance(child, nn.ReLU):\n            setattr(model, child_name, Mish())\n        else:\n            to_Mish(child)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def _resnext(url, block, layers, pretrained, progress, **kwargs):\n    model = ResNet(block, layers, **kwargs)\n    #state_dict = load_state_dict_from_url(url, progress=progress)\n    #model.load_state_dict(state_dict)\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Resnext_tile(nn.Module):\n    def __init__(self, arch='resnext50_32x4d', n=1, pre=True):\n        super().__init__()\n        #m = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', arch,pre)\n        m = _resnext(semi_supervised_model_urls[arch], Bottleneck, [3, 4, 6, 3], False, \n                progress=False,groups=32,width_per_group=4)\n        self.enc = nn.Sequential(*list(m.children())[:-2])       \n        nc = list(m.children())[-1].in_features\n        self.head = nn.Sequential(AdaptiveConcatPool2d(),Flatten(),nn.GroupNorm(32,2*nc) ,nn.Linear(2*nc,512),\n                            Mish(),nn.GroupNorm(16,512), nn.Dropout(0.4),nn.Linear(512,n))\n        #layers=[AdaptiveConcatPool2d(),Flatten()]+bn_drop_lin(nc*2, 512, True, 0.5, Mish()) + \\\n        #    bn_drop_lin(512, n, True, 0.3) #nn.BatchNorm1d(512)\n        #self.head = nn.Sequential(*layers)\n        \n    def forward(self,  x):\n        #print(len(x))\n        '''\n        shape = x[0].shape\n        n = len(x)\n        x = torch.stack(x,1).view(-1,shape[1],shape[2],shape[3]).cpu()\n        # filter white features\n        #x: bs*N x 3 x 128 x 128\n        #l=[ get_emptiness(empty)  for empty in  x ]\n        #print(l)\n        #print(x[0])\n        #print(x.size())\n        #x1=[empty  for empty in  x if get_emptiness(empty)<0.80]\n        print(len(x))\n        #n=len(x)\n        #x = self.enc(torch.stack(x))\n        '''\n        x = self.enc(x)\n        #x: bs*N x C x 4 x 4\n        shape = x.shape\n        #print('shape',shape)\n        #concatenate the output for tiles into a single map\n        #x = x.view(-1,n,shape[1],shape[2],shape[3]).permute(0,2,1,3,4).contiguous()\\\n        #  .view(-1,shape[1],shape[2]*n,shape[3])\n        #x: bs x C x N*4 x 4\n        #print(x.size())\n        #print('x',x.size())\n        x = self.head(x)\n        #print('out',x.size())\n        #x: bs x n\n        return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gpu = torch.device(\"cuda:0\")\ngpu = torch.device(\"cpu\")\n#state_dict = torch.load('../work/models/resnext_adj_sampling150-Copy1',map_location=gpu)\nstate_dicts=[]\n#state_dict = torch.load('../input/tile-open-slide-1/tile_224_resnxt_15_1.pth',map_location=gpu)\n#state_dict = torch.load('../input/tile-224-grp/tile_224_grp_norm_clean.pth',map_location=gpu)\n#state_dict = torch.load('../input/tile-224-grp/group_norm_2_best.pth',map_location=gpu)\n#state_dict = torch.load('../input/combine-tiles/open_slide_concat_20_2_tiles_bst8414.pth',map_location=gpu)\n#state_dict = torch.load('../input/resnext-ordinal1/open_slide_rexnext_ordinal.pth',map_location=gpu)\n#state_dict = torch.load('../input/resnext-ordinal1/bestmodel_868_36.pth',map_location=gpu)\n#state_dict1 = torch.load('../input/albu-resnext/bestmodel_36_87_albu.pth',map_location=gpu)\n#state_dicts.append(state_dict1)\n#state_dict2 = torch.load('../input/albu-resnext/bestmodel_36_87_albu.pth',map_location=gpu) bestmodel_49_albu_200_908\n#state_dict3 = torch.load('../input/albu-resnext/best_albu_256_36_8995.pth',map_location=gpu)\n#state_dicts.append(state_dict3)\n#state_dict2 = torch.load('../input/albu-resnext/resnext_albu_200_36_89_fold0.pth',map_location=gpu)\n#state_dict3=torch.load('../input/albu-resnext/best_model_albu_200_49_90_fold0.pth',map_location=gpu)\n##state_dicts.append(state_dict2)\n#state_dict4=torch.load('../input/albu-resnext/bestmodel_49_albu_200_908.pth',map_location=gpu)\n\n#state_dicts.append(state_dict4)\n\n#state_dict3=torch.load('../input/resnext-ord-opt/bestmodel_256_36_bal_902_fold1.pth',map_location=gpu)\n#state_dict3=torch.load('../input/psuedo/bestmodel_256_25_corr_bal_pseudo_937.pthh',map_location=gpu)\n#state_dict3=torch.load('../input/psuedo/bestmodel_256_36_bal_pseudo_corr_913_fold1.pth',map_location=gpu)\n#state_dicts.append(state_dict3)\n#bestmodel_256_36_bal_pseudo_corr_913_fold1 this excludes all harder Kas  only ra correction\n# ../input/psuedo/bestmodel_bal_psuedo_913_9033.pth-this excludes some of kas>2 remaining predictions corrected whose prediction is 0\n\n#bestmodel_256_36_bal_pseudo_corr_914_inc_ka exclude only >2 kas no correction 40\n#state_dict2=torch.load('../input/psuedo/bestmodel_bal_psuedo_913_9033.pth',map_location=gpu)\n#state_dicts.append(state_dict2)\n \n#state_dict3=torch.load('../input/psuedo/bestmodel_256_36_bal_pseudo_corr_914_inc_ka.pth',map_location=gpu)\n#state_dicts.append(state_dict3)\n\n#state_dict3=torch.load('../input/psuedo/bestmodel_36_bal_psuedo_ka_inc_raka99_corr61_fold1_it2_923.pth',map_location=gpu)\n#state_dicts.append(state_dict3)\n\n#state_dict4=torch.load('../input/psuedo/bestmodel_36_bal_psuedo_raka99_no_mask_69_fold1_it1_946.pth',map_location=gpu)\n#state_dict3=torch.load('../input/psuedo/bestmodel_256_36_bal_pseudo_corr_913_fold1.pth',map_location=gpu)\n#state_dicts.append(state_dict3)\n\nstate_dict3=torch.load('../input/psuedo/bestmodel_36_bal_psuedo_raka99_no_mask_69_fold1_it1_946.pth',map_location=gpu)\nstate_dicts.append(state_dict3)# ../input/psuedo/bestmodel_36_bal_psuedo_raka99_no_mask_69_fold1_it1_946.pth\n#state_dict4=torch.load('../input/psuedo/bestmodel_ka_99_ra_69_corr_934.pth',map_location=gpu)\n#state_dicts.append(state_dict4)\n\n\nstate_dict5=torch.load('../input/resnext-ord-opt/best_albu_256_36_bal_901_fold0.pth',map_location=gpu)\n\n#state_dict5=torch.load('../input/resnext-ord-opt/bestmodel_256_36_bal_902_fold1.pth',map_location=gpu)\n\nstate_dicts.append(state_dict5)\n \n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#state_dicts[1].values()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#from efficientnet_pytorch import EfficientNet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#learn.create_opt(lr=6e-4)\n#learn.model.load_state_dict(state_dict)\n\n#model.load_state_dict(state_dict )\n#model.half()\n#model.eval()\nprint('x')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#x=torch.randn(2,3,224,224)\n\n#model(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#torch.cuda.empty_cache()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"''' \nmodels = []\nMODELS=[model]\nfor _  in MODELS:\n    #state_dict = torch.load(path,map_location=torch.device('cpu'))\n    #model = Model()\n    #model.load_state_dict(state_dict)\n    model.float()\n    #model.eval()\n    model.cuda()\n    models.append(model)\n \n\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#torch.cuda.set_device(2)\n'''\nmodels = []\nmodel.eval()\nmodel.float()\nmodel.eval()\nmodel.cuda()\nmodels.append(model)\n#model.cuda()\n'''\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import openslide\nfrom openslide import OpenSlide, ImageSlide, OpenSlideError","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def aug_unit(x,fwd=True,mask=False):\n    return x\ndef aug_flipV(x,fwd=True,mask=False):\n    \n    if mask:\n        \n            \n            \n           \n        x=x.flip(-2)\n        #print('x1',x.size())\n    else:\n        #print('before',x.size())\n        \n        x=x.flip(-2)\n        #print('xfalse',x.size())\n        \n        \n    #return torch.transpose(x,1,2) if mask else torch.transpose(x,2,3)\n    return x\n    #return x.flip(2) if mask else x.flip(3)\n\ndef aug_flipH(x,fwd=True,mask=False):\n    return x.flip(-1) if mask else x.flip(-1)\n\ndef aug_T(x,fwd=True,mask=False):\n    if mask:\n        x=torch.transpose(x,2,3)\n        #print('x1',x.size())\n    else:\n        #print('before',x.size())\n        x=torch.transpose(x,2,3)\n        #print('xfalse',x.size())\n        \n        \n    #return torch.transpose(x,1,2) if mask else torch.transpose(x,2,3)\n    return x\n\ndef aug_rot_2(x,fwd=True,mask=False): #rotate pi/2\n    return aug_flipV(aug_flipH(x,fwd,mask),fwd,mask)\n\ndef aug_rot_4cr(x,fwd=True,mask=False): #rotate pi/4 counterclockwise\n    return aug_flipV(aug_T(x,fwd,mask),fwd,mask) if fwd else \\\n        aug_T(aug_flipV(x,fwd,mask),fwd,mask)\n\ndef aug_rot_4cw(x,fwd=True,mask=False): #rotate pi/4 clockwise\n    return aug_flipH(aug_T(x,fwd,mask),fwd,mask) if fwd else \\\n        aug_T(aug_flipH(x,fwd,mask),fwd,mask)\n\ndef aug_rot_2T(x,fwd=True,mask=False): #transpose and rotate pi/2\n    return aug_rot_2(aug_T(x,fwd,mask),fwd,mask)\n\n\ntrms_side_on = [aug_unit,aug_flipH]\ntrms_top_down = [aug_unit,aug_flipV]\n#trms_dihedral = [aug_unit,aug_flipH,aug_flipV,aug_T,aug_rot_2,aug_rot_2T,\n#                 aug_rot_4cw,aug_rot_4cr]\ntrms_dihedral = [aug_unit,aug_flipH,aug_flipV ,\n                 aug_rot_4cw,aug_rot_4cr]\n#trms_dihedral = [aug_unit,aug_flipH, aug_rot_4cw ]\n#trms_dihedral = [aug_unit,aug_rot_4cr ]\ntrms_side_on = [aug_unit,aug_flipH]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import albumentations as A\nimport cv2\nfrom albumentations import *\nfinal_aug=A.Compose([ \n    \n        #A.ElasticTransform(p=0.1),\n        #A.ImageCompression(p=0.3),\n        #A.ChannelDropout(p=0.3),\n        A.OneOf([\n            A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2,\n                               rotate_limit=15,\n                               border_mode=cv2.BORDER_CONSTANT, value=0,p=0.25),\n            \n            A.OpticalDistortion(distort_limit=0.11, shift_limit=0.15,\n                                border_mode=cv2.BORDER_CONSTANT,\n                                value=0,p=0.2),\n            A.NoOp()\n        ],p=1) \n        \n         ,\n        A.OneOf([\n            #A.RandomBrightnessContrast(brightness_limit=0.5,\n            #                           contrast_limit=0.4,p=0.3),\n            \n            A.RandomGamma(gamma_limit=(50, 150),p=0.4),\n            A.NoOp()\n        ],p=0.7),\n        \n       \n        A.OneOf([\n            A.MotionBlur(p =.2),\n            A.MedianBlur(blur_limit=3, p =.2),\n            A.Blur(blur_limit=3, p =.3), #0.3\n        ], p=.4),\n    \n        A.OneOf([\n            \n            A.CLAHE(p=0.63),#0.5\n            A.NoOp()\n        ] ,p=0.7) \n         ] ,p=1)\nprint('x')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mean_aug=torch.tensor(imagenet_stats[0])\nstd_aug=torch.tensor(imagenet_stats[1])\ndef pred_aug(x,model, aug=[aug_unit],aug_albu=[] ):\n    pred = []\n    #print(torch.transpose(x,1,3).shape)\n    #x=exposure.rescale_intensity(torch.transpose(x,1,3), in_range=(0, 150))\n    #print(x.shape)\n    for aug_cur in aug:\n        #py = to_np(aug_cur(torch.sigmoid(learn.model(aug_cur(x))),\n                           #fwd=False, mask=True))\n        #py =aug_cur(torch.sigmoid(learn.model(aug_cur(x))),\n                           #fwd=False, mask=True) \n        aug_pred= model(aug_cur(x))\n        if type(aug_pred)==list:\n            aug_pred=torch.sigmoid(aug_pred[0])\n        else:\n            aug_pred=torch.sigmoid(aug_pred)\n        py =aug_pred\n        #torch.softmax(aug_cur(aug_pred),dim=1)\n                           #fwd=False, mask=True) \n        #print(py.size())\n        #pred.append(torch.sqrt(py))\n        pred.append(py)\n    #pred = np.stack(pred, axis=0).mean(axis=0)\n    pred = torch.stack(pred).mean(dim=0)\n    pred_albu=[]\n    \n    if len(aug_albu)!=0:\n        x=x.cpu()\n        for _ in range(3):\n            \n            \n            #print(x.size(),mean_aug[...,None,None].size())\n            #print('x',x.size(),x.data.size())\n            x_rec=  mean_aug[...,None,None]+x*std_aug[...,None,None]\n            x_rec=x_rec*255.\n            \n            x_rec=x_rec.permute(0,3,2,1)\n            \n            im_out=[aug_albu[0](image=np.asarray(im_out).astype('uint8'))['image'] for im_out in x_rec]\n            img=[pil2tensor(PIL.Image.fromarray(im_out), np.float32).div_(255) for im_out in im_out]\n            #print(torch.stack(img).size())\n            img=(torch.stack(img)-mean_aug[...,None,None])/std_aug[...,None,None]\n            pred_a=model(img.cuda())\n            pred_albu.append(torch.sigmoid(pred_a))\n        pred_a = torch.stack(pred_albu).mean(dim=0) \n        pred=torch.stack([pred,pred_a]).mean(dim=0)\n    return pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imagenet_stats=( [1-0.81904339, 1-0.62563849,1- 0.75185707] ,  [0.39888887, 0.50950183 ,0.41362463])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mean_aug=torch.tensor(imagenet_stats[0])\nstd_aug=torch.tensor(imagenet_stats[1])\n\ndef pred_albu_aug(x,model,aug_albu=[]):\n    pred_albu=[]\n    if len(aug_albu)!=0:\n        x=x.cpu()\n        for _ in range(6):\n            \n            \n            #print(x.size(),mean_aug[...,None,None].size())\n            #print('x',x.size(),x.data.size())\n            x_rec=  mean_aug[...,None,None]+x*std_aug[...,None,None]\n            x_rec=x_rec*255.\n            \n            x_rec=x_rec.permute(0,3,2,1)\n            \n            im_out=[aug_albu[0](image=np.asarray(im_out).astype('uint8'))['image'] for im_out in x_rec]\n            img=[pil2tensor(PIL.Image.fromarray(im_out), np.float32).div_(255) for im_out in im_out]\n            #print(torch.stack(img).size())\n            img=(torch.stack(img)-mean_aug[...,None,None])/std_aug[...,None,None]\n            pred_a=model(img.cuda())\n            pred_albu.append(torch.sigmoid(pred_a))\n        pred_a = torch.stack(pred_albu).mean(dim=0) \n        \n        return pred_a\n    return None ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#test_in.permute(0,3,2,1)*mean_aug","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#cnt=30\n#N=40\n#start=0 .permute(1,2,0)\n#tile_list=np.arange(np.clip(start,start,cnt-N) ,np.clip(start,start,cnt-N)+N )\n#tile_list \n'''\nmodel=Resnext_tile(n=6)\nmodel.load_state_dict(state_dicts[0])\ntest_in=torch.randn(2,3,128,128)\npred_aug(test_in,model,trms_dihedral,[final_aug])\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#x=torch.randn(2,3,128,128).cuda()\n\n#p=[x.permute(2,1,0) for x in x.cpu()]\n#torch.stack(p).permute(0,3,1,2).size()\n\n#torch.stack,\n#mean=torch.tensor( [1-0.82996891, 1-0.64982929, 1-0.76662381])\n#mean[...,None,None].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_path = '../input/prostate-cancer-grade-assessment/train_images/'\nimport warnings\nimport skimage.io\ndef read_image(image_path, resize_ratio=1):\n    \n    if not(isinstance(image_path, str)):\n        # if tensor with byte string\n        image_path = image_path.numpy().decode('utf-8')\n        \n    image_level_1 = skimage.io.MultiImage(image_path)[1]\n    \n    if resize_ratio != 1:\n        new_w = int(image_level_1.shape[1]*resize_ratio)\n        new_h = int(image_level_1.shape[0]*resize_ratio)\n        image_level_1 = cv2.resize(\n            image_level_1, (new_w, new_h), interpolation=cv2.INTER_AREA)\n    \n    return image_level_1\n\n#image = read_image(input_path + data.image_id[0] + '.tiff')\ndef _mask_tissue(image, kernel_size=(5, 5), gray_threshold=220):\n    \"\"\"Masks tissue in image. Uses gray-scaled image, as well as\n    dilation kernels and 'gap filling'\n    \"\"\"\n    # Define elliptic kernel\n    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, kernel_size)\n    # Convert rgb to gray scale for easier masking\n    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n    # Now mask the gray-scaled image (capturing tissue in biopsy)\n    mask = np.where(gray < gray_threshold, 1, 0).astype(np.uint8)\n    # Use dilation and findContours to fill in gaps/holes in masked tissue\n    mask = cv2.dilate(mask, kernel, iterations=1)\n    contour, _ = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)\n    for cnt in contour:\n        cv2.drawContours(mask, [cnt], 0, 1, -1)\n    return mask\n\n\ndef _pad_image(image, pad_len, pad_val):\n    \"\"\"Pads inputted image, accepts both \n    2-d (mask) and 3-d (rgb image) arrays\n    \"\"\"\n    if image is None:\n        return None\n    elif image.ndim == 2:\n        return np.pad(\n            image, ((pad_len, pad_len), (pad_len, pad_len)), pad_val)\n    elif image.ndim == 3:\n        return np.pad(\n            image, ((pad_len, pad_len), (pad_len, pad_len), (0, 0)), pad_val)\n    return None\n\ndef _transpose_image(image):\n    \"\"\"Inputs an image and transposes it, accepts \n    both 2-d (mask) and 3-d (rgb image) arrays\n    \"\"\"\n    if image is None:\n        return None\n    elif image.ndim == 2:\n        return np.transpose(image, (1, 0)).copy()\n    elif image.ndim == 3:\n        return np.transpose(image, (1, 0, 2)).copy()\n    return None\n\ndef _get_tissue_parts_indices(tissue, min_consec_info):\n    \"\"\"If there are multiple tissue parts in 'tissue', 'tissue' will be \n    split it. Each tissue part will be taken care of separately, and if \n    the tissue part is less than min_consec_info, it's considered to small\n    and won't be returned.\n    \"\"\"\n    split_points = np.where(np.diff(tissue) != 1)[0]+1\n    tissue_parts = np.split(tissue, split_points)\n    return [\n        tp for tp in tissue_parts if len(tp) >= min_consec_info\n    ]\n\ndef _get_tissue_subparts_coords(subtissue, patch_size, min_decimal_keep):\n    \"\"\"Inputs a tissue part resulting from '_get_tissue_parts_indices'.\n    This tissue part is divided into N subparts and returned.\n    Argument min_decimal_keep basically decides if we should squeeze in the \n    N subparts in an area bigger than the sum of the N subparts or not.\n    \"\"\"\n    start, end = subtissue[0], subtissue[-1]\n    num_subparts = (end-start)/patch_size\n    if num_subparts % 1 < min_decimal_keep and num_subparts >= 1:\n        num_subparts = math.floor(num_subparts)\n    else:\n        num_subparts = math.ceil(num_subparts)\n\n    excess = (num_subparts*patch_size) - (end-start)\n    shift = excess // 2\n\n    return [\n        i * patch_size + start - shift \n        for i in range(num_subparts)\n    ]\n\ndef _eval_and_append_xy_coords(coords,\n                               image, \n                               mask, \n                               patch_size, \n                               x, y, \n                               min_patch_info,\n                               transposed,\n                               precompute):\n    \"\"\"Based on computed x and y coordinates of patch: \n    slices out patch from original image, flattens it,\n    preprocesses it, and finally evaluates its mask.\n    If patch contains more info than min_patch_info,\n    the patch coordinates are kept, along with a \n    value 'val1' that estimates how much information\n    there is in the patch.\n    \"\"\"\n    patch_1d = (\n        image[y: y+patch_size, x:x+patch_size, :]\n        .mean(axis=2)\n        .reshape(-1)\n    )\n    idx_tissue = np.where(patch_1d <= 220)[0]#210\n    idx_black = np.where(patch_1d < 5)[0]\n    idx_background = np.where(patch_1d > 220)[0]#210\n\n    if len(idx_tissue) > 0:\n        patch_1d[idx_black] = 220#210\n        patch_1d[idx_background] = 220#210\n        val1 = int(patch_1d.mean())\n        val2 = mask[y:y+patch_size, x:x+patch_size].mean()\n        if val2 > min_patch_info:\n            if precompute:\n                if transposed:\n                    coords = np.concatenate([\n                        coords, [[val1, x-patch_size, y-patch_size]]\n                    ])\n                else:\n                    coords = np.concatenate([\n                        coords, [[val1, y-patch_size, x-patch_size]]\n                    ])\n            else:\n                coords = np.concatenate([\n                    coords, [[val1, y, x]]\n                ])\n               \n    return coords\n\ndef compute_coords(image,\n                   patch_size=256,\n                   precompute=False,\n                   min_patch_info=0.15,\n                   min_axis_info=0.15,\n                   min_consec_axis_info=0.15,\n                   min_decimal_keep=0.7):\n\n    \"\"\"\n    Input:\n        image : 3-d np.ndarray\n        patch_size : size of patches/tiles, will be of \n            size (patch_size x patch_size x 3)\n        precompute : If True, only coordinates will be returned,\n            these coordinates match the inputted 'original' image.\n            If False, both an image and coordinates will be returned,\n            the coordinates does not match the inputted image but the\n            image that it is returned with.\n        min_patch_info : Minimum required information in patch\n            (see '_eval_and_append_xy_coords')\n        min_axis_info : Minimum fraction of on-bits in x/y dimension to be \n            considered enough information. For x, this would be fraction of \n            on-bits in x-dimension of a y:y+patch_size slice. For y, this would \n            be the fraction of on-bits for the whole image in y-dimension\n        min_consec_axis_info : Minimum consecutive x/y on-bits from 'min_axis_info'\n            (see '_get_tissue_parts_indices')\n        min_decimal_keep : Threshold for decimal point for removing \"excessive\" patch\n            (see '_get_tissue_subparts_coords')\n    \n    Output:\n        image [only if precompute is True] : similar to input image, but fits \n            to the computed coordinates\n        coords : the coordinates that will be used to compute the patches later on\n    \"\"\"\n    \n    \n    if type(image) != np.ndarray:\n        # if image is a Tensor\n        image = image.numpy()\n    \n    # masked tissue will be used to compute the coordinates\n    mask = _mask_tissue(image)\n\n    # initialize coordinate accumulator\n    coords = np.zeros([0, 3], dtype=int)\n\n    # pad image and mask to make sure no tissue is potentially missed out\n    image = _pad_image(image, patch_size, 'maximum')\n    mask = _pad_image(mask, patch_size, 'minimum')\n    \n    y_sum = mask.sum(axis=1)\n    x_sum = mask.sum(axis=0)\n    # if on bits in x_sum is greater than in y_sum, the tissue is\n    # likely aligned horizontally. The algorithm works better if\n    # the image is aligned vertically, thus the image will be transposed\n    if len(np.where(x_sum > 0)[0]) > len(np.where(y_sum > 0)[0]):\n        image = _transpose_image(image)\n        mask = _transpose_image(mask)\n        y_sum, _ = x_sum, y_sum\n        transposed = True\n    else:\n        transposed = False\n    \n    # where y_sum is more than the minimum number of on-bits\n    y_tissue = np.where(y_sum >= (patch_size*min_axis_info))[0]\n    \n    if len(y_tissue) < 1:\n        warnings.warn(\"Not enough tissue in image (y-dim)\", RuntimeWarning)\n        if precompute: return [(0, 0, 0)]\n        else: return image, [(0, 0, 0)]\n    \n    y_tissue_parts_indices = _get_tissue_parts_indices(\n        y_tissue, patch_size*min_consec_axis_info)\n    \n    if len(y_tissue_parts_indices) < 1: \n        warnings.warn(\"Not enough tissue in image (y-dim)\", RuntimeWarning)\n        if precompute: return [(0, 0, 0)]\n        else: return image, [(0, 0, 0)]\n    \n    # loop over the tissues in y-dimension\n    for yidx in y_tissue_parts_indices:\n        y_tissue_subparts_coords = _get_tissue_subparts_coords(\n            yidx, patch_size, min_decimal_keep)\n        \n        for y in y_tissue_subparts_coords:\n            # in y_slice, where x_slice_sum is more than the minimum number of on-bits\n            x_slice_sum = mask[y:y+patch_size, :].sum(axis=0)\n            x_tissue = np.where(x_slice_sum >= (patch_size*min_axis_info))[0]\n            \n            x_tissue_parts_indices = _get_tissue_parts_indices(\n                x_tissue, patch_size*min_consec_axis_info)\n            \n            # loop over tissues in x-dimension (inside y_slice 'y:y+patch_size')\n            for xidx in x_tissue_parts_indices:\n                x_tissue_subparts_coords = _get_tissue_subparts_coords(\n                    xidx, patch_size, min_decimal_keep)\n                \n                for x in x_tissue_subparts_coords:\n                    coords = _eval_and_append_xy_coords(\n                        coords, image, mask, patch_size, x, y, \n                        min_patch_info, transposed, precompute\n                    )     \n    \n    if len(coords) < 1:\n        warnings.warn(\"Not enough tissue in image (x-dim)\", RuntimeWarning)\n        if precompute: return [(0, 0, 0)]\n        else: return image, [(0, 0, 0)]\n    \n    if precompute: return coords\n    else: return image, coords\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"N=49\nNg=7\ncrop_size=280\npatch_size=280\ncrop_level=1\ndown_samples={ 0 :1, 1 :4, 2 :16}\ng=0\ndef split_image(img):\n    \"\"\"\n    Splits the given image into multiple images if 256x256\n    \"\"\"\n    #print(data_dir, name+'.tiff')\n    #openslide_image = openslide.OpenSlide(os.path.join(data_dir, name+'.tiff'))\n    #width, height = openslide_image.level_dimensions[0]\n    #print(width,height)\n    # Get the size of the given image\n    width, height = img.level_dimensions[crop_level]\n    #print(width,height)\n    # Get the dimensions of level 0 resolution, as it's required in \"read_region()\" function\n    #base_height = down_samples[crop_level] * height  # height of level 0\n    #base_width = down_samples[crop_level] * width  # width of level 0\n\n    # Get the number of smaller images \n    h_crops = math.ceil(width / crop_size)\n    v_crops = math.ceil(height / crop_size)\n\n    splits = []\n    for v in range(v_crops):\n        for h in range(h_crops): \n            #x_location = h*crop_size*down_samples[crop_level]\n            #y_location = v*crop_size*down_samples[crop_level]\n            x_location = h*crop_size*down_samples[crop_level] \n            y_location = v*crop_size*down_samples[crop_level] \n            #x_location = h*crop_size+crop_size*2\n            #y_location = v*crop_size+crop_size*2\n            patch = img.read_region((x_location, y_location), crop_level, (crop_size, crop_size))\n\n            splits.append(patch)\n    return splits ,width*height \n\ndef split_image_coord(img,coords):\n    \"\"\"\n    Splits the given image into multiple images if 256x256\n    \"\"\"\n    #print(data_dir, name+'.tiff')\n    #openslide_image = openslide.OpenSlide(os.path.join(data_dir, name+'.tiff'))\n    #width, height = openslide_image.level_dimensions[0]\n    #print(width,height)\n    # Get the size of the given image\n    width, height = img.level_dimensions[crop_level]\n    #print(width,height)\n    # Get the dimensions of level 0 resolution, as it's required in \"read_region()\" function\n    #base_height = down_samples[crop_level] * height  # height of level 0\n    #base_width = down_samples[crop_level] * width  # width of level 0\n\n    # Get the number of smaller images \n    #h_crops = math.ceil(width / crop_size)\n    #v_crops = math.ceil(height / crop_size)\n\n    splits = []\n    #for v in range(v_crops):\n    for v,y,x in coords: \n            x_location = int(x*down_samples[crop_level])\n            y_location = int(y*down_samples[crop_level])\n            \n            #end_point = (x+patch_size, y+patch_size)\n            #x_location = h*crop_size*down_samples[crop_level]+ down_samples[crop_level]*128\n            #y_location = v*crop_size*down_samples[crop_level]+ down_samples[crop_level]*128\n            #x_location = h*crop_size+crop_size*2\n            #y_location = v*crop_size+crop_size*2\n            patch = img.read_region((x_location, y_location), crop_level, (crop_size, crop_size))\n\n            splits.append(patch)\n    return splits ,width*height\n\n \ndef get_emptiness(arr):\n    total_ele = arr.size\n    white_ele = np.count_nonzero(arr > 219) + np.count_nonzero(arr == 0)\n    #print(white_ele/total_ele)\n    return white_ele / total_ele\n\n \n\n\ndef select_best_images(images,sel_N):\n    #s#el_N=N+32\n    #coll=[np.asarray(img)[...,:3] for img in crops]\n    #images=[np.asarray(img)[...,:3] for img in images]\n    #images_flat=np.asarray(images).reshape(len(images),-1) \n    non_empty_crops = []\n   \n    empty_score=[]\n    sum_score=[]\n    if len(images)<N:\n        sel_N=len(images)\n    #idxs =  np.argsort( images_flat.sum(-1)[::-1])[:sel_N]\n    #np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n    #\n    #print(idxs)\n    #images=np.asarray(images)[idxs]    \n    #print(len(images))\n    for i,image in enumerate(images):\n        image_arr = np.array(image)[...,:3]\n        emptiness=get_emptiness(image_arr)\n        #img_sum = image_arr.sum()\n        #get_emptiness(image_arr)\n        #if  emptiness >0.8:\n        #    continue\n        #sum_score.append(img_sum)\n        empty_score.append(emptiness)\n        non_empty_crops.append(image)\n        \n        #non_empty_crops.append({ 'id':i,'score':emptiness, 'img':image})\n        #print(images[i].shape)\n        \n            #continue\n        \n        #non_empty_crops=[sorted(d.items(), key=lambda x: x[1]) for d in non_empty_crops ]\n        #non_empty_crops.append(Image.fromarray( images[i]))\n    #print(len(empty_score))\n    \n    ids=np.argsort(empty_score)[:sel_N]\n    #ids=np.argsort(sum_score)[:sel_N]\n    non_empty_crops=[np.asarray(non_empty_crops[i])[...,:3] for i in ids  ]\n    #non_empty_crops=[non_empty_crops[i] for i in ids if empty_score[i]<0.88   ]\n    #print(len(non_empty_crops))\n    return non_empty_crops\n    \n    #print(len(non_empty_crops))\n     \n\ndef equalize_light(image, limit=3, grid=(7,7), gray=False):\n    if (len(image.shape) == 2):\n        image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)\n        gray = True\n    \n    clahe = cv2.createCLAHE(clipLimit=limit, tileGridSize=grid)\n    lab = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)\n    l, a, b = cv2.split(lab)\n\n    cl = clahe.apply(l)\n    limg = cv2.merge((cl,a,b))\n\n    image = cv2.cvtColor(limg, cv2.COLOR_YUV2RGB)\n    if gray: \n        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\n    return np.uint8(image) \n\ndef open_im(fn): #a function that reads RGBY image\n    #print(fn)\n    combine_tensor_list=[]\n    \n    image = read_image(fn + '.tiff')\n    coords = compute_coords(image,precompute=True,patch_size=patch_size)\n    coords = sorted(coords, key= lambda x: x[0], reverse=False)\n     \n    fname=Path(fn).stem +'.png'\n    #cnt=count_dict[fname]\n    #print(fname)\n    image = openslide.OpenSlide(fn+'.tiff') #MASKS\n       \n    crops,hw_product = split_image_coord(image,coords)\n    #if hw_product>4200*2200 :# 25 quantile for height and width\n    ''' \n    if (hw_product<5000*5000):\n        sel_n=9\n    elif( hw_product>5000*5000) :\n        sel_n=18\n    '''\n    if (hw_product<5000*5000):\n        sel_n=36\n    elif( hw_product>5000*5000) and (hw_product<9000*9000):\n        sel_n=49\n    else:\n        sel_n=64\n     \n    non_empty_crops =  select_best_images(crops,sel_n)\n    \n    cnt=len(non_empty_crops)\n    \n    #cnt=count_dict[fname]\n    #start=  np.random.randint(0,cnt+1)\n    #g=random.sample(range(math.ceil(cnt/N)),1)[0] # select only fixed slots\n        #0,1,2 ->36 0,1,2,3->64\n    #print('g',g)\n    start=g*N \n    #start=0\n\n    tile_list=np.arange(np.clip(start,start,cnt-N) ,np.clip(start,start,cnt-N)+N )\n    \n    if len(np.where(tile_list<0)[0].tolist())>0:\n        \n            \n        tile_list=tile_list[len(np.where(tile_list<0)[0].tolist()):].tolist()\n            \n        '''      \n        while len(tile_list)<N:\n            \n            tile_list=tile_list*2\n        tile_list=tile_list[:N]\n        '''\n    #print(len(tile_list))\n    \n    \n    #fnames = [Path(PATH_PRE+'/'+str(fname).split('.')[0]+'_'+str(i)+'.png') for i in tile_list]\n    imgs=[non_empty_crops[i] for i in tile_list]\n        #print(non_empty_crops[0].shape)\n    #print(type(imgs[0]))    \n    #imgs=[PIL.Image.open(fname) for fname in fnames]\n    im_out = PIL.Image.new('RGB', (sz*Ng,sz*Ng))\n    x=0\n    y=0\n    for i,img in enumerate(imgs):\n        if i%Ng==0 and i >0 :\n            #print(i)\n            y=0\n            x=x+sz\n        elif i>0:\n            y=y+sz\n             \n     \n        im_out.paste(PIL.Image.fromarray(img), (y,x))\n    #im_out=equalize_light(np.asarray(im_out))\n    #\n    if albu_iter=='Y':\n        \n        img=pil2tensor(im_out, np.float32).div_(255)\n    else:\n        im_out=final_aug(image=np.asarray(im_out))['image'] #albumnetations\n        \n        \n        img=pil2tensor(PIL.Image.fromarray(im_out), np.float32).div_(255)\n        \n    return Image(img)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#data.show_batch(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class CustomImageList(ImageList):\n    def open(self,fn):\n        return open_im(fn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#data.show_batch(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def model_pred_mult(learns, \n        ds_type:DatasetType=DatasetType.Test, \n        tta:bool=True,aug=None): #if use train dl, disable shuffling\n    \n    for learn in learns: \n        learn.model.eval();\n    \n    dl = learn.data.dl(ds_type)\n    \n    predictions=[]\n    \n    print(len(learns))\n    #sampler = dl.batch_sampler.sampler\n    #dl.batch_sampler.sampler = torch.utils.data.sampler.SequentialSampler(sampler.data_source)\n    #name_list = [Path(n).stem for n in dl.dataset.items]\n    num_batchs = len(dl)\n    t = progress_bar(iter(dl), leave=False, total=num_batchs)\n    count = 0\n    #wt_dict={'1':0.25,'2':0.25,'3':0.50}\n    wt_dict={'1':0.45,'2':0.55 }\n    with torch.no_grad():\n        for x,y in t:\n            #print(len(x),type(x),x[0].shape,x[0].size())\n            #x = [x.cuda() for x in x]\n            x=x.cuda()\n            #print(x.size())\n            preds = []\n            for i,learn in enumerate(learns):\n                #i, hights, widths, classes\n                \n                if tta:\n                    #you can comment some transfromations to save time\n                    py = pred_aug(x,learn.model,aug,aug_albu=[final_aug]).detach()\n                    \n                    \n                else:\n                    py = (learn.model(x)).detach()\n                    py=torch.sigmoid(py)\n                    if albu_iter =='Y':\n                        \n                        preds_albu=pred_albu_aug(x,learn.model,aug_albu=[final_aug]).detach()\n                        py=torch.stack([py,preds_albu]).mean(0)\n                    \n                if len(learns)>1:\n                    \n                    preds.append(py)\n                    #preds.append(py*wts[i])\n                else :\n                    preds.append( py)\n            #py = torch.stack([preds[0]*wt_dict['1'],preds[1]*wt_dict['2'] ]).sum(0).cpu()\n            py = torch.stack([preds[0]*wt_dict['1'],preds[1]*wt_dict['2']] ).sum(0).cpu()\n            #torch.stack(preds).mean(0).cpu()#.numpy() learns means\n            #py=(py>0.5).sum(-1)-1\n            \n                \n            predictions.append(py)\n            \n          \n            \n    return torch.cat(predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#imagenet_stats=( [1-0.8752738, 1-0.76093115, 1-0.83536946] , [0.42382025, 0.52960518, 0.44709087])\n#imagenet_stats=( [1-0.82145999, 1-0.63085968,1- 0.7553875] ,  [0.39911893, 0.51142571 ,0.41508214])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#models=[model]*2\n\n#models[0].load_state_dict(state_dicts[0])\n#models.append(model)\n#models[1].load_state_dict(state_dicts[1])\n#models.append(model)\n              ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#state_dicts[1]\n#models[1].state_dict()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#models[0].state_dict().values()  #-3.2158e-03,  1.5717e-02,  1.0086e-02","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_models(state_dicts,test_df,path,tfm):\n    learns=[]\n    \n    for st_dict in state_dicts:\n        model=Resnext_tile(n=6)\n        \n        learn2 = Learner(data, \n                    model, \n                    model_dir='.',\n                    #loss_func=loss_func,#,\n                    #opt_func=Over9000,\n                    #metrics = [qk,r2_score,exp_rmspe],\n             #[accuracy,kappa,precision,recall],\n                     #[qk,r2_score,exp_rmspe], learn.create_opt(lr=6e-4) tile-open-slide-1'\n                    path='../input/open-slide-24') \n\n        learn2.data.add_test(CustomImageList.from_df( test_df,path))\n        #orig_test_tfms=learn2.data.test_ds.tfms.copy()\n        #learn2.data.test_ds.tfms=learn2.data.test_ds.tfms+tfm\n        if len(tfm)!=0:\n            \n            learn2.data.test_ds.tfms=tfm\n            \n        learn2.model.load_state_dict(st_dict)\n        learn2.model.float()\n        learn2.model.eval()\n        learn2.model.cuda()\n        learns.append(learn2)\n    return learns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#learns=get_models(state_dicts[0])\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=10, max_zoom=1.2, \n                          p_lighting=0.1,max_warp=0.1 ,#,\n                     #xtra_tfms=[ contrast(scale=(0.5, 1.5), p=0.75)]\n                     )\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sz=crop_size\nbs=6\n\nprint(Ng,N,sz,crop_size)\n \n \nif val=='N':\n    \n    test_df=pd.read_csv(TEST)\n    test_path=Path('../input/prostate-cancer-grade-assessment/test_images')\nelse:\n    \n    \n    test_path=Path('../input/prostate-cancer-grade-assessment/train_images')\n    train_csv='/train.csv'\n    \n    df=pd.read_csv(path+train_csv)\n    test_df=df[0:100]\n \nif os.path.exists(DATA):\n    tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=12, max_zoom=1.2, \n                          p_lighting=0.1,max_warp=0.1 ,#,\n                     #xtra_tfms=[ contrast(scale=(0.5, 1.5), p=0.75)]\n                     )\n    print('x')\n    train_csv='/train.csv'\n    \n    df=pd.read_csv(path+train_csv)\n    df['isup_grade']=0. \n#path=''\n    train_folder='train_images'\n    src = (\n        CustomImageList.from_df(df,path ,cols='image_id',folder= train_folder,suffix=None)\n            .no_split()\n            .label_from_df(cols= 'isup_grade')\n        )\n    \n    #\n    data = (\n        src.transform(tfms,size=(sz,sz),resize_method =ResizeMethod.NO,padding_mode='zeros')\n        .databunch(bs=bs)\n        .normalize(imagenet_stats)\n    )\n    zoom1 =RandTransform(tfm=TfmAffine (zoom), \n                            kwargs={'scale': (1.0, 1.2), 'row_pct': (0, 1), 'col_pct': (0, 1)}, \n                            p=0.95, resolved={'scale': 1.0160933028189043, 'row_pct': 0.36382384205587703, \n                                             'col_pct': 0.2180587035012601}, \n                            do_run=True, is_random=True, use_on_y=True)\n\n        #brightness= RandTransform(tfm=TfmLighting (brightness), kwargs={'change': (0.45, 0.55)}, \n        #             p=0.1, resolved={}, \n        #             do_run=True, is_random=True, use_on_y=True)\n\n    brightness1=RandTransform(tfm=TfmLighting (brightness), kwargs={'change': (0.4, 0.6)}, p=0.10, \n                                 resolved={}, do_run=True, is_random=True, use_on_y=True)\n        #contrast = RandTransform(tfm=TfmLighting (contrast), kwargs={'scale':  (0.9, 1.11)}, p=0.1, resolved={}, \n        #                         do_run=True, is_random=True, use_on_y=True)\n        #warp=tfms[0][2]\n    contrast1=   RandTransform(tfm=TfmLighting (contrast), kwargs={'scale': (0.8, 1.25)}, \n                      p=0.10, resolved={},\n                      do_run=True, is_random=True, use_on_y=True)\n\n    rotate1=     RandTransform(tfm=TfmAffine (rotate), kwargs={'degrees': (-15, 15)}, p=0.6, \n                                  resolved={'degrees': 7.534146769796671}, \n                             do_run=True, is_random=True, use_on_y=True)\n    \n        \n    \n    if test=='Y'or val=='Y':\n        #test_folder= 'train_images'\n        TEST_DATA = test_path\n        tfm= data.train_ds.tfms\n        #[zoom1] +[brightness1 ]+[contrast1] \n        learns=get_models(state_dicts,test_df,TEST_DATA,tfm)\n        g=0\n        preds1=model_pred_mult(learns,aug=trms_dihedral,tta=False)\n        \n        \n        g=1\n        #tfm= [contrast1]\n        #tfm=data.test_ds.tfms\n        learns=get_models(state_dicts,test_df,TEST_DATA,tfm)\n        preds2=model_pred_mult(learns,aug=trms_dihedral,tta=False)\n        \n    \n         \n        #df[0:100]\n        #pd.read_csv(TEST)\n    #preds=torch.stack([preds1,preds2]).mean(0) \n    preds=torch.stack([preds1*0.45,preds2*0.55]).sum(0) \n       \n   \n    preds=(preds>0.5).sum(-1)-1\n    preds=preds.numpy()\n    '''\n   \n    #learn2.model.load_state_dict(state_dict)  #tile_224_resnxt_15_1\n  \n    #preds1=learn2.get_preds(ds_type=learn2.data.train_ds,with_loss=False)\n    #preds2=learn2.get_preds(ds_type=learn2.data.train_ds,with_loss=False)\n   \n    #preds_numpy=np.concatenate([preds1_numpy,preds2_numpy],1).mean(1)\n    names=[Path(n).stem  for n in learns[0].data.test_dl.dataset.items]\n    #final_preds= \n    '''\n    #preds=np.concatenate(preds_numpy).astype(int)\n    names=[Path(n).stem  for n in learns[0].data.test_dl.dataset.items]\n    preds=preds.astype(int)\n    sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds})\n    sub_df.to_csv('submission.csv', index=False)\n    \n    print('x')\nelse:\n    sub_smple_df = pd.read_csv(SAMPLE)\n    sub_smple_df.to_csv(\"submission.csv\", index=False)\n    print('empty',sub_smple_df.head())\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#data.train_ds.tfms\n#learns[0].model.state_dict().values() mix nets,psuedo labelling with best model difference of 2,1024\n#preds=preds.cpu().numpy()\n#names=[Path(n).stem  for n in learns[0].data.test_dl.dataset.items]\n#preds=preds.astype(int)\n#sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#preds.shape\n#sub_df\n#preds=model_pred_mult(learns,aug=trms_dihedral)\n#names=[Path(n).stem  for n in learn2.data.test_dl.dataset.items]\n    #final_preds= \n\n    #preds=np.concatenate(preds_numpy).astype(int)\n#sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds_numpy})\n#data.train_ds[0][0]\n#preds1","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#preds_numpy.dtype\n#sub_df\n#learn2.data.train_ds.tfms\n'''\npreds=torch.stack([preds1*0.45 ,preds2*0.55 ]).sum(0)\npreds=(preds>0.5).sum(-1)-1\npreds=preds.numpy()\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#RandTransform(tfm=TfmLighting (brightness), kwargs={'change': (0.25, 0.75)}, p=0.75, resolved={'change': 0.4072242732642682}, do_run=False, is_random=True, use_on_y=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import gc\ntorch.cuda.empty_cache()\ngc.collect()\n#del zoom","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#x,y=next(iter(learn2.data.test_dl))\n#learn2.model.cpu()(x.cpu())\ndf=pd.read_csv(path+train_csv)\ndf[0:100].isup_grade.value_counts()\n#learn2.data.train_ds.tfms","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#preds=preds.astype(int)\n#sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds})\nsub_df.isup_grade.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()\n'''\n1    31\n0    22\n2    13\n5    12\n3    12\n4    10\n\n1    28\n0    22\n2    16\n3    13\n5    12\n4     9\n\n1    30\n0    22\n2    15\n4    12\n5    11\n3    10\n\n1    28\n0    22\n2    17\n5    11\n4    11\n3    11\n\n1    31\n0    22\n2    14\n3    12\n5    11\n4    10\n\n1    27\n0    22\n2    15\n3    13\n4    12\n5    11\n\n1    28\n0    22\n2    16\n4    12\n5    11\n3    11\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()\n#!nvidia-smi\n'''\n1    32\n0    22\n4    12\n3    12\n5    11\n2    11\nName: isu\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#final_preds\n#panda=pd.read_csv(path+train_csv)[0:600]\n#panda.isup_grade.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#preds_numpy = (preds[0]).numpy()\n#names=[Path(n).stem  for n in learn2.data.train_dl.dataset.items]\n#final_preds=OptimizedRounder().predict(preds_numpy , coefficients)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#df \n#preds[0:5]\n#sub_df\n#df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#len(models)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Prediction","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#p.size()\n#p.view(6,8*len(models),-1).mean(1).size()\nimport openslide\nfrom openslide import OpenSlide, ImageSlide, OpenSlideError","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#len(models)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#OptimizedRounder().predict(preds_numpy , optR.coefficients())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_smple_df = pd.read_csv(SAMPLE)\n\n'''\nbs=6\nmodel.training=False\ncoefficients = [0.52619428, 1.57791494 ,2.50821417, 3.1141565, 4.70752549]\nif os.path.exists(DATA):\n    ds = PandaDataset(DATA,TEST)\n    dl = DataLoader(ds, batch_size=bs, num_workers=6, shuffle=False)\n    names,preds = [],[]\n\n    with torch.no_grad():\n        \n        for x,y in tqdm(dl):\n            x = x.cuda()\n            bs=x.size(0)\n            #dihedral TTA\n            x = torch.stack([x,x.flip(-1),x.flip(-2),x.flip(-1,-2),\n              x.transpose(-1,-2),x.transpose(-1,-2).flip(-1),\n              x.transpose(-1,-2).flip(-2),x.transpose(-1,-2).flip(-1,-2)],1)\n            x = x.view(-1,N,3,sz,sz)\n            #print(x.size())\n            p = [model(x) for model in models]\n            #p=model.half().cuda()(x.half())\n            p = torch.stack(p,1)\n            #print(p.size())\n            \n            p = p.view(bs,8*len(models),-1).mean(1).cpu()\n            #print('p',p.size(),p)\n            names.append(y)\n            preds.append(p)\n           \n        preds = torch.cat(preds).numpy()\n        \n        names = np.concatenate(names)\n        final_preds = OptimizedRounder().predict(preds , coefficients)\n \n        preds=np.concatenate(final_preds).astype('int')\n        sub_df = pd.DataFrame({'image_id': names, 'isup_grade': preds})\n        sub_df.to_csv('submission.csv', index=False)\nelse:\n    sub_smple_df.to_csv(\"submission.csv\", index=False)\n    print('empty',sub_smple_df.head())\n\n\n#sub_df.head()\n    \n    \n'''    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#preds[1].size()\n#preds[0:5]\n#sub_df.head()\n#preds","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#preds = torch.cat(preds).numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df = pd.read_csv(TEST)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#torch.cat(preds).numpy().shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#optimized_rounder.fit(preds, valid_labels)\n\n#optimized_rounder.coefficients()\n#preds=torch.cat(preds ).numpy()\n \n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#sub_df.isup_grade.value_counts()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}