Пример #1
0
    def __init__(self, mode):
        """
        :param mode: train2014 or val2014
        """
        self.mode = mode
        self.root = os.path.join(COCO_PATH, mode)
        self.ann_file = os.path.join(COCO_PATH, 'annotations',
                                     'instances_{}.json'.format(mode))
        self.coco = COCO(self.ann_file)
        self.ids = [
            k for k in self.coco.imgs.keys() if len(self.coco.imgToAnns[k]) > 0
        ]

        tform = []
        if self.is_train:
            tform.append(
                RandomOrder([
                    Grayscale(),
                    Brightness(),
                    Contrast(),
                    Sharpness(),
                    Hue(),
                ]))

        tform += [
            SquarePad(),
            Resize(IM_SCALE),
            ToTensor(),
            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
        ]

        self.transform_pipeline = Compose(tform)
        self.ind_to_classes = ['__background__'] + [
            v['name'] for k, v in self.coco.cats.items()
        ]
        # COCO inds are weird (84 inds in total but a bunch of numbers are skipped)
        self.id_to_ind = {
            coco_id: (ind + 1)
            for ind, coco_id in enumerate(self.coco.cats.keys())
        }
        self.id_to_ind[0] = 0

        self.ind_to_id = {x: y for y, x in self.id_to_ind.items()}
Пример #2
0
 def coco(self):
     """
     :return: a Coco-like object that we can use to evaluate detection!
     """
     anns = []
     for i, (cls_array,
             box_array) in enumerate(zip(self.gt_classes, self.gt_boxes)):
         for cls, box in zip(cls_array.tolist(), box_array.tolist()):
             anns.append({
                 'area': (box[3] - box[1] + 1) * (box[2] - box[0] + 1),
                 'bbox':
                 [box[0], box[1], box[2] - box[0] + 1, box[3] - box[1] + 1],
                 'category_id':
                 cls,
                 'id':
                 len(anns),
                 'image_id':
                 i,
                 'iscrowd':
                 0,
             })
     fauxcoco = COCO()
     fauxcoco.dataset = {
         'info': {
             'description': 'ayy lmao'
         },
         'images': [{
             'id': i
         } for i in range(self.__len__())],
         'categories': [{
             'supercategory': 'person',
             'id': i,
             'name': name
         } for i, name in enumerate(self.ind_to_classes)
                        if name != '__background__'],
         'annotations':
         anns,
     }
     fauxcoco.createIndex()
     return fauxcoco
Пример #3
0
  def __init__(self, image_set, year):
    imdb.__init__(self, 'coco_' + year + '_' + image_set)
    # COCO specific config options
    self.config = {'use_salt': True,
                   'cleanup': True}
    # name, paths
    self._year = year
    self._image_set = image_set
    self._data_path = osp.join(cfg.DATA_DIR, 'coco')
    # load COCO API, classes, class <-> id mappings
    self._COCO = COCO(self._get_ann_file())
    cats = self._COCO.loadCats(self._COCO.getCatIds())
    self._classes = tuple(['__background__'] + [c['name'] for c in cats])
    self._class_to_ind = dict(list(zip(self.classes, list(range(self.num_classes)))))
    self._class_to_coco_cat_id = dict(list(zip([c['name'] for c in cats],
                                               self._COCO.getCatIds())))
    self._image_index = self._load_image_set_index()
    # Default to roidb handler
    self.set_proposal_method('gt')
    self.competition_mode(False)

    # Some image sets are "views" (i.e. subsets) into others.
    # For example, minival2014 is a random 5000 image subset of val2014.
    # This mapping tells us where the view's images and proposals come from.
    self._view_map = {
      'minival2014': 'val2014',  # 5k val2014 subset
      'valminusminival2014': 'val2014',  # val2014 \setminus minival2014
      'test-dev2015': 'test2015',
      'valminuscapval2014': 'val2014',
      'capval2014': 'val2014',
      'captest2014': 'val2014'
    }
    coco_name = image_set + year  # e.g., "val2014"
    self._data_name = (self._view_map[coco_name]
                       if coco_name in self._view_map
                       else coco_name)
    # Dataset splits that have ground-truth annotations (test splits
    # do not have gt annotations)
    self._gt_splits = ('train', 'val', 'minival')
