Beispiel #1
0
    def _load_coco_annotation(self, index):
        """
    Loads COCO bounding-box instance annotations. Crowd instances are
    handled by marking their overlaps (with all categories) to -1. This
    overlap value means that crowd "instances" are excluded from training.
    """
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)
        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        # Lookup table to map from COCO category ids to our internal class
        # indices
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])

        for ix, obj in enumerate(objs):
            cls = coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas
        }
Beispiel #2
0
    def _load_coco_annotation(self, index):
        """
        Loads COCO bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)
        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        # Lookup table to map from COCO category ids to our internal class
        # indices
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])

        for ix, obj in enumerate(objs):
            cls = coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {'boxes' : boxes,
                'gt_classes': gt_classes,
                'gt_overlaps' : overlaps,
                'flipped' : False,
                'seg_areas' : seg_areas}
Beispiel #3
0
    def _load_display_annotation(self, index):
        """
        Loads Display bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """

        annotation_file = os.path.join(
            self._data_path,
            'annotations/instances_{}.txt'.format(self._image_set))
        assert os.path.isfile(annotation_file), annotation_file

        txt_annotations = open(annotation_file, 'r')
        annotations = txt_annotations.readlines()

        num_objs = 3
        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)
        width, height = 0, 0

        for i in range(0, len(annotations), 3):
            if i != index:
                continue
            temp = annotations[i].split(',')

            # data in ground truth file has 3 line for each img
            for j in range(0, 3):
                temp = annotations[i + j].split(',')
                x1 = int(temp[1])
                y1 = int(temp[2])
                x2 = int(temp[3])
                y2 = int(temp[4])
                width = x2 - x1
                height = y2 - y1
                box = [x1, y1, x2, y2]
                boxes[j, :] = box
                gt_classes[j] = int(temp[5][0]) + 1
                seg_areas[j] = (x2 - x1) * (y2 - y1)
                overlaps[j, int(temp[5][0]) + 1] = 1.0
        print("===================[display.py:173] ", overlaps)
        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        print("===================[display.py:173] ", overlaps)

        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas
        }
    def _loadCarlaAnnotation(self, index):
        """
        Load the annotations of the ith image from the info list
        """
        width, height = 1920, 1080

        objList = np.array(self.infos[self.image_id_at(index)])
        nObjects = len(objList)

        # Ignores
        ignores = np.array([False] * nObjects, dtype = int)

        # Ground-truth class
        objClasses = ds.getClasses(objList, self.classes[0]) # Class name of objects
        gtClassses = np.array([self.cls2Ind[c] for c in objClasses]) # convert to the index of class
        
        # Boxes
        boxes = ds.get2dBoxes(objList)

        # 3d object centers
        locations = ds.get3dLocations(objList)
        centers = self._convertTo(locations)

        # Overlaps (one-hot vector form of labels)
        overlaps = np.zeros((nObjects, self.num_classes), dtype = float)
        overlaps[np.arange(nObjects), gtClassses] += 1
        overlaps = scipy.sparse.csr_matrix(overlaps) # convert to a sparse matrix

        # Area of segments
        segAreas = self._computeSegAreas(boxes)

        # End-of-video id (flag)
        endvids = np.zeros(nObjects, dtype = int)
        if (index == len(self.infos) - 1):
            # Last image of the video, set them to 1
            endvids += 1

        # Validation boxes
        ds.validate_boxes(boxes, width=width, height=height)

        info_set = {
            "width": width,
            "height": height,
            "boxes": boxes,
            "gt_classes": gtClassses,
            "gt_overlaps": overlaps,
            "flipped": False,
            "seg_areas": segAreas,
            "ignore": ignores,
            "end_vid": endvids,
            "center": centers
            }

        return info_set
Beispiel #5
0
    def _load_proposals(self, method, gt_roidb):
        """
        Load pre-computed proposals in the format provided by Jan Hosang:
        http://www.mpi-inf.mpg.de/departments/computer-vision-and-multimodal-
          computing/research/object-recognition-and-scene-understanding/how-
          good-are-detection-proposals-really/
        For MCG, use boxes from http://www.eecs.berkeley.edu/Research/Projects/
          CS/vision/grouping/mcg/ and convert the file layout using
        lib/datasets/tools/mcg_munge.py.
        """
        box_list = []
        imsize_list = []
        top_k = self.config['top_k']
        valid_methods = [
            'MCG',
            'selective_search',
            'edge_boxes_AR',
            'edge_boxes_70']
        assert method in valid_methods

        print 'Loading {} boxes'.format(method)
        for i, index in enumerate(self._image_index):
            if i % 1000 == 0:
                print '{:d} / {:d}'.format(i + 1, len(self._image_index))

            box_file = osp.join(
                cfg.DATA_DIR, 'coco_proposals', method, 'mat',
                self._get_box_file(index))

            raw_data = sio.loadmat(box_file)['boxes']
            boxes = np.maximum(raw_data - 1, 0).astype(np.uint16)
            if method == 'MCG':
                # Boxes from the MCG website are in (y1, x1, y2, x2) order
                boxes = boxes[:, (1, 0, 3, 2)]
            im_ann = self._COCO.loadImgs(index)[0]
            width = im_ann['width']
            height = im_ann['height']
            imsize_list.append([height,width])
            # Remove duplicate boxes and very small boxes and then take top k
            keep = ds_utils.unique_boxes(boxes)
            boxes = boxes[keep, :]
            keep = ds_utils.filter_small_boxes(boxes, self.config['min_size'])
            boxes = boxes[keep, :]
            boxes = boxes[:top_k, :]
            boxes[:,0]=np.maximum(boxes[:,0]-cfg.MARGIN,0)
            boxes[:,2]=np.minimum(boxes[:,2]+cfg.MARGIN,width-1)
            boxes[:,1]=np.maximum(boxes[:,1]-cfg.MARGIN,0)
            boxes[:,3]=np.minimum(boxes[:,3]+cfg.MARGIN,height-1)
            box_list.append(boxes)
            # Sanity check
            
            ds_utils.validate_boxes(boxes, width=width, height=height)
        return self.create_roidb_from_box_list(box_list, gt_roidb,imsize_list)
Beispiel #6
0
    def _load_vg_annotation(self, index):
        """
        Loads VG bounding-box instance annotations.
        """
        img = self._VG.load_imgs(index)[0]
        width = img['width']
        height = img['height']

        ann_ids = self._VG.get_ann_ids(img_ids=index)
        anns = self._VG.load_anns(ann_ids)
        # Sanitize bboxes -- some are invalid
        valid_anns = []
        for ann in anns:
            x1 = np.max((0, ann['x']))
            y1 = np.max((0, ann['y']))
            x2 = np.min((width - 1, x1 + np.max((0, ann['w'] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, ann['h'] - 1))))
            if x2 >= x1 and y2 >= y1:
                ann['clean_bbox'] = [x1, y1, x2, y2]
                valid_anns.append(ann)
        anns = valid_anns
        num_anns = len(anns)

        boxes = np.zeros((num_anns, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_anns), dtype=np.int32)
        overlaps = np.zeros((num_anns, self.num_classes), dtype=np.float32)

        # # Lookup table to map from VG category ids to our internal class
        # # indices
        # vg_id_to_class_ind = dict([(self._class_to_vg_id[cls], self._class_to_ind[cls])
        #                            for cls in self._classes[1:]])

        for ix, ann in enumerate(anns):
            cls = self._vg_id_to_class_ind[ann['category_id']]
            boxes[ix, :] = ann['clean_bbox']
            gt_classes[ix] = cls
            overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False
        }
Beispiel #7
0
  def _load_vg_annotation(self, index):
    """
    Loads visual genome bounding-box instance annotations. 
    """
    object_anno = self._VG.loadImgsAnno(index)
    height, width = self._VG.getImageWidthHeights(index)
    objs = object_anno['objects']
    # Sanitize bboxes -- some are invalid
    valid_objs = []
    for obj in objs:
      if obj['names'][0] in self.cats:
          x1 = np.max((0, obj['x']))
          y1 = np.max((0, obj['y']))
          x2 = np.min((width - 1, x1 + np.max((0, obj['w'] - 1))))
          y2 = np.min((height - 1, y1 + np.max((0, obj['h'] - 1))))
          obj['area'] = obj['w'] * obj['h']
          if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
            obj['clean_bbox'] = [x1, y1, x2, y2]
            valid_objs.append(obj)
    objs = valid_objs
    num_objs = len(objs)

    boxes = np.zeros((num_objs, 4), dtype=np.uint16)
    gt_classes = np.zeros((num_objs), dtype=np.int32)
    overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
    seg_areas = np.zeros((num_objs), dtype=np.float32)

    for ix, obj in enumerate(objs):
      cls = self._class_to_ind[obj["names"][0]]
      boxes[ix, :] = obj['clean_bbox']
      gt_classes[ix] = cls
      seg_areas[ix] = obj['area']
      overlaps[ix, cls] = 1.0

    ds_utils.validate_boxes(boxes, width=width, height=height)
    overlaps = scipy.sparse.csr_matrix(overlaps)
    #print('processing time:{}'.format(time.time()-t))
    return {'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas}
    def _load_stacked_annotation(self, index, gtfile):
        """
    Loads bounding-box instance annotations.
    """
        gtboxes, classes, gtids = self.idx2gtinstance(index, gtfile)
        width = self._widths[index]
        height = self._heights[index]

        num_objs = len(gtboxes)
        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes),
                            dtype=np.float32)  # 1 at the gt class, 0 otherwise
        seg_areas = np.zeros((num_objs), dtype=np.float32)  # box area here
        gt_ids = np.array(gtids, dtype=int)

        for i, ibox in enumerate(gtboxes):
            bbox = ibox - 1  # start at 0
            x1 = bbox[0]
            y1 = bbox[1]
            x2 = bbox[2]
            y2 = bbox[3]

            boxes[i, :] = bbox
            cls = classes[i]
            gt_classes[i] = cls
            overlaps[i, cls] = 1
            seg_areas[i] = (x2 - x1 + 1) * (y2 - y1 + 1)

        overlaps = scipy.sparse.csr_matrix(overlaps)
        ds_utils.validate_boxes(boxes, width=width, height=height)

        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'gt_ids': gt_ids,
            'flipped': False,
            'seg_areas': seg_areas
        }
Beispiel #9
0
    def _load_coco_text_annotation(self, index):
        """
        coco returns dict with keys ('boxes', gt_classes',
        'gt_overlaps', 'flipped', 'seg_areas')
        """
        im_ann = self._ct.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        objs = self._ct.loadAnns(self._ct.getAnnIds(imgIds=index))
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)
        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.ones((num_objs), dtype=np.int32)  # always the same
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        for ix, obj in enumerate(objs):
            boxes[ix, :] = obj['clean_bbox']
            seg_areas[ix] = obj['area']
            overlaps[ix, 1] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas
        }
Beispiel #10
0
    def _load_mot_annotation(self):
        gt_roidb = []
        annotationfile = os.path.join(self._data_path, 'gt_bbox.txt')
        f = open(annotationfile)
        split_line = f.readline().strip().split()
        num = 1
        while (split_line):

            num_objs = int(split_line[1])
            boxes = np.zeros((num_objs, 4), dtype=np.int32)
            gt_classes = np.zeros((num_objs), dtype=np.int32)
            overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
            for i in range(num_objs):

                x1 = max(0, float(split_line[2 + i * 4]))
                y1 = max(0, float(split_line[3 + i * 4]))
                x2 = min(1919, float(split_line[4 + i * 4]))
                y2 = min(1079, float(split_line[5 + i * 4]))
                # x1 = float(split_line[2 + i * 4])
                # y1 = float(split_line[3 + i * 4])
                # x2 = float(split_line[4 + i * 4])
                # y2 = float(split_line[5 + i * 4])
                cls = self._class_to_ind['person']
                boxes[i, :] = [x1, y1, x2, y2]
                gt_classes[i] = cls
                overlaps[i, cls] = 1.0
            ds_utils.validate_boxes(boxes, 1920, 1080)

            overlaps = scipy.sparse.csr_matrix(overlaps)
            gt_roidb.append({
                'boxes': boxes,
                'gt_classes': gt_classes,
                'gt_overlaps': overlaps,
                'flipped': False
            })
            split_line = f.readline().strip().split()

        f.close()
        return gt_roidb
Beispiel #11
0
    def _load_coco_annotation(self, index):
        """
        Loads COCO bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        if cfg.DEBUG:
            print '_load_coco_annotation'
            
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)
        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)
        masks = np.zeros((num_objs, height, width), dtype=np.uint16)

        # Lookup table to map from COCO category ids to our internal class
        # indices
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])

        for ix, obj in enumerate(objs):
            cls = coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

            #*************************************
            #   read mask_name for every instance
            #*************************************
            # path e.g., /data/coco/gt_mask/train2014/groundt_train_000000000009.png
            mask_path = osp.join(self._data_path, 'gt_mask', self._data_name, obj['mask_name'])

            mask = np.asarray(mat4py.loadmat(mask_path)['mask_gt'])
            # mask_flat = np.asarray(list(PIL.Image.open(mask_path).getdata()))
            # mask = mask_flat.reshape((height, width))
            mask[np.where(mask != obj['id'])] = 0
            mask[np.where(mask == obj['id'])] = 1
            masks[ix,:,:] = mask

        if cfg.DEBUG:
            print masks.shape

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {'boxes' : boxes,
                'gt_classes': gt_classes,
                'gt_overlaps' : overlaps,
                'flipped' : False,
                'seg_areas' : seg_areas,
                'masks' : masks}
