{"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":"!cp ../input/gdcm-conda-install/gdcm.tar .\n!tar -xvzf gdcm.tar\n!conda install --offline ./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:00:03.191429Z","iopub.execute_input":"2021-08-08T12:00:03.191809Z","iopub.status.idle":"2021-08-08T12:00:28.439585Z","shell.execute_reply.started":"2021-08-08T12:00:03.191731Z","shell.execute_reply":"2021-08-08T12:00:28.438623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \ndf = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')\nif df.shape[0] == 2477:\n    fast_sub = True\n    df.to_csv('submission.csv', index=False)\nelse:\n    fast_sub = False","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:00:28.443301Z","iopub.execute_input":"2021-08-08T12:00:28.443575Z","iopub.status.idle":"2021-08-08T12:00:28.691147Z","shell.execute_reply.started":"2021-08-08T12:00:28.443545Z","shell.execute_reply":"2021-08-08T12:00:28.690317Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm\nimport numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data\n\ndef resize(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im\n\nimage_id = []\nstudy_id = []\ndim0 = []\ndim1 = []\nsplit = 'test'\nsave_dir = f'/kaggle/tmp/{split}/image/'\nos.makedirs(save_dir, exist_ok=True)\n\nfor dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n    for file in filenames:\n        # set keep_ratio=True to have original aspect ratio\n        xray = read_xray(os.path.join(dirname, file))\n        im = resize(xray, size=512)  \n        im.save(os.path.join(save_dir, file.replace('.dcm', '.png')))\n        image_id.append(file.replace('.dcm', ''))\n        study_id.append(dirname.split('/')[-2])\n        dim0.append(xray.shape[0])\n        dim1.append(xray.shape[1])\n        \n        if len(dim0) >10 and fast_sub:\n            break\n    if len(dim0) >10 and fast_sub:\n            break\nmeta = pd.DataFrame.from_dict({'image_id': image_id, 'dim0': dim0, 'dim1': dim1, 'study_id': study_id})","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:00:28.693012Z","iopub.execute_input":"2021-08-08T12:00:28.693363Z","iopub.status.idle":"2021-08-08T12:00:37.073181Z","shell.execute_reply.started":"2021-08-08T12:00:28.693324Z","shell.execute_reply":"2021-08-08T12:00:37.072251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta.to_csv('test_meta.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:00:37.074757Z","iopub.execute_input":"2021-08-08T12:00:37.07512Z","iopub.status.idle":"2021-08-08T12:00:37.0836Z","shell.execute_reply.started":"2021-08-08T12:00:37.075082Z","shell.execute_reply":"2021-08-08T12:00:37.08282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir det_txt\n!mkdir det\n! pip install ../input/siim-libs/addict-2.4.0-py3-none-any.whl >> /dev/null\n! pip install ../input/siim-libs/timm-0.4.12-py3-none-any.whl >> /dev/null\n! pip install ../input/siim-libs/ensemble_boxes-1.0.6-py3-none-any.whl >> /dev/null\n! pip install ../input/siim-libs/loguru-0.5.3-py3-none-any.whl >> /dev/null\n! pip install ../input/siim-libs/thop-0.0.31.post2005241907-py3-none-any.whl >> /dev/null\n! pip install ../input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl >> /dev/null\n! pip install ../input/omegaconf/omegaconf-2.0.5-py3-none-any.whl >> /dev/null","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-08-08T12:00:37.084843Z","iopub.execute_input":"2021-08-08T12:00:37.085204Z","iopub.status.idle":"2021-08-08T12:03:41.908665Z","shell.execute_reply.started":"2021-08-08T12:00:37.085164Z","shell.execute_reply":"2021-08-08T12:03:41.907562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/siim-v5-mh/* .","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:03:41.910334Z","iopub.execute_input":"2021-08-08T12:03:41.9107Z","iopub.status.idle":"2021-08-08T12:03:43.019866Z","shell.execute_reply.started":"2021-08-08T12:03:41.910659Z","shell.execute_reply":"2021-08-08T12:03:43.018797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python inference.py --is_val 0 --output_path det_txt/ --input_path '/kaggle/tmp/test/image/*png' --weight_path '/kaggle/input/siim-v5-weights/*/*/*/best.pt'","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:03:43.02144Z","iopub.execute_input":"2021-08-08T12:03:43.021781Z","iopub.status.idle":"2021-08-08T12:04:43.431149Z","shell.execute_reply.started":"2021-08-08T12:03:43.021745Z","shell.execute_reply":"2021-08-08T12:04:43.43009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/yolox-inference/* .","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:04:43.434478Z","iopub.execute_input":"2021-08-08T12:04:43.434941Z","iopub.status.idle":"2021-08-08T12:04:44.417953Z","shell.execute_reply.started":"2021-08-08T12:04:43.434898Z","shell.execute_reply":"2021-08-08T12:04:44.416713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python inference.py -f ../input/yolox-weights/yolox_weights/yolox_siim_d_f0.py -c ss --path '/kaggle/tmp/test/image/*png' \\\n    --wei_dir ../input/yolox-weights/yolox_weights/ --conf 0.0001 --nms 0.5 --tsize 384 --save_result --device gpu","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:04:44.423702Z","iopub.execute_input":"2021-08-08T12:04:44.425849Z","iopub.status.idle":"2021-08-08T12:05:33.528196Z","shell.execute_reply.started":"2021-08-08T12:04:44.425807Z","shell.execute_reply":"2021-08-08T12:05:33.527129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/siimeffdet/* .","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:05:33.529904Z","iopub.execute_input":"2021-08-08T12:05:33.530268Z","iopub.status.idle":"2021-08-08T12:05:34.662082Z","shell.execute_reply.started":"2021-08-08T12:05:33.530217Z","shell.execute_reply":"2021-08-08T12:05:34.661011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python predict_oof_det.py --is_val 0 --test_path '/kaggle/tmp/test/image/*png' --model_dir ../input/siim-det-models/","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:05:34.665465Z","iopub.execute_input":"2021-08-08T12:05:34.665746Z","iopub.status.idle":"2021-08-08T12:06:03.367993Z","shell.execute_reply.started":"2021-08-08T12:05:34.665716Z","shell.execute_reply":"2021-08-08T12:06:03.367017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! cp det/* det_txt","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:06:03.369521Z","iopub.execute_input":"2021-08-08T12:06:03.369883Z","iopub.status.idle":"2021-08-08T12:06:04.013524Z","shell.execute_reply.started":"2021-08-08T12:06:03.369844Z","shell.execute_reply":"2021-08-08T12:06:04.012473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! python ensemble1.py --input_path  'det_txt/*txt' --image_path '/kaggle/tmp/test/image/*png' --thr 0.001","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:06:04.016887Z","iopub.execute_input":"2021-08-08T12:06:04.01717Z","iopub.status.idle":"2021-08-08T12:06:10.214946Z","shell.execute_reply.started":"2021-08-08T12:06:04.017139Z","shell.execute_reply":"2021-08-08T12:06:10.213932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#mask_dir = f'/kaggle/tmp/{split}/mask/'\n#os.makedirs(mask_dir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:06:10.216608Z","iopub.execute_input":"2021-08-08T12:06:10.216971Z","iopub.status.idle":"2021-08-08T12:06:10.221632Z","shell.execute_reply.started":"2021-08-08T12:06:10.216932Z","shell.execute_reply":"2021-08-08T12:06:10.220509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cp -r ../input/siimsegs/* .","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:06:10.222983Z","iopub.execute_input":"2021-08-08T12:06:10.223467Z","iopub.status.idle":"2021-08-08T12:06:17.530628Z","shell.execute_reply.started":"2021-08-08T12:06:10.223431Z","shell.execute_reply":"2021-08-08T12:06:17.529575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!python train_seg.py --image_path '/kaggle/tmp/test/image/*png' --weight_path best_loss.pth","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:06:17.532707Z","iopub.execute_input":"2021-08-08T12:06:17.533452Z","iopub.status.idle":"2021-08-08T12:06:34.919324Z","shell.execute_reply.started":"2021-08-08T12:06:17.533376Z","shell.execute_reply":"2021-08-08T12:06:34.918398Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r ../input/siim-cls-code/* .","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:06:39.374573Z","iopub.execute_input":"2021-08-08T12:06:39.374913Z","iopub.status.idle":"2021-08-08T12:06:40.811698Z","shell.execute_reply.started":"2021-08-08T12:06:39.374874Z","shell.execute_reply":"2021-08-08T12:06:40.81048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python main.py -C n_cf11_6 -M test -W ../input/siim-cls-weights/n_cf11_6_f3/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:06:40.813502Z","iopub.execute_input":"2021-08-08T12:06:40.81384Z","iopub.status.idle":"2021-08-08T12:08:11.226819Z","shell.execute_reply.started":"2021-08-08T12:06:40.813803Z","shell.execute_reply":"2021-08-08T12:08:11.225813Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python main.py -C n_cf11 -M test -W ../input/siim-cls-weights/n_cf11_l1/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:08:11.228453Z","iopub.execute_input":"2021-08-08T12:08:11.228827Z","iopub.status.idle":"2021-08-08T12:08:36.924729Z","shell.execute_reply.started":"2021-08-08T12:08:11.228786Z","shell.execute_reply":"2021-08-08T12:08:36.92368Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python main.py -C n_cf11_7 -M test -W ../input/siim-cls-weights/n_cf11_7/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:08:36.928288Z","iopub.execute_input":"2021-08-08T12:08:36.928572Z","iopub.status.idle":"2021-08-08T12:09:06.817821Z","shell.execute_reply.started":"2021-08-08T12:08:36.928538Z","shell.execute_reply":"2021-08-08T12:09:06.816858Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!python main.py -C n_cf11_8 -M test -W ../input/siim-cls-weights/n_cf11_8/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:09:06.821367Z","iopub.execute_input":"2021-08-08T12:09:06.821646Z","iopub.status.idle":"2021-08-08T12:09:32.271982Z","shell.execute_reply.started":"2021-08-08T12:09:06.821618Z","shell.execute_reply":"2021-08-08T12:09:32.270994Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python main.py -C n_cf11_9 -M test -W ../input/siim-cls-weights/n_cf11_9/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:09:32.277539Z","iopub.execute_input":"2021-08-08T12:09:32.277842Z","iopub.status.idle":"2021-08-08T12:09:57.439844Z","shell.execute_reply.started":"2021-08-08T12:09:32.277809Z","shell.execute_reply":"2021-08-08T12:09:57.438849Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python main.py -C n_cf11_10 -M test -W ../input/siim-cls-weights/n_cf11_10/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:09:57.44285Z","iopub.execute_input":"2021-08-08T12:09:57.443219Z","iopub.status.idle":"2021-08-08T12:10:21.010391Z","shell.execute_reply.started":"2021-08-08T12:09:57.443178Z","shell.execute_reply":"2021-08-08T12:10:21.009425Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python main.py -C n_cf11_1 -M test -W ../input/siim-cls-weights/n_cf_11_1/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:10:21.014058Z","iopub.execute_input":"2021-08-08T12:10:21.014347Z","iopub.status.idle":"2021-08-08T12:11:10.929389Z","shell.execute_reply.started":"2021-08-08T12:10:21.014318Z","shell.execute_reply":"2021-08-08T12:11:10.928373Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python main.py -C n_cf11_rot1 -M test -W ../input/siim-cls-weights/n_cf11_rot1/","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:11:10.931037Z","iopub.execute_input":"2021-08-08T12:11:10.931422Z","iopub.status.idle":"2021-08-08T12:11:38.833588Z","shell.execute_reply.started":"2021-08-08T12:11:10.931382Z","shell.execute_reply":"2021-08-08T12:11:38.832481Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q ../input/landmark-additional-packages/EfficientNet-PyTorch/EfficientNet-PyTorch-master \n!pip install -U ../input/landmark-additional-packages/timm-0.4.12-py3-none-any.whl # to fix \n!pip install ../input/siim-libs/segmentation_models_pytorch-0.1.3-py3-none-any.whl --no-deps  ","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-08-08T12:11:40.173385Z","iopub.execute_input":"2021-08-08T12:11:40.173785Z","iopub.status.idle":"2021-08-08T12:12:56.796755Z","shell.execute_reply.started":"2021-08-08T12:11:40.173742Z","shell.execute_reply":"2021-08-08T12:12:56.795629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model difinition","metadata":{}},{"cell_type":"code","source":"import sys\nsys.path.append('../usr/lib/siim_cov_model_v0/')\nfrom siim_cov_model_v0 import *","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:56.798546Z","iopub.execute_input":"2021-08-08T12:12:56.798929Z","iopub.status.idle":"2021-08-08T12:12:57.680692Z","shell.execute_reply.started":"2021-08-08T12:12:56.798885Z","shell.execute_reply":"2021-08-08T12:12:57.679718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## tools and difinition","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tqdm\nimport cv2\nimport glob\nimport math\nimport csv\nimport torch\nimport operator\nimport sys\nimport os\nfrom path import Path\nfrom skimage.io import imread\nfrom scipy.ndimage.interpolation import zoom\nimport matplotlib.pyplot as plt\nfrom skimage.transform import resize\nimport torchvision\nfrom scipy.stats import rankdata\n\nfrom torchvision.transforms import (\n    ToTensor, Normalize, Compose, Resize, CenterCrop, RandomCrop,\n    RandomHorizontalFlip, RandomAffine, RandomVerticalFlip, RandomChoice, ColorJitter, RandomRotation)\n\nsys.path.append('../input/siim-covid-aggron-eecf6c/covid19-aggron')\n\n# from utils import parse_args, prepare_for_result\n# from torch.utils.data import DataLoader, Dataset, WeightedRandomSampler\n# from models import get_model\nfrom losses import get_loss, get_class_balanced_weighted\n# from dataloaders import get_dataloader\n# from utils import load_matched_state\nfrom configs import Config\n# import seaborn as sns\n# from dataloaders.transform_loader import get_tfms\n\nfrom sklearn.metrics import f1_score, roc_auc_score, average_precision_score","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:57.682139Z","iopub.execute_input":"2021-08-08T12:12:57.682497Z","iopub.status.idle":"2021-08-08T12:12:58.354455Z","shell.execute_reply.started":"2021-08-08T12:12:57.682458Z","shell.execute_reply":"2021-08-08T12:12:58.353509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Uncompress test data","metadata":{}},{"cell_type":"code","source":"test_path = '/kaggle/tmp/test/image/'","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:58.362891Z","iopub.execute_input":"2021-08-08T12:12:58.363615Z","iopub.status.idle":"2021-08-08T12:12:58.369933Z","shell.execute_reply.started":"2021-08-08T12:12:58.36353Z","shell.execute_reply":"2021-08-08T12:12:58.368962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class COVIDDataset(torch.utils.data.Dataset):\n    def __init__(self, df, cfg=None, tfms=None, path='.'):\n        self.df = df\n        self.cfg = cfg\n        self.tfms = tfms\n        self.tensor_tfms = torchvision.transforms.Compose([\n            torchvision.transforms.ToTensor(),\n            torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n            # torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n        ])\n        self.path = './'\n        # self.path = Path('/home/sheep/kaggle/siim')\n        self.studys = self.df['StudyInstanceUID'].unique()\n        print(len(self.studys))\n        self.cols = ['Negative for Pneumonia', 'Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance']\n        self.cols2index = {x: i for i, x in enumerate(self.cols)}\n        self.path = path\n\n    def __len__(self):\n        return len(self.studys)\n\n    def __getitem__(self, idx):\n        study_id = self.studys[idx]\n        sub_df = self.df[self.df.StudyInstanceUID == study_id].copy()\n        images = []\n        masks = []\n        study = [idx for _ in range(sub_df.shape[0])]\n        image_as_study = []\n        bbox = []\n        label_study = 0\n        has_masks = []\n        iids = []\n        for i, row in sub_df.iterrows():\n            img = cv2.imread(str(self.path + f'/{row.ImageUID}.png'))\n            sz = self.cfg.transform.size\n            mask = np.zeros((sz, sz))\n            has_mask = 1\n            has_masks.append(has_mask)\n            label = 0\n            if self.tfms:\n                tf = self.tfms(image=img, mask=mask)\n                img = tf['image']\n                mask = tf['mask']\n            if not img.shape[0] == self.cfg.transform.size:\n                img = cv2.resize(img, (self.cfg.transform.size, self.cfg.transform.size))\n            # resize to aux\n            if self.cfg.transform.size == 512:\n                msksz = 32\n            elif self.cfg.transform.size == 384:\n                msksz = 24\n            elif self.cfg.transform.size == 640:\n                msksz = 40\n            elif self.cfg.transform.size == 700:\n                msksz = 44\n            elif self.cfg.transform.size == 720:\n                msksz = 45\n            elif self.cfg.transform.size == 768:\n                msksz = 48\n            else:\n                msksz = 32\n            mask = cv2.resize(mask, (msksz, msksz))\n            masks.append(torch.FloatTensor(mask).view(1, mask.shape[0], mask.shape[1]))\n            img = self.tensor_tfms(img)\n            images.append(img)\n            image_as_study.append(label)\n            iids.append(row.ImageUID)\n        images = torch.stack(images)\n        masks = torch.stack(masks)\n        return images, study, label_study, image_as_study, masks, iids\n    \ndef idoit_collect_func(batch):\n    img, study, lbl, image_as_study, bbox, has_masks = [], [], [], [], [], []\n    for im, st, lb, ias, bb, has_m in batch:\n        img.extend(im)\n        study.extend(st)\n        lbl.append(lb)\n        image_as_study.extend(ias)\n        bbox.extend(bb)\n        has_masks.extend(has_m)\n    return torch.stack(img), study, torch.tensor(lbl), torch.tensor(image_as_study), torch.stack(bbox), has_masks","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:58.371767Z","iopub.execute_input":"2021-08-08T12:12:58.372483Z","iopub.status.idle":"2021-08-08T12:12:58.396443Z","shell.execute_reply.started":"2021-08-08T12:12:58.372438Z","shell.execute_reply":"2021-08-08T12:12:58.395347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(cfg):\n    if cfg.model.name == 'v2m_aux':\n        #     def __init__(self, name, dropout=0, pool='AdaptiveAvgPool2d'):\n        drop = cfg.model.param.get('dropout', 0)\n        pool = cfg.model.param.get('last_pool', 'AdaptiveAvgPool2d')\n        return AUXNet(name='tf_efficientnetv2_m', dropout=drop, pool=pool)\n    elif cfg.model.name == 'v2m_aux_v2':\n        drop = cfg.model.param.get('dropout', 0)\n        pool = cfg.model.param.get('last_pool', 'AdaptiveAvgPool2d')\n        return AUXNetV2(name='tf_efficientnetv2_m', dropout=drop, pool=pool)\n    elif cfg.model.name == 'b5_aux':\n        drop = cfg.model.param.get('dropout', 0)\n        pool = cfg.model.param.get('last_pool', 'AdaptiveAvgPool2d')\n        return AUXNetb5(name='tf_efficientnetv2_m', dropout=drop)\n    elif cfg.model.name == 'v2l_aux':\n        drop = cfg.model.param.get('dropout', 0)\n        pool = cfg.model.param.get('last_pool', 'AdaptiveAvgPool2d')\n        return AUXNetL(name='tf_efficientnetv2_l', dropout=drop)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:58.398166Z","iopub.execute_input":"2021-08-08T12:12:58.398625Z","iopub.status.idle":"2021-08-08T12:12:58.410584Z","shell.execute_reply.started":"2021-08-08T12:12:58.398581Z","shell.execute_reply":"2021-08-08T12:12:58.409208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_run(RUN, test, is_none = False):\n    df = pd.read_csv(f'{RUN}/train.log', sep='\\t')\n    fold2epochs = {}\n    for i in range(0, 5):\n        eph = df[df.Fold == i].sort_values('F1@0.3', ascending=False).iloc[0].Epochs\n        fold2epochs[i] = int(eph)\n\n    print(fold2epochs.values())\n\n    predicted = []\n    # load the model\n    models = []\n    for f in range(5):\n        cfg = Config.load_json(f'{RUN}/config.json')\n        cfg.experiment.run_fold = f\n        model = load_model(cfg).cuda()\n        load_matched_state(model, torch.load(\n            glob.glob(f'{RUN}/checkpoints/f{f}*-{fold2epochs[f]}*')[0]))\n        model.eval()\n        models.append(model)\n        \n    # inference\n    test_ds = COVIDDataset(test, cfg=cfg, path=test_path)\n    test_dl = torch.utils.data.DataLoader(test_ds, num_workers=2, batch_size=32, collate_fn=idoit_collect_func)\n    with torch.no_grad():\n        results = []\n        predicted, image_ids = [], []\n        for i, (img, study_index, lbl_study, label_image, mask_t, iids) in tqdm.tqdm(enumerate(test_dl)):\n            img = img.cuda()\n            sz = img.size()[0]\n            img = torch.stack([img,img.flip(-1)],0) # hflip\n            img = img.view(-1, 3, img.shape[-1], img.shape[-1])\n            preds = []\n            for m in models:\n                with torch.cuda.amp.autocast():\n                    logits, mask = m(img)\n                logits = logits.float()\n                if cfg.loss.name == 'bce':\n                    logits = torch.sigmoid(logits)\n                else:\n                    logits = torch.softmax(logits, 1)\n                cls = (logits[:sz] + logits[sz:]) / 2\n                preds.append(cls)\n            predicted.append(torch.stack(preds).mean(0).cpu())\n            image_ids.extend(iids)\n    if is_none:\n        return pd.DataFrame(torch.cat(predicted).numpy(), index=image_ids,\n             columns=['Negative for Pneumonia', 'Typical Appearance'])\n    else:\n        return pd.DataFrame(torch.cat(predicted).numpy(), index=image_ids,\n             columns=['Negative for Pneumonia', 'Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance'])","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:58.412265Z","iopub.execute_input":"2021-08-08T12:12:58.412703Z","iopub.status.idle":"2021-08-08T12:12:58.428146Z","shell.execute_reply.started":"2021-08-08T12:12:58.412662Z","shell.execute_reply":"2021-08-08T12:12:58.426992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test assets","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('test_meta.csv')[[\"image_id\", \"study_id\"]]\ntest = test.rename(columns={\"image_id\": \"ImageUID\", \"study_id\": \"StudyInstanceUID\"})\nfor e in ['Negative for Pneumonia', 'Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance']:\n    test[e] = 0","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:58.429473Z","iopub.execute_input":"2021-08-08T12:12:58.42986Z","iopub.status.idle":"2021-08-08T12:12:58.449985Z","shell.execute_reply.started":"2021-08-08T12:12:58.429823Z","shell.execute_reply":"2021-08-08T12:12:58.449182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:58.452059Z","iopub.execute_input":"2021-08-08T12:12:58.452781Z","iopub.status.idle":"2021-08-08T12:12:58.470996Z","shell.execute_reply.started":"2021-08-08T12:12:58.452688Z","shell.execute_reply":"2021-08-08T12:12:58.470102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## BCE loss with pl","metadata":{}},{"cell_type":"code","source":"r9 = predict_run(\n    '../input/siim-covid-aux-bce-agg-exp-rot-30-20-v2l-pl/aux_bce_agg_exp_rot_30_20_v2l_pl.upload', test)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:12:58.47397Z","iopub.execute_input":"2021-08-08T12:12:58.474276Z","iopub.status.idle":"2021-08-08T12:13:48.531131Z","shell.execute_reply.started":"2021-08-08T12:12:58.474247Z","shell.execute_reply":"2021-08-08T12:13:48.530192Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r8 = predict_run(\n    '../input/aux-bce-agg-exp-rot-30-20-b5-pl/aux_bce_agg_exp_rot_30_20_b5_pl.upload', test)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:13:48.533218Z","iopub.execute_input":"2021-08-08T12:13:48.533588Z","iopub.status.idle":"2021-08-08T12:14:02.634202Z","shell.execute_reply.started":"2021-08-08T12:13:48.533548Z","shell.execute_reply":"2021-08-08T12:14:02.633265Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = predict_run(\n    '../input/siim-covid-aux-aug-v2m-lm-aggron-40-clean-cut1/aux_aug_v2m_lm_aggron_40_clean_cut1.yaml_upload', test)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:14:02.636401Z","iopub.execute_input":"2021-08-08T12:14:02.636754Z","iopub.status.idle":"2021-08-08T12:14:25.39373Z","shell.execute_reply.started":"2021-08-08T12:14:02.636713Z","shell.execute_reply":"2021-08-08T12:14:25.392549Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r3 = predict_run(\n    '../input/siim-covid-aux-bce-agg-exp-rot-30-20-pl/aux_bce_agg_exp_rot_30_20_pl.upload', test)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:14:25.395995Z","iopub.execute_input":"2021-08-08T12:14:25.396538Z","iopub.status.idle":"2021-08-08T12:14:51.413171Z","shell.execute_reply.started":"2021-08-08T12:14:25.396492Z","shell.execute_reply":"2021-08-08T12:14:51.412181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CE model","metadata":{}},{"cell_type":"code","source":"r2 = predict_run(\n    '../input/siim-covid-aux-bce-v2m-lm-aggron-40-clean-cut1-pl/aux_bce_v2m_lm_aggron_40_clean_cut1_bce_pl.upload', test)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:14:51.415322Z","iopub.execute_input":"2021-08-08T12:14:51.415695Z","iopub.status.idle":"2021-08-08T12:15:14.920759Z","shell.execute_reply.started":"2021-08-08T12:14:51.415654Z","shell.execute_reply":"2021-08-08T12:15:14.919886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r4 = predict_run(\n    '../input/siim-cov-dddddd-dbg-1-aux-2/dddddd_dbg_1_aux_2_upload', test)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:15:14.922545Z","iopub.execute_input":"2021-08-08T12:15:14.922888Z","iopub.status.idle":"2021-08-08T12:15:37.95226Z","shell.execute_reply.started":"2021-08-08T12:15:14.922849Z","shell.execute_reply":"2021-08-08T12:15:37.951285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r5 = predict_run(\n    '../input/siim-cov-clean-oof-clean-agree-upload/clean_oof_clean_agree_upload', test)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:15:37.954466Z","iopub.execute_input":"2021-08-08T12:15:37.95504Z","iopub.status.idle":"2021-08-08T12:16:03.094763Z","shell.execute_reply.started":"2021-08-08T12:15:37.954992Z","shell.execute_reply":"2021-08-08T12:16:03.093783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r6 = predict_run(\n    '../input/siim-cov-aux-aug-agg-exp-rot-30/aux_aug_agg_exp_rot_30.yaml_upload', test)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:16:03.096687Z","iopub.execute_input":"2021-08-08T12:16:03.097183Z","iopub.status.idle":"2021-08-08T12:16:25.330113Z","shell.execute_reply.started":"2021-08-08T12:16:03.097144Z","shell.execute_reply":"2021-08-08T12:16:25.328113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r7 = predict_run(\n    '../input/siim-cov-model-modelv2upload/model_modelV2.upload', test)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:16:25.334805Z","iopub.execute_input":"2021-08-08T12:16:25.336983Z","iopub.status.idle":"2021-08-08T12:16:49.432402Z","shell.execute_reply.started":"2021-08-08T12:16:25.336938Z","shell.execute_reply":"2021-08-08T12:16:49.431273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_result = (r + r2 + r3 + 0.5 * r4 + r5 + r6 + 0.75 * r7 + r8 + r9) / 8.25","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:16:49.442173Z","iopub.execute_input":"2021-08-08T12:16:49.442804Z","iopub.status.idle":"2021-08-08T12:16:49.458374Z","shell.execute_reply.started":"2021-08-08T12:16:49.442758Z","shell.execute_reply":"2021-08-08T12:16:49.457471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_result = image_result.reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:16:49.468039Z","iopub.execute_input":"2021-08-08T12:16:49.468722Z","iopub.status.idle":"2021-08-08T12:16:49.476028Z","shell.execute_reply.started":"2021-08-08T12:16:49.468684Z","shell.execute_reply":"2021-08-08T12:16:49.475192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_result.to_csv('sheep_df.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:16:49.479482Z","iopub.execute_input":"2021-08-08T12:16:49.47982Z","iopub.status.idle":"2021-08-08T12:16:49.508581Z","shell.execute_reply.started":"2021-08-08T12:16:49.479779Z","shell.execute_reply":"2021-08-08T12:16:49.507683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**2 class**","metadata":{}},{"cell_type":"code","source":"class AUXNet(nn.Module):\n    def __init__(self, name, dropout=0, pool='AdaptiveAvgPool2d'):\n        super(AUXNet, self).__init__()\n\n        print('[ AUX model ] dropout: {}, pool: {}'.format(dropout, pool))\n        e = timm.models.__dict__[name](pretrained=False, drop_rate=0.3, drop_path_rate=0.2)\n        self.model = e\n        self.b0 = nn.Sequential(\n            e.conv_stem,\n            e.bn1,\n            e.act1,\n        )\n        self.b1 = e.blocks[0]\n        self.b2 = e.blocks[1]\n        self.b3 = e.blocks[2]\n        self.b4 = e.blocks[3]\n        self.b5 = e.blocks[4]\n        self.b6 = e.blocks[5]\n        self.b7 = e.blocks[6]\n        self.b8 = nn.Sequential(\n            e.conv_head, #384, 1536\n            e.bn2,\n            e.act2,\n        )\n\n        self.logit = nn.Linear(1280,2)\n        self.mask = nn.Sequential(\n            nn.Conv2d(176, 128, kernel_size=3, padding=1),\n            nn.BatchNorm2d(128),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(128, 128, kernel_size=3, padding=1),\n            nn.BatchNorm2d(128),\n            nn.ReLU(inplace=True),\n            nn.Conv2d(128, 1, kernel_size=1, padding=0),\n        )\n\n        self.dropout = nn.Dropout(p=dropout)\n\n        if pool == 'AdaptiveAvgPool2d':\n            self.pooling = nn.AdaptiveAvgPool2d(1)\n        elif pool == 'gem':\n            self.pooling = GeM()\n\n    # @torch.cuda.amp.autocast()\n    def forward(self, image):\n        batch_size = len(image)\n        # x = 2*image-1     # ; print('input ',   x.shape)\n        x = image\n\n        x = self.b0(x) #; print (x.shape)  # torch.Size([2, 40, 256, 256])\n        x = self.b1(x) #; print (x.shape)  # torch.Size([2, 24, 256, 256])\n        x = self.b2(x) #; print (x.shape)  # torch.Size([2, 32, 128, 128])\n        x = self.b3(x) #; print (x.shape)  # torch.Size([2, 48, 64, 64])\n        x = self.b4(x) #; print (x.shape)  # torch.Size([2, 96, 32, 32])\n        x = self.b5(x) #; print (x.shape)  # torch.Size([2, 136, 32, 32])\n        #------------\n        mask = self.mask(x)\n        #-------------\n        x = self.b6(x) #; print (x.shape)  # torch.Size([2, 232, 16, 16])\n        x = self.b7(x) #; print (x.shape)  # torch.Size([2, 384, 16, 16])\n        x = self.b8(x) #; print (x.shape)  # torch.Size([2, 1536, 16, 16])\n        # x = F.adaptive_avg_pool2d(x,1).reshape(batch_size,-1)\n        x = nn.Flatten()(self.pooling(x))\n        x = self.dropout(x)\n        logit = self.logit(x)\n        return logit, mask","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:16:49.515951Z","iopub.execute_input":"2021-08-08T12:16:49.516508Z","iopub.status.idle":"2021-08-08T12:16:49.533936Z","shell.execute_reply.started":"2021-08-08T12:16:49.516471Z","shell.execute_reply":"2021-08-08T12:16:49.532969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"none_1 = predict_run(\n    '../input/two-class-bce-fix-valid-bbox-two-classes-12-pl-2x/two_class_bce_fix_valid_bbox_two_classes_12_pl_2x.upload', test, True)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:16:49.53552Z","iopub.execute_input":"2021-08-08T12:16:49.535997Z","iopub.status.idle":"2021-08-08T12:17:12.724606Z","shell.execute_reply.started":"2021-08-08T12:16:49.535955Z","shell.execute_reply":"2021-08-08T12:17:12.723165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"none_2 = predict_run(\n    '../input/two-class-bce-fix-valid-bbox-two-classes-12upload/two_class_bce_fix_valid_bbox_two_classes_12.upload', test, True)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-08T12:17:12.728919Z","iopub.execute_input":"2021-08-08T12:17:12.729293Z","iopub.status.idle":"2021-08-08T12:17:34.415556Z","shell.execute_reply.started":"2021-08-08T12:17:12.729251Z","shell.execute_reply":"2021-08-08T12:17:34.414268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"none_result = (none_1 + none_2) / 2","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:17:34.417932Z","iopub.execute_input":"2021-08-08T12:17:34.418312Z","iopub.status.idle":"2021-08-08T12:17:34.426345Z","shell.execute_reply.started":"2021-08-08T12:17:34.41827Z","shell.execute_reply":"2021-08-08T12:17:34.425054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"none_result.to_csv('public_test_sheep_predict_none.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:17:34.427953Z","iopub.execute_input":"2021-08-08T12:17:34.428413Z","iopub.status.idle":"2021-08-08T12:17:34.444315Z","shell.execute_reply.started":"2021-08-08T12:17:34.428373Z","shell.execute_reply":"2021-08-08T12:17:34.443303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! python tocsv.py --input_path  'test_v5neg_2a.txt' --meta_path 'test_meta.csv'","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:17:34.448432Z","iopub.execute_input":"2021-08-08T12:17:34.448804Z","iopub.status.idle":"2021-08-08T12:17:36.823855Z","shell.execute_reply.started":"2021-08-08T12:17:34.448764Z","shell.execute_reply":"2021-08-08T12:17:36.82282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prob2str(row):\n    return f'negative {row.pred_cls1:.6f} 0 0 1 1 typical {row.pred_cls2:.6f} 0 0 1 1 indeterminate {row.pred_cls3:.6f} 0 0 1 1 atypical {row.pred_cls4:.6f} 0 0 1 1'\n    #return f''","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:17:36.825544Z","iopub.execute_input":"2021-08-08T12:17:36.825907Z","iopub.status.idle":"2021-08-08T12:17:36.831905Z","shell.execute_reply.started":"2021-08-08T12:17:36.825869Z","shell.execute_reply":"2021-08-08T12:17:36.830203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def combine_image(row):\n     return f'none {row.pred_cls5:.6f} 0 0 1 1 {row.PredictionString}'\n        #return f''","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:17:36.833624Z","iopub.execute_input":"2021-08-08T12:17:36.834298Z","iopub.status.idle":"2021-08-08T12:17:36.845257Z","shell.execute_reply.started":"2021-08-08T12:17:36.834258Z","shell.execute_reply":"2021-08-08T12:17:36.844389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tosub(df1):\n    df1_image = df1[['image_id', 'pred_cls1', 'pred_cls5', 'PredictionString']].copy()\n    df1_image.rename(columns={\"image_id\": \"id\"}, inplace=True)\n    df1_image['id'] = df1_image['id'].apply(lambda x: f'{x}_image')\n    df1_image['PredictionString'] = df1_image.apply(lambda r: combine_image(r), axis=1) \n    df1_image = df1_image[['id', 'PredictionString']]\n    \n    df1_study = df1[['study_id', 'pred_cls1', 'pred_cls2', 'pred_cls3', 'pred_cls4']].copy()\n    df1_study = df1_study.groupby('study_id').agg('mean').reset_index()\n    df1_study.rename(columns={\"study_id\": \"id\"}, inplace=True)\n    df1_study['id'] = df1_study['id'].apply(lambda x: f'{x}_study')\n    df1_study[\"PredictionString\"] = df1_study.apply(lambda r: prob2str(r), axis=1) \n    df1_study = df1_study[['id', 'PredictionString']]\n\n    df1_sub = pd.concat([df1_study, df1_image])\n\n    return df1_sub","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:17:36.846879Z","iopub.execute_input":"2021-08-08T12:17:36.847327Z","iopub.status.idle":"2021-08-08T12:17:36.86575Z","shell.execute_reply.started":"2021-08-08T12:17:36.847285Z","shell.execute_reply":"2021-08-08T12:17:36.864817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_csv('n_cf11_9.csv') \ndf2 = pd.read_csv('n_cf11.csv')#.head(10)\ndf3 = pd.read_csv('n_cf11_6.csv')\ndf4 = pd.read_csv('n_cf11_7.csv')\ndf5 = pd.read_csv('n_cf11_10.csv')\ndf6 = pd.read_csv('n_cf11_rot1.csv')\ndf7 = pd.read_csv('n_cf11_1.csv')\n#df8 = pd.read_csv('n_cf11_8.csv')\n\ndf2 = df1[[\"image_id\"]].merge(df2, on=[\"image_id\"])\ndf3 = df1[[\"image_id\"]].merge(df3, on=[\"image_id\"])\ndf4 = df1[[\"image_id\"]].merge(df4, on=[\"image_id\"])\ndf5 = df1[[\"image_id\"]].merge(df5, on=[\"image_id\"])\ndf6 = df1[[\"image_id\"]].merge(df6, on=[\"image_id\"])\ndf7 = df1[[\"image_id\"]].merge(df7, on=[\"image_id\"])\n#df8 = df1[[\"image_id\"]].merge(df8, on=[\"image_id\"])\n\nsheep_df = pd.read_csv('sheep_df.csv')\nsheep_df = sheep_df.rename(columns={\"index\": \"image_id\", \"Negative for Pneumonia\": \"pred_cls1\", \n                         \"Typical Appearance\": \"pred_cls2\", \"Indeterminate Appearance\": \"pred_cls3\",\n                        \"Atypical Appearance\": \"pred_cls4\"})\nsheep_df = df2[[\"image_id\"]].merge(sheep_df, on=[\"image_id\"])\n\nfor col in ['pred_cls1', 'pred_cls2', 'pred_cls3', 'pred_cls4', 'pred_cls5']:\n    df1[col] = (df1[col] + df2[col] + df3[col] + df4[col] + df5[col] + df6[col] + df7[col])/7\n    \nfor col in ['pred_cls1', 'pred_cls2', 'pred_cls3', 'pred_cls4']:\n    df1[col] = (1*df1[col] + sheep_df[col])/2\n    \nsheep_none_df = pd.read_csv('public_test_sheep_predict_none.csv')\nsheep_none_df[\"image_id\"] = sheep_none_df['Unnamed: 0']\nsheep_none_df = df1[[\"image_id\"]].merge(sheep_none_df, on=[\"image_id\"])\n\n#df1['pred_cls5'] = sheep_none_df['Negative for Pneumonia']\ndf1['pred_cls5'] = 2*(1-df1['pred_cls5']) + 1*sheep_df['pred_cls1'] + 1*sheep_none_df['Negative for Pneumonia']\n\nimage_sub = pd.read_csv('v5_50.csv')\nimage_sub = df1[[\"image_id\"]].merge(image_sub, on=[\"image_id\"])\ndf1['PredictionString'] = image_sub['PredictionString']\ndf_sub  = tosub(df1)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:17:36.867034Z","iopub.execute_input":"2021-08-08T12:17:36.86731Z","iopub.status.idle":"2021-08-08T12:17:36.970584Z","shell.execute_reply.started":"2021-08-08T12:17:36.867285Z","shell.execute_reply":"2021-08-08T12:17:36.96972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.tail()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:18:56.104103Z","iopub.execute_input":"2021-08-08T12:18:56.104535Z","iopub.status.idle":"2021-08-08T12:18:56.114724Z","shell.execute_reply.started":"2021-08-08T12:18:56.104492Z","shell.execute_reply":"2021-08-08T12:18:56.113595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:19:00.697904Z","iopub.execute_input":"2021-08-08T12:19:00.69828Z","iopub.status.idle":"2021-08-08T12:19:00.710119Z","shell.execute_reply.started":"2021-08-08T12:19:00.698225Z","shell.execute_reply":"2021-08-08T12:19:00.709207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r ./*","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:19:07.641743Z","iopub.execute_input":"2021-08-08T12:19:07.642111Z","iopub.status.idle":"2021-08-08T12:19:08.365875Z","shell.execute_reply.started":"2021-08-08T12:19:07.642078Z","shell.execute_reply":"2021-08-08T12:19:08.364617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T12:19:10.511077Z","iopub.execute_input":"2021-08-08T12:19:10.51149Z","iopub.status.idle":"2021-08-08T12:19:10.524809Z","shell.execute_reply.started":"2021-08-08T12:19:10.511457Z","shell.execute_reply":"2021-08-08T12:19:10.523984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}