{"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":"!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-14T06:43:57.062752Z","iopub.execute_input":"2021-07-14T06:43:57.06317Z","iopub.status.idle":"2021-07-14T06:45:03.660481Z","shell.execute_reply.started":"2021-07-14T06:43:57.06308Z","shell.execute_reply":"2021-07-14T06:45:03.659548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ntorch.__version__\nimport pandas as pd\nimport os\n\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm\nimport numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-14T06:45:03.663781Z","iopub.execute_input":"2021-07-14T06:45:03.664079Z","iopub.status.idle":"2021-07-14T06:45:05.061381Z","shell.execute_reply.started":"2021-07-14T06:45:03.664049Z","shell.execute_reply":"2021-07-14T06:45:05.060535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%wget https://github.com/SwinTransformer/storage/releases/download/v1.0.2/mask_rcnn_swin_tiny_patch4_window7.pth","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://akshays12@bitbucket.org/akshays12/covid-detection.git","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:45:05.063135Z","iopub.execute_input":"2021-07-14T06:45:05.063478Z","iopub.status.idle":"2021-07-14T06:45:10.941483Z","shell.execute_reply.started":"2021-07-14T06:45:05.063442Z","shell.execute_reply":"2021-07-14T06:45:10.940568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:45:10.943493Z","iopub.execute_input":"2021-07-14T06:45:10.943883Z","iopub.status.idle":"2021-07-14T06:45:26.789631Z","shell.execute_reply.started":"2021-07-14T06:45:10.943841Z","shell.execute_reply":"2021-07-14T06:45:26.788674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\n\n# Modify values in the id column\ndf['id'] = df.apply(lambda row: row.id.split('_')[0], axis=1)\n# Add absolute path\n# df['path'] = df.apply(lambda row: TRAIN_PATH+row.id+'.jpg', axis=1)\n# Get image level labels\ndf['image_level'] = df.apply(lambda row: row.label.split(' ')[0], axis=1)\n\ndf.head(5)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:45:26.792391Z","iopub.execute_input":"2021-07-14T06:45:26.792733Z","iopub.status.idle":"2021-07-14T06:45:27.073178Z","shell.execute_reply.started":"2021-07-14T06:45:26.792699Z","shell.execute_reply":"2021-07-14T06:45:27.07222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data\n\ndef resize_xray(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((512, 512), resample)\n    else:\n        im = im.resize((512, 512), resample)\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:45:27.074481Z","iopub.execute_input":"2021-07-14T06:45:27.074858Z","iopub.status.idle":"2021-07-14T06:45:27.082701Z","shell.execute_reply.started":"2021-07-14T06:45:27.074805Z","shell.execute_reply":"2021-07-14T06:45:27.081641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = []\ndim0 = []\ndim1 = []\nsave_dir = f'/kaggle/working/train/'\n\nos.makedirs(save_dir, exist_ok=True)\n\nfor dirname, _, filenames in tqdm(os.walk(f'/kaggle/input/siim-covid19-detection/train')):\n    for file in filenames:\n        # set keep_ratio=True to have original aspect ratio\n        xray = read_xray(os.path.join(dirname, file))\n        im = resize_xray(xray, size=512)  \n        im.save(os.path.join(save_dir, file.replace('dcm', 'jpg')))\n\n        image_id.append(file.replace('.dcm', ''))\n        dim0.append(xray.shape[0])\n        dim1.append(xray.shape[1])","metadata":{"execution":{"iopub.status.busy":"2021-07-14T06:47:21.29321Z","iopub.execute_input":"2021-07-14T06:47:21.293539Z","iopub.status.idle":"2021-07-14T07:30:05.327259Z","shell.execute_reply.started":"2021-07-14T06:47:21.293509Z","shell.execute_reply":"2021-07-14T07:30:05.325701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadf = pd.DataFrame.from_dict({'id': image_id, 'dim0': dim0, 'dim1': dim1})\nmetadf.to_csv('meta.csv', index=False)\nlen(metadf)","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.328838Z","iopub.execute_input":"2021-07-14T07:30:05.329328Z","iopub.status.idle":"2021-07-14T07:30:05.36388Z","shell.execute_reply.started":"2021-07-14T07:30:05.329289Z","shell.execute_reply":"2021-07-14T07:30:05.362989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_bbox(row):\n    bboxes = []\n    bbox = []\n    for i, l in enumerate(row.label.split(' ')):\n        if (i % 6 == 0) | (i % 6 == 1):\n            continue\n        bbox.append(float(l))\n        if i % 6 == 5:\n            bboxes.append(bbox)\n            bbox = []  \n            \n    return bboxes\n\n# Scale the bounding boxes according to the size of the resized image. \ndef scale_bbox(row, bboxes):\n    # Get scaling factor\n    scale_x = 1024/row.dim1\n    scale_y = 1024/row.dim0\n    \n    scaled_bboxes = []\n    for bbox in bboxes:\n        x = int(np.round(bbox[0]*scale_x, 4))\n        y = int(np.round(bbox[1]*scale_y, 4))\n        x1 = int(np.round(bbox[2]*(scale_x), 4))\n        y1 = int(np.round(bbox[3]*scale_y, 4))\n\n        scaled_bboxes.append([x, y, x1, y1]) # xmin, ymin, xmax, ymax\n        \n    return scaled_bboxes\n\n# Convert the bounding boxes in YOLO format.\n# def get_yolo_format_bbox(img_w, img_h, bboxes):\n#     yolo_boxes = []\n#     for bbox in bboxes:\n#         w = bbox[2] - bbox[0] # xmax - xmin\n#         h = bbox[3] - bbox[1] # ymax - ymin\n#         xc = bbox[0] + int(np.round(w/2)) # xmin + width/2\n#         yc = bbox[1] + int(np.round(h/2)) # ymin + height/2\n        \n#         yolo_boxes.append([xc/img_w, yc/img_h, w/img_w, h/img_h]) # x_center y_center width height\n    \n#     return yolo_boxes","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.365649Z","iopub.execute_input":"2021-07-14T07:30:05.366003Z","iopub.status.idle":"2021-07-14T07:30:05.374626Z","shell.execute_reply.started":"2021-07-14T07:30:05.365968Z","shell.execute_reply":"2021-07-14T07:30:05.373652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.merge(metadf,how='left',on='id')","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.376251Z","iopub.execute_input":"2021-07-14T07:30:05.376693Z","iopub.status.idle":"2021-07-14T07:30:05.407864Z","shell.execute_reply.started":"2021-07-14T07:30:05.376659Z","shell.execute_reply":"2021-07-14T07:30:05.407155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def voc_classes():\n    return ['opacity']\nlabel_ids = {name: i for i, name in enumerate(voc_classes())}\nimport os.path as osp","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.408923Z","iopub.execute_input":"2021-07-14T07:30:05.409246Z","iopub.status.idle":"2021-07-14T07:30:05.414618Z","shell.execute_reply.started":"2021-07-14T07:30:05.409212Z","shell.execute_reply":"2021-07-14T07:30:05.413592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.415961Z","iopub.execute_input":"2021-07-14T07:30:05.416432Z","iopub.status.idle":"2021-07-14T07:30:05.435046Z","shell.execute_reply.started":"2021-07-14T07:30:05.416395Z","shell.execute_reply":"2021-07-14T07:30:05.433886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def traindf_to_annotations(args):\n    img_path, bboxes_inp = args\n    w = '512'\n    h = '512'\n    bboxes = []\n    labels = []\n    bboxes_ignore = []\n    labels_ignore = []\n    difficult = False\n    for obj in bboxes_inp:\n        name = 'opacity'\n        label = 0\n        bbox = obj\n        if difficult:\n            bboxes_ignore.append(bbox)\n            labels_ignore.append(label)\n        else:\n            bboxes.append(bbox)\n            labels.append(label)\n    if not bboxes:\n        bboxes = np.zeros((0, 4))\n        labels = np.zeros((0, ))\n    else:\n        bboxes = np.array(bboxes, ndmin=2) - 1\n        labels = np.array(labels)\n    if not bboxes_ignore:\n        bboxes_ignore = np.zeros((0, 4))\n        labels_ignore = np.zeros((0, ))\n    else:\n        bboxes_ignore = np.array(bboxes_ignore, ndmin=2) - 1\n        labels_ignore = np.array(labels_ignore)\n    annotation = {\n        'filename': img_path,\n        'width': w,\n        'height': h,\n        'ann': {\n            'bboxes': bboxes.astype(np.float32),\n            'labels': labels.astype(np.int64),\n            'bboxes_ignore': bboxes_ignore.astype(np.float32),\n            'labels_ignore': labels_ignore.astype(np.int64)\n        }\n    }\n    return annotation","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.436481Z","iopub.execute_input":"2021-07-14T07:30:05.436862Z","iopub.status.idle":"2021-07-14T07:30:05.447353Z","shell.execute_reply.started":"2021-07-14T07:30:05.436825Z","shell.execute_reply":"2021-07-14T07:30:05.44655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cvt_to_coco_json(annotations):\n    image_id = 0\n    annotation_id = 0\n    coco = dict()\n    coco['images'] = []\n    coco['type'] = 'instance'\n    coco['categories'] = []\n    coco['annotations'] = []\n    image_set = set()\n\n    def addAnnItem(annotation_id, image_id, category_id, bbox, difficult_flag):\n        annotation_item = dict()\n        annotation_item['segmentation'] = []\n\n        seg = []\n        # bbox[] is x1,y1,x2,y2\n        # left_top\n        seg.append(int(bbox[0]))\n        seg.append(int(bbox[1]))\n        # left_bottom\n        seg.append(int(bbox[0]))\n        seg.append(int(bbox[3]))\n        # right_bottom\n        seg.append(int(bbox[2]))\n        seg.append(int(bbox[3]))\n        # right_top\n        seg.append(int(bbox[2]))\n        seg.append(int(bbox[1]))\n\n        annotation_item['segmentation'].append(seg)\n\n        xywh = np.array(\n            [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]])\n        annotation_item['area'] = int(xywh[2] * xywh[3])\n        if difficult_flag == 1:\n            annotation_item['ignore'] = 0\n            annotation_item['iscrowd'] = 1\n        else:\n            annotation_item['ignore'] = 0\n            annotation_item['iscrowd'] = 0\n        annotation_item['image_id'] = int(image_id)\n        annotation_item['bbox'] = xywh.astype(int).tolist()\n        annotation_item['category_id'] = int(category_id)\n        annotation_item['id'] = int(annotation_id)\n        coco['annotations'].append(annotation_item)\n        return annotation_id + 1\n\n    for category_id, name in enumerate(voc_classes()):\n        category_item = dict()\n        category_item['supercategory'] = str('none')\n        category_item['id'] = int(category_id)\n        category_item['name'] = str(name)\n        coco['categories'].append(category_item)\n\n    for ann_dict in annotations:\n        file_name = ann_dict['filename']\n        ann = ann_dict['ann']\n        assert file_name not in image_set\n        image_item = dict()\n        image_item['id'] = int(image_id)\n        image_item['file_name'] = str(file_name)\n        image_item['height'] = int(ann_dict['height'])\n        image_item['width'] = int(ann_dict['width'])\n        coco['images'].append(image_item)\n        image_set.add(file_name)\n\n        bboxes = ann['bboxes'][:, :4]\n        labels = ann['labels']\n        for bbox_id in range(len(bboxes)):\n            bbox = bboxes[bbox_id]\n            label = labels[bbox_id]\n            annotation_id = addAnnItem(\n                annotation_id, image_id, label, bbox, difficult_flag=0)\n\n        bboxes_ignore = ann['bboxes_ignore'][:, :4]\n        labels_ignore = ann['labels_ignore']\n        for bbox_id in range(len(bboxes_ignore)):\n            bbox = bboxes_ignore[bbox_id]\n            label = labels_ignore[bbox_id]\n            annotation_id = addAnnItem(\n                annotation_id, image_id, label, bbox, difficult_flag=1)\n\n        image_id += 1\n\n    return coco","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.450121Z","iopub.execute_input":"2021-07-14T07:30:05.450476Z","iopub.status.idle":"2021-07-14T07:30:05.467679Z","shell.execute_reply.started":"2021-07-14T07:30:05.45044Z","shell.execute_reply":"2021-07-14T07:30:05.466968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('/kaggle/working/train')))","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.46932Z","iopub.execute_input":"2021-07-14T07:30:05.469669Z","iopub.status.idle":"2021-07-14T07:30:05.48599Z","shell.execute_reply.started":"2021-07-14T07:30:05.469633Z","shell.execute_reply":"2021-07-14T07:30:05.485175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cvt_annotations(devkit_path, years, split, out_file):\n    annosArray = []\n    devkitpath = 'pascalvocdevkitpath' # has to be changed later.\n    years = '2012'\n    if not isinstance(years, list):\n        years = [years]\n    annotations = []\n    if split =='train':   \n        target_df = train\n    else:\n        target_df = val\n    for year in years:\n        img_names = os.listdir(f'/kaggle/working/train')\n        img_paths = [\n            f'/kaggle/working/train/{img_name}.jpg' for img_name in img_names\n        ]\n        for i,row in tqdm(target_df.iterrows()):\n#             row = target_df.loc[i]\n            # Get image id\n            img_id = row.id\n            label = row.image_level\n\n            if label=='opacity':\n                # Get bboxes\n                bboxes = get_bbox(row)\n                # Scale bounding boxes\n                scale_bboxes = scale_bbox(row, bboxes)\n                args = (f'/kaggle/working/train/{img_id}.jpg',scale_bboxes)\n                part_annotations = traindf_to_annotations(args)\n                annosArray.append(part_annotations)\n                \n        annotations.extend(annosArray)\n    if out_file.endswith('json'):\n        annotations = cvt_to_coco_json(annotations)\n    mmcv.dump(annotations, out_file)\n    return annotations","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:05.487385Z","iopub.execute_input":"2021-07-14T07:30:05.487733Z","iopub.status.idle":"2021-07-14T07:30:05.49583Z","shell.execute_reply.started":"2021-07-14T07:30:05.4877Z","shell.execute_reply":"2021-07-14T07:30:05.494786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import mmcv\nfrom sklearn.model_selection import train_test_split\n\ntrain=df.sample(frac=0.8,random_state=200) #random state is a seed value\nval=df.drop(train.index)\n\ndataset_name = 'instances_train2017'\nout_dir = '/kaggle/working'\noutfile = osp.join(out_dir, dataset_name + '.json')\ncvt_annotations('ya','2-2','train',outfile)\ndataset_name = 'instances_val2017'\noutfile = osp.join(out_dir, dataset_name + '.json')\ncvt_annotations('ya','2-2','val',outfile)\n# !mv instances_train2017.json #path to swin transformer data/coco/annotations","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd covid-detection","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:10.567681Z","iopub.execute_input":"2021-07-14T07:30:10.568049Z","iopub.status.idle":"2021-07-14T07:30:10.575459Z","shell.execute_reply.started":"2021-07-14T07:30:10.568011Z","shell.execute_reply":"2021-07-14T07:30:10.574567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%mkdir data\n%cd data\n%mkdir coco\n%cd coco\n%mkdir annotations\n%cd annotations\n%cd ../../..\n%mv ../instances_val2017.json data/coco/annotations/instances_val2017.json\n%mv ../instances_train2017.json data/coco/annotations/instances_train2017.json","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:10.577138Z","iopub.execute_input":"2021-07-14T07:30:10.577643Z","iopub.status.idle":"2021-07-14T07:30:13.78874Z","shell.execute_reply.started":"2021-07-14T07:30:10.57759Z","shell.execute_reply":"2021-07-14T07:30:13.787657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:13.79119Z","iopub.execute_input":"2021-07-14T07:30:13.791468Z","iopub.status.idle":"2021-07-14T07:30:14.423939Z","shell.execute_reply.started":"2021-07-14T07:30:13.791442Z","shell.execute_reply":"2021-07-14T07:30:14.42306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -r requirements/build.txt","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:30:14.426985Z","iopub.execute_input":"2021-07-14T07:30:14.427257Z","iopub.status.idle":"2021-07-14T07:30:21.732013Z","shell.execute_reply.started":"2021-07-14T07:30:14.42723Z","shell.execute_reply":"2021-07-14T07:30:21.731032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -v -e .","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/NVIDIA/apex\n%cd apex\n!pip install -v --disable-pip-version-check --no-cache-dir --global-option=\"--cpp_ext\" --global-option=\"--cuda_ext\" ./\n%cd ..","metadata":{"execution":{"iopub.status.idle":"2021-07-14T07:34:56.921892Z","shell.execute_reply.started":"2021-07-14T07:30:43.727141Z","shell.execute_reply":"2021-07-14T07:34:56.920949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python tools/train.py configs/swin/mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py","metadata":{"execution":{"iopub.status.busy":"2021-07-14T07:40:30.9911Z","iopub.execute_input":"2021-07-14T07:40:30.99148Z","iopub.status.idle":"2021-07-14T07:52:07.149478Z","shell.execute_reply.started":"2021-07-14T07:40:30.991447Z","shell.execute_reply":"2021-07-14T07:52:07.148542Z"},"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":[]}]}