Beispiel #12
0
    def _load_coco_annotation(self, index):
        """
        Loads COCO bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        count = 0
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                if cfg.TRAIN.MASK_REG:
                    count = count + 1
                    obj['seg_mask_ind'] = [index, count]
                    seg_mask = self._COCO.annToMask(obj)
                    seg_mask_save = seg_mask
                    seg_mask_save = seg_mask_save.astype(bool)
                    seg_mask_path = './data/cache/' + 'GTsegmask_' + cfg.TRAIN.TRAINING_DATA + '/' + str(index) + '_' + str(count) + '_segmask.sm'
                    with open(seg_mask_path, 'wb') as f_seg_save:
                        cPickle.dump(seg_mask_save, f_seg_save, cPickle.HIGHEST_PROTOCOL)
                valid_objs.append(obj)

        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)
        seg_mask_inds = np.zeros((num_objs, 2), dtype=np.uint32)
        # Lookup table to map from COCO category ids to our internal class
        # indices
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])

        for ix, obj in enumerate(objs):
            cls = coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            if cfg.TRAIN.MASK_REG:
                seg_mask_inds[ix, :] = obj['seg_mask_ind']
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)

        if not cfg.TRAIN.MASK_REG:
            return {'boxes' : boxes,
                    'gt_classes' : gt_classes,
                    'gt_overlaps' : overlaps,
                    'flipped' : False,
                    'seg_areas' : seg_areas}
        else:
            return {'boxes' : boxes,
                    'gt_classes' : gt_classes,
                    'gt_overlaps' : overlaps,
                    'flipped' : False,
                    'seg_areas' : seg_areas,
                    'seg_mask_inds' : seg_mask_inds}
Beispiel #13
0
    def _load_gta_annotation(self, index):
        """
        Loads GTA bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        width = 1920
        height = 1080

        info = self.dataset[self.image_id_at(index)]
        labels = info['labels']  # a list of dict
        # get the kitti part out and insert the tracking id
        boxes = ds.get_box2d_array(labels).astype(float)[:, :4]
        tid = ds.get_label_array(labels, ['id'], (0)).astype(int)
        num_objs = len(tid)
        #gt_cls = ds.get_label_array(labels, ['class'], (0))
        gt_cls = np.array(['foreground'] * num_objs)
        gt_classes = np.ones(num_objs)
        # actually just one single value,
        ignore = ds.get_label_array(labels, ['attributes', 'ignore'],
                                    (0)).astype(int)
        cam_calib = np.array(info['intrinsics']['cali'])
        location = ds.get_label_array(labels, ['box3d', 'location'],
                                      (0, 3)).astype(float)
        ext_loc = np.hstack([location, np.ones([len(location), 1])])  # (B, 4)
        proj_loc = ext_loc.dot(cam_calib.T)  # (B, 4) dot (3, 4).T => (B, 3)
        center = proj_loc[:, :2] / proj_loc[:, 2:3]  # normalize

        seg_areas = (boxes[:, 2] - boxes[:, 0] + 1) * \
                    (boxes[:, 3] - boxes[:, 1] + 1)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        endvid = np.zeros((num_objs), dtype=np.uint16)
        # pad to make it consistent
        if self.endvid[self.image_id_at(index)]:
            endvid += 1

        for ix in range(num_objs):
            cls = self._class_to_ind[gt_cls[ix].strip()]
            gt_classes[ix] = cls
            overlaps[ix, cls] = 1.0

        ds.validate_boxes(boxes, width=width, height=height)
        print("overlaps:", overlaps)
        overlaps = scipy.sparse.csr_matrix(overlaps)

        # print("end of video:", endvid)
        # print("overlaps:", overlaps)

        info_set = {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas,
            'ignore': ignore,
            'end_vid': endvid,
            'center': center
        }

        # print(info_set)
        return info_set
