{"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":"markdown","source":"## Import","metadata":{}},{"cell_type":"code","source":"!conda install '/kaggle/input/pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:44:26.113755Z","iopub.execute_input":"2021-07-14T10:44:26.114113Z","iopub.status.idle":"2021-07-14T10:45:37.698183Z","shell.execute_reply.started":"2021-07-14T10:44:26.114037Z","shell.execute_reply":"2021-07-14T10:45:37.697237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom pathlib import Path\nimport os\nfrom tqdm import tqdm\nimport numpy as np\n\nfrom PIL import Image\n\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\n# for .dcm file\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:18.152361Z","iopub.execute_input":"2021-07-14T10:46:18.152719Z","iopub.status.idle":"2021-07-14T10:46:18.543023Z","shell.execute_reply.started":"2021-07-14T10:46:18.152684Z","shell.execute_reply":"2021-07-14T10:46:18.541766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load submission file","metadata":{}},{"cell_type":"markdown","source":"If submission file is public, just submit fast!","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/siim-covid19-detection/sample_submission.csv\")\nif submission.shape[0] == 2477:\n    fast_sub = True\nelse:\n    fast_sub = False","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:21.346428Z","iopub.execute_input":"2021-07-14T10:46:21.346801Z","iopub.status.idle":"2021-07-14T10:46:21.365105Z","shell.execute_reply.started":"2021-07-14T10:46:21.346771Z","shell.execute_reply":"2021-07-14T10:46:21.364357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## .dcm to .jpg","metadata":{}},{"cell_type":"code","source":"# extract image pixel data from .dcm\ndef read_xray(path):\n    dicom = pydicom.read_file(path)\n    data = apply_voi_lut(dicom.pixel_array, dicom)\n    \n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:34.126596Z","iopub.execute_input":"2021-07-14T10:46:34.12693Z","iopub.status.idle":"2021-07-14T10:46:34.134761Z","shell.execute_reply.started":"2021-07-14T10:46:34.126901Z","shell.execute_reply":"2021-07-14T10:46:34.133877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert .dcm to .jpg\nos.makedirs('/kaggle/tmp/siim/images/test', exist_ok=True)\nfor path in tqdm(Path(\"../input/siim-covid19-detection/test\").rglob('*.dcm')):\n    xray = read_xray(str(path))\n    im = Image.fromarray(xray)\n    im.save(os.path.join('/kaggle/tmp/siim/images/test', path.name.replace('.dcm', '.jpg')))\n    if fast_sub: break","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:39.413127Z","iopub.execute_input":"2021-07-14T10:46:39.413464Z","iopub.status.idle":"2021-07-14T10:46:40.742591Z","shell.execute_reply.started":"2021-07-14T10:46:39.413433Z","shell.execute_reply":"2021-07-14T10:46:40.74073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference YOLOv5","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:00:15.257246Z","iopub.execute_input":"2021-07-10T08:00:15.257834Z","iopub.status.idle":"2021-07-10T08:00:16.000387Z","shell.execute_reply.started":"2021-07-10T08:00:15.257778Z","shell.execute_reply":"2021-07-10T08:00:15.999075Z"}}},{"cell_type":"code","source":"# Copy YOLOv5 directory to working directory\n!cp -r /kaggle/input/yolov5/yolov5 yolov5","metadata":{"execution":{"iopub.status.busy":"2021-07-14T10:46:59.580085Z","iopub.execute_input":"2021-07-14T10:46:59.580451Z","iopub.status.idle":"2021-07-14T10:47:37.133799Z","shell.execute_reply.started":"2021-07-14T10:46:59.580412Z","shell.execute_reply":"2021-07-14T10:47:37.132725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/kaggle/working/yolov5')\n!rm -rf runs/detect/\n!rm -rf runs/test/\n!python3 detect.py \\\n        --weights ../input/myweight/best.pt \\\n        --source /kaggle/tmp/siim/images/test \\\n        --iou-thres 0.5 \\\n        --save-txt \\\n        --save-conf \\\n        --device 0\nos.chdir('/kaggle/working')","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2021-07-14T10:49:21.004246Z","iopub.execute_input":"2021-07-14T10:49:21.004596Z","iopub.status.idle":"2021-07-14T10:49:27.697176Z","shell.execute_reply.started":"2021-07-14T10:49:21.004558Z","shell.execute_reply":"2021-07-14T10:49:27.696018Z"},"trusted":true},"execution_count":null,"outputs":[]}]}