{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport copy\nimport time\nimport random\nimport string\nimport joblib\nimport tifffile\nimport numpy as np \nimport pandas as pd \nimport torch\nfrom torch import nn\nimport seaborn as sns\nfrom torchvision import models\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nfrom tqdm.notebook import tqdm\nfrom torch.optim import lr_scheduler\nimport warnings\nwarnings.filterwarnings(\"ignore\")\ngc.enable()","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":3.063995,"end_time":"2022-07-08T14:24:41.045696","exception":false,"start_time":"2022-07-08T14:24:37.981701","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-03T12:45:23.490948Z","iopub.execute_input":"2022-10-03T12:45:23.491417Z","iopub.status.idle":"2022-10-03T12:45:23.500791Z","shell.execute_reply.started":"2022-10-03T12:45:23.491387Z","shell.execute_reply":"2022-10-03T12:45:23.499324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!conda install ../input/offline-packages/*.tar.bz2 \n!pip install ../input/offline-packages/timm/timm-0.6.11.zip","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:45:23.527663Z","iopub.execute_input":"2022-10-03T12:45:23.528022Z","iopub.status.idle":"2022-10-03T12:46:07.609213Z","shell.execute_reply.started":"2022-10-03T12:45:23.527978Z","shell.execute_reply":"2022-10-03T12:46:07.607494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm\nimport pyvips","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:46:07.61241Z","iopub.execute_input":"2022-10-03T12:46:07.613395Z","iopub.status.idle":"2022-10-03T12:46:07.628068Z","shell.execute_reply.started":"2022-10-03T12:46:07.613343Z","shell.execute_reply":"2022-10-03T12:46:07.625616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ntimm_model, timm_image_size = [\n    ['maxvit_nano_rw_256', 256],\n    ['coatnet_1_rw_224', 224],\n    ['coatnet_0_224', 224]\n][2]\ntimm_model_pretrained = True\nIMAGE_SIZE = timm_image_size if timm_model_pretrained else 512 #512\nBATCH_SIZE = 12\nMAX_IMG_PER_PATIENT = 10","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:46:07.636627Z","iopub.execute_input":"2022-10-03T12:46:07.638563Z","iopub.status.idle":"2022-10-03T12:46:07.659727Z","shell.execute_reply.started":"2022-10-03T12:46:07.63851Z","shell.execute_reply":"2022-10-03T12:46:07.658218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"debug = False\ngenerate_new = True\ntest_df = pd.read_csv(\"../input/mayo-clinic-strip-ai/test.csv\")\ndirs = [\"../input/mayo-clinic-strip-ai/train/\", \"../input/mayo-clinic-strip-ai/test/\"]\ntest_df","metadata":{"papermill":{"duration":0.02504,"end_time":"2022-07-08T14:24:41.073811","exception":false,"start_time":"2022-07-08T14:24:41.048771","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-03T12:46:07.663687Z","iopub.execute_input":"2022-10-03T12:46:07.664619Z","iopub.status.idle":"2022-10-03T12:46:07.689705Z","shell.execute_reply.started":"2022-10-03T12:46:07.664577Z","shell.execute_reply":"2022-10-03T12:46:07.688227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df = pd.DataFrame({\"image_id\" : [\"006388_0\", \"008e5c_0\", \"00c058_0\", \"01adc5_0\", \"01adc5_0\"], \"patient_id\" : [\"006388\", \"008e5c\", \"00c058\", \"01adc5\", \"01adc5\"]})","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:46:07.691964Z","iopub.execute_input":"2022-10-03T12:46:07.692833Z","iopub.status.idle":"2022-10-03T12:46:07.698923Z","shell.execute_reply.started":"2022-10-03T12:46:07.692788Z","shell.execute_reply":"2022-10-03T12:46:07.697499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"format_to_dtype = {\n   'uchar': np.uint8,\n   'char': np.int8,\n   'ushort': np.uint16,\n   'short': np.int16,\n   'uint': np.uint32,\n   'int': np.int32,\n   'float': np.float32,\n   'double': np.float64,\n   'complex': np.complex64,\n   'dpcomplex': np.complex128,\n}\ndef vips2numpy(vi):\n    return np.ndarray(\n        buffer=vi.write_to_memory(),\n        dtype=format_to_dtype[vi.format],\n        shape=[vi.height, vi.width, vi.bands])","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:46:07.701055Z","iopub.execute_input":"2022-10-03T12:46:07.702033Z","iopub.status.idle":"2022-10-03T12:46:07.712996Z","shell.execute_reply.started":"2022-10-03T12:46:07.701992Z","shell.execute_reply":"2022-10-03T12:46:07.711623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tile(img, sz=128):\n    shape = img.shape\n    pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n    img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)\n    img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n    img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n    idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[0:MAX_IMG_PER_PATIENT]\n    img = img[idxs]\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:46:07.715154Z","iopub.execute_input":"2022-10-03T12:46:07.71616Z","iopub.status.idle":"2022-10-03T12:46:07.729208Z","shell.execute_reply.started":"2022-10-03T12:46:07.716113Z","shell.execute_reply":"2022-10-03T12:46:07.727956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crop_size = 1024\ndf_test = pd.DataFrame()\ncolumns = [x for x in test_df.columns] + ['sub_id']\ntry:\n    os.mkdir(\"../test/\")\nexcept:\n    pass\nfor i in range(test_df.shape[0]):\n    row_df = test_df[test_df.index == i]\n    img_id = test_df.iloc[i].image_id\n    try:\n        sz = os.path.getsize(dirs[1] + img_id + \".tif\")\n    except:\n        sz = 1000000000\n    if(sz == 1000000000):\n        img = np.zeros((IMAGE_SIZE,IMAGE_SIZE,3), np.uint8)\n    else:\n        try:\n            img = pyvips.Image.thumbnail(dirs[1] + img_id + \".tif\", 20000)\n            img = vips2numpy(img)\n            img = tile(img, sz=crop_size)\n        except Exception as e:\n            print(e)\n            img = np.zeros((IMAGE_SIZE,IMAGE_SIZE,3), np.uint8)\n    if len(img.shape) != 3:\n        sub_id_list = []\n        for i, im in enumerate(img):\n            cv2.imwrite(f\"../test/{img_id}_{i}.jpg\", im)\n            sub_id_list.append(f\"{img_id}_{i}\")\n        row_df = pd.DataFrame(row_df.values.repeat(len(sub_id_list), axis=0), columns=row_df.columns)\n        row_df[['sub_id']] = pd.DataFrame(sub_id_list)\n    else:\n        cv2.imwrite(f\"../test/{img_id}.jpg\", img)\n        row_df[['sub_id']] = pd.DataFrame([img_id])\n        \n    df_test = pd.concat([df_test, row_df]).reset_index()[columns]\n    del img\n    gc.collect()","metadata":{"papermill":{"duration":69.477711,"end_time":"2022-07-08T14:25:50.554416","exception":false,"start_time":"2022-07-08T14:24:41.076705","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-03T12:46:07.731741Z","iopub.execute_input":"2022-10-03T12:46:07.732285Z","iopub.status.idle":"2022-10-03T12:47:29.013001Z","shell.execute_reply.started":"2022-10-03T12:46:07.732246Z","shell.execute_reply":"2022-10-03T12:47:29.011491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = 0.6342223\nstd = 0.31723675","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.015293Z","iopub.execute_input":"2022-10-03T12:47:29.015913Z","iopub.status.idle":"2022-10-03T12:47:29.024064Z","shell.execute_reply.started":"2022-10-03T12:47:29.015851Z","shell.execute_reply":"2022-10-03T12:47:29.021424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImgDataset(Dataset):\n    def __init__(self, df, img_size=IMAGE_SIZE):\n        self.df = df\n        self.img_size = img_size\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        try:\n            image = cv2.imread(\"../test/\" + self.df.iloc[index].sub_id + \".jpg\")\n        except:\n            image = np.zeros((self.img_size,self.img_size,3), np.uint8)\n        try:\n            if len(image.shape) == 5:\n                image = image.squeeze().transpose(1, 2, 0)\n            image = cv2.resize(image, (self.img_size, self.img_size)).transpose(2, 0, 1)\n            image = image / 255\n            image = (image - mean) / (std * 2)\n        except:\n            image = np.zeros((3, self.img_size, self.img_size))\n        patient_id = self.df.iloc[index].patient_id\n        return image, patient_id","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-03T12:47:29.032291Z","iopub.execute_input":"2022-10-03T12:47:29.032924Z","iopub.status.idle":"2022-10-03T12:47:29.048502Z","shell.execute_reply.started":"2022-10-03T12:47:29.032859Z","shell.execute_reply":"2022-10-03T12:47:29.046449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, dataloader):\n    model.cuda()\n    model.eval()\n    dataloader = dataloader\n    outputs = []\n    s = nn.Softmax(dim=1)\n    ids = []\n    with torch.no_grad():\n        for item in tqdm(dataloader, leave=False):\n            patient_id = item[1][0]\n            try:\n                images = item[0].cuda().float()\n                ids.append(patient_id)\n                output = model(images)\n                outputs.append(s(output.cpu())[0].detach().numpy())\n            except:\n                ids.append(patient_id)\n                outputs.append(s(torch.tensor([[1, 1]]).float())[0].detach().numpy())\n    return np.array(outputs), ids","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-03T12:47:29.050473Z","iopub.execute_input":"2022-10-03T12:47:29.050984Z","iopub.status.idle":"2022-10-03T12:47:29.068315Z","shell.execute_reply.started":"2022-10-03T12:47:29.050937Z","shell.execute_reply":"2022-10-03T12:47:29.066916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = timm.create_model(timm_model, pretrained=False, num_classes=2, in_chans=3, img_size=IMAGE_SIZE)\n# if not isinstance(model, nn.DataParallel):\n#     model = nn.DataParallel(model)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.070148Z","iopub.execute_input":"2022-10-03T12:47:29.071715Z","iopub.status.idle":"2022-10-03T12:47:29.081478Z","shell.execute_reply.started":"2022-10-03T12:47:29.071657Z","shell.execute_reply":"2022-10-03T12:47:29.080072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pretrained_path = '../input/mayomodels/fold-0.pth'\n# checkpoint = torch.load(pretrained_path, map_location=device)\n# missing_keys, unexpected_keys = model.load_state_dict(checkpoint['model_state_dict'], strict=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.083983Z","iopub.execute_input":"2022-10-03T12:47:29.084518Z","iopub.status.idle":"2022-10-03T12:47:29.094098Z","shell.execute_reply.started":"2022-10-03T12:47:29.084477Z","shell.execute_reply":"2022-10-03T12:47:29.092713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ensemble model","metadata":{}},{"cell_type":"code","source":"class Ensemble(nn.Module):\n    def __init__(self, models, weights):\n        super().__init__()\n        self.models = models\n        self.weights = weights\n\n    def forward(self, x):\n        y = torch.zeros((x.shape[0], 2), device=device)\n        for i, z in enumerate(self.models):\n            w = torch.mul(self.models[i](x), self.weights[i])\n            y += w\n        return y","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.096116Z","iopub.execute_input":"2022-10-03T12:47:29.096998Z","iopub.status.idle":"2022-10-03T12:47:29.108095Z","shell.execute_reply.started":"2022-10-03T12:47:29.096941Z","shell.execute_reply":"2022-10-03T12:47:29.107089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CoAtNet ensemble","metadata":{}},{"cell_type":"code","source":"def get_model(pretrained_path):\n    model = timm.create_model(timm_model, pretrained=False, num_classes=2, in_chans=3, img_size=IMAGE_SIZE)\n    if not isinstance(model, nn.DataParallel):\n        model = nn.DataParallel(model)\n    pretrained_path = f'../input/mayomodels/{pretrained_path}.pth'\n    checkpoint = torch.load(pretrained_path, map_location=device)\n    missing_keys, unexpected_keys = model.load_state_dict(checkpoint['model_state_dict'], strict=False)\n    model = model.to(device)\n    model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.110615Z","iopub.execute_input":"2022-10-03T12:47:29.111511Z","iopub.status.idle":"2022-10-03T12:47:29.128249Z","shell.execute_reply.started":"2022-10-03T12:47:29.111346Z","shell.execute_reply":"2022-10-03T12:47:29.12685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pretrained_paths = ['fold-0', 'fold-1', 'fold-2', 'fold-3', 'fold-4']\n# models = [get_model(p) for p in pretrained_paths]\n# weights = [1 - 0.307543, 1 - 0.415670, 1 - 0.357801, 1 - 0.349552, 1 - 0.306017]\n# coatnet_weights_mean = np.mean(weights)\n# weights_sum = np.sum(weights)\n# weights = [x/weights_sum for x in weights]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.130117Z","iopub.execute_input":"2022-10-03T12:47:29.130658Z","iopub.status.idle":"2022-10-03T12:47:29.141137Z","shell.execute_reply.started":"2022-10-03T12:47:29.13062Z","shell.execute_reply":"2022-10-03T12:47:29.139661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coatnet_ensemble = Ensemble(models, weights)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.143398Z","iopub.execute_input":"2022-10-03T12:47:29.143926Z","iopub.status.idle":"2022-10-03T12:47:29.154998Z","shell.execute_reply.started":"2022-10-03T12:47:29.143878Z","shell.execute_reply":"2022-10-03T12:47:29.153545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Efficient ensemble","metadata":{}},{"cell_type":"code","source":"def get_efficient_model(pretrained_path):\n    model = timm.create_model('efficientnet_b1', pretrained=False, num_classes=2, in_chans=3)\n    if not isinstance(model, nn.DataParallel):\n        model = nn.DataParallel(model)\n    pretrained_path = f'../input/../input/mayo-clinic-coatnet-efficientnet/{pretrained_path}.pth'\n    checkpoint = torch.load(pretrained_path, map_location=device)\n    missing_keys, unexpected_keys = model.load_state_dict(checkpoint['model_state_dict'], strict=False)\n    model = model.to(device)\n    model.eval()\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.156754Z","iopub.execute_input":"2022-10-03T12:47:29.157624Z","iopub.status.idle":"2022-10-03T12:47:29.169786Z","shell.execute_reply.started":"2022-10-03T12:47:29.157583Z","shell.execute_reply":"2022-10-03T12:47:29.168964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pretrained_paths = ['fold-0', 'fold-1', 'fold-2', 'fold-3', 'fold-4']\n# models = [get_efficient_model(p) for p in pretrained_paths]\n# weights = [1 - 0.854071, 1 - 0.820791, 1 - 0.862231, 1 - 0.895316, 1 - 0.793110]\n# efficient_weights_mean = np.mean(weights)\n# weights_sum = np.sum(weights)\n# weights = [x/weights_sum for x in weights]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.171231Z","iopub.execute_input":"2022-10-03T12:47:29.172169Z","iopub.status.idle":"2022-10-03T12:47:29.184064Z","shell.execute_reply.started":"2022-10-03T12:47:29.172112Z","shell.execute_reply":"2022-10-03T12:47:29.182646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# efficient_ensemble = Ensemble(models, weights)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.186419Z","iopub.execute_input":"2022-10-03T12:47:29.187439Z","iopub.status.idle":"2022-10-03T12:47:29.202878Z","shell.execute_reply.started":"2022-10-03T12:47:29.187398Z","shell.execute_reply":"2022-10-03T12:47:29.201262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# BeitV2","metadata":{}},{"cell_type":"code","source":"def get_beitv2_model(pretrained_path):\n    model = timm.create_model('beitv2_base_patch16_224_in22k', pretrained=False, num_classes=2, in_chans=3, img_size=IMAGE_SIZE)\n    if not isinstance(model, nn.DataParallel):\n        model = nn.DataParallel(model)\n    pretrained_path = f'../input/fork-3-of-mayo-clinic-coatnet-efficien/{pretrained_path}.pth'\n    checkpoint = torch.load(pretrained_path, map_location=device)\n    missing_keys, unexpected_keys = model.load_state_dict(checkpoint['model_state_dict'], strict=False)\n    model = model.to(device)\n    model.eval()\n    return (model, checkpoint['best_auc'])","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.20455Z","iopub.execute_input":"2022-10-03T12:47:29.205895Z","iopub.status.idle":"2022-10-03T12:47:29.218455Z","shell.execute_reply.started":"2022-10-03T12:47:29.205854Z","shell.execute_reply":"2022-10-03T12:47:29.217087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pretrained_paths = ['fold-0', 'fold-1', 'fold-2', 'fold-3', 'fold-4']\n# checkpoints_data = [get_coAtNet2_model(p) for p in pretrained_paths]\n# models = [p[0] for p in checkpoints_data]\n# weights = [p[1] for p in checkpoints_data]\n# print(weights)\n# coatnet2_weights_mean = np.mean(weights)\n# weights_sum = np.sum(weights)\n# weights = [x/weights_sum for x in weights]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.220249Z","iopub.execute_input":"2022-10-03T12:47:29.222272Z","iopub.status.idle":"2022-10-03T12:47:29.231595Z","shell.execute_reply.started":"2022-10-03T12:47:29.222241Z","shell.execute_reply":"2022-10-03T12:47:29.230079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# beitv2_ensemble = Ensemble(models, weights)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.234021Z","iopub.execute_input":"2022-10-03T12:47:29.235694Z","iopub.status.idle":"2022-10-03T12:47:29.244881Z","shell.execute_reply.started":"2022-10-03T12:47:29.235637Z","shell.execute_reply":"2022-10-03T12:47:29.24309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CoAtNet 2","metadata":{}},{"cell_type":"code","source":"def get_coAtNet2_model(pretrained_path):\n    model = timm.create_model(timm_model, pretrained=False, num_classes=2, in_chans=3, img_size=IMAGE_SIZE)\n    if not isinstance(model, nn.DataParallel):\n        model = nn.DataParallel(model)\n    pretrained_path = f'../input/fork-of-fork-3-of-mayo-clinic-coatnet-efficien/{pretrained_path}.pth'\n    checkpoint = torch.load(pretrained_path, map_location=device)\n    missing_keys, unexpected_keys = model.load_state_dict(checkpoint['model_state_dict'], strict=False)\n    model = model.to(device)\n    model.eval()\n    return (model, checkpoint['best_auc'])","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:47:29.246862Z","iopub.execute_input":"2022-10-03T12:47:29.248236Z","iopub.status.idle":"2022-10-03T12:47:29.259337Z","shell.execute_reply.started":"2022-10-03T12:47:29.248196Z","shell.execute_reply":"2022-10-03T12:47:29.257978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_paths = ['fold-0', 'fold-1', 'fold-2', 'fold-3', 'fold-4']\ncheckpoints_data = [get_coAtNet2_model(p) for p in pretrained_paths]\ncheckpoints_data = checkpoints_data + [get_beitv2_model(p) for p in pretrained_paths]\nmodels = [p[0] for p in checkpoints_data]\nweights = [p[1] for p in checkpoints_data]\nprint(weights)\ncoatnet2_weights_mean = np.mean(weights)\nweights_sum = np.sum(weights)\nweights = [x/weights_sum for x in weights]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:53:59.756878Z","iopub.execute_input":"2022-10-03T12:53:59.757311Z","iopub.status.idle":"2022-10-03T12:55:24.217573Z","shell.execute_reply.started":"2022-10-03T12:53:59.757281Z","shell.execute_reply":"2022-10-03T12:55:24.215623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coatnet2_ensemble = Ensemble(models, weights)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:24.221241Z","iopub.execute_input":"2022-10-03T12:55:24.222384Z","iopub.status.idle":"2022-10-03T12:55:24.229009Z","shell.execute_reply.started":"2022-10-03T12:55:24.222335Z","shell.execute_reply":"2022-10-03T12:55:24.227593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submiting","metadata":{}},{"cell_type":"code","source":"#model = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_efficientnet_b4', pretrained=True)\n# model = torch.jit.load('../input/cnnstripai/model.pth')\nbatch_size = 1\ntest_loader_224 = DataLoader(\n    ImgDataset(df_test), \n    batch_size=batch_size, \n    shuffle=False, \n    num_workers=1\n)\n# efficient_test_loader = DataLoader(\n#     ImgDataset(df_test, img_size=256), \n#     batch_size=batch_size, \n#     shuffle=False, \n#     num_workers=1\n# )","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-03T12:55:24.230808Z","iopub.execute_input":"2022-10-03T12:55:24.231643Z","iopub.status.idle":"2022-10-03T12:55:24.259239Z","shell.execute_reply.started":"2022-10-03T12:55:24.231601Z","shell.execute_reply":"2022-10-03T12:55:24.25769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coatnet_weight = coatnet_weights_mean / (coatnet_weights_mean + efficient_weights_mean)\n# efficient_weight = efficient_weights_mean / (coatnet_weights_mean + efficient_weights_mean)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:24.264019Z","iopub.execute_input":"2022-10-03T12:55:24.264808Z","iopub.status.idle":"2022-10-03T12:55:24.273827Z","shell.execute_reply.started":"2022-10-03T12:55:24.26474Z","shell.execute_reply":"2022-10-03T12:55:24.272201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coatnet_anss, coatnet_ids = predict(coatnet2_ensemble, test_loader_224)\n# efficient_anss, efficient_ids = predict(efficient_ensemble, efficient_test_loader)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-03T12:55:24.276341Z","iopub.execute_input":"2022-10-03T12:55:24.276937Z","iopub.status.idle":"2022-10-03T12:55:35.72413Z","shell.execute_reply.started":"2022-10-03T12:55:24.276819Z","shell.execute_reply":"2022-10-03T12:55:35.722337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coatnet_anss = [[x[0]*efficient_weight, x[1]*] for x in coatnet_anss]\n# efficient_anss = [np.multiply(x, efficient_weight) for x in efficient_anss]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.727388Z","iopub.execute_input":"2022-10-03T12:55:35.72895Z","iopub.status.idle":"2022-10-03T12:55:35.735994Z","shell.execute_reply.started":"2022-10-03T12:55:35.728896Z","shell.execute_reply":"2022-10-03T12:55:35.734003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prob = pd.DataFrame({\"CE\" : coatnet_anss[:,0], \"LAA\" : coatnet_anss[:,1], \"id\" : coatnet_ids})\n# efficient_prob = pd.DataFrame({\"CE\" : efficient_anss[:,0], \"LAA\" : efficient_anss[:,1], \"id\" : efficient_ids})","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.738186Z","iopub.execute_input":"2022-10-03T12:55:35.739054Z","iopub.status.idle":"2022-10-03T12:55:35.756331Z","shell.execute_reply.started":"2022-10-03T12:55:35.738975Z","shell.execute_reply":"2022-10-03T12:55:35.754614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coatnet_prob['CE'] = coatnet_prob['CE'] * coatnet_weight\n# coatnet_prob['LAA'] = coatnet_prob['LAA'] * coatnet_weight\n\n# efficient_prob['CE'] = efficient_prob['CE'] * efficient_weight\n# efficient_prob['LAA'] = efficient_prob['LAA'] * efficient_weight","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.758691Z","iopub.execute_input":"2022-10-03T12:55:35.759222Z","iopub.status.idle":"2022-10-03T12:55:35.774952Z","shell.execute_reply.started":"2022-10-03T12:55:35.759177Z","shell.execute_reply":"2022-10-03T12:55:35.773344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prob = pd.concat([coatnet_prob, efficient_prob]).reset_index()[['CE', 'LAA', 'id']]","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.778263Z","iopub.execute_input":"2022-10-03T12:55:35.77942Z","iopub.status.idle":"2022-10-03T12:55:35.788724Z","shell.execute_reply.started":"2022-10-03T12:55:35.779259Z","shell.execute_reply":"2022-10-03T12:55:35.787081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prob = pd.DataFrame({\"CE\" : anss[:,0], \"LAA\" : anss[:,1], \"id\" : ids}).groupby(\"id\").mean()\nprob = prob.groupby(\"id\").mean()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.794775Z","iopub.execute_input":"2022-10-03T12:55:35.79609Z","iopub.status.idle":"2022-10-03T12:55:35.820381Z","shell.execute_reply.started":"2022-10-03T12:55:35.796029Z","shell.execute_reply":"2022-10-03T12:55:35.818844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/mayo-clinic-strip-ai/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.822772Z","iopub.execute_input":"2022-10-03T12:55:35.823408Z","iopub.status.idle":"2022-10-03T12:55:35.846601Z","shell.execute_reply.started":"2022-10-03T12:55:35.823363Z","shell.execute_reply":"2022-10-03T12:55:35.845078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.CE = prob.CE.to_list()\nsubmission.LAA = prob.LAA.to_list()","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.849194Z","iopub.execute_input":"2022-10-03T12:55:35.849776Z","iopub.status.idle":"2022-10-03T12:55:35.857925Z","shell.execute_reply.started":"2022-10-03T12:55:35.849701Z","shell.execute_reply":"2022-10-03T12:55:35.856375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.860251Z","iopub.execute_input":"2022-10-03T12:55:35.861268Z","iopub.status.idle":"2022-10-03T12:55:35.881587Z","shell.execute_reply.started":"2022-10-03T12:55:35.861224Z","shell.execute_reply":"2022-10-03T12:55:35.880181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2022-10-03T12:55:35.883831Z","iopub.execute_input":"2022-10-03T12:55:35.884199Z","iopub.status.idle":"2022-10-03T12:55:35.90121Z","shell.execute_reply.started":"2022-10-03T12:55:35.884168Z","shell.execute_reply":"2022-10-03T12:55:35.899771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}