Beispiel #14
0
    def _load_coco_annotation(self, index):
        """
    Loads COCO bounding-box instance annotations. Crowd instances are
    handled by marking their overlaps (with all categories) to -1. This
    overlap value means that crowd "instances" are excluded from training.
    """
        # print('index:\n', index)
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']
        # catIds = self._COCO.getCatIds(catNms=['cell phone', 'laptop'])

        # Change here to load only boxes that correspond to cell & laptot ? or load everything and call them "other"
        annIds = self._COCO.getAnnIds(imgIds=index,
                                      catIds=self._c_to_keep,
                                      iscrowd=None)
        # print('annIds size:\n', len(annIds))
        objs = self._COCO.loadAnns(annIds)
        # print('objs:\n', objs)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)
        objs = valid_objs

        self._total_positive += 1 if len(
            objs) >= 1 else 0  # <-------------- ADDED

        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        # Lookup table to map from COCO category ids to our internal class
        # indices
        ############################################################################### lookup table
        # <------------------------------------------------------------------------------------------ Modified
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])
        #coco_cat_id_to_class_ind = dict([(c, i+1) for i, c in enumerate(self._c_to_keep)])
        # coco_cat_id_to_class_ind = {73:1, 77:2}

        #print('coco_cat_id_to_class_ind:\n', coco_cat_id_to_class_ind)

        for ix, obj in enumerate(objs):
            cls = coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas
        }
    def _load_openimages_annotation(self, i):
        """
    Loads openimages bounding-box instance annotations. Crowd instances are
    handled by marking their overlaps (with all categories) to -1. This
    overlap value means that crowd "instances" are excluded from training.
    """
        width = self._imsize[i][1]
        height = self._imsize[i][2]

        index = self._image_index[i]
        if index not in self._annotation_dict:
            objs = []
            print("not loading image {0}".format(index))
        else:
            objs = self._annotation_dict[index]

        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for number, obj in enumerate(objs):
            '''
      x1 = np.max((0, int(width * obj[2])))
      x2 = np.min((width - 1, int((width - 1) * obj[3])))
      y1 = np.max((0, int(height * obj[4])))
      y2 = np.min((height - 1, int((height - 1) * obj[5])))
      '''
            x1 = int(width * obj[2])
            x2 = int((width - 1) * obj[3])
            y1 = int(height * obj[4])
            y2 = int((height - 1) * obj[5])

            # TODO are the BB cooridnates diferent?

            if x2 >= x1 and y2 >= y1:
                valid_objs.append((obj[0], obj[1], x1, y1, x2, y2))
            else:
                print("filtered")

        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        # Lookup table to map from COCO category ids to our internal class
        # indices
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])

        for ix, obj in enumerate(objs):
            cls = coco_cat_id_to_class_ind[obj[1]]
            boxes[ix, :] = [obj[2], obj[3], obj[4], obj[5]]
            gt_classes[ix] = cls
            seg_areas[ix] = (obj[4] - obj[2]) * (obj[5] - obj[3])

            # TODO what does overlaps do?
            overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas
        }
