{"cells":[{"metadata":{"papermill":{"duration":0.017124,"end_time":"2021-03-06T20:16:51.796938","exception":false,"start_time":"2021-03-06T20:16:51.779814","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### IanPan part"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2021-03-06T20:16:51.838291Z","iopub.status.busy":"2021-03-06T20:16:51.837492Z","iopub.status.idle":"2021-03-06T20:18:17.478355Z","shell.execute_reply":"2021-03-06T20:18:17.477649Z"},"papermill":{"duration":85.665023,"end_time":"2021-03-06T20:18:17.47856","exception":false,"start_time":"2021-03-06T20:16:51.813537","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Install required packages\n!pip install /kaggle/input/timm-0-4-5/timm-0.4.5-py3-none-any.whl\n!pip install /kaggle/input/omegaconf/omegaconf-2.0.2-py3-none-any.whl\n!pip install /kaggle/input/pretrainedmodels/pretrained-models.pytorch/","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:17.528325Z","iopub.status.busy":"2021-03-06T20:18:17.527616Z","iopub.status.idle":"2021-03-06T20:18:17.609136Z","shell.execute_reply":"2021-03-06T20:18:17.608088Z"},"papermill":{"duration":0.109273,"end_time":"2021-03-06T20:18:17.609281","exception":false,"start_time":"2021-03-06T20:18:17.500008","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"# Import from my own library\nimport glob\nimport os.path as osp\nimport pandas as pd\n\nfiles = sorted(glob.glob('/kaggle/input/ranzcr-clip-catheter-line-classification/test/*'))\nfiles = [osp.basename(f) for f in files]\nwith open('/kaggle/working/test.txt', 'w') as f:\n    if len(files) == 3582: files = files[:10]\n    for fi in files:\n        _ = f.write(f'{fi}\\n')   ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:17.664901Z","iopub.status.busy":"2021-03-06T20:18:17.663512Z","iopub.status.idle":"2021-03-06T20:18:55.179275Z","shell.execute_reply":"2021-03-06T20:18:55.178075Z"},"papermill":{"duration":37.548096,"end_time":"2021-03-06T20:18:55.179437","exception":false,"start_time":"2021-03-06T20:18:17.631341","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"!cd /kaggle/input/ranzcr-src/ ; python main.py full_inference configs/inf/inf019_kaggle.yaml \\\n    --imgfiles /kaggle/working/test.txt \\\n    --data-dir /kaggle/input/ranzcr-clip-catheter-line-classification/test/ \\\n    --save-preds-file /kaggle/working/submission_ianpan.csv \\\n    --num-workers 1 \\\n    --rank-average","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:55.254337Z","iopub.status.busy":"2021-03-06T20:18:55.253406Z","iopub.status.idle":"2021-03-06T20:18:55.417385Z","shell.execute_reply":"2021-03-06T20:18:55.417929Z"},"papermill":{"duration":0.207919,"end_time":"2021-03-06T20:18:55.418122","exception":false,"start_time":"2021-03-06T20:18:55.210203","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"COLUMNS = ['StudyInstanceUID', 'ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal', \n           'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', 'NGT - Normal', \n           'CVC - Abnormal', 'CVC - Borderline', 'CVC - Normal', \n           'Swan Ganz Catheter Present']\n\ndf = pd.read_csv('/kaggle/working/submission_ianpan.csv')\ndf.columns = COLUMNS\ndf.to_csv('/kaggle/working/submission_ianpan.csv', index=False)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:55.632339Z","iopub.status.busy":"2021-03-06T20:18:55.63132Z","iopub.status.idle":"2021-03-06T20:18:55.6339Z","shell.execute_reply":"2021-03-06T20:18:55.634428Z"},"papermill":{"duration":0.182169,"end_time":"2021-03-06T20:18:55.634612","exception":false,"start_time":"2021-03-06T20:18:55.452443","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%reset -f","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.031459,"end_time":"2021-03-06T20:18:55.698241","exception":false,"start_time":"2021-03-06T20:18:55.666782","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### YurBol part"},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:55.77518Z","iopub.status.busy":"2021-03-06T20:18:55.774428Z","iopub.status.idle":"2021-03-06T20:18:58.83035Z","shell.execute_reply":"2021-03-06T20:18:58.829087Z"},"papermill":{"duration":3.100065,"end_time":"2021-03-06T20:18:58.830508","exception":false,"start_time":"2021-03-06T20:18:55.730443","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"import os\nimport sys\nsys.path = [\n    '../input/pytorch-image-models/pytorch-image-models-master',\n] + sys.path\n\nimport time\nimport random\nimport cv2\nimport sklearn\nfrom scipy.stats import rankdata\nimport torch\nfrom torch import nn\nimport pandas as pd\nimport numpy as np\n\nfrom glob import glob\nfrom tqdm import tqdm\nfrom datetime import datetime\n\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2\n\nimport timm\nfrom torch.utils.data import Dataset,DataLoader\nfrom torch.utils.data.sampler import SequentialSampler, RandomSampler\nfrom torch.nn import functional as F\n\nimport warnings\n\nwarnings.filterwarnings(\"ignore\") \nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning) \n\nSEED = 42\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    torch.backends.cudnn.benchmark = True\n\nseed_everything(SEED)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:58.901895Z","iopub.status.busy":"2021-03-06T20:18:58.901215Z","iopub.status.idle":"2021-03-06T20:18:58.926555Z","shell.execute_reply":"2021-03-06T20:18:58.925841Z"},"papermill":{"duration":0.06578,"end_time":"2021-03-06T20:18:58.926689","exception":false,"start_time":"2021-03-06T20:18:58.860909","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"FASTCOMMIT = True\nsample_submission = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/sample_submission.csv').sort_values(by=['StudyInstanceUID'])\nif FASTCOMMIT and sample_submission.shape[0] == 3582:\n    sample_submission = sample_submission.iloc[:10,:]\n\ntarget_cols = sample_submission.columns[1:].tolist()\nDATA_PATH = '../input/ranzcr-clip-catheter-line-classification/test/'\n    \nclass RanzcrDS(torch.utils.data.Dataset):\n    def __init__(self, image_ids, transforms=None, augmix=False):\n        self.image_ids = image_ids\n        self.transforms = transforms\n        self.len = len(self.image_ids)\n        \n    def __len__(self):\n        return self.len\n    \n    def __getitem__(self, index):\n        image_id = self.image_ids[index]\n        path = f'{DATA_PATH}/{image_id}.jpg'\n        image = cv2.imread(path, cv2.IMREAD_COLOR)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        if self.transforms:\n            sample = {'image': image}\n            sample = self.transforms(**sample)\n            image = sample['image']\n        \n        return image","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:59.008614Z","iopub.status.busy":"2021-03-06T20:18:59.003319Z","iopub.status.idle":"2021-03-06T20:18:59.036265Z","shell.execute_reply":"2021-03-06T20:18:59.035714Z"},"papermill":{"duration":0.074486,"end_time":"2021-03-06T20:18:59.036419","exception":false,"start_time":"2021-03-06T20:18:58.961933","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def gem(x, p=3, eps=1e-6):\n    return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1./p)\n\nclass GeM(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super(GeM,self).__init__()\n        self.p = Parameter(torch.ones(1)*p)\n        self.eps = eps\n\n    def forward(self, x):\n        return gem(x, p=self.p, eps=self.eps)\n        \n    def __repr__(self):\n        return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + ', ' + 'eps=' + str(self.eps) + ')'\n\nclass GeMmp(nn.Module):\n    def __init__(self, p=3, mp=1, eps=1e-6):\n        super(GeMmp,self).__init__()\n        self.p = Parameter(torch.ones(mp)*p)\n        self.mp = mp\n        self.eps = eps\n\n    def forward(self, x):\n        return gem(x, p=self.p.unsqueeze(-1).unsqueeze(-1), eps=self.eps)\n        \n    def __repr__(self):\n        return self.__class__.__name__ + '(' + 'p=' + '[{}]'.format(self.mp) + ', ' + 'eps=' + str(self.eps) + ')'\n    \n@torch.jit.script\ndef mish_jit_fwd(x):\n    return x.mul(torch.tanh(F.softplus(x)))\n\n@torch.jit.script\ndef mish_jit_bwd(x, grad_output):\n    x_sigmoid = torch.sigmoid(x)\n    x_tanh_sp = F.softplus(x).tanh()\n    return grad_output.mul(x_tanh_sp + x * x_sigmoid * (1 - x_tanh_sp * x_tanh_sp))\n\nclass MishFunction(torch.autograd.Function):\n    @staticmethod\n    def forward(ctx, x):\n        ctx.save_for_backward(x)\n        return mish_jit_fwd(x)\n\n    @staticmethod\n    def backward(ctx, grad_output):\n        x = ctx.saved_tensors[0]\n        return mish_jit_bwd(x,grad_output)\n\nclass Mish(nn.Module):\n    def forward(self, x):\n        return MishFunction.apply(x)\n    \n    \nclass net(nn.Module):\n    def __init__(self, backbone):\n        super(net, self).__init__()\n        \n        if any(b in backbone for b in ['efficientnet','mixnet','resnest','tresnet','seresnext','inception','vit_large','nfnet','resnet']):\n            self.backbone = backbone\n            self.net = timm.create_model(self.backbone, num_classes=1000 if ('inception' not in self.backbone) else 1001, pretrained=False)\n\n            if ('efficientnet' in self.backbone) or ('mixnet' in self.backbone):\n                #self.net.classifier = nn.Linear(self.net.classifier.in_features, 11)\n\n                #self.net.classifier = nn.Sequential(\n                #    nn.Dropout(0.2),\n                #    nn.Linear(self.net.classifier.in_features, int(self.net.classifier.in_features / 2)),\n                #    Mish(),\n                #    nn.BatchNorm1d(int(self.net.classifier.in_features / 2)),\n                #    nn.Dropout(0.1),\n                #    nn.Linear(int(self.net.classifier.in_features / 2), 5),\n                #)\n                \n                self.dropouts = nn.ModuleList([\n                nn.Dropout(0.5) for _ in range(5)\n                ])\n                \n                self.fc = nn.Linear(self.net.classifier.in_features, 11)\n                self.net.classifier = nn.Identity()\n\n            elif 'resnest' in self.backbone:\n                self.net.fc = nn.Linear(self.net.fc.in_features, 11)\n\n                #self.net.fc = nn.Sequential(\n                #    nn.Dropout(0.2),\n                #    nn.Linear(self.net.fc.in_features, int(self.net.fc.in_features / 2)),\n                #    Mish(),\n                #    nn.BatchNorm1d(int(self.net.fc.in_features / 2)),\n                #    nn.Dropout(0.1),\n                #    nn.Linear(int(self.net.fc.in_features / 2), 5)\n                #    \n                #    \n                    #nn.Linear(int(self.net.fc.in_features / 2), 5)\n                #    \n                #    nn.Linear(int(self.net.fc.in_features / 2), 512),\n                #    AdaCos(512, 5)\n                #    \n                #    \n                #)\n                \n                \n                #self.net.global_pool = nn.AdaptiveMaxPool2d(1)\n                #self.net.fc = nn.Sequential(\n                #    nn.Linear(self.net.fc.in_features, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n                #    nn.Linear(1024, 1024), nn.ReLU(), nn.Dropout(p=0.2),\n                #    nn.Linear(1024, NUM_CLASSES))\n                \n                #self.net.global_pool = GeM()\n                #self.net.fc = nn.Sequential(\n                #    #nn.Dropout(0.2),\n                #    nn.Linear(self.net.fc.in_features, 512),\n                #    nn.BatchNorm1d(512),\n                #    #nn.Linear(512, NUM_CLASSES),\n                #    )\n                #self._init_params()\n                \n            elif 'tresnet' in self.backbone:\n                self.net.head = nn.Linear(self.net.head.fc.in_features, 11)\n\n                #self.net.head = nn.Sequential(\n                #    nn.Dropout(0.2),\n                #    nn.Linear(self.net.head.fc.in_features, int(self.net.head.fc.in_features / 2)),\n                #    Mish(),\n                #    nn.BatchNorm1d(int(self.net.head.fc.in_features / 2)),\n                #    nn.Dropout(0.1),\n                #    nn.Linear(int(self.net.head.fc.in_features / 2), 5),\n                #)\n                \n\n            elif ('seresnext' in self.backbone) or ('inception' in self.backbone):\n                #self.net.last_linear = nn.Linear(self.net.last_linear.in_features, 5)\n\n                self.net.last_linear = nn.Sequential(\n                    nn.Dropout(0.2),\n                    nn.Linear(self.net.last_linear.in_features, int(self.net.last_linear.in_features / 2)),\n                    Mish(),\n                    nn.BatchNorm1d(int(self.net.last_linear.in_features / 2)),\n                    nn.Dropout(0.1),\n                    nn.Linear(int(self.net.last_linear.in_features / 2), 11),\n                )\n                \n            elif 'vit_large' in self.backbone:\n                self.net.head = nn.Linear(self.net.head.in_features, 11)\n\n                #self.net.head = nn.Sequential(\n                #    nn.Dropout(0.2),\n                #    nn.Linear(self.net.head.fc.in_features, int(self.net.head.fc.in_features / 2)),\n                #    Mish(),\n                #    nn.BatchNorm1d(int(self.net.head.fc.in_features / 2)),\n                #    nn.Dropout(0.1),\n                #    nn.Linear(int(self.net.head.fc.in_features / 2), 5),\n                #)\n                \n            elif 'nfnet' in self.backbone:\n                #self.net.head.fc = nn.Linear(self.net.head.fc.in_features, 11)\n                \n                self.dropouts = nn.ModuleList([\n                nn.Dropout(0.5) for _ in range(5)\n                ])\n                \n                self.fc = nn.Linear(self.net.head.fc.in_features, 11)\n                self.net.head.fc = nn.Identity()\n            elif 'resnet' in self.backbone:\n                #self.net.fc = nn.Linear(self.net.fc.in_features, 11)\n                \n                self.dropouts = nn.ModuleList([\n                nn.Dropout(0.5) for _ in range(5)\n                ])\n                \n                self.fc = nn.Linear(self.net.fc.in_features, 11)\n                self.net.fc = nn.Identity()\n                \n        else:\n            self.net = torchvision_models.resnet50(pretrained=True)\n            self.net.fc = nn.Linear(self.net.fc.in_features, 11)\n                \n    def forward(self, x):\n        x = self.net(x)\n        #return x\n        #if 'nfnet' in self.backbone: \n            #for i, dropout in enumerate(self.dropouts):\n            #    if i == 0:\n            #        out = self.fc(dropout(x))\n            #    else:\n            #        out += self.fc(dropout(x))\n            #out /= len(self.dropouts)\n            \n        out = self.fc(x)\n        return out","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:59.108623Z","iopub.status.busy":"2021-03-06T20:18:59.106784Z","iopub.status.idle":"2021-03-06T20:18:59.109463Z","shell.execute_reply":"2021-03-06T20:18:59.10992Z"},"papermill":{"duration":0.042728,"end_time":"2021-03-06T20:18:59.110079","exception":false,"start_time":"2021-03-06T20:18:59.067351","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def load_model(model_name, weight_name):\n    m = net(model_name)\n    m.load_state_dict(torch.load(f'../input/ranzcr/{weight_name}',map_location='cpu')['state_dict'])\n    m.cuda()\n    m.eval()\n    return m\n\nclass RANZCRResNet200D(nn.Module):\n    def __init__(self, model_name='resnet200d', out_dim=11, pretrained=False):\n        super().__init__()\n        self.model = timm.create_model(model_name, pretrained=False)\n        n_features = self.model.fc.in_features\n        self.model.global_pool = nn.Identity()\n        self.model.fc = nn.Identity()\n        self.pooling = nn.AdaptiveAvgPool2d(1)\n        self.fc = nn.Linear(n_features, out_dim)\n\n    def forward(self, x):\n        bs = x.size(0)\n        features = self.model(x)\n        pooled_features = self.pooling(features).view(bs, -1)\n        output = self.fc(pooled_features)\n        return output\n    \ndef load_modified_model(model_name, weight_name):\n    m = RANZCRResNet200D()\n    m.load_state_dict(torch.load(f'../input/ranzcr/{weight_name}',map_location='cpu'))\n    m.cuda()\n    m.eval()\n    return m\n\nmodels_dict = [\n    ['tf_efficientnet_l2_ns_475','tf_efficientnet_l2_ns_475_RocAucLoss_f0_pseudo_mdropout_g02.pth'],\n    ['tf_efficientnet_l2_ns_475','tf_efficientnet_l2_ns_475_RocAucLoss_f1_pseudo_mdropout_g02.pth'],\n    ['tf_efficientnet_l2_ns_475','tf_efficientnet_l2_ns_475_RocAucLoss_f2_pseudo_mdropout_g02.pth'],\n    ['tf_efficientnet_l2_ns_475','tf_efficientnet_l2_ns_475_RocAucLoss_f3_pseudo_mdropout_g02.pth'],\n    \n    ['resnet200d','resnet200d_RocAucLoss_f0_chestx_pseudo_mdropout_g02.pth'],\n    ['resnet200d','resnet200d_RocAucLoss_f1_chestx_pseudo_mdropout_g02.pth'],\n    ['resnet200d','resnet200d_RocAucLoss_f2_chestx_pseudo_mdropout_g02.pth'],\n    ['resnet200d','resnet200d_RocAucLoss_f3_chestx_pseudo_mdropout_g02.pth'],\n    \n    #['resnet200d_m','resnet200d_fold0_cv953.pth'],\n    ['resnet200d_m','resnet200d_fold1_cv955.pth'],\n    ['resnet200d_m','resnet200d_fold2_cv955.pth'],\n    ['resnet200d_m','resnet200d_fold3_cv957.pth'],\n    ['resnet200d_m','resnet200d_fold4_cv954.pth'],\n    \n]","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:59.192768Z","iopub.status.busy":"2021-03-06T20:18:59.191866Z","iopub.status.idle":"2021-03-06T20:18:59.19775Z","shell.execute_reply":"2021-03-06T20:18:59.198197Z"},"papermill":{"duration":0.057055,"end_time":"2021-03-06T20:18:59.198349","exception":false,"start_time":"2021-03-06T20:18:59.141294","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"IMG_SIZE = 512\n\ntta_multiple = A.Compose([\n    #A.Resize(IMG_SIZE, IMG_SIZE, p=1.0),\n    A.RandomResizedCrop(IMG_SIZE, IMG_SIZE, p=1.0, scale=(0.9,1)),\n    #A.Transpose(p=0.5),\n    A.HorizontalFlip(p=0.5),\n    #A.VerticalFlip(p=0.5),\n    #A.RandomRotate90(p=0.5),\n    A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),\n    A.RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),\n    A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),\n    ToTensorV2(p=1.0),\n], p=1.0)\n\ntta = [tta_multiple,#tta_multiple,\n       #tta_multiple,tta_multiple,\n       #tta_multiple,tta_multiple,tta_multiple,tta_multiple\n      ]\n\ndef test_loaders_generator(tta):\n    test_loaders = []\n    for t in tta:\n        test_dataset = RanzcrDS(\n            image_ids=sample_submission['StudyInstanceUID'].values,\n            transforms=t,\n        )\n\n        test_loader = torch.utils.data.DataLoader(\n            test_dataset, \n            batch_size=10,\n            num_workers=4,\n            shuffle=False,\n            sampler=SequentialSampler(test_dataset),\n            pin_memory=False,\n            drop_last=False,\n        )\n\n        test_loaders.append(test_loader)\n    \n    return test_loaders\n\ndef ensemble_predictions(predictions, weights, method=\"simple\"):\n    \"\"\"\n    Available methods: simple, weighted, harmonic, geometric, rank, power_geometric_smoothing, \n    power_geometric_sharpening, power_smoothing, power_sharpening\n    \"\"\"\n    assert np.isclose(np.sum(weights), 1.0)\n    if method == \"simple\":\n        res = np.mean(predictions, axis=0)\n    elif method == \"weighted\":\n        res = np.average(predictions, weights=weights, axis=0)\n    elif method == \"harmonic\":\n        res = np.average([1 / p for p in predictions], weights=weights, axis=0)\n        return 1 / res\n    elif method == \"geometric\":\n        numerator = np.average(\n            [np.log(p) for p in predictions], weights=weights, axis=0\n        )\n        res = np.exp(numerator / sum(weights))\n        return res\n    elif method == \"rank\":\n        res = np.average([pd.DataFrame(p).apply(rankdata,axis=0).values for p in predictions], weights=weights, axis=0)\n        return res / (res.shape[0] + 1)\n    elif 'power_geometric' in method:\n        power = 2 if 'sharpening' in method else 0.5\n        res = np.mean(np.stack(predictions) ** power,axis=0) ** (1/power)\n    elif 'power_smoothing' in method:\n        res =  np.mean(np.stack(predictions) ** 0.5,axis=0)\n    elif 'power_sharpening' in method:\n        res =  np.mean(np.stack(predictions) ** 2, axis=0)\n    return res\n\ndef inference(test_loaders, models_dict, weights=[0.5,0.5], method='simple'):\n    final_predictions = []\n    for model_name, weight_name in models_dict:\n        if model_name == 'resnet200d_m':\n            m = load_modified_model(model_name, weight_name)\n        else:\n            m = load_model(model_name, weight_name)\n        \n        predictions = []\n        for t_loader in test_loaders:\n            result = []\n            for images in tqdm(t_loader, total=len(t_loader)):\n                with torch.no_grad():\n                    images = images.cuda().float()\n                    outputs = torch.sigmoid(m(images)).data.cpu().numpy()\n                result.append(outputs)\n            predictions.append(np.vstack(result))\n\n        predictions = np.mean(predictions,axis=0)\n        final_predictions.append(predictions)\n\n    final_predictions = ensemble_predictions(final_predictions, weights=weights, method=method)\n    return final_predictions","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:18:59.270084Z","iopub.status.busy":"2021-03-06T20:18:59.269352Z","iopub.status.idle":"2021-03-06T20:21:20.243979Z","shell.execute_reply":"2021-03-06T20:21:20.244499Z"},"papermill":{"duration":141.015239,"end_time":"2021-03-06T20:21:20.244691","exception":false,"start_time":"2021-03-06T20:18:59.229452","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"weights = [1/12, 1/12, 1/12, 1/12, \n           1/12, 1/12, 1/12, 1/12,\n           1/12, 1/12, 1/12, 1/12,\n          ]\nmethod = 'power_smoothing'\n\ntest_loaders = test_loaders_generator(tta)\nfinal_predictions = inference(test_loaders, models_dict, weights, method)\n\nsample_submission[target_cols] = final_predictions","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:21:20.385432Z","iopub.status.busy":"2021-03-06T20:21:20.384058Z","iopub.status.idle":"2021-03-06T20:21:20.389221Z","shell.execute_reply":"2021-03-06T20:21:20.390836Z"},"papermill":{"duration":0.082128,"end_time":"2021-03-06T20:21:20.39109","exception":false,"start_time":"2021-03-06T20:21:20.308962","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"sample_submission.to_csv('/kaggle/working/submission_yurbol.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:21:20.534642Z","iopub.status.busy":"2021-03-06T20:21:20.533669Z","iopub.status.idle":"2021-03-06T20:21:20.563073Z","shell.execute_reply":"2021-03-06T20:21:20.562305Z"},"papermill":{"duration":0.105076,"end_time":"2021-03-06T20:21:20.563293","exception":false,"start_time":"2021-03-06T20:21:20.458217","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"ianpan_sub = pd.read_csv('/kaggle/working/submission_ianpan.csv')\nif (ianpan_sub['StudyInstanceUID'].values != sample_submission['StudyInstanceUID'].values).sum()!=0:\n    print('Need to sort predictions for final ensemble')\n    ianpan_sub = ianpan_sub.sort_values(by=['StudyInstanceUID']).reset_index(drop=True)\n    sample_submission = sample_submission.sort_values(by=['StudyInstanceUID']).reset_index(drop=True)\n    \nsample_submission[target_cols] = ensemble_predictions(\n    [\n        ianpan_sub[target_cols].values,\n        sample_submission[target_cols].values\n    ], weights=[0.5,0.5], method='rank')","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:21:20.700553Z","iopub.status.busy":"2021-03-06T20:21:20.69964Z","iopub.status.idle":"2021-03-06T20:21:20.703507Z","shell.execute_reply":"2021-03-06T20:21:20.704029Z"},"papermill":{"duration":0.074594,"end_time":"2021-03-06T20:21:20.704206","exception":false,"start_time":"2021-03-06T20:21:20.629612","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"sample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:21:20.828026Z","iopub.status.busy":"2021-03-06T20:21:20.793033Z","iopub.status.idle":"2021-03-06T20:21:21.538994Z","shell.execute_reply":"2021-03-06T20:21:21.537716Z"},"papermill":{"duration":0.79164,"end_time":"2021-03-06T20:21:21.53918","exception":false,"start_time":"2021-03-06T20:21:20.74754","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"!rm submission_yurbol.csv","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:21:21.660804Z","iopub.status.busy":"2021-03-06T20:21:21.625737Z","iopub.status.idle":"2021-03-06T20:21:22.356894Z","shell.execute_reply":"2021-03-06T20:21:22.356308Z"},"papermill":{"duration":0.776593,"end_time":"2021-03-06T20:21:22.357082","exception":false,"start_time":"2021-03-06T20:21:21.580489","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"!rm submission_ianpan.csv","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-03-06T20:21:22.445981Z","iopub.status.busy":"2021-03-06T20:21:22.444586Z","iopub.status.idle":"2021-03-06T20:21:22.449429Z","shell.execute_reply":"2021-03-06T20:21:22.448842Z"},"papermill":{"duration":0.051105,"end_time":"2021-03-06T20:21:22.449581","exception":false,"start_time":"2021-03-06T20:21:22.398476","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"sample_submission.to_csv('/kaggle/working/submission.csv', index=False)","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}