{"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":"%load_ext autoreload\n%autoreload 2\n\n!conda install '/kaggle/input/pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -y --offline\n\n!pip install '/kaggle/input/kerasapplications' --no-deps\n!pip install '/kaggle/input/efficientnet-keras-source-code' --no-deps\n!pip install '/kaggle/input/effdet-latestvinbigdata-wbf-fused/ensemble_boxes-1.0.4-py3-none-any.whl' --no-deps\n\n## MMDetection compatible torch installation\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torch-1.7.0+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchvision-0.8.1+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchaudio-0.7.0-cp37-cp37m-linux_x86_64.whl' --no-deps\n\n## Compatible Cuda Toolkit installation\n!mkdir -p /kaggle/tmp && cp /kaggle/input/pytorch-170-cuda-toolkit-110221/cudatoolkit-11.0.221-h6bb024c_0 /kaggle/tmp/cudatoolkit-11.0.221-h6bb024c_0.tar.bz2 && conda install /kaggle/tmp/cudatoolkit-11.0.221-h6bb024c_0.tar.bz2 -y --offline\n\n## MMDetection Offline Installation\n!pip install '/kaggle/input/mmdetectionv2140/addict-2.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminal-0.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmcv_full-1_3_8-cu110-torch1_7_0/mmcv_full-1.3.8-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n\n!cp -r /kaggle/input/mmdetectionv2140/mmdetection-2.14.0 /kaggle/working/\n!mv /kaggle/working/mmdetection-2.14.0 /kaggle/working/mmdetection\n%cd /kaggle/working/mmdetection\n!pip install -e . --no-deps\n%cd /kaggle/working/","metadata":{"_uuid":"52b5bd21-4d92-4491-96fc-835adefbadd1","_cell_guid":"c87eb6ab-174b-4b3b-b079-988049491d7f","collapsed":false,"papermill":{"duration":589.21781,"end_time":"2021-07-17T19:03:09.933611","exception":false,"start_time":"2021-07-17T18:53:20.715801","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:28:19.664182Z","iopub.execute_input":"2021-08-04T03:28:19.664584Z","iopub.status.idle":"2021-08-04T03:39:19.197467Z","shell.execute_reply.started":"2021-08-04T03:28:19.6645Z","shell.execute_reply":"2021-08-04T03:39:19.194707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('/kaggle/working/mmdetection')\nsys.path.append('../input/boxfusion/Weighted-Boxes-Fusion-master')\nfrom ensemble_boxes import *\n\nimport os\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm\nimport gc\nimport glob\nimport numpy as np","metadata":{"_uuid":"50bcae9e-02a0-42ba-8da7-e007d7997a28","_cell_guid":"5124d427-78b9-42fa-af21-f3f4dbded560","collapsed":false,"papermill":{"duration":0.104274,"end_time":"2021-07-17T19:03:10.131834","exception":false,"start_time":"2021-07-17T19:03:10.02756","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:19.209606Z","iopub.execute_input":"2021-08-04T03:39:19.214911Z","iopub.status.idle":"2021-08-04T03:39:20.605445Z","shell.execute_reply.started":"2021-08-04T03:39:19.214857Z","shell.execute_reply":"2021-08-04T03:39:20.604388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #00857e; font-family: Segoe UI; font-size: 1.8em; font-weight: 300;\">Create Study and Image Level Dataframes</span>","metadata":{"_uuid":"73024c01-5e91-47cf-b0fe-4a56d2c0f869","_cell_guid":"c465f636-ccc1-4c67-82c8-196ed0ccf8bf","papermill":{"duration":0.092535,"end_time":"2021-07-17T19:03:10.317337","exception":false,"start_time":"2021-07-17T19:03:10.224802","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"sub_df = pd.read_csv('/kaggle/input/siim-covid19-detection/sample_submission.csv')\n\n# Form study and image dataframes\nsub_df['level'] = sub_df.id.map(lambda idx: idx[-5:])\nstudy_df = sub_df[sub_df.level=='study'].rename({'id':'study_id'}, axis=1)\nimage_df = sub_df[sub_df.level=='image'].rename({'id':'image_id'}, axis=1)\n\ndcm_path = glob.glob('/kaggle/input/siim-covid19-detection/test/**/*dcm', recursive=True)\ntest_meta = pd.DataFrame({'dcm_path':dcm_path})\ntest_meta['image_id'] = test_meta.dcm_path.map(lambda x: x.split('/')[-1].replace('.dcm', '')+'_image')\ntest_meta['study_id'] = test_meta.dcm_path.map(lambda x: x.split('/')[-3].replace('.dcm', '')+'_study')\n\nstudy_df = study_df.merge(test_meta, on='study_id', how='left')\nimage_df = image_df.merge(test_meta, on='image_id', how='left')\n\n# Remove duplicates study_ids from study_df\nstudy_df.drop_duplicates(subset=\"study_id\",keep='first', inplace=True)","metadata":{"_uuid":"640ce86d-7737-4b3e-8064-f701ff4c4490","_cell_guid":"fdef40b6-ada5-4a8f-9eaf-31db4f0e6b41","collapsed":false,"papermill":{"duration":5.695531,"end_time":"2021-07-17T19:03:16.105354","exception":false,"start_time":"2021-07-17T19:03:10.409823","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:20.909757Z","iopub.execute_input":"2021-08-04T03:39:20.91146Z","iopub.status.idle":"2021-08-04T03:39:26.255101Z","shell.execute_reply.started":"2021-08-04T03:39:20.911413Z","shell.execute_reply":"2021-08-04T03:39:26.25393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #00857e; font-family: Segoe UI; font-size: 1.8em; font-weight: 300;\">Fast or Full Predictions</span>\n\nIn case of non-competetion submission commits, we run the notebook with just two images each for image level and study level inference from the public test data.","metadata":{"_uuid":"07558e04-18fd-4f2f-8587-255541aae7f2","_cell_guid":"fc811959-7e4d-46fd-b4b8-71976176a972","papermill":{"duration":0.154867,"end_time":"2021-07-17T19:03:16.415952","exception":false,"start_time":"2021-07-17T19:03:16.261085","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"study_df = study_df.reset_index(drop=True)","metadata":{"_uuid":"3b68d401-d9a2-4ac4-b760-ec846a18bad8","_cell_guid":"330ccf65-a8f4-4185-b8ff-d3c09646586c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:26.257585Z","iopub.execute_input":"2021-08-04T03:39:26.257878Z","iopub.status.idle":"2021-08-04T03:39:26.320228Z","shell.execute_reply.started":"2021-08-04T03:39:26.25785Z","shell.execute_reply":"2021-08-04T03:39:26.318873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df","metadata":{"_uuid":"c9292dc3-f3ff-486d-8eda-2d44c351e85f","_cell_guid":"ff810886-c239-41eb-9050-0cc824de6735","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:26.323746Z","iopub.execute_input":"2021-08-04T03:39:26.326096Z","iopub.status.idle":"2021-08-04T03:39:26.41034Z","shell.execute_reply.started":"2021-08-04T03:39:26.326048Z","shell.execute_reply":"2021-08-04T03:39:26.409145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fast_sub = False","metadata":{"_uuid":"3a1d5c69-b531-49bf-8ba6-861ead665307","_cell_guid":"910f41f8-d786-464d-9ee0-34c089875b9c","collapsed":false,"papermill":{"duration":0.28084,"end_time":"2021-07-17T19:03:16.86864","exception":false,"start_time":"2021-07-17T19:03:16.5878","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:26.413321Z","iopub.execute_input":"2021-08-04T03:39:26.415359Z","iopub.status.idle":"2021-08-04T03:39:26.470534Z","shell.execute_reply.started":"2021-08-04T03:39:26.415298Z","shell.execute_reply":"2021-08-04T03:39:26.469054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if sub_df.shape[0] == 2477:\n    fast_sub = True\n    study_df = study_df.sample(2)\n    study_df = study_df.reset_index(drop=True)\n    image_df = image_df.sample(2)\n    image_df = image_df.reset_index(drop=True)\n    \n    print(\"\\nstudy_df\")\n    display(study_df.head(2))\n    print(\"\\nimage_df\")\n    display(image_df.head(2))\n    print(\"\\ntest_meta\")\n    display(test_meta.head(2))\n    ","metadata":{"execution":{"iopub.status.busy":"2021-08-04T03:39:26.474109Z","iopub.execute_input":"2021-08-04T03:39:26.47627Z","iopub.status.idle":"2021-08-04T03:39:26.581083Z","shell.execute_reply.started":"2021-08-04T03:39:26.476223Z","shell.execute_reply":"2021-08-04T03:39:26.579897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\nSTUDY_DIMS = (768, 768)\nIMAGE_DIMS = (512, 512)\n\nstudy_dir = f'/kaggle/tmp/test/study/'\nos.makedirs(study_dir, exist_ok=True)\n\nimage_dir = f'/kaggle/tmp/test/image/'\nos.makedirs(image_dir, exist_ok=True)\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    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    return im\nstudy_df['dim0'] = -1\nstudy_df['dim1'] = -1\n\nfor index, row in tqdm(study_df[['study_id', 'dcm_path','dim0', 'dim1']].iterrows(), total=study_df.shape[0]):\n    # set keep_ratio=True to have original aspect ratio\n    xray = read_xray(row['dcm_path'])\n    im = resize(xray, size=STUDY_DIMS[0])\n    im.save(os.path.join(study_dir, row['study_id']+'.png'))\n    study_df.loc[study_df.study_id==row.study_id, 'dim0'] = xray.shape[0]\n    study_df.loc[study_df.study_id==row.study_id, 'dim1'] = xray.shape[1]\n\nimage_df['dim0'] = -1\nimage_df['dim1'] = -1\n\nfor index, row in tqdm(image_df[['image_id', 'dcm_path', 'dim0', 'dim1']].iterrows(), total=image_df.shape[0]):\n    # set keep_ratio=True to have original aspect ratio\n    xray = read_xray(row['dcm_path'])\n    im = resize(xray, size=IMAGE_DIMS[0])  \n    im.save(os.path.join(image_dir, row['image_id']+'.png'))\n    image_df.loc[image_df.image_id==row.image_id, 'dim0'] = xray.shape[0]\n    image_df.loc[image_df.image_id==row.image_id, 'dim1'] = xray.shape[1]","metadata":{"_uuid":"9b0aff5f-d1f3-4062-81e0-4d41700559b7","_cell_guid":"4a2dbbe9-b3f9-449d-a7d6-53ee7198944b","collapsed":false,"papermill":{"duration":4.178244,"end_time":"2021-07-17T19:03:21.225656","exception":false,"start_time":"2021-07-17T19:03:17.047412","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:26.583855Z","iopub.execute_input":"2021-08-04T03:39:26.585714Z","iopub.status.idle":"2021-08-04T03:39:31.066334Z","shell.execute_reply.started":"2021-08-04T03:39:26.585669Z","shell.execute_reply":"2021-08-04T03:39:31.065104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df['image_path'] = study_dir+study_df['study_id']+'.png'\nimage_df['image_path'] = image_dir+image_df['image_id']+'.png'","metadata":{"_uuid":"86900d9f-9932-4349-ad21-d78ba4e7e678","_cell_guid":"8835fcf9-d8db-41d0-b205-3cc8e3c95680","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:31.069505Z","iopub.execute_input":"2021-08-04T03:39:31.071431Z","iopub.status.idle":"2021-08-04T03:39:31.148973Z","shell.execute_reply.started":"2021-08-04T03:39:31.071384Z","shell.execute_reply":"2021-08-04T03:39:31.14785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df","metadata":{"_uuid":"f15023fe-a12c-4080-aa48-024b9fcbf342","_cell_guid":"6daa041b-cf22-4200-86aa-a4fe91a7c16c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:31.152018Z","iopub.execute_input":"2021-08-04T03:39:31.153935Z","iopub.status.idle":"2021-08-04T03:39:31.221711Z","shell.execute_reply.started":"2021-08-04T03:39:31.153893Z","shell.execute_reply":"2021-08-04T03:39:31.220187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"color: #00857e; font-family: Segoe UI; font-size: 1.8em; font-weight: 300;\">Custom Wrapper for Loading TFHub Model trained in TPU</span>\n\nSince the EffNetV2 Classifier models were trained on a TPU with the `tfhub.KerasLayer` formed with the handle argument as a GCS path, while loading the saved model for inference, the method tries to download the pre-trained weights from the definition of the layer from training i.e a GCS path.\n\nSince, inference notebooks don't have GCS and internet access, it is not possible to load the model without the pretrained weights explicitly loaded from the local directory.\n\nIf the models were trained on a GPU, we can use the cache location method to load the pre-trained weights by storing them in a cache folder with the hashed key of the model location, as the folder name. I tried this method here but, it doesn't seem to work as the model was trained with a GCS path defined in the `tfhub.KerasLayer` and the method kept on hitting the GCS path rather than loading the weights from the cache location.\n\nThe only solution was to create a wrapper class to correct the handle argument to load the right pretrained weights explicitly from the local directory.","metadata":{"_uuid":"aadbdd25-2db1-4e70-a103-228764da59a0","_cell_guid":"ad42e477-5d97-4cec-9e59-c045104132f5","papermill":{"duration":0.096404,"end_time":"2021-07-17T19:03:21.416262","exception":false,"start_time":"2021-07-17T19:03:21.319858","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"markdown","source":"<span style=\"color: #00857e; font-family: Segoe UI; font-size: 1.8em; font-weight: 300;\">Predict Study Level</span>","metadata":{"_uuid":"6973f55f-0fd4-435e-81ec-eb6447110a88","_cell_guid":"7a011de4-48be-4e3d-b6f7-dd62ac44ac7d","papermill":{"duration":0.087465,"end_time":"2021-07-17T19:03:25.943841","exception":false,"start_time":"2021-07-17T19:03:25.856376","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"markdown","source":"<span style=\"color: #00857e; font-family: Segoe UI; font-size: 1.8em; font-weight: 300;\">Predict 2Class Image Level</span>\n\nUsing [@Alien](https://www.kaggle.com/h053473666) 2class model.","metadata":{"_uuid":"3d3d373c-766e-4866-a250-3b827ffa7a96","_cell_guid":"0fc1d4ec-3812-4ac5-89ef-c8ea01fbccd1","papermill":{"duration":0.096091,"end_time":"2021-07-17T19:04:41.039546","exception":false,"start_time":"2021-07-17T19:04:40.943455","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"markdown","source":"<span style=\"color: #00857e; font-family: Segoe UI; font-size: 1.8em; font-weight: 300;\">Predict Image Level</span>","metadata":{"_uuid":"c1f82219-5d67-455c-a516-01a57228d553","_cell_guid":"dc5c652a-c632-4afb-ad7f-f10f95752f5e","papermill":{"duration":0.094734,"end_time":"2021-07-17T19:06:44.55103","exception":false,"start_time":"2021-07-17T19:06:44.456296","status":"completed"},"tags":[],"trusted":true}},{"cell_type":"code","source":"from numba import cuda\nimport torch\ncuda.select_device(0)\ncuda.close()\ncuda.select_device(0)","metadata":{"_uuid":"9032395c-78d8-4835-814f-6cc428ece64b","_cell_guid":"bc140bed-d25d-4462-ae8e-6647fa3a1771","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:31.224002Z","iopub.execute_input":"2021-08-04T03:39:31.225215Z","iopub.status.idle":"2021-08-04T03:39:32.58868Z","shell.execute_reply.started":"2021-08-04T03:39:31.225166Z","shell.execute_reply":"2021-08-04T03:39:32.587306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\n\nimport torch\ndevice = torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")\nprint(device.type)\n\nimport torchvision\nprint(torch.__version__, torch.cuda.is_available())\n\n# Check mmcv installation\nfrom mmcv.ops import get_compiling_cuda_version, get_compiler_version\nprint(get_compiling_cuda_version())\nprint(get_compiler_version())\n\n# Check MMDetection installation\nfrom mmdet.apis import set_random_seed\n\n# Imports\nimport mmdet\nfrom mmdet.apis import set_random_seed\nfrom mmdet.datasets import build_dataset\nfrom mmdet.models import build_detector\n\nimport mmcv\nfrom mmcv import Config\nfrom mmcv.runner import load_checkpoint\nfrom mmcv.parallel import MMDataParallel\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot\nfrom mmdet.apis import single_gpu_test\nfrom mmdet.datasets import build_dataloader, build_dataset","metadata":{"_uuid":"dafadaa0-7801-4f44-82e1-8d723cb4f1e8","_cell_guid":"42f10ef7-92be-4b2b-b450-b0ac65ff7532","collapsed":false,"papermill":{"duration":24.187988,"end_time":"2021-07-17T19:07:11.351071","exception":false,"start_time":"2021-07-17T19:06:47.163083","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:32.591236Z","iopub.execute_input":"2021-08-04T03:39:32.591626Z","iopub.status.idle":"2021-08-04T03:39:49.276972Z","shell.execute_reply.started":"2021-08-04T03:39:32.591566Z","shell.execute_reply":"2021-08-04T03:39:49.275793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nlabel2color = [[59, 238, 119]]\n\nviz_labels =  [\"Covid_Abnormality\"]\n\ndef plot_img(img, size=(18, 18), is_rgb=True, title=\"\", cmap=None):\n    plt.figure(figsize=size)\n    plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    plt.show()\n    \ndef plot_imgs(imgs, cols=2, size=10, is_rgb=True, title=\"\", cmap=None, img_size=None):\n    rows = len(imgs)//cols + 1\n    fig = plt.figure(figsize=(cols*size, rows*size))\n    for i, img in enumerate(imgs):\n        if img_size is not None:\n            img = cv2.resize(img, img_size)\n        fig.add_subplot(rows, cols, i+1)\n        plt.imshow(img, cmap=cmap)\n    plt.suptitle(title)\n    return fig\n    \ndef draw_bbox(image, box, color):   \n    alpha = 0.1\n    alpha_font = 0.6\n    thickness = 8\n    font_size = 2.0\n    font_weight = 1\n    overlay_bbox = image.copy()\n    overlay_text = image.copy()\n    output = image.copy()\n\n    text_width, text_height = cv2.getTextSize(label.upper(), cv2.FONT_HERSHEY_SIMPLEX, font_size, font_weight)[0]\n    cv2.rectangle(overlay_bbox, (box[0], box[1]), (box[2], box[3]),\n                color, -1)\n    cv2.addWeighted(overlay_bbox, alpha, output, 1 - alpha, 0, output)\n    cv2.rectangle(overlay_text, (box[0], box[1]-18-text_height), (box[0]+text_width+8, box[1]),\n                (0, 0, 0), -1)\n    cv2.addWeighted(overlay_text, alpha_font, output, 1 - alpha_font, 0, output)\n    cv2.rectangle(output, (box[0], box[1]), (box[2], box[3]),\n                    color, thickness)\n    #cv2.putText(output, label.upper(), (box[0], box[1]-12),\n    #        cv2.FONT_HERSHEY_SIMPLEX, font_size, (255, 255, 255), font_weight, cv2.LINE_AA)\n    return output\n\ndef draw_bbox_small(image, box, label, color):   \n    alpha = 0.1\n    alpha_text = 0.3\n    thickness = 1\n    font_size = 0.4\n    overlay_bbox = image.copy()\n    overlay_text = image.copy()\n    output = image.copy()\n\n    text_width, text_height = cv2.getTextSize(label.upper(), cv2.FONT_HERSHEY_SIMPLEX, font_size, thickness)[0]\n    cv2.rectangle(overlay_bbox, (box[0], box[1]), (box[2], box[3]),\n                color, -1)\n    cv2.addWeighted(overlay_bbox, alpha, output, 1 - alpha, 0, output)\n    cv2.rectangle(overlay_text, (box[0], box[1]-7-text_height), (box[0]+text_width+2, box[1]),\n                (0, 0, 0), -1)\n    cv2.addWeighted(overlay_text, alpha_text, output, 1 - alpha_text, 0, output)\n    cv2.rectangle(output, (box[0], box[1]), (box[2], box[3]),\n                    color, thickness)\n    cv2.putText(output, label.upper(), (box[0], box[1]-5),\n            cv2.FONT_HERSHEY_SIMPLEX, font_size, (255, 255, 255), thickness, cv2.LINE_AA)\n    return output","metadata":{"_uuid":"74d50f5f-b89a-45cc-bfb6-d3a97923e47b","_cell_guid":"8b85fbaa-d6ff-4535-84a8-b1312f4d7974","collapsed":false,"papermill":{"duration":0.117768,"end_time":"2021-07-17T19:07:28.313348","exception":false,"start_time":"2021-07-17T19:07:28.19558","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:39:49.279776Z","iopub.execute_input":"2021-08-04T03:39:49.280478Z","iopub.status.idle":"2021-08-04T03:39:49.398061Z","shell.execute_reply.started":"2021-08-04T03:39:49.280406Z","shell.execute_reply":"2021-08-04T03:39:49.396811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"baseline_cfg_path = \"../input/k/polkaggel/siim-mmdetection-cascadercnn-weight-bias/job4_vfnet_x101_32x4d_fpn_fold1/job4_vfnet_x101_32x4d_fpn_mdconv_c3-c5_mstrain_2x_coco.py\"\ncfg = Config.fromfile(baseline_cfg_path)\n\ncfg.classes = (\"Covid_Abnormality\")\ncfg.data.test.img_prefix = ''\ncfg.data.test.classes = cfg.classes\n\n# cfg.model.roi_head.bbox_head.num_classes = 1\ncfg.model.bbox_head.num_classes = 1\n#for head in cfg.model.roi_head.bbox_head:\n#    head.num_classes = 1\n\n# Set seed thus the results are more reproducible\ncfg.seed = 111\nset_random_seed(111, deterministic=False)\ncfg.gpu_ids = [0]\n\ncfg.data.test.pipeline=[\n            dict(type='LoadImageFromFile'),\n            dict(\n                type='MultiScaleFlipAug',\n                img_scale=(1333, 800),\n                flip=False,\n                transforms=[\n                    dict(type='Resize', keep_ratio=True),\n                    dict(type='RandomFlip', direction='horizontal'),\n                    dict(\n                        type='Normalize',\n                        mean=[123.675, 116.28, 103.53],\n                        std=[58.395, 57.12, 57.375],\n                        to_rgb=True),\n                    dict(type='Pad', size_divisor=32),\n                    dict(type='DefaultFormatBundle'),\n                    dict(type='Collect', keys=['img'])\n                ])\n        ]\n\ncfg.test_pipeline = [\n            dict(type='LoadImageFromFile'),\n            dict(\n                type='MultiScaleFlipAug',\n                img_scale=(1333, 800),\n                flip=False,\n                transforms=[\n                    dict(type='Resize', keep_ratio=True),\n                    dict(type='RandomFlip', direction='horizontal'),\n                    dict(\n                        type='Normalize',\n                        mean=[123.675, 116.28, 103.53],\n                        std=[58.395, 57.12, 57.375],\n                        to_rgb=True),\n                    dict(type='Pad', size_divisor=32),\n                    dict(type='DefaultFormatBundle'),\n                    dict(type='Collect', keys=['img'])\n                ])\n        ]\n\n# cfg.data.samples_per_gpu = 4\n# cfg.data.workers_per_gpu = 4\n#cfg.model.test_cfg.nms.iou_threshold = 0.3\n#cfg.model.test_cfg.rcnn.score_thr = 0.001\n\nWEIGHTS_FILE1 = '../input/detvfnet/epoch_9.pth'\nWEIGHTS_FILE2 = '../input/detvfnet/epoch_7.pth'\noptions = dict(classes = (\"Covid_Abnormality\"))\nmodel1 = init_detector(cfg, WEIGHTS_FILE1, device='cuda:0')\nmodel2 = init_detector(cfg, WEIGHTS_FILE2, device='cuda:0')\n#model3","metadata":{"_uuid":"d9316d7e-16c9-4dfc-9c7a-12fa34da734a","_cell_guid":"b711b893-ec42-4f88-824d-cc1065ecb56a","collapsed":false,"papermill":{"duration":16.649888,"end_time":"2021-07-17T19:07:28.101611","exception":false,"start_time":"2021-07-17T19:07:11.451723","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:18:34.978649Z","iopub.execute_input":"2021-08-04T04:18:34.979037Z","iopub.status.idle":"2021-08-04T04:18:39.29262Z","shell.execute_reply.started":"2021-08-04T04:18:34.979004Z","shell.execute_reply":"2021-08-04T04:18:39.290595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_bbox(bbox, rows, cols):\n    \"\"\"Normalize coordinates of a bounding box. Divide x-coordinates by image width and y-coordinates\n    by image height.\n    \"\"\"\n    x_min, y_min, x_max, y_max = bbox\n    normalized_bbox = [x_min / cols, y_min / rows, x_max / cols, y_max / rows]\n    return normalized_bbox \ndef denormalize_bbox(bbox, rows, cols):\n    \"\"\"Denormalize coordinates of a bounding box. Multiply x-coordinates by image width and y-coordinates\n    by image height. This is an inverse operation for :func:`~albumentations.augmentations.bbox.normalize_bbox`.\n    \"\"\"\n    x_min, y_min, x_max, y_max = bbox\n    denormalized_bbox = [x_min * cols, y_min * rows, x_max * cols, y_max * rows]\n    return denormalized_bbox\ndef convert_bbox_to_albumentations(bbox, source_format, rows, cols, check_validity=False):\n\n\n    if source_format == 'coco':\n        x_min, y_min, width, height = bbox\n        x_max = x_min + width\n        y_max = y_min + height\n\n    bbox = [x_min, y_min, x_max, y_max]\n    bbox = normalize_bbox(bbox, rows, cols)\n    if check_validity:\n        check_bbox(bbox)\n    return bbox\n\ndef convert_bbox_from_albumentations(bbox, target_format, rows, cols, check_validity=False):\n\n    bbox = denormalize_bbox(bbox, rows, cols)\n    if target_format == 'coco':\n        x_min, y_min, x_max, y_max = bbox\n        width = x_max - x_min\n        height = y_max - y_min\n        bbox = [x_min, y_min, width, height]\n    return bbox","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:23:45.953647Z","iopub.execute_input":"2021-08-04T04:23:45.954174Z","iopub.status.idle":"2021-08-04T04:23:46.054039Z","shell.execute_reply.started":"2021-08-04T04:23:45.95414Z","shell.execute_reply":"2021-08-04T04:23:46.052514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ensemble_boxes import weighted_boxes_fusion, nms, soft_nms\nimport albumentations\nviz_images = []\nresults = []\nscore_threshold = .001\n\ndef format_pred(boxes: np.ndarray, scores: np.ndarray, labels: np.ndarray) -> str:\n    pred_strings = []\n    label_str = ['opacity']\n    for label, score, bbox in zip(labels, scores, boxes):\n        xmin, ymin, xmax, ymax = bbox.astype(np.int64)\n        pred_strings.append(f\"{label_str[int(label)]} {score:.16f} {xmin} {ymin} {xmax} {ymax}\")\n    return \" \".join(pred_strings)\n\nmodel1.to(device)\nmodel1.eval()\n\nmodel2.to(device)\nmodel2.eval()\n\nviz_images = []\n\nwith torch.no_grad():\n    for index, row in tqdm(image_df.iterrows(), total=image_df.shape[0]):\n        original_H, original_W = (int(row.dim0), int(row.dim1))\n        predictions = inference_detector(model1, row.image_path)\n        predictions2 = inference_detector(model2, row.image_path)\n        boxes1, scores1, labels1 = (list(), list(), list())\n        boxes2, scores2, labels2 = (list(), list(), list())\n        \n        \n\n        for k, cls_result in enumerate(predictions):\n#             print(\"cls_result\", cls_result)\n            if cls_result.size != 0:\n                if len(labels1)==0:\n                    boxes1 = np.array(cls_result[:, :4])\n                    scores1 = np.array(cls_result[:, 4])\n                    labels1 = np.array([k]*len(cls_result[:, 4]))\n                else:    \n                    boxes1 = np.concatenate((boxes1, np.array(cls_result[:, :4])))\n                    scores1 = np.concatenate((scores1, np.array(cls_result[:, 4])))\n                    labels1 = np.concatenate((labels1, [k]*len(cls_result[:, 4])))\n        \n        for k2, cls_result2 in enumerate(predictions2):\n#             print(\"cls_result\", cls_result)\n            if cls_result2.size != 0:\n                if len(labels2)==0:\n                    boxes2 = np.array(cls_result2[:, :4])\n                    scores2 = np.array(cls_result2[:, 4])\n                    labels2 = np.array([k2]*len(cls_result2[:, 4]))\n                else:    \n                    boxes2 = np.concatenate((boxes2, np.array(cls_result2[:, :4])))\n                    scores2 = np.concatenate((scores2, np.array(cls_result2[:, 4])))\n                    labels2 = np.concatenate((labels2, [k2]*len(cls_result2[:, 4])))\n        if fast_sub:\n                    img_viz = cv2.imread(row.image_path)\n                    for box, label, score in zip(boxes2, labels2, scores2):\n                        color = label2color[int(label)]\n                        img_viz = draw_bbox_small(img_viz, box.astype(np.int32), f'opacity_{score:.4f}', color)\n                    viz_images.append(img_viz)\n        box = []\n        score = []\n        label = []\n        bbox1 = []\n        bbox2 = []\n        indexes = np.where(scores1 > .001)\n        boxes1 = boxes1[indexes]\n        scores1 = scores1[indexes]\n        labels1 = labels1[indexes]\n        \n        indexes = np.where(scores2 > .001)\n        boxes2 = boxes2[indexes]\n        scores2 = scores2[indexes]\n        labels2 = labels2[indexes]\n        \n        boxes1 = list(boxes1)\n        boxes2 = list(boxes2)\n\n        \n        for bbox in boxes1:\n            #print(bbox)\n            bbox = convert_bbox_to_albumentations(bbox, 'coco', original_H, original_W)\n            bbox = list(bbox)\n            bbox1.append(bbox)\n        bbox1 = list(bbox1)\n        for bbox in boxes2:\n            bbox = convert_bbox_to_albumentations(bbox, 'coco', original_H, original_W)\n            bbox = list(bbox)\n            bbox2.append(bbox)  \n        bbox2 = list(bbox2)\n        boxens = []\n        boxens.append(bbox1)\n        boxens.append(bbox2)\n        iou_thr = 0.1\n        weights = [2, 1]\n        score.append(scores1)\n        score.append(scores2)\n        label.append(labels1)\n        label.append(labels2)\n\n        final = list()\n        #boxes, scores, labels = weighted_boxes_fusion(boxens, score, label, weights=weights, iou_thr=iou_thr, skip_box_thr = .001)\n        boxes, scores, labels = soft_nms(boxens, score, label, weights=weights, sigma = .1, iou_thr=iou_thr)\n        for bbox in boxes:\n            boxes = convert_bbox_from_albumentations(bbox, 'coco', original_H, original_W)\n            #print(boxes)\n            final.append(list(boxes))\n        #print(final)\n \n        boxes = final\n        boxes = np.array(boxes)\n        #print(boxes)\n        if fast_sub:\n                img_viz = cv2.imread(row.image_path)\n                for box, label, score in zip(boxes, labels, scores):\n                    color = label2color[int(label)]\n                    img_viz = draw_bbox_small(img_viz, box.astype(np.int32), f'opacity_{score:.4f}', color)\n                viz_images.append(img_viz)\n    \n        if len(labels) != 0:\n            h_ratio = original_H/IMAGE_DIMS[0]\n            w_ratio = original_W/IMAGE_DIMS[1]\n            boxes[:, [0, 2]] *= w_ratio\n            boxes[:, [1, 3]] *= h_ratio\n\n            result = {\n                \"id\": row.image_id,\n                \"PredictionString\": format_pred(\n                    boxes, scores, labels\n                ),\n            }\n            results.append(result)\n\ndetection_df = pd.DataFrame(results, columns=['id', 'PredictionString'])\nif fast_sub:\n    display(detection_df.sample(2))\n    # Plot sample images\n    plot_imgs(viz_images, cmap=None)\n    plt.savefig('viz_fig_siim.png', bbox_inches='tight')\n    plt.show()","metadata":{"_uuid":"d43bd964-b9a8-4064-b1dc-0afbf283aa1d","_cell_guid":"c79113c7-7fed-4a70-be42-88eb135b9f14","collapsed":false,"papermill":{"duration":2.481425,"end_time":"2021-07-17T19:07:30.8895","exception":false,"start_time":"2021-07-17T19:07:28.408075","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:46:46.94801Z","iopub.execute_input":"2021-08-04T04:46:46.948441Z","iopub.status.idle":"2021-08-04T04:46:51.099145Z","shell.execute_reply.started":"2021-08-04T04:46:46.948393Z","shell.execute_reply":"2021-08-04T04:46:51.097374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"viz_images = []\nmaskids = []\nwith torch.no_grad():\n    for index, row in tqdm(study_df.iterrows(), total=study_df.shape[0]):\n        original_H, original_W = (int(row.dim0), int(row.dim1))\n        #print(original_H, original_W)\n        predictions = inference_detector(model1, row.image_path)\n        predictions2 = inference_detector(model2, row.image_path)\n        boxes1, scores1, labels1 = (list(), list(), list())\n        boxes2, scores2, labels2 = (list(), list(), list())\n        \n        \n\n        for k, cls_result in enumerate(predictions):\n#             print(\"cls_result\", cls_result)\n            if cls_result.size != 0:\n                if len(labels1)==0:\n                    boxes1 = np.array(cls_result[:, :4])\n                    scores1 = np.array(cls_result[:, 4])\n                    labels1 = np.array([k]*len(cls_result[:, 4]))\n                else:    \n                    boxes1 = np.concatenate((boxes1, np.array(cls_result[:, :4])))\n                    scores1 = np.concatenate((scores1, np.array(cls_result[:, 4])))\n                    labels1 = np.concatenate((labels1, [k]*len(cls_result[:, 4])))\n        \n        for k2, cls_result2 in enumerate(predictions2):\n#             print(\"cls_result\", cls_result)\n            if cls_result2.size != 0:\n                if len(labels2)==0:\n                    boxes2 = np.array(cls_result2[:, :4])\n                    scores2 = np.array(cls_result2[:, 4])\n                    labels2 = np.array([k2]*len(cls_result2[:, 4]))\n                else:    \n                    boxes2 = np.concatenate((boxes2, np.array(cls_result2[:, :4])))\n                    scores2 = np.concatenate((scores2, np.array(cls_result2[:, 4])))\n                    labels2 = np.concatenate((labels2, [k2]*len(cls_result2[:, 4])))\n        if fast_sub:\n                    img_viz = cv2.imread(row.image_path)\n                    for box, label, score in zip(boxes2, labels2, scores2):\n                        color = label2color[int(label)]\n                        img_viz = draw_bbox_small(img_viz, box.astype(np.int32), f'opacity_{score:.4f}', color)\n                    viz_images.append(img_viz)\n        box = []\n        score = []\n        label = []\n        bbox1 = []\n        bbox2 = []\n        boxes1 = list(boxes1)\n        boxes2 = list(boxes2)\n\n        \n        for bbox in boxes1:\n            #print(bbox)\n            bbox = convert_bbox_to_albumentations(bbox, 'coco', original_H, original_W)\n            bbox = list(bbox)\n            bbox1.append(bbox)\n        bbox1 = list(bbox1)\n        for bbox in boxes2:\n            bbox = convert_bbox_to_albumentations(bbox, 'coco', original_H, original_W)\n            bbox = list(bbox)\n            bbox2.append(bbox)  \n        bbox2 = list(bbox2)\n        boxens = []\n        boxens.append(bbox1)\n        boxens.append(bbox2)\n        iou_thr = 0.6\n        weights = [1, 1]\n        score.append(scores1)\n        score.append(scores2)\n        label.append(labels1)\n        label.append(labels2)\n\n        final = list()\n        boxes, scores, labels = weighted_boxes_fusion(boxens, score, label, weights=weights, iou_thr=iou_thr, skip_box_thr=.1)\n        for bbox in boxes:\n            boxes = convert_bbox_from_albumentations(bbox, 'coco', original_H, original_W)\n            final.append(list(boxes))\n \n        boxes = final\n        boxes = np.array(boxes)\n        if fast_sub:\n                img_viz = cv2.imread(row.image_path)\n                for box, label, score in zip(boxes, labels, scores):\n                    color = label2color[int(label)]\n                    img_viz = draw_bbox_small(img_viz, box.astype(np.int32), f'opacity_{score:.4f}', color)\n                viz_images.append(img_viz)\n        if len(labels) != 0:\n            h_ratio = original_H/STUDY_DIMS[0]\n            w_ratio = original_W/STUDY_DIMS[1]\n            boxes[:, [0, 2]] *= w_ratio\n            boxes[:, [1, 3]] *= h_ratio\n\n            result = {\n                \"id\": row.study_id,\n                \"PredictionString\": format_pred(\n                    boxes, scores, labels\n                ),\n            }\n\n            maskids.append(result)\ngc.collect()\n\n\nmask_df = pd.DataFrame(maskids, columns=['id', 'PredictionString'])\n\nif fast_sub:\n    display(mask_df.sample(2))\n    # Plot sample images\n    plot_imgs(viz_images, cmap=None)\n    plt.savefig('viz_fig_siim.png', bbox_inches='tight')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:47:10.099694Z","iopub.execute_input":"2021-08-04T04:47:10.100089Z","iopub.status.idle":"2021-08-04T04:47:14.897533Z","shell.execute_reply.started":"2021-08-04T04:47:10.100057Z","shell.execute_reply":"2021-08-04T04:47:14.895948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del model1, model2","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"xmin, ymin, xmax, ymax = np.ndarray(boxes1).astype(np.float64)\nxmin2, ymin2, xmax2, ymax2 = np.ndarray(boxes2).astype(np.float64)\n\nbbox1 = []\nbbox2 = []\n\nbbox1.append([xmin,ymin,xmax,ymax])\nbbox2.append([xmin2,ymin2,xmax2,ymax2])\n\nboxens = []\nboxens.append(bbox1)\nboxens.append(bbox2)","metadata":{}},{"cell_type":"code","source":"mask_df","metadata":{"_uuid":"ae07fe21-0052-45ca-a53e-ac4ab5d5a200","_cell_guid":"6c586dfa-3df0-4ce2-bade-07c265c83cb6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:14.97944Z","iopub.execute_input":"2021-08-04T04:35:14.979852Z","iopub.status.idle":"2021-08-04T04:35:15.085748Z","shell.execute_reply.started":"2021-08-04T04:35:14.979821Z","shell.execute_reply":"2021-08-04T04:35:15.084319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:35:16.132427Z","iopub.execute_input":"2021-08-04T04:35:16.132823Z","iopub.status.idle":"2021-08-04T04:35:16.246893Z","shell.execute_reply.started":"2021-08-04T04:35:16.132791Z","shell.execute_reply":"2021-08-04T04:35:16.245563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"backup = study_df.copy()","metadata":{"_uuid":"6c6100e6-2e02-4f5d-9625-a7a90c9e8324","_cell_guid":"9a2bba12-313e-4dfc-bf74-6aa9fd7fbc72","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:17.539265Z","iopub.execute_input":"2021-08-04T04:35:17.539702Z","iopub.status.idle":"2021-08-04T04:35:17.63445Z","shell.execute_reply.started":"2021-08-04T04:35:17.539671Z","shell.execute_reply":"2021-08-04T04:35:17.632907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_df['dim0'] = study_df['dim0'].astype(int)\nmask_df['dim1'] = study_df['dim1'].astype(int)\nmask_df","metadata":{"_uuid":"693ad3db-26c1-4d77-9c2b-8c9b5e4baf6d","_cell_guid":"3b59fb4f-1cd3-4b07-b8ab-73100b9d50a9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:17.801574Z","iopub.execute_input":"2021-08-04T04:35:17.801967Z","iopub.status.idle":"2021-08-04T04:35:17.918388Z","shell.execute_reply.started":"2021-08-04T04:35:17.801935Z","shell.execute_reply":"2021-08-04T04:35:17.916939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"48f32e77-a998-45af-940c-d6369ea0d283","_cell_guid":"02a37ffe-0421-438f-81c2-c5784e150275","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"ffc119c4-a298-4196-8b09-db4b50bcf7af","_cell_guid":"3bd348ac-d51a-4903-ba7a-272d5f8bb1a8","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image_id = '00188a671292_study'\n#i = int(mask_df[mask_df.id==image_id].index.values)\ni = 0\nsize = 512\nlabel = mask_df.loc[0, 'PredictionString'].split()\nif label != 'none':\n    label = label\n    dim0 = mask_df.loc[i, 'dim0']\n    dim1 = mask_df.loc[i, 'dim1']\n\n    mask = np.zeros((size, size), dtype=np.float32)\n    for j in range(len(label) // 6):\n        xmin = int(np.round(float(label[6*j+2])*size/dim1, 4))\n        ymin = int(np.round(float(label[6*j+3])*size/dim0, 4))\n        xmax = int(np.round(float(label[6*j+4])*size/dim1, 4))\n        ymax = int(np.round(float(label[6*j+5])*size/dim0, 4))\n        cv2.rectangle(mask, (xmin, ymin), (xmax, ymax), color=1, thickness=-1)\n\n    plt.imshow(mask)","metadata":{"_uuid":"0158aa11-f9e7-45c4-980b-49247c988ba9","_cell_guid":"993a535a-0e4f-4d27-9746-1b527e7327f4","jupyter":{"outputs_hidden":false},"collapsed":false,"execution":{"iopub.status.busy":"2021-08-04T04:35:20.08105Z","iopub.execute_input":"2021-08-04T04:35:20.081515Z","iopub.status.idle":"2021-08-04T04:35:20.435577Z","shell.execute_reply.started":"2021-08-04T04:35:20.08145Z","shell.execute_reply":"2021-08-04T04:35:20.434091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_dir = '/kaggle/tmp/mask'\nos.makedirs(mask_dir, exist_ok=True)\nsize = 384","metadata":{"_uuid":"457b2759-36f9-4518-8c86-38c4fd8e0ba8","_cell_guid":"6bc851ae-d225-4115-801f-78d348a1a741","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:29.62872Z","iopub.execute_input":"2021-08-04T04:35:29.629121Z","iopub.status.idle":"2021-08-04T04:35:29.718069Z","shell.execute_reply.started":"2021-08-04T04:35:29.629091Z","shell.execute_reply":"2021-08-04T04:35:29.71609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(len(mask_df))):\n    label = mask_df.loc[i, 'PredictionString'].split()\n    \n    if label[0] == 'none':\n        continue\n    \n    image_id = mask_df.loc[i, 'id']\n    dim0 = mask_df.loc[i, 'dim0']\n    dim1 = mask_df.loc[i, 'dim1']\n    \n    if np.isnan(float(dim0)) and np.isnan(float(dim1)):\n        dim0 = mask_df.loc[1, 'dim0']\n        dim1 = mask_df.loc[1, 'dim1']\n    \n    \n    mask = np.zeros((size, size), dtype=np.uint8)\n    for j in range(len(label) // 6):\n        xmin = int(np.round(float(label[j*6+2])*size/dim1, 4))\n        ymin = int(np.round(float(label[j*6+3])*size/dim0, 4))\n        xmax = int(np.round(float(label[j*6+4])*size/dim1, 4))\n        ymax = int(np.round(float(label[j*6+5])*size/dim0, 4))\n        cv2.rectangle(mask, (xmin, ymin), (xmax, ymax), color=255, thickness=-1)\n    cv2.imwrite(f'{mask_dir}/{image_id}.png', mask)","metadata":{"_uuid":"77eaf008-57c4-4c69-b9d2-e3bc7e2bebb2","_cell_guid":"1ee04ee5-2895-4038-a56d-75750a1ef401","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:29.839684Z","iopub.execute_input":"2021-08-04T04:35:29.840056Z","iopub.status.idle":"2021-08-04T04:35:30.023414Z","shell.execute_reply.started":"2021-08-04T04:35:29.840026Z","shell.execute_reply":"2021-08-04T04:35:30.021945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask_df['mask_path'] = mask_dir+'/'+mask_df['id']+'.png'\nmask_df","metadata":{"_uuid":"a4f4c293-0b9c-4e2f-ba66-c4d6bbf382f9","_cell_guid":"2e033b3a-9bab-4394-ac84-1456fff55454","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:30.175036Z","iopub.execute_input":"2021-08-04T04:35:30.175548Z","iopub.status.idle":"2021-08-04T04:35:30.288692Z","shell.execute_reply.started":"2021-08-04T04:35:30.175505Z","shell.execute_reply":"2021-08-04T04:35:30.287118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"o = cv2.imread(mask_df['mask_path'][0])\nplt.imshow(o)","metadata":{"_uuid":"fb45941d-3a7d-4745-9fac-aabafa785e27","_cell_guid":"89c29565-3b4b-4b70-ad1f-616822682a6b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:30.504157Z","iopub.execute_input":"2021-08-04T04:35:30.504895Z","iopub.status.idle":"2021-08-04T04:35:30.93222Z","shell.execute_reply.started":"2021-08-04T04:35:30.504801Z","shell.execute_reply":"2021-08-04T04:35:30.93103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df['mask_path'] = mask_df['mask_path']\nstudy_df","metadata":{"_uuid":"051536e2-39b0-44a6-a094-61337143159c","_cell_guid":"80807322-528f-4fbc-85f9-4495ef4165b4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:33.552056Z","iopub.execute_input":"2021-08-04T04:35:33.5524Z","iopub.status.idle":"2021-08-04T04:35:33.664106Z","shell.execute_reply.started":"2021-08-04T04:35:33.55237Z","shell.execute_reply":"2021-08-04T04:35:33.662255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"eedebd24-eb1a-41fe-8a49-f5d325e345d1","_cell_guid":"317ba957-309c-43a1-a511-b990e15505fb","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np, pandas as pd\nfrom glob import glob\nimport shutil, os\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import GroupKFold\nfrom tqdm.notebook import tqdm\nimport seaborn as sns\n\nimport cv2\nimport sys\nimport math\n\nfrom timeit import default_timer as timer\nfrom datetime import datetime\nfrom numba import cuda\nsys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')\n#_det_model_path = \"/kaggle/input/collect-submit-model/det/\"\n_classify_model_path = \"../input/torchseggv2/\"\n\n_test_files_path = \"/kaggle/input/covid19512/test/\"\n_data_dir = \"/kaggle/input/covid19512/\"\n\nmeta_df = pd.read_csv(_data_dir + \"meta.csv\")\nIMG_SIZE = 384","metadata":{"_uuid":"a836be4b-8e18-440e-bed9-2614656631ec","_cell_guid":"5371d628-77f5-4137-8fbf-059d968a8dc8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:34.175322Z","iopub.execute_input":"2021-08-04T04:35:34.17573Z","iopub.status.idle":"2021-08-04T04:35:34.351519Z","shell.execute_reply.started":"2021-08-04T04:35:34.175698Z","shell.execute_reply":"2021-08-04T04:35:34.348824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('../input/timm-pytorch-image-models/pytorch-image-models-master')","metadata":{"_uuid":"2bdb30f7-8816-4ecd-865a-b614ba493f7d","_cell_guid":"16dee276-fc76-4bc3-9d27-6c8ad4269b4a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:35.276265Z","iopub.execute_input":"2021-08-04T04:35:35.276711Z","iopub.status.idle":"2021-08-04T04:35:35.369623Z","shell.execute_reply.started":"2021-08-04T04:35:35.276679Z","shell.execute_reply":"2021-08-04T04:35:35.368009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.cuda.amp as amp\nimport albumentations\nfrom albumentations import *\nimport random\nfrom torch.optim.optimizer import Optimizer\n##\n\nclass Mish_func(torch.autograd.Function):\n    \n    @staticmethod\n    def forward(ctx, i):\n        result = i * torch.tanh(F.softplus(i))\n        ctx.save_for_backward(i)\n        return result\n\n    @staticmethod\n    def backward(ctx, grad_output):\n        i = ctx.saved_tensors[0]\n  \n        v = 1. + i.exp()\n        h = v.log() \n        grad_gh = 1./h.cosh().pow_(2) \n\n        # Note that grad_hv * grad_vx = sigmoid(x)\n        #grad_hv = 1./v  \n        #grad_vx = i.exp()\n        \n        grad_hx = i.sigmoid()\n\n        grad_gx = grad_gh *  grad_hx #grad_hv * grad_vx \n        \n        grad_f =  torch.tanh(F.softplus(i)) + i * grad_gx \n        \n        return grad_output * grad_f \n\n\nclass Mish(nn.Module):\n    def __init__(self, **kwargs):\n        super().__init__()\n        print(\"Mish initialized\")\n        pass\n    def forward(self, input_tensor):\n        return Mish_func.apply(input_tensor)\n\ndef replace_activations(model, existing_layer, new_layer):\n    for name, module in reversed(model._modules.items()):\n        if len(list(module.children())) > 0:\n            model._modules[name] = replace_activations(module, existing_layer, new_layer)\n\n        if type(module) == existing_layer:\n            layer_old = module\n            layer_new = new_layer\n            model._modules[name] = layer_new\n    return model","metadata":{"_uuid":"4e20afaa-64fd-475f-9f83-217a219bc017","_cell_guid":"c4e95ea8-75d2-4c60-965c-8819dc6e29b7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:36.284163Z","iopub.execute_input":"2021-08-04T04:35:36.284609Z","iopub.status.idle":"2021-08-04T04:35:36.385544Z","shell.execute_reply.started":"2021-08-04T04:35:36.284558Z","shell.execute_reply":"2021-08-04T04:35:36.383975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms_val = albumentations.Compose([\n    albumentations.Resize(384, 384),\n])","metadata":{"_uuid":"1e5a092e-d95c-4231-b001-b9f996d80639","_cell_guid":"e7a1611c-3116-41e4-9c1e-fa458dc27399","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:37.615235Z","iopub.execute_input":"2021-08-04T04:35:37.61563Z","iopub.status.idle":"2021-08-04T04:35:37.705551Z","shell.execute_reply.started":"2021-08-04T04:35:37.615599Z","shell.execute_reply":"2021-08-04T04:35:37.703907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nfrom torch.nn.parallel.data_parallel import data_parallel\n\nfrom torch.utils.data.dataset import Dataset\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data.sampler import *\n\nimport collections\nfrom collections import defaultdict\n\nimport timm\nfrom timm.models.efficientnet import *\n\nimport torch.cuda.amp as amp\ndata_dir = _data_dir\nimage_size = 384\n\nstudy_name_to_predict_string = {\n    'Negative for Pneumonia'  :'negative',\n    'Typical Appearance'      :'typical',\n    'Indeterminate Appearance':'indeterminate',\n    'Atypical Appearance'     :'atypical',\n}\n\nstudy_name_to_label = {\n    'negative'  :0,\n    'typical'      :1,\n    'indeterminate':2,\n    'atypical'     :3,\n}\nstudy_label_to_name = { v:k for k,v in study_name_to_label.items()}\nnum_study_label = len(study_name_to_label)\ndef make_fold(mode='train-0'):\n    if 'test' in mode:\n        df_meta  = pd.read_csv(data_dir+'meta.csv')\n        df_valid = df_meta[df_meta['split']=='test'].copy()\n\n        for l in study_name_to_label.keys():\n            df_valid.loc[:,l]=0\n        df_valid = df_valid.reset_index(drop=True)\n        return df_valid\nclass SiimDataset(Dataset):\n    def __init__(self, df, augment=None):\n        super().__init__()\n        self.df = df\n        self.augment = augment\n        self.length = len(df)\n\n    def __str__(self):\n        string  = ''\n        string += '\\tlen = %d\\n'%len(self)\n        string += '\\tdf  = %s\\n'%str(self.df.shape)\n\n        string += '\\tlabel distribution\\n'\n        for i in range(num_study_label):\n            n = self.df[study_label_to_name[i]].sum()\n            string += '\\t\\t %d %26s: %5d (%0.4f)\\n'%(i, study_label_to_name[i], n, n/len(self.df) )\n        return string\n\n\n    def __len__(self):\n        return self.length\n\n    def __getitem__(self, index):\n        d = self.df.iloc[index]\n\n        #image_file = data_dir + '/%s_640/%s/%s/%s.png' % (d.set, d.study, d.series, d.image)\n        #image_file = _data_dir + 'test/%s.png' % (d.image_id)\n        image_file = '%s' % (d.image_path)\n        image = cv2.imread(image_file,cv2.IMREAD_GRAYSCALE)[:,::-1]\n        image = cv2.resize(image,(384,384))\n        onehot = d[study_name_to_label.keys()].values\n\n        #try:\n        mask_file = '%s' % (d.mask_path)\n        mask = cv2.imread(mask_file,cv2.IMREAD_GRAYSCALE)[:,:2]\n        mask = cv2.resize(mask,(384,384))\n            \n        #except:\n            #mask = np.zeros_like(image)[:,:2]\n        \n        res = self.augment(image=image,mask=mask)\n        r = {\n            'index' : index,\n            'd' : d,\n            'image' : np.concatenate([res['image'],res['mask']]),\n            'mask' : res['mask'],\n            'onehot' : onehot,\n            #np.concatenate([res['image'].astype(np.float32).transpose(2, 0, 1) / 255.,\n            #                          res['mask'].astype(np.float32).transpose(2, 0, 1) / 255.], 0),\n        }\n#.astype(np.float32).transpose(2, 0, 1) / 255.\n        return r\n\n\ndef null_collate(batch):\n    collate = defaultdict(list)\n\n    for r in batch:\n        for k, v in r.items():\n            collate[k].append(v)\n\n    # ---\n    batch_size = len(batch)\n    onehot = np.ascontiguousarray(np.stack(collate['onehot'])).astype(np.float32)\n    collate['onehot'] = torch.from_numpy(onehot)\n\n    image = np.stack(collate['image'])\n    image = image.reshape(batch_size, 2, image_size,image_size)\n    image = np.ascontiguousarray(image)\n    image = image.astype(np.float32)/ 255\n    collate['image'] = torch.from_numpy(image)\n\n    mask = np.stack(collate['mask'])\n    mask = mask.reshape(batch_size, 1, image_size,image_size)\n    mask = np.ascontiguousarray(mask)\n    mask = mask.astype(np.float32) / 255\n    collate['mask'] = torch.from_numpy(mask)\n    \n    #image = np.concatenate([image, mask], 0)\n    #collate['image'] = torch.from_numpy(image)\n    \n    return collate\nn_ch = 2\nclass enetv2(nn.Module):\n    def __init__(self):\n        super(enetv2, self).__init__()\n        self.enet = timm.create_model('tf_efficientnet_b5', False, drop_rate=0.3, drop_path_rate=0.2)\n        existing_layer = torch.nn.SiLU()\n        new_layer = Mish()\n        for name, module in reversed(self.enet._modules.items()):\n            if len(list(module.children())) > 0:\n                self.enet._modules[name] = replace_activations(module, existing_layer, new_layer)\n\n            if type(module) == existing_layer:\n                layer_old = module\n                layer_new = new_layer\n                self.enet._modules[name] = layer_new\n        self.dropout = nn.Dropout(0.1)\n        self.enet.conv_stem.weight = nn.Parameter(self.enet.conv_stem.weight.repeat(1,n_ch//3+1,1,1)[:,:n_ch])\n        self.myfc = nn.Linear(self.enet.classifier.in_features, 4)\n        self.enet.classifier = nn.Identity()\n        self.b0 = nn.Sequential(\n            self.enet.conv_stem,\n            self.enet.bn1,\n            self.enet.act1,\n        )\n        self.b1 = self.enet.blocks[0]\n        self.b2 = self.enet.blocks[1]\n        self.b3 = self.enet.blocks[2]\n        self.b4 = self.enet.blocks[3]\n        self.b5 = self.enet.blocks[4]\n        self.b6 = self.enet.blocks[5]\n        self.b7 = self.enet.blocks[6]\n        self.b8 = nn.Sequential(\n            self.enet.conv_head, #384, 1536\n            self.enet.bn2,\n            self.enet.act2,\n        )\n\n        self.logit = nn.Linear(1536,num_study_label)\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    def extract(self, x):\n        return self.enet(x)\n\n    def forward(self, x):\n        \n        #logit = self.logit(x)\n        batch_size = len(x)\n        x = 2*x-1     # ; print('input ',   x.shape)\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        \n        \n        h = self.myfc(x)\n        return h, mask\ndef probability_to_df_study(df_valid, probability):\n    df_study = pd.DataFrame()\n    df_image = df_valid.copy()\n    df_study.loc[:,'id'] = df_valid.study + '_study'\n    for i in range(num_study_label):\n        df_study.loc[:,study_name_to_predict_string[study_label_to_name[i]]]=probability[:,i]\n        df_image.loc[:,study_name_to_predict_string[study_label_to_name[i]]]=probability[:,i]\n    \n    \n    df_study = df_study.groupby('id', as_index=False).mean()\n    df_study.loc[:, 'PredictionString'] = \\\n           'negative '      + df_study.negative.apply(lambda x: '%0.6f'%x)      + ' 0 0 1 1' \\\n        + ' typical '       + df_study.typical.apply(lambda x: '%0.6f'%x)       + ' 0 0 1 1' \\\n        + ' indeterminate ' + df_study.indeterminate.apply(lambda x: '%0.6f'%x) + ' 0 0 1 1' \\\n        + ' atypical '      + df_study.atypical.apply(lambda x: '%0.6f'%x)      + ' 0 0 1 1'\n\n    df_study = df_study[['id','PredictionString']]\n    return df_study, df_image\ndef do_predict(net, valid_loader, tta=['flip,scale']): #flip\n\n    valid_probability = []\n    valid_num = 0\n\n    start_timer = timer()\n    for t, batch in enumerate(valid_loader):\n        batch_size = len(batch['index'])\n        image  = batch['image'].cuda()\n        onehot = batch['onehot']\n        label  = onehot.argmax(-1)\n\n        #<todo> TTA\n        net.eval()\n        with torch.no_grad():\n            probability = []\n            logit,mask = net(image)\n            probability.append(F.softmax(logit,-1))\n            \n            if 'flip' in tta:\n                logit, mask = net(torch.flip(image,dims=(3,)))\n                probability.append(F.softmax(logit,-1))\n\n            if 'scale' in tta:\n                # size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None):\n                logit, mask = net(F.interpolate(image, scale_factor=1.33, mode='bilinear', align_corners=False))\n                probability.append(F.softmax(logit,-1))\n\n\n            #--------------\n            probability = torch.stack(probability,0).mean(0)\n\n        valid_num += batch_size\n        valid_probability.append(probability.data.cpu().numpy())\n        print('\\r %8d / %d  %s' % (valid_num, len(valid_loader.dataset), time_to_str(timer() - start_timer, 'sec')),\n              end='', flush=True)\n\n    assert(valid_num == len(valid_loader.dataset))\n    print('')\n\n    probability = np.concatenate(valid_probability)\n    return probability\nclass Logger(object):\n    def __init__(self):\n        self.terminal = sys.stdout  #stdout\n        self.file = None\n\n    def open(self, file, mode=None):\n        if mode is None: mode ='w'\n        self.file = open(file, mode)\n\n    def write(self, message, is_terminal=1, is_file=1 ):\n        if '\\r' in message: is_file=0\n\n        if is_terminal == 1:\n            self.terminal.write(message)\n            self.terminal.flush()\n            #time.sleep(1)\n\n        if is_file == 1:\n            self.file.write(message)\n            self.file.flush()\n\n    def flush(self):\n        # this flush method is needed for python 3 compatibility.\n        # this handles the flush command by doing nothing.\n        # you might want to specify some extra behavior here.\n        pass\ndef time_to_str(t, mode='min'):\n    if mode=='min':\n        t  = int(t)/60\n        hr = t//60\n        min = t%60\n        return '%2d hr %02d min'%(hr,min)\n\n    elif mode=='sec':\n        t   = int(t)\n        min = t//60\n        sec = t%60\n        return '%2d min %02d sec'%(min,sec)\n\n    else:\n        raise NotImplementedError\ndf_valid = study_df.copy()\nlabel_cols = ['negative', 'typical', 'indeterminate', 'atypical']\ndf_valid[label_cols] = 0\ndef run_submit():\n    for fold in [0,1,2,3,4,5,6,7,8]:\n        out_dir = './study_predict/'\n        \n        initial_checkpoint = \\\n                '../input/channel5aux/' + f'fold{fold}_model.pth'\n      \n        ## setup  ----------------------------------------\n        #mode = 'local'\n        mode = 'remote'\n\n        submit_dir = out_dir + '%s-fold%d'%(mode, fold)\n        os.makedirs(submit_dir, exist_ok=True)\n\n        log = Logger()\n        log.open(out_dir + 'log.submit.txt', mode='a')\n        log.write('\\n--- [START %s] %s\\n\\n' % (IDENTIFIER, '-' * 64))\n        #log.write('\\t%s\\n' % COMMON_STRING)\n        log.write('\\n')\n\n    \n        valid_dataset = SiimDataset(df_valid,transforms_val)\n        valid_loader  = DataLoader(\n            valid_dataset,\n            sampler = SequentialSampler(valid_dataset),\n            batch_size  = 32,#128, #\n            drop_last   = False,\n            num_workers = 8,\n            pin_memory  = True,\n            collate_fn  = null_collate,\n        )\n        log.write('mode : %s\\n'%(mode))\n       \n        if 1:\n            net = enetv2().cuda()\n            net.load_state_dict(torch.load(initial_checkpoint)['state_dict'], strict=True)\n\n            #---\n            start_timer = timer()\n            df_valid[label_cols]  += do_predict(net, valid_loader)/9\n            log.write('time %s \\n' % time_to_str(timer() - start_timer, 'min'))\n           \n    return df_valid","metadata":{"_uuid":"fd7b38d5-117c-4711-b2f5-129ca233c374","_cell_guid":"3773e9bb-edf1-4981-960d-0dad008f9823","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:38.176376Z","iopub.execute_input":"2021-08-04T04:35:38.176762Z","iopub.status.idle":"2021-08-04T04:35:40.859442Z","shell.execute_reply.started":"2021-08-04T04:35:38.176731Z","shell.execute_reply":"2021-08-04T04:35:40.857986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IDENTIFIER   = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')\nstudypreds = run_submit()","metadata":{"_uuid":"99b14fb7-4e51-4680-8517-0777ce56d747","_cell_guid":"ae6eca8d-387f-4ec4-b507-39d49e22cde1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:35:45.200222Z","iopub.execute_input":"2021-08-04T04:35:45.200673Z","iopub.status.idle":"2021-08-04T04:36:23.089444Z","shell.execute_reply.started":"2021-08-04T04:35:45.200636Z","shell.execute_reply":"2021-08-04T04:36:23.087783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"if 'flip' in tta:\n                logit, mask = net(torch.flip(image,dims=(3,)))\n                probability.append(F.softmax(logit,-1))\n\n            if 'scale' in tta:\n                # size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None):\n                logit, mask = net(F.interpolate(image, scale_factor=1.33, mode='bilinear', align_corners=False))\n                probability.append(F.softmax(logit,-1))","metadata":{"_uuid":"ba45af89-ba42-431b-b3e3-b6085f271ee1","_cell_guid":"e8195f9f-621c-4235-ac89-0b1f86f89df2","trusted":true}},{"cell_type":"code","source":"studypreds","metadata":{"_uuid":"8f0e4232-48eb-4fd1-8c91-0028f8d6c272","_cell_guid":"118ff9b6-da45-4119-a64d-0156a6a5865f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:36:23.095219Z","iopub.execute_input":"2021-08-04T04:36:23.095764Z","iopub.status.idle":"2021-08-04T04:36:23.378417Z","shell.execute_reply.started":"2021-08-04T04:36:23.095708Z","shell.execute_reply":"2021-08-04T04:36:23.377188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#studypreds['PredictionString'] = studypreds[label_cols].apply(lambda row: f'negative {row.negative} 0 0 1 1 typical {row.typical} 0 0 1 1 indeterminate {row.indeterminate} 0 0 1 1 atypical {row.atypical} 0 0 1 1', axis=1)","metadata":{"_uuid":"98c6182c-3233-4598-a6fd-b8158fa153cd","_cell_guid":"de0f4f7b-90ed-4ede-ae92-9117eabcdca8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:40:12.469733Z","iopub.status.idle":"2021-08-04T03:40:12.470744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"studypreds","metadata":{"_uuid":"14c02eeb-290d-4111-a9f4-0f9a4578d81d","_cell_guid":"997a839d-2af9-404a-9e8d-1b5d3dd79c49","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:36:51.250607Z","iopub.execute_input":"2021-08-04T04:36:51.251029Z","iopub.status.idle":"2021-08-04T04:36:51.445031Z","shell.execute_reply.started":"2021-08-04T04:36:51.250996Z","shell.execute_reply":"2021-08-04T04:36:51.443549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as tfhub\n\nMODEL_ARCH = 'efficientnetv2-l-21k-ft1k'\n# Get the TensorFlow Hub model URL\nhub_type = 'feature_vector' # ['classification', 'feature_vector']\nMODEL_ARCH_PATH = f'/kaggle/input/efficientnetv2-tfhub-weight-files/tfhub_models/{MODEL_ARCH}/{hub_type}'\n\n# Custom wrapper class to load the right pretrained weights explicitly from the local directory\nclass KerasLayerWrapper(tfhub.KerasLayer):\n    def __init__(self, handle, **kwargs):\n        handle = tfhub.KerasLayer(tfhub.load(MODEL_ARCH_PATH))\n        super().__init__(handle, **kwargs)\nMODEL_PATH = '/kaggle/input/siim-effnetv2-keras-study-train-tpu-cv0-805'\ntest_paths = study_df.image_path.tolist()\nBATCH_SIZE = 16\n\ndef build_decoder(with_labels=True, target_size=(300, 300), ext='jpg'):\n    def decode(path):\n        file_bytes = tf.io.read_file(path)\n        if ext == 'png':\n            img = tf.image.decode_png(file_bytes, channels=3)\n        elif ext in ['jpg', 'jpeg']:\n            img = tf.image.decode_jpeg(file_bytes, channels=3)\n        else:\n            raise ValueError(\"Image extension not supported\")\n\n        img = tf.cast(img, tf.float32) / 255.0\n        img = tf.image.resize(img, target_size)\n\n        return img\n\n    def decode_with_labels(path, label):\n        return decode(path), label\n\n    return decode_with_labels if with_labels else decode\n\ndef build_augmenter(with_labels=True):\n    def augment(img):\n        img = tf.image.random_flip_left_right(img)\n        img = tf.image.random_flip_up_down(img)\n        return img\n\n    def augment_with_labels(img, label):\n        return augment(img), label\n\n    return augment_with_labels if with_labels else augment\n\ndef build_dataset(paths, labels=None, bsize=32, cache=True,\n                  decode_fn=None, augment_fn=None,\n                  augment=True, repeat=True, shuffle=1024, \n                  cache_dir=\"\"):\n    if cache_dir != \"\" and cache is True:\n        os.makedirs(cache_dir, exist_ok=True)\n\n    if decode_fn is None:\n        decode_fn = build_decoder(labels is not None)\n\n    if augment_fn is None:\n        augment_fn = build_augmenter(labels is not None)\n\n    AUTO = tf.data.experimental.AUTOTUNE\n    slices = paths if labels is None else (paths, labels)\n\n    dset = tf.data.Dataset.from_tensor_slices(slices)\n    dset = dset.map(decode_fn, num_parallel_calls=AUTO)\n    dset = dset.cache(cache_dir) if cache else dset\n    dset = dset.map(augment_fn, num_parallel_calls=AUTO) if augment else dset\n    dset = dset.repeat() if repeat else dset\n    dset = dset.shuffle(shuffle) if shuffle else dset\n    dset = dset.batch(bsize).prefetch(AUTO)\n\n    return dset\nimport efficientnet.tfkeras as efn\n\n\nlabel_cols = ['negative', 'typical', 'indeterminate', 'atypical']\nstudy_df[label_cols] = 0\n\ntest_decoder = build_decoder(with_labels=False,\n                             target_size=(STUDY_DIMS[0],\n                                          STUDY_DIMS[0]), ext='png')\ntest_dataset = build_dataset(\n    test_paths, bsize=BATCH_SIZE, repeat=False, \n    shuffle=False, augment=False, cache=False,\n    decode_fn=test_decoder\n)\n\nwith tf.device('/device:GPU:0'):\n    models = []\n    models0 = tf.keras.models.load_model(f'{MODEL_PATH}/model0.h5',\n                                         custom_objects={'KerasLayer': KerasLayerWrapper})\n    models1 = tf.keras.models.load_model(f'{MODEL_PATH}/model1.h5',\n                                         custom_objects={'KerasLayer': KerasLayerWrapper})\n    models2 = tf.keras.models.load_model(f'{MODEL_PATH}/model2.h5',\n                                         custom_objects={'KerasLayer': KerasLayerWrapper})\n    models3 = tf.keras.models.load_model(f'{MODEL_PATH}/model3.h5',\n                                         custom_objects={'KerasLayer': KerasLayerWrapper})\n    models4 = tf.keras.models.load_model(f'{MODEL_PATH}/model4.h5',\n                                         custom_objects={'KerasLayer': KerasLayerWrapper})\n    models.append(models0)\n    models.append(models1)\n    models.append(models2)\n    models.append(models3)\n    models.append(models4)\n\nstudy_df[label_cols] = .6*(sum([model.predict(test_dataset, verbose=1) for model in models]) / len(models)) + .4*studypreds[label_cols]\ndel models\ndel models0, models1, models2, models3, models4\ndel test_dataset, test_decoder\n","metadata":{"_uuid":"33957dba-8321-4a01-af44-7324e71f5713","_cell_guid":"14db4de0-ccbe-4d6a-a134-6faa681a45fc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:36:54.324939Z","iopub.execute_input":"2021-08-04T04:36:54.325363Z","iopub.status.idle":"2021-08-04T04:41:38.587908Z","shell.execute_reply.started":"2021-08-04T04:36:54.325329Z","shell.execute_reply":"2021-08-04T04:41:38.586569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def LabelSmoothing(encodings , alpha):\n    K = encodings.shape[1]\n    y_ls = (1 - alpha) * encodings + alpha / K\n    return y_ls\nec = study_df[label_cols].values\nstudy_df[label_cols] = LabelSmoothing(ec , alpha=0.001)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:41:38.590674Z","iopub.execute_input":"2021-08-04T04:41:38.591358Z","iopub.status.idle":"2021-08-04T04:41:39.427154Z","shell.execute_reply.started":"2021-08-04T04:41:38.591305Z","shell.execute_reply":"2021-08-04T04:41:39.425476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:41:39.430121Z","iopub.execute_input":"2021-08-04T04:41:39.430643Z","iopub.status.idle":"2021-08-04T04:41:39.609913Z","shell.execute_reply.started":"2021-08-04T04:41:39.430597Z","shell.execute_reply":"2021-08-04T04:41:39.608558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"studypreds['PredictionString'] = study_df[label_cols].apply(lambda row: f'negative {row.negative} 0 0 1 1 typical {row.typical} 0 0 1 1 indeterminate {row.indeterminate} 0 0 1 1 atypical {row.atypical} 0 0 1 1', axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:41:39.613705Z","iopub.execute_input":"2021-08-04T04:41:39.614223Z","iopub.status.idle":"2021-08-04T04:41:39.787233Z","shell.execute_reply.started":"2021-08-04T04:41:39.614174Z","shell.execute_reply":"2021-08-04T04:41:39.785868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"studypreds","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:41:39.790203Z","iopub.execute_input":"2021-08-04T04:41:39.7922Z","iopub.status.idle":"2021-08-04T04:41:40.014809Z","shell.execute_reply.started":"2021-08-04T04:41:39.792154Z","shell.execute_reply":"2021-08-04T04:41:40.013589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_PATH = '/kaggle/input/siim-covid19-efnb7-train-fold0-5-2class'\n\ntest_paths = image_df.image_path.tolist()\nimage_df['none'] = 0\nlabel_cols = ['none']\n\ntest_decoder = build_decoder(with_labels=False,\n                             target_size=(IMAGE_DIMS[0],\n                                          IMAGE_DIMS[0]), ext='png')\ntest_dataset = build_dataset(\n    test_paths, bsize=BATCH_SIZE, repeat=False, \n    shuffle=False, augment=False, cache=False,\n    decode_fn=test_decoder\n)\n\nwith tf.device('/device:GPU:0'):\n    models = []\n    models0 = tf.keras.models.load_model(\n        '/kaggle/input/siim-covid19-efnb7-train-fold0-5-2class/model0.h5'\n    )\n    models1 = tf.keras.models.load_model(\n        '/kaggle/input/siim-covid19-efnb7-train-fold0-5-2class/model1.h5'\n    )\n    models2 = tf.keras.models.load_model(\n        '/kaggle/input/siim-covid19-efnb7-train-fold0-5-2class/model2.h5'\n    )\n    models3 = tf.keras.models.load_model(\n        '/kaggle/input/siim-covid19-efnb7-train-fold0-5-2class/model3.h5'\n    )\n    models4 = tf.keras.models.load_model(\n        '/kaggle/input/siim-covid19-efnb7-train-fold0-5-2class/model4.h5'\n    )\n    models5 = tf.keras.models.load_model(\n        '../input/b77trainyukita/model0.h5'\n    )\n    models6 = tf.keras.models.load_model(\n        '../input/b77trainyukita/model1.h5'\n    )\n    models7 = tf.keras.models.load_model(\n        '../input/b77trainyukita/model2.h5'\n    )\n    models8 = tf.keras.models.load_model(\n        '../input/b77trainyukita/model3.h5'\n    )\n    models9 = tf.keras.models.load_model(\n        '../input/b77trainyukita/model4.h5'\n    )\n    models10 = tf.keras.models.load_model(\n        '../input/restnet152modelsyukit/model3.h5'\n    )\n    models11 = tf.keras.models.load_model(\n        '../input/restnet152modelsyukit/model2.h5'\n    )\n    models12 = tf.keras.models.load_model(\n        '../input/restnet152modelsyukit/model1.h5'\n    )\n    models13 = tf.keras.models.load_model(\n        '../input/restnet152modelsyukit/model4.h5'\n    )\n    models14 = tf.keras.models.load_model(\n        '../input/restnet152modelsyukit/model0.h5'\n    )\n#     models15 = tf.keras.models.load_model(\n#         '../input/siimefnmod/model4.h5'\n#     )\n    \n    models.append(models0)\n    models.append(models1)\n    models.append(models2)\n    models.append(models3)\n    models.append(models4)\n    models.append(models5)\n    models.append(models6)\n    models.append(models7)\n    models.append(models8)\n    models.append(models9)\n    models.append(models10)\n    models.append(models11)\n    models.append(models12)\n    models.append(models13)\n    models.append(models14)\n    del models0, models1, models2, models3, models4, models5, models6, models7, models8, models9, models10, models11, models12, models13, models14\n#     models.append(models15)\n\nweights = {\n    0: 3,\n    1: 2,\n    2: 2,\n    3: 2,\n    4: 2,\n    5: 2,\n    6: 1,\n    7: 1,\n    8: 1,\n    9: 2,\n    10: 0,\n    11: 2.25,\n    12: 0,\n    13: 0,\n    14: 2.25\n    \n}\n\nweights_sum = sum(weights.values())\nweights = {k: v/weights_sum for k, v in weights.items()}\n\npredictions = [model.predict(test_dataset, verbose=1) for model in models]\nfor i, pred in enumerate(predictions):\n    predictions[i] = weights[i] * pred\n    \nimage_df[label_cols] = sum(predictions)\n\ndel models\n#del models0, models1, models2, models3, models4, models5, models6, models7, models8, models9, models10, models11, models12, models13, models14\ndel test_dataset, test_decoder\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:41:40.018069Z","iopub.execute_input":"2021-08-04T04:41:40.020276Z","iopub.status.idle":"2021-08-04T04:44:18.936089Z","shell.execute_reply.started":"2021-08-04T04:41:40.020211Z","shell.execute_reply":"2021-08-04T04:44:18.934727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df","metadata":{"_uuid":"ae59cecb-6dd3-462d-800d-3508d9522823","_cell_guid":"cb98ff6c-dd50-4ad2-9552-54feb2a21f57","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:44:18.9467Z","iopub.execute_input":"2021-08-04T04:44:18.947269Z","iopub.status.idle":"2021-08-04T04:44:19.40956Z","shell.execute_reply.started":"2021-08-04T04:44:18.947188Z","shell.execute_reply":"2021-08-04T04:44:19.405133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"studypreds","metadata":{"_uuid":"4641c936-0d86-414c-8a7e-cbbaea1fe322","_cell_guid":"8970ffb2-c599-45f2-beb5-c6ffe36f7a8d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:44:19.416432Z","iopub.execute_input":"2021-08-04T04:44:19.417185Z","iopub.status.idle":"2021-08-04T04:44:19.602599Z","shell.execute_reply.started":"2021-08-04T04:44:19.417136Z","shell.execute_reply":"2021-08-04T04:44:19.600469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dback = detection_df.copy()","metadata":{"_uuid":"d92ae68b-b29c-43cf-8330-d42df6096f62","_cell_guid":"0268c693-1550-40db-9648-375fb7b0212a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:44:19.606742Z","iopub.execute_input":"2021-08-04T04:44:19.607494Z","iopub.status.idle":"2021-08-04T04:44:19.771761Z","shell.execute_reply.started":"2021-08-04T04:44:19.607411Z","shell.execute_reply":"2021-08-04T04:44:19.7697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"iback = image_df.copy()","metadata":{"_uuid":"cabbaf83-2332-489f-b57f-f30773058ac8","_cell_guid":"8d640af6-0c35-4285-8a20-571416699cc7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:44:19.774548Z","iopub.execute_input":"2021-08-04T04:44:19.775716Z","iopub.status.idle":"2021-08-04T04:44:19.948307Z","shell.execute_reply.started":"2021-08-04T04:44:19.77558Z","shell.execute_reply":"2021-08-04T04:44:19.946922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detection_df = detection_df.merge(image_df[['image_id','none']].rename({'image_id':'id'}, axis=1),\n                                  on='id', how='left')\n\nfor i in range(detection_df.shape[0]):\n    if detection_df.loc[i,'PredictionString'] != 'none 1 0 0 1 1':\n        detection_df.loc[i,'PredictionString'] = detection_df.loc[i,'PredictionString'] + ' none ' + str(detection_df.loc[i,'none']) + ' 0 0 1 1'\ndetection_df = detection_df[['id', 'PredictionString']]\n\nresults_df = studypreds[['study_id', 'PredictionString']].rename({'study_id':'id'}, axis=1)\nresults_df = results_df.append(detection_df[['id', 'PredictionString']])","metadata":{"_uuid":"fa5d6eeb-b331-4043-b7cc-69e0ef0ae0f7","_cell_guid":"b7d893c3-11ea-479b-b0cd-fee1a1d97cf8","collapsed":false,"papermill":{"duration":0.130739,"end_time":"2021-07-17T19:07:31.131057","exception":false,"start_time":"2021-07-17T19:07:31.000318","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:44:19.952289Z","iopub.execute_input":"2021-08-04T04:44:19.954768Z","iopub.status.idle":"2021-08-04T04:44:20.157315Z","shell.execute_reply.started":"2021-08-04T04:44:19.954716Z","shell.execute_reply":"2021-08-04T04:44:20.155904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_df.to_csv('submission.csv',index=False)","metadata":{"_uuid":"5dde936d-4f46-4237-a008-837f8b05dcf8","_cell_guid":"7da27e89-fa6d-4dec-91cf-d85052d617bd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T04:44:20.161274Z","iopub.execute_input":"2021-08-04T04:44:20.163528Z","iopub.status.idle":"2021-08-04T04:44:20.342608Z","shell.execute_reply.started":"2021-08-04T04:44:20.16342Z","shell.execute_reply":"2021-08-04T04:44:20.34123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_df","metadata":{"execution":{"iopub.status.busy":"2021-08-04T04:44:43.696926Z","iopub.execute_input":"2021-08-04T04:44:43.697349Z","iopub.status.idle":"2021-08-04T04:44:43.84274Z","shell.execute_reply.started":"2021-08-04T04:44:43.697304Z","shell.execute_reply":"2021-08-04T04:44:43.840835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r /kaggle/working/mmdetection","metadata":{"_uuid":"4db003a2-9363-4857-b6de-3d7b31f7c816","_cell_guid":"0fe42286-9e5b-44be-906c-fb3c7570c761","collapsed":false,"papermill":{"duration":0.363451,"end_time":"2021-07-17T19:07:32.023003","exception":false,"start_time":"2021-07-17T19:07:31.659552","status":"completed"},"tags":[],"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-04T03:40:12.532492Z","iopub.status.idle":"2021-08-04T03:40:12.533706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style='text-align: center;'><span style=\"color: #000508; font-family: Segoe UI; font-size: 2.4em; font-weight: 300;\">HAVE A GREAT DAY!</span></p>\n\n<p style='text-align: center;'><span style=\"color: #000508; font-family: Segoe UI; font-size: 1.4em; font-weight: 300;\">Let me know if you have any suggestions!</span></p>","metadata":{"_uuid":"3a9959d7-50eb-4e36-9468-4b8baec8ec04","_cell_guid":"3ccbc8ad-75cc-42b8-b381-749eaf73ff3f","trusted":true}}]}