Beispiel #16
0
    def _load_coco_annotation(self, index):
        """
        Loads COCO bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        # print type(index)
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']
        img_name = im_ann['file_name']

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)
        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        for ix, obj in enumerate(objs):
            cls = self._coco_ind_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        # check bbox boundary validation
        ds_utils.validate_boxes(boxes, width=width, height=height)
        
        # max overlap with gt over classes (columns)
        max_overlaps = overlaps.argmax(axis=1)
        # gt class that had the max overlap
        max_classes = overlaps.argmax(axis=1)
        # sanity checks
        # max overlap of 0 => class should be zero (background)
        zero_inds = np.where(max_overlaps == 0)[0]
        assert all(max_classes[zero_inds] == 0)
        # max overlap > 0 => class should not be zero (must be a fg class)        
        nonzero_inds = np.where(max_overlaps > 0)[0]
        assert all(max_classes[nonzero_inds] != 0)  

        return {'image': img_name,
                'height': height,
                'width': width,        
                'boxes' : boxes,
                'gt_classes': gt_classes,
                'gt_overlaps' : overlaps,
                'max_classes': max_classes,
                'max_overlaps': max_overlaps,                
                'flipped' : False,
                'seg_areas' : seg_areas}        
    def _loadCarlaAnnotation(self, index, vis=False):
        """
        Load the annotations of the ith image from the info list
        """
        width, height = 1920, 1080

        # "labels"
        info = self.infos[self.image_id_at(index)]
        labels = info["labels"]

        # Object ids
        objIds = ds.get_label_array(labels, key_list=["id"],
                                    empty_shape=(0, )).astype(int)
        nObjects = len(objIds)

        # Ignores
        ignores = ds.get_label_array(labels,
                                     key_list=["attributes", "ignore"],
                                     empty_shape=(0, )).astype(int)

        # Ground-truth class
        objClasses = ds.getClassArray(
            labels, self.classes[0], emptyShape=(0, ))  # Class name of objects
        gtClassses = np.array([self.cls2Ind[c] for c in objClasses
                               ])  # convert to the index of class

        # Boxes
        boxes = ds.get_box2d_array(labels)[:, :4]  # ignore the confidence

        # 3d object centers
        locations = ds.get_label_array(labels,
                                       key_list=["box3d", "location"],
                                       empty_shape=(0, 3)).astype(float)
        centers = self._projection(locations)

        # Overlaps (one-hot vector form of labels)
        overlaps = np.zeros((nObjects, self.num_classes), dtype=float)
        overlaps[np.arange(nObjects), gtClassses] += 1

        overlaps = scipy.sparse.csr_matrix(
            overlaps)  # convert to a sparse matrix

        # Area of segments
        segAreas = self._computeSegAreas(boxes)

        # End-of-video id (flag)
        endvids = np.zeros(nObjects, dtype=int)
        if (self.endvids[self.image_id_at(index)]):
            # Last image of the video, set them to 1
            endvids += 1

        # Validation boxes
        flag = ds.validate_boxes(boxes, width=width, height=height)

        # Information set
        infoSet = None

        if (flag):
            # Debug
            # print(self.image_path_at(index))
            # print(endvids)

            if vis:
                im = cv2.imread(self.image_path_at(index))
                im2show = np.copy(im)
                im2show = self._drawBBoxAndCenter(im2show, boxes, centers)
                cv2.imwrite(
                    os.path.join("vis", "inputs",
                                 'result%d.png' % (index + 1)), im2show)

            infoSet = {
                "width": width,
                "height": height,
                "boxes": boxes,
                "gt_classes": gtClassses,
                "gt_overlaps": overlaps,
                "flipped": False,
                "seg_areas": segAreas,
                "ignore": ignores,
                "end_vid": endvids,
                "center": centers
            }

        return infoSet
Beispiel #18
0
    def _load_coco_annotation(self, index):
        """
        Loads COCO bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        '''
         annotation{
                    "id": int,
                    "image_id": int,
                    "category_id": int,
                    "segmentation": RLE or [polygon],
                    "area": float,
                    "bbox": [x,y,width,height],
                    "iscrowd": 0 or 1,
                    }
        '''
        ############################ write information for debugging
        # f = open("./tools/tracking_file.txt", "w")
        # f.write("\nimage index: " + str(index) + "\n")
        # f.write("\nimage height, width: " + str(height) + " " + str(width) + "\n")
        ############################
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        count = 0
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))  #col
            y1 = np.max((0, obj['bbox'][1]))  #row
            x2 = np.min((width - 1, x1 + np.max(
                (0, obj['bbox'][2] - 1))))  #col
            y2 = np.min((height - 1, y1 + np.max(
                (0, obj['bbox'][3] - 1))))  #row
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                ################# get segmentation mask of this object###############################################
                if cfg.TRAIN.MASK_REG:
                    count = count + 1
                    obj['seg_mask_ind'] = [
                        index, count
                    ]  #image index and order of this object in the image
                    seg_mask = self._COCO.annToMask(obj)
                    seg_mask_save = np.zeros((height, width), dtype=np.bool_)
                    seg_mask_save = seg_mask
                    seg_mask_save = seg_mask_save.astype(bool)
                    # save the mask into disk
                    #seg_mask_path = './data/cache/seg_mask_coco_gt/' + str(index) + '_' + str(count) + '_segmask.sm'
                    seg_mask_path = './data/cache/' + 'GTsegmask_' + cfg.TRAIN.TRAINING_DATA + '/' + str(
                        index) + '_' + str(count) + '_segmask.sm'
                    with open(seg_mask_path, 'wb') as f_seg_save:
                        cPickle.dump(seg_mask_save, f_seg_save,
                                     cPickle.HIGHEST_PROTOCOL)

                    # f.write("\nobject: " + str(count) + "\n")
                    # f.write("\ncoordinate (x1(col) y1(row) x2 y2): " + str(x1) + " " + str(y1) + " " + str(x2) + " " + str(y2) + "\n")
                    # f.write("\narea: " + str(obj['area']) + "\n")
                    # f.write("\nsegmentation mask size: " + str(seg_mask.shape[0]) + " " + str(seg_mask.shape[1]) + "\n")
                    # f.write("\n")
                    # print segmentation mask for debugging
                    # if index == 262145 and count == 1:
                    #     f_seg = open("./tools/segment_mask.txt", "w")
                    #     for i1 in xrange(0, height):
                    #         for i2 in xrange(0, width):
                    #             f_seg.write(str(seg_mask[i1][i2]) + " ")
                    #         f_seg.write("\n")
                    #     f_seg.close()
                ######################################################################################################
                valid_objs.append(obj)
        # f.write("\n")
        # f.close()

        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)
        ################
        seg_mask_inds = np.zeros(
            (num_objs, 2),
            dtype=np.uint32)  #save image index and object index in this image
        #seg_masks = np.zeros((num_objs, height * width), dtype=np.bool_) #bool data type
        ################
        # Lookup table to map from COCO category ids to our internal class
        # indices
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])

        for ix, obj in enumerate(
                objs
        ):  #moi element cua objs la 1 obj voi nhieu fields nhu 'category_id','clean_bbox',.... Chu ban than objs ko co cac fields nhu tren. objs chi chua obj
            cls = coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            ###########
            if cfg.TRAIN.MASK_REG:
                #seg_masks[ix] = obj['seg_mask']
                seg_mask_inds[ix, :] = obj['seg_mask_ind']
            ###########
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        ############
        #seg_masks = scipy.sparse.csr_matrix(seg_masks) #store seg_masks as sparse matrix
        ############
        if not cfg.TRAIN.MASK_REG:
            return {
                'boxes': boxes,
                'gt_classes': gt_classes,
                'gt_overlaps': overlaps,
                'flipped': False,  #--->flag day la anh original
                'seg_areas': seg_areas
            }
        else:
            #print "========================Have seg_mask_inds======================"
            return {
                'boxes': boxes,
                'gt_classes': gt_classes,
                'gt_overlaps': overlaps,
                'flipped': False,  #--->flag day la anh original
                'seg_areas': seg_areas,
                'seg_mask_inds': seg_mask_inds
            }
