{"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":"%%capture\n!conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!pip install '/kaggle/input/ensembleboxes-106/ensemble_boxes-1.0.6-py3-none-any.whl' -f ./ --no-index --no-deps","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:02.166464Z","iopub.execute_input":"2021-08-07T22:13:02.167032Z","iopub.status.idle":"2021-08-07T22:13:26.523787Z","shell.execute_reply.started":"2021-08-07T22:13:02.166908Z","shell.execute_reply":"2021-08-07T22:13:26.522754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport os\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm\nimport numpy as np\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom fastai.vision.all import *\nimport albumentations as A\nimport cv2\nfrom pathlib import Path\nfrom joblib import Parallel, delayed\nimport shutil\nfrom ensemble_boxes import *","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:26.527462Z","iopub.execute_input":"2021-08-07T22:13:26.527733Z","iopub.status.idle":"2021-08-07T22:13:30.496803Z","shell.execute_reply.started":"2021-08-07T22:13:26.527705Z","shell.execute_reply":"2021-08-07T22:13:30.495952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\nimport timm","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:30.498571Z","iopub.execute_input":"2021-08-07T22:13:30.498944Z","iopub.status.idle":"2021-08-07T22:13:31.849945Z","shell.execute_reply.started":"2021-08-07T22:13:30.498909Z","shell.execute_reply":"2021-08-07T22:13:31.848982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dicom2np(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    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \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(img, max_size, keep_ratio=True, interpolation=cv2.INTER_LANCZOS4):\n    \n    if keep_ratio:\n        tfms = A.Compose(\n            [A.LongestMaxSize(max_size = max_size, interpolation = interpolation)])\n    else:\n        tfms = A.Compose(\n            [A.Resize(height = max_size, width = max_size, interpolation = interpolation)])\n        \n    tfmd = tfms(image=img)    \n   \n    return tfmd\n\ndef process_item(path):\n    \n    tfmd = resize(dicom2np(path), 1024)\n    \n    img_path = Path(*path.parts[3:]).with_suffix('.jpg')\n    cv2.imwrite('test/' + img_path.name, tfmd['image'])","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:31.851840Z","iopub.execute_input":"2021-08-07T22:13:31.852181Z","iopub.status.idle":"2021-08-07T22:13:31.861804Z","shell.execute_reply.started":"2021-08-07T22:13:31.852145Z","shell.execute_reply":"2021-08-07T22:13:31.860836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fast_sub = (pd.read_csv('/kaggle/input/siim-covid19-detection/sample_submission.csv').shape[0] == 2477)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:31.863234Z","iopub.execute_input":"2021-08-07T22:13:31.863891Z","iopub.status.idle":"2021-08-07T22:13:31.883712Z","shell.execute_reply.started":"2021-08-07T22:13:31.863854Z","shell.execute_reply":"2021-08-07T22:13:31.882993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if fast_sub:\n    dicom_files = get_files(f'../input/siim-covid19-detection/test', extensions=['.dcm', '.dicom'])[:32]\nelse:\n    dicom_files = get_files(f'../input/siim-covid19-detection/test', extensions=['.dcm', '.dicom'])\n\nif not os.path.isdir('/kaggle/working/test'):\n    os.makedirs('/kaggle/working/test')\no = Parallel(n_jobs=4)(delayed(process_item)(f) for f in tqdm(dicom_files))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:31.884810Z","iopub.execute_input":"2021-08-07T22:13:31.885169Z","iopub.status.idle":"2021-08-07T22:13:48.947286Z","shell.execute_reply.started":"2021-08-07T22:13:31.885133Z","shell.execute_reply":"2021-08-07T22:13:48.946364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img2path = {f.stem : f for f in dicom_files}\nimg2study = {f.stem : f.parent.parent.name for f in dicom_files}","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:48.948647Z","iopub.execute_input":"2021-08-07T22:13:48.948975Z","iopub.status.idle":"2021-08-07T22:13:48.957105Z","shell.execute_reply.started":"2021-08-07T22:13:48.948937Z","shell.execute_reply":"2021-08-07T22:13:48.953184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Detection","metadata":{}},{"cell_type":"code","source":"if not os.path.isdir('/kaggle/working/yolov5'):\n    shutil.copytree('/kaggle/input/yolov5-official-v31-dataset/yolov5', '/kaggle/working/yolov5')\nos.chdir('/kaggle/working/yolov5')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:48.960030Z","iopub.execute_input":"2021-08-07T22:13:48.960302Z","iopub.status.idle":"2021-08-07T22:13:49.469851Z","shell.execute_reply.started":"2021-08-07T22:13:48.960275Z","shell.execute_reply":"2021-08-07T22:13:49.468980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_results(file):\n    dicom_path = Path(*(['/kaggle'] + list(img2path[file.stem].parts)[1:]))\n    with open(file, 'r') as f:\n        data = np.array(f.read().replace('\\n', ' ').strip().split(' ')).astype(np.float32).reshape(-1, 6)\n    bbox = data[:,1:5]\n    conf = data[:,  5]\n    label = data[:, 0]\n    \n    dicom = pydicom.filereader.dcmread(dicom_path, stop_before_pixels=True)\n    width, height = dicom.Columns, dicom.Rows\n    \n    bbox[:,[0,1]] = bbox[:,[0,1]] - bbox[:,[2,3]]/2\n    bbox[:,[2,3]] = bbox[:,[0,1]] + bbox[:,[2,3]]\n    \n    ids = file.stem\n    \n    return {'ids':ids, 'label':label, 'conf':conf, 'bbox':bbox, 'width': width, 'height':height}","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:49.473360Z","iopub.execute_input":"2021-08-07T22:13:49.473644Z","iopub.status.idle":"2021-08-07T22:13:49.481388Z","shell.execute_reply.started":"2021-08-07T22:13:49.473618Z","shell.execute_reply":"2021-08-07T22:13:49.480654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n\ntest_dir = '/kaggle/working/test'\n#img_size = 640\n\nweights_list = [\n    [' '.join([\n    '/kaggle/input/siimcovid19-models/yolov5s_10_640_BL1-CV0.pt',\n    '/kaggle/input/siimcovid19-models/yolov5s_10_640_BL1-CV1.pt',\n    '/kaggle/input/siimcovid19-models/yolov5s_10_640_BL1-CV2.pt',\n    '/kaggle/input/siimcovid19-models/yolov5s_10_640_BL1-CV3.pt']), 640, 0.001, 0.5],\n    [' '.join([\n    '/kaggle/input/siimcovid19modelsv2/yolov5m_30_768_BL2-CV0.pth',\n    '/kaggle/input/siimcovid19modelsv2/yolov5m_30_768_BL2-CV1.pth',\n    '/kaggle/input/siimcovid19modelsv2/yolov5m_30_768_BL2-CV2.pth',   \n    '/kaggle/input/siimcovid19modelsv2/yolov5m_30_768_BL2-CV3.pth']), 786, 0.001, 0.5],   \n]\n\nresults = []\n\nfor weights, img_size, conf, iou in weights_list:\n    !python detect.py --weights $weights\\\n    --img $img_size\\\n    --augment\\\n    --conf $conf\\\n    --iou $iou\\\n    --source $test_dir\\\n    --device 0\\\n    --save-txt --save-conf --exist-ok\n\n    txt_files = get_files('runs/detect/exp/labels', extensions=['.txt'])\n    results.append([read_results(txt_file) for txt_file in txt_files])\n    shutil.rmtree('/kaggle/working/yolov5/runs/detect/exp/labels')\n    \nresults = list(zip(*results))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:18:49.448937Z","iopub.execute_input":"2021-08-07T22:18:49.449288Z","iopub.status.idle":"2021-08-07T22:19:34.126286Z","shell.execute_reply.started":"2021-08-07T22:18:49.449254Z","shell.execute_reply":"2021-08-07T22:19:34.124453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fuse_and_scale(result):\n    \n    ids, labels, scores, bboxes, width, height = zip(*[d.values() for d in result])\n\n    iou_thr = 0.45\n    skip_box_thr = 0.0001\n    weights = [1] * len(labels)\n\n    bboxes, scores, labels = weighted_boxes_fusion(\n        bboxes, scores, labels,\n        weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)\n    bboxes = bboxes * np.array([[width[0], height[0], width[0], height[0]]])\n    \n    return {'id':ids[0]+'_image', 'scores':scores, 'bboxes':bboxes, 'labels':labels}","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:20:25.584517Z","iopub.execute_input":"2021-08-07T22:20:25.584903Z","iopub.status.idle":"2021-08-07T22:20:25.593315Z","shell.execute_reply.started":"2021-08-07T22:20:25.584869Z","shell.execute_reply":"2021-08-07T22:20:25.592342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\nfused_results = pd.DataFrame([fuse_and_scale(r) for r in results])\n#fused_results = results[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:20:25.871714Z","iopub.execute_input":"2021-08-07T22:20:25.872117Z","iopub.status.idle":"2021-08-07T22:20:27.643243Z","shell.execute_reply.started":"2021-08-07T22:20:25.872085Z","shell.execute_reply":"2021-08-07T22:20:27.642333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Classification","metadata":{}},{"cell_type":"code","source":"def get_dls(df, presize, size, bs):\n\n    covid19 = DataBlock(\n        blocks=(ImageBlock(cls=PILImageBW), MultiCategoryBlock),\n        splitter = RandomSplitter(),\n        getters=[ColReader('path'), ColReader('label')],\n        item_tfms = Resize(presize, method='squish'),\n        batch_tfms = [*aug_transforms(size = size, mult=0.5), Normalize.from_stats(mean=0.53, std=0.23)]\n        )\n    return covid19.dataloaders(df, bs = bs, workers = 4)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:55.217795Z","iopub.execute_input":"2021-08-07T22:13:55.218721Z","iopub.status.idle":"2021-08-07T22:13:55.227019Z","shell.execute_reply.started":"2021-08-07T22:13:55.218692Z","shell.execute_reply":"2021-08-07T22:13:55.226185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_files = get_image_files('/kaggle/working/test')\ndf = pd.DataFrame({'path': img_files})\ndf['ImageUID'] = df['path'].apply(lambda x: x.stem + '_image')\ndf['StudyUID'] = df['path'].apply(lambda x: img2study[x.stem] + '_study')\ndf['label'] = [[\"negative\", \"typical\", \"indeterminate\", \"atypical\"]] * len(df)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:55.228352Z","iopub.execute_input":"2021-08-07T22:13:55.228716Z","iopub.status.idle":"2021-08-07T22:13:55.263344Z","shell.execute_reply.started":"2021-08-07T22:13:55.228680Z","shell.execute_reply":"2021-08-07T22:13:55.262459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = get_dls(df, 768, 384, 64)\ntest_dl = dls.test_dl(img_files)\ntest_dl.show_batch()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:13:55.264790Z","iopub.execute_input":"2021-08-07T22:13:55.265116Z","iopub.status.idle":"2021-08-07T22:14:02.006279Z","shell.execute_reply.started":"2021-08-07T22:13:55.265083Z","shell.execute_reply":"2021-08-07T22:14:02.005170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MultiHeadModel(Module):\n    def __init__(self, body, head1, head4):\n        self.body = body\n        self.head1 = head1\n        self.head4 = head4\n    \n    def forward(self, x):\n        features = self.body(x)\n        #ys = [self.head4(features), self.head1(features)]\n        #y = torch.cat(ys, dim = -1)\n        return self.head4(features)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:14:02.007505Z","iopub.execute_input":"2021-08-07T22:14:02.007911Z","iopub.status.idle":"2021-08-07T22:14:02.015179Z","shell.execute_reply.started":"2021-08-07T22:14:02.007865Z","shell.execute_reply":"2021-08-07T22:14:02.013908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TransformerModel(Module):\n    def __init__(self, model):\n        self.model = model\n    \n    def forward(self, x):\n        y = self.model(x)\n        return y[:, :-1]\n    \nclass TransformerModelAux(Module):\n    def __init__(self, model):\n        self.model = model\n    \n    def forward(self, x):\n        y = self.model(x)\n        return y[:, -1:]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:14:02.017066Z","iopub.execute_input":"2021-08-07T22:14:02.017512Z","iopub.status.idle":"2021-08-07T22:14:02.026972Z","shell.execute_reply.started":"2021-08-07T22:14:02.017470Z","shell.execute_reply":"2021-08-07T22:14:02.025860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_metadata = [\n    ['/kaggle/input/siimcovid19modelsv2/deit_base_patch16_384-CV0.pth', 'deit_base_patch16_384', 1, 512, 384, 32],\n    ['/kaggle/input/siimcovid19modelsv2/deit_base_patch16_384-CV1.pth', 'deit_base_patch16_384', 1, 512, 384, 32],\n    ['/kaggle/input/siimcovid19modelsv2/deit_base_patch16_384-CV2.pth', 'deit_base_patch16_384', 1, 512, 384, 32],\n    ['/kaggle/input/siimcovid19modelsv2/deit_base_patch16_384-CV3.pth', 'deit_base_patch16_384', 1, 512, 384, 32],\n    ['/kaggle/input/siimcovid19modelsv2/swin_base_patch4_window12_384-CV0.pth', 'swin_base_patch4_window12_384', 1, 512, 384, 32],\n    ['/kaggle/input/siimcovid19modelsv2/swin_base_patch4_window12_384-CV1.pth', 'swin_base_patch4_window12_384', 1, 512, 384, 32],\n    ['/kaggle/input/siimcovid19modelsv2/swin_base_patch4_window12_384-CV2.pth', 'swin_base_patch4_window12_384', 1, 512, 384, 32],\n    ['/kaggle/input/siimcovid19modelsv2/swin_base_patch4_window12_384-CV3.pth', 'swin_base_patch4_window12_384', 1, 512, 384, 32],  \n    ['/kaggle/input/siimcovid19modelsv2/twins_pcpvt_base-CV0.pth', 'twins_pcpvt_base', 1, 512, 384, 32],  \n    ['/kaggle/input/siimcovid19modelsv2/twins_pcpvt_base-CV1.pth', 'twins_pcpvt_base', 1, 512, 384, 32],  \n    ['/kaggle/input/siimcovid19modelsv2/twins_pcpvt_base-CV2.pth', 'twins_pcpvt_base', 1, 512, 384, 32],  \n    ['/kaggle/input/siimcovid19modelsv2/twins_pcpvt_base-CV3.pth', 'twins_pcpvt_base', 1, 512, 384, 32],  \n]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:14:02.028664Z","iopub.execute_input":"2021-08-07T22:14:02.029098Z","iopub.status.idle":"2021-08-07T22:14:02.041228Z","shell.execute_reply.started":"2021-08-07T22:14:02.029061Z","shell.execute_reply":"2021-08-07T22:14:02.040176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n\npreds_acc = []\n\nfor weights, arch, cut, presize, size, bs in model_metadata:\n    \n    dls = get_dls(df, presize, size, bs)\n    test_dl = dls.test_dl(img_files)\n    \n    if cut < 0:\n        body = timm.create_model(arch, pretrained=False, num_classes = 0, in_chans = 1)\n        body = nn.Sequential(*list(body.children())[:cut])\n        head1 = create_head(num_features_model(body), 1, concat_pool=True)\n        head4 = create_head(num_features_model(body), 4, concat_pool=True)\n        model = MultiHeadModel(body, head1, head4)\n    \n    else:\n        model = timm.create_model(arch, pretrained=False, num_classes = 5, in_chans = 1)\n    \n    if torch.cuda.is_available():\n        model.load_state_dict(torch.load(weights))\n    else:\n        model.load_state_dict(torch.load(weights, map_location=torch.device('cpu')))\n        \n    learn = Learner(dls, model, loss_func = BCEWithLogitsLossFlat())\n    preds = learn.tta(dl = test_dl)[0]\n    preds_acc += [preds]\n    \n    \npreds_acc = torch.stack(preds_acc).mean(dim=0)\n# preds_acc, preds_acc_aux = torch.split(preds_acc, 4, dim=1)\npreds_acc, preds_acc_aux = preds_acc[:,:4], preds_acc[:,3:4]\npreds_acc.mean(dim = 0), preds_acc_aux.mean()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:14:02.042785Z","iopub.execute_input":"2021-08-07T22:14:02.043181Z","iopub.status.idle":"2021-08-07T22:16:18.517761Z","shell.execute_reply.started":"2021-08-07T22:14:02.043146Z","shell.execute_reply":"2021-08-07T22:16:18.516613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab = [\"atypical\", \"indeterminate\", \"typical\", \"negative\"]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:16:18.519427Z","iopub.execute_input":"2021-08-07T22:16:18.519806Z","iopub.status.idle":"2021-08-07T22:16:18.524859Z","shell.execute_reply.started":"2021-08-07T22:16:18.519766Z","shell.execute_reply":"2021-08-07T22:16:18.523583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df = pd.DataFrame(torch.cat([preds_acc, preds_acc_aux], dim = 1), columns = vocab + ['is_none'])\npreds_df['id'] = df['StudyUID']\npreds_df = preds_df.groupby('id').agg('mean').reset_index()\npreds_df.corr()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:16:18.526187Z","iopub.execute_input":"2021-08-07T22:16:18.526585Z","iopub.status.idle":"2021-08-07T22:16:18.560901Z","shell.execute_reply.started":"2021-08-07T22:16:18.526549Z","shell.execute_reply":"2021-08-07T22:16:18.559863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df = preds_df.drop('is_none', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:16:18.562315Z","iopub.execute_input":"2021-08-07T22:16:18.562863Z","iopub.status.idle":"2021-08-07T22:16:18.570850Z","shell.execute_reply.started":"2021-08-07T22:16:18.562805Z","shell.execute_reply":"2021-08-07T22:16:18.569582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df_aux = pd.DataFrame({\n    'id': df['ImageUID'],\n    'is_none': preds_acc_aux.flatten()\n})","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:16:18.576347Z","iopub.execute_input":"2021-08-07T22:16:18.576669Z","iopub.status.idle":"2021-08-07T22:16:18.583536Z","shell.execute_reply.started":"2021-08-07T22:16:18.576639Z","shell.execute_reply":"2021-08-07T22:16:18.582592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building the prediction Strings","metadata":{}},{"cell_type":"markdown","source":"### Study level prediction","metadata":{}},{"cell_type":"code","source":"prediction_string = [' '.join([f'{v} {p:.6f} 0 0 1 1' for v, p in zip(list(preds_df.columns)[1:], pp)]) for pp in preds_df.drop('id', axis = 1).values]\nsubmission_study = pd.DataFrame({\n    'id': preds_df['id'],\n    'PredictionString': prediction_string\n}).sort_values('id').reset_index(drop=True)\nsubmission_study.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:20:40.641433Z","iopub.execute_input":"2021-08-07T22:20:40.641794Z","iopub.status.idle":"2021-08-07T22:20:40.658347Z","shell.execute_reply.started":"2021-08-07T22:20:40.641761Z","shell.execute_reply":"2021-08-07T22:20:40.657296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Image level prediction","metadata":{}},{"cell_type":"code","source":"submission_dummy = pd.DataFrame({\n    'id': preds_df_aux['id'],\n    'PredictionString': preds_df_aux['is_none'].apply(lambda x: f'none {x:.6f} 0 0 1 1')\n})\nsubmission_dummy.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:20:43.799171Z","iopub.execute_input":"2021-08-07T22:20:43.799507Z","iopub.status.idle":"2021-08-07T22:20:43.814193Z","shell.execute_reply.started":"2021-08-07T22:20:43.799477Z","shell.execute_reply":"2021-08-07T22:20:43.813320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fused_results = fused_results.merge(preds_df_aux, on = 'id')\nfused_results['is_none'] = fused_results.apply(lambda x: [x['is_none']] * len(x.labels), axis = 1)\nfused_results['PredictionString'] = fused_results.apply(lambda x: ' '.join(f\"opacity {(score*(1-is_none)):.6f} {' '.join(map(str, map(round, bbox)))}\" for score, bbox, is_none in zip(x.scores, x.bboxes, x.is_none)), axis = 1)\n#fused_results.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:20:45.902870Z","iopub.execute_input":"2021-08-07T22:20:45.903256Z","iopub.status.idle":"2021-08-07T22:20:45.976243Z","shell.execute_reply.started":"2021-08-07T22:20:45.903216Z","shell.execute_reply":"2021-08-07T22:20:45.975306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_image = fused_results[['id', 'PredictionString']]\nsubmission_dummy['PredictionString'] = submission_dummy.merge(submission_image, on = 'id', how = 'outer').fillna('').apply(lambda x: x[1] + ' ' + x[2], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:20:55.991749Z","iopub.execute_input":"2021-08-07T22:20:55.992098Z","iopub.status.idle":"2021-08-07T22:20:56.004201Z","shell.execute_reply.started":"2021-08-07T22:20:55.992069Z","shell.execute_reply":"2021-08-07T22:20:56.003099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Final prediction","metadata":{}},{"cell_type":"code","source":"submission = pd.concat([submission_study, submission_dummy])\nsubmission.to_csv('/kaggle/working/submission.csv',index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:20:57.833544Z","iopub.execute_input":"2021-08-07T22:20:57.833895Z","iopub.status.idle":"2021-08-07T22:20:57.858621Z","shell.execute_reply.started":"2021-08-07T22:20:57.833865Z","shell.execute_reply":"2021-08-07T22:20:57.857663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/yolov5')\nshutil.rmtree('/kaggle/working/test')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T22:16:18.886059Z","iopub.status.idle":"2021-08-07T22:16:18.886789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}