Skip to content

Module sagemaker_defect_detection.utils.coco_utils

BSD 3-Clause License

Copyright (c) Soumith Chintala 2016, All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This module is a modified version of https://github.com/pytorch/vision/tree/03b1d38ba3c67703e648fb067570eeb1a1e61265/references/detection

View Source
"""

BSD 3-Clause License

Copyright (c) Soumith Chintala 2016,

All rights reserved.

Redistribution and use in source and binary forms, with or without

modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this

  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,

  this list of conditions and the following disclaimer in the documentation

  and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its

  contributors may be used to endorse or promote products derived from

  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE

FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This module is a modified version of https://github.com/pytorch/vision/tree/03b1d38ba3c67703e648fb067570eeb1a1e61265/references/detection

"""

from pycocotools.coco import COCO

def convert_to_coco_api(ds):

    coco_ds = COCO()

    # annotation IDs need to start at 1, not 0, see torchvision issue #1530

    ann_id = 1

    dataset = {"images": [], "categories": [], "annotations": []}

    categories = set()

    for img_idx in range(len(ds)):

        # find better way to get target

        # targets = ds.get_annotations(img_idx)

        img, targets, _ = ds[img_idx]

        image_id = targets["image_id"].item()

        img_dict = {}

        img_dict["id"] = image_id

        img_dict["height"] = img.shape[-2]

        img_dict["width"] = img.shape[-1]

        dataset["images"].append(img_dict)

        bboxes = targets["boxes"]

        bboxes[:, 2:] -= bboxes[:, :2]

        bboxes = bboxes.tolist()

        labels = targets["labels"].tolist()

        areas = targets["area"].tolist()

        iscrowd = targets["iscrowd"].tolist()

        num_objs = len(bboxes)

        for i in range(num_objs):

            ann = {}

            ann["image_id"] = image_id

            ann["bbox"] = bboxes[i]

            ann["category_id"] = labels[i]

            categories.add(labels[i])

            ann["area"] = areas[i]

            ann["iscrowd"] = iscrowd[i]

            ann["id"] = ann_id

            dataset["annotations"].append(ann)

            ann_id += 1

    dataset["categories"] = [{"id": i} for i in sorted(categories)]

    coco_ds.dataset = dataset

    coco_ds.createIndex()

    return coco_ds

Functions

convert_to_coco_api

def convert_to_coco_api(
    ds
)
View Source
def convert_to_coco_api(ds):

    coco_ds = COCO()

    # annotation IDs need to start at 1, not 0, see torchvision issue #1530

    ann_id = 1

    dataset = {"images": [], "categories": [], "annotations": []}

    categories = set()

    for img_idx in range(len(ds)):

        # find better way to get target

        # targets = ds.get_annotations(img_idx)

        img, targets, _ = ds[img_idx]

        image_id = targets["image_id"].item()

        img_dict = {}

        img_dict["id"] = image_id

        img_dict["height"] = img.shape[-2]

        img_dict["width"] = img.shape[-1]

        dataset["images"].append(img_dict)

        bboxes = targets["boxes"]

        bboxes[:, 2:] -= bboxes[:, :2]

        bboxes = bboxes.tolist()

        labels = targets["labels"].tolist()

        areas = targets["area"].tolist()

        iscrowd = targets["iscrowd"].tolist()

        num_objs = len(bboxes)

        for i in range(num_objs):

            ann = {}

            ann["image_id"] = image_id

            ann["bbox"] = bboxes[i]

            ann["category_id"] = labels[i]

            categories.add(labels[i])

            ann["area"] = areas[i]

            ann["iscrowd"] = iscrowd[i]

            ann["id"] = ann_id

            dataset["annotations"].append(ann)

            ann_id += 1

    dataset["categories"] = [{"id": i} for i in sorted(categories)]

    coco_ds.dataset = dataset

    coco_ds.createIndex()

    return coco_ds