Beispiel #19
0
    def _load_coco_annotation(self, index):
        """
        Loads COCO bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        im_ann = self._COCO.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                # mask = self._COCO.annToMask(obj)
                # start_h = np.round(np.max((1, y1))).astype(np.int)
                # end_h = np.round(np.min((height, y2))).astype(np.int)
                # start_w = np.round(np.max((1, x1))).astype(np.int)
                # end_w = np.round(np.min((width, x2))).astype(np.int)
                # # print 'mask shape---',mask.shape
                # # print '\nsize:',start_h,end_h,start_w,end_w
                # # print '\n,x1,y1,x2,y2',x1,y1,x2,y2
                # if start_h==end_h:
                #     end_h +=1
                # if start_w == end_w:
                #     end_w +=1
                # cropped_mask = mask[start_h:end_h, start_w:end_w]
                # resized_mask = cv2.resize(cropped_mask, (cfg.MASK_SIZE, cfg.MASK_SIZE), interpolation=cv2.INTER_CUBIC)
                obj['mask'] = obj
                # valid_objs.append(obj)
                if obj['iscrowd'] == 0:
                    valid_objs.append(obj)

        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)
        masks = []
        # masks = np.zeros((num_objs,cfg.MASK_SIZE,cfg.MASK_SIZE),dtype=np.float32)

        # Lookup table to map from COCO category ids to our internal class
        # indices
        coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
                                          self._class_to_ind[cls])
                                         for cls in self._classes[1:]])

        for ix, obj in enumerate(objs):
            cls = coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            # masks[ix,:,:] = obj['mask']
            masks.append(obj['mask'])
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas,
            'masks': masks
        }
Beispiel #20
0
    def _load_kitti_annotation(self, index):
        """
        Loads KITTI bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        im_ann = self._KITTI.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        # Follow 'demo_load_kitti_dataset.py by Soonmin'
        hRng, occLevel, tRng = self.config['hRng'], self.config[
            'occLevel'], self.config['truncRng']

        # Load annotation ids
        annIds = self._KITTI.getAnnIds(imgIds=index,
                                       catIds=self._raw_cat_ids,
                                       hRng=hRng,
                                       occLevel=occLevel,
                                       truncRng=tRng)
        #annIds = self._KITTI.getAnnIds(imgIds=index, hRng=hRng, occLevel=occLevel, truncRng=tRng)

        objs = self._KITTI.loadAnns(annIds)

        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for obj in objs:
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))

            # All valid annotations must satisfy below condition
            if x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)

        objs = valid_objs
        num_objs = len(objs)

        # In traffic scene datasets (e.g. KITTI, KAIST),
        #   some images may not contain any target object instance.
        #   Then, num_objs == 0.
        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)

        # Lookup table to map from KITTI category ids to our internal class indices
        kitti_cat_id_to_class_ind = dict([(self._class_to_kitti_cat_id[cls],
                                           self._class_to_ind[cls])
                                          for cls in self._classes[1:]])

        for ix, obj in enumerate(objs):
            cls = kitti_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)

        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,  # Data augmentation
            'gamma': False,  # Data augmentation
            'crop': None,  # Data augmentation
            'jitter': False
        }  # Data augmentation
