{"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-08-09T09:28:37.13475Z","iopub.execute_input":"2021-08-09T09:28:37.134971Z","iopub.status.idle":"2021-08-09T09:29:24.778845Z","shell.execute_reply.started":"2021-08-09T09:28:37.13495Z","shell.execute_reply":"2021-08-09T09:29:24.777871Z"},"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-08-09T09:29:24.790521Z","iopub.execute_input":"2021-08-09T09:29:24.790782Z","iopub.status.idle":"2021-08-09T09:29:24.803189Z","shell.execute_reply.started":"2021-08-09T09:29:24.790758Z","shell.execute_reply":"2021-08-09T09:29:24.802284Z"},"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\")\n\n#submission = pd.read_csv(\"/kaggle/working/submission.csv\")","metadata":{"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":{"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')))","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:43:42.015769Z","iopub.execute_input":"2021-08-09T08:43:42.016107Z","iopub.status.idle":"2021-08-09T08:43:42.021828Z","shell.execute_reply.started":"2021-08-09T08:43:42.016077Z","shell.execute_reply":"2021-08-09T08:43:42.021038Z"},"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-08-09T08:43:43.03124Z","iopub.execute_input":"2021-08-09T08:43:43.031614Z","iopub.status.idle":"2021-08-09T08:44:16.362328Z","shell.execute_reply.started":"2021-08-09T08:43:43.031581Z","shell.execute_reply":"2021-08-09T08:44:16.361311Z"},"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 /kaggle/input/myweght/best-3.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,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Write submission file","metadata":{}},{"cell_type":"markdown","source":"### Did YOLOv5 detect well?","metadata":{}},{"cell_type":"code","source":"label_path = 'yolov5/runs/detect/exp/labels/'\nimage_path = '/kaggle/tmp/siim/images/test/'","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:44:34.49917Z","iopub.execute_input":"2021-08-09T08:44:34.499572Z","iopub.status.idle":"2021-08-09T08:44:34.50659Z","shell.execute_reply.started":"2021-08-09T08:44:34.499516Z","shell.execute_reply":"2021-08-09T08:44:34.50569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsample_id = \"a29c5a68b07b\"\nfig, ax = plt.subplots()\n\nim = Image.open(os.path.join(image_path, f'{sample_id}.jpg'))\nax.imshow(im, cmap='gray')\n\nwith open(os.path.join(label_path, f'{sample_id}.txt'), 'r') as f:\n    labels = f.read().split('\\n')\n\nfor label in labels[:-1]:\n    _, x_center, y_center, width, height, _ = list(map(lambda x: float(x), label.split(' ')))\n    rect = patches.Rectangle(((x_center - width/2)*im.size[0], (y_center-height/2)*im.size[1]),\n                             width*im.size[0], height*im.size[1], linewidth=1, edgecolor='r', facecolor='none')\n    ax.add_patch(rect)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:44:34.509546Z","iopub.execute_input":"2021-08-09T08:44:34.509952Z","iopub.status.idle":"2021-08-09T08:44:34.747123Z","shell.execute_reply.started":"2021-08-09T08:44:34.509914Z","shell.execute_reply":"2021-08-09T08:44:34.745659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Now, submit!","metadata":{}},{"cell_type":"code","source":"\nfor idx in tqdm(submission.index):\n    data_id = submission.loc[idx]['id']\n    if data_id.endswith('_image'):\n        if os.path.exists(os.path.join(label_path, data_id.replace(\"_image\", \".txt\"))):\n            im = Image.open(os.path.join(image_path, data_id.replace(\"_image\", \".jpg\")))\n\n            with open(os.path.join(label_path, data_id.replace(\"_image\", \".txt\")), 'r') as f:\n                labels = f.read().split('\\n')\n\n            ans = \"\"\n            for label in labels[:-1]:\n                labe, x_center, y_center, width, height, confidence = list(map(lambda x: float(x), label.split(' ')))\n                xmin = int((x_center - width / 2) * im.size[0])\n                xmax = int((x_center + width / 2) * im.size[0])\n                ymin = int((y_center - height / 2) * im.size[1])\n                ymax = int((y_center + height / 2) * im.size[1])\n                if labe==0:\n                    this_label = \"Typical Appearance\"\n\n                if labe==1:\n                    this_label = \"Indeterminate Appearance\"\n\n                if labe==2:\n                    this_label = \"Atypical Appearance\"\n                ans += f\"{this_label} {confidence} {xmin} {ymin} {xmax} {ymax} \"\n            submission.loc[idx, 'PredictionString'] = ans\n","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:01:42.594943Z","iopub.status.idle":"2021-08-09T08:01:42.596013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/yolov5\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)  \nos.makedirs('/kaggle/working/result/', exist_ok=True)\nsubmission.to_csv('/kaggle/working/result/submission.csv', index=False) ","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:01:42.597703Z","iopub.status.idle":"2021-08-09T08:01:42.598565Z"},"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":[]}]}