Пример #4
0
    def load_coco(self,
                  dataset_dir,
                  subsets,
                  class_ids=None,
                  class_map=None,
                  return_coco=False):
        """Load a subset of the COCO dataset.
        dataset_dir: The root directory of the COCO dataset.
        subset: What to load (train, val, minival, val35k)
        class_ids: If provided, only loads images that have the given classes.
        class_map: TODO: Not implemented yet. Supports maping classes from
            different datasets to the same class ID.
        return_coco: If True, returns the COCO object.
        """
        # Path
        self._view_map = {
            'minival2014': 'val2014',  # 5k val2014 subset
            'valminusminival2014': 'val2014',  # val2014 \setminus minival2014
            'test-dev2015': 'test2015',
        }
        self.image_ids = []
        for subset in subsets:

            image_dir = os.path.join(
                dataset_dir, 'images',
                "train2014" if subset == "train" else "val2014")

            # Create COCO object
            json_path_dict = {
                "train": "annotations/instances_train2014.json",
                "val": "annotations/instances_val2014.json",
                "minival": "annotations/instances_minival2014.json",
                "val35k": "annotations/instances_valminusminival2014.json",
            }
            coco = COCO(os.path.join(dataset_dir, json_path_dict[subset]))
            image_ids = []
            # Load all classes or a subset?
            if not class_ids:
                # All classes
                class_ids = sorted(coco.getCatIds())

            # All images or a subset?
            if class_ids:
                for id in class_ids:
                    image_ids.extend(list(coco.getImgIds(catIds=[id])))
                # Remove duplicates
                image_ids = list(set(image_ids))
            else:
                # All images
                image_ids = list(coco.imgs.keys())

            # Add classes
            for i in class_ids:
                self.add_class("coco", i, coco.loadCats(i)[0]["name"])

            # Add images
            self.image_ids += image_ids
            for i in image_ids:
                self.add_image("coco",
                               image_id=i,
                               path=os.path.join(image_dir,
                                                 coco.imgs[i]['file_name']),
                               width=coco.imgs[i]["width"],
                               height=coco.imgs[i]["height"],
                               annotations=coco.loadAnns(
                                   coco.getAnnIds(imgIds=[i],
                                                  catIds=class_ids,
                                                  iscrowd=None)))
            print(subset)
            print(len(self.image_info))
        if return_coco:
            return coco
Пример #5
0
    def load_coco(self,
                  dataset_dir,
                  subset,
                  year=DEFAULT_DATASET_YEAR,
                  class_ids=None,
                  class_map=None,
                  return_coco=False,
                  auto_download=False):
        """Load a subset of the COCO dataset.
        dataset_dir: The root directory of the COCO dataset.
        subset: What to load (train, val, minival, valminusminival)
        year: What dataset year to load (2014, 2017) as a string, not an integer
        class_ids: If provided, only loads images that have the given classes.
        class_map: TODO: Not implemented yet. Supports maping classes from
            different datasets to the same class ID.
        return_coco: If True, returns the COCO object.
        auto_download: Automatically download and unzip MS-COCO images and annotations
        """

        if auto_download is True:
            self.auto_download(dataset_dir, subset, year)

        coco = COCO("{}/annotations/instances_{}{}.json".format(
            dataset_dir, subset, year))
        if subset == "minival" or subset == "valminusminival":
            subset = "val"
        image_dir = "{}/{}{}".format(dataset_dir, subset, year)

        # Load all classes or a subset?
        if not class_ids:
            # All classes
            class_ids = sorted(coco.getCatIds())

        # All images or a subset?
        if class_ids:
            image_ids = []
            for id in class_ids:
                image_ids.extend(list(coco.getImgIds(catIds=[id])))
            # Remove duplicates
            image_ids = list(set(image_ids))
        else:
            # All images
            image_ids = list(coco.imgs.keys())

        # Add classes
        for i in class_ids:
            self.add_class("coco", i, coco.loadCats(i)[0]["name"])

        # Add images
        for i in image_ids:
            self.add_image("coco",
                           image_id=i,
                           path=os.path.join(image_dir,
                                             coco.imgs[i]['file_name']),
                           width=coco.imgs[i]["width"],
                           height=coco.imgs[i]["height"],
                           annotations=coco.loadAnns(
                               coco.getAnnIds(imgIds=[i],
                                              catIds=class_ids,
                                              iscrowd=None)))
        if return_coco:
            return coco