Beispiel #21
0
    def _load_coco_annotation(self, index):
        """
        Loads COCO bounding-box instance annotations. Crowd instances are
        handled by marking their overlaps (with all categories) to -1. This
        overlap value means that crowd "instances" are excluded from training.
        """
        im_ann = self._COCO.loadImgs(index)[0]
        im_path = self.image_path_from_index(index)
        width = im_ann['width']
        height = im_ann['height']

        # Get the useful information
        # reference = self.reference_image[index]
        # save_seq = reference.keys()

        annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)
        objs = self._COCO.loadAnns(annIds)
        # Sanitize bboxes -- some are invalid
        valid_objs = []
        for i, obj in enumerate(objs):
            x1 = np.max((0, obj['bbox'][0]))
            y1 = np.max((0, obj['bbox'][1]))
            x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)

                # if i in save_seq:
                entry = {'boxes': obj['clean_bbox'], 'image_path': im_path}

                self.cat_data[self.coco_cat_id_to_class_ind[
                    obj['category_id']]].append(entry)

        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        for ix, obj in enumerate(objs):
            cls = self.coco_cat_id_to_class_ind[obj['category_id']]
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            seg_areas[ix] = obj['area']
            if obj['iscrowd']:
                # Set overlap to -1 for all classes for crowd objects
                # so they will be excluded during training
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas
        }
Beispiel #22
0
    def _load_hico_annotation(self, index):
        """
    Loads COCO bounding-box instance annotations. Crowd instances are
    handled by marking their overlaps (with all categories) to -1. This
    overlap value means that crowd "instances" are excluded from training.
    """
        #im_ann = self._COCO.loadImgs(index)[0]#----!!!
        #print(f'label:{index}' )
        im_ann = self._HICO[index]  #----!!!! (0~N-1)#---!?!?!?

        width = im_ann['width']
        height = im_ann['height']

        #annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)#----!!!
        #objs = self._COCO.loadAnns(annIds)#----!!!
        objs = im_ann['ann']  #----!!!

        # Sanitize bboxes -- some are invalid
        valid_objs = []
        clean_bbox = []
        clean_label = []
        for ix in range(len(objs['bboxes'])):  #----!!!
            #for obj in objs:
            x1 = np.max((0, objs['bboxes'][ix][0]))
            y1 = np.max((0, objs['bboxes'][ix][1]))
            x2 = np.min((width - 1, x1 + np.max(
                (0, objs['bboxes'][ix][2] - 1))))
            y2 = np.min((height - 1, y1 + np.max(
                (0, objs['bboxes'][ix][3] - 1))))
            if x2 >= x1 and y2 >= y1 and x2 < width - 1 and y2 < height - 1:
                clean_bbox.append([x1, y1, x2, y2])
                clean_label.append(objs['labels'][ix])
        objs['clean_bbox'] = clean_bbox
        objs['clean_label'] = clean_label

        num_objs = len(objs['clean_bbox'])  #len(objs)#---!!!

        boxes = np.zeros((num_objs, 4), dtype=np.uint16)
        gt_classes = np.zeros((num_objs), dtype=np.int32)
        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)
        seg_areas = np.zeros((num_objs), dtype=np.float32)

        # Lookup table to map from COCO category ids to our internal class
        # indices
        #coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],
        #                                  self._class_to_ind[cls])
        #                                 for cls in self._classes[1:]])

        for ix in range(num_objs):  #----!!!
            #for ix, obj in enumerate(objs):
            #cls = coco_cat_id_to_class_ind[obj['category_id']]#----!!!
            cls = objs['clean_label'][ix]  #---!!!
            boxes[ix, :] = objs['clean_bbox'][ix]  #obj['clean_bbox']#----!!!
            gt_classes[ix] = cls
            #seg_areas[ix] = obj['area']
            #if obj['iscrowd']:
            #  # Set overlap to -1 for all classes for crowd objects
            #  # so they will be excluded during training
            #  overlaps[ix, :] = -1.0
            #else:
            overlaps[ix, cls] = 1.0

        ds_utils.validate_boxes(boxes, width=width, height=height)
        overlaps = scipy.sparse.csr_matrix(overlaps)
        return {
            'width': width,
            'height': height,
            'boxes': boxes,
            'gt_classes': gt_classes,
            'gt_overlaps': overlaps,
            'flipped': False,
            'seg_areas': seg_areas
        }