天天看点

SNIPER/lib/data_utils load_data加载数据

  • def load_gt_roidb(dataset_name, image_set_name, root_path, dataset_path, result_path=None, flip=False):
  • def load_proposal_roidb(dataset_name, image_set_name, root_path, dataset_path, result_path=None, proposal=‘rpn’, append_gt=True, flip=False,proposal_path=‘proposals’, only_gt=False, get_imdb=False, load_mask=False):
imdb = eval(dataset_name)(image_set_name, root_path, dataset_path, result_path,load_mask=load_mask)

    roidb = imdb.gt_roidb()
           

imdb.gt_roidb

imdb.py

def gt_roidb(self):
        raise NotImplementedError
           

在子类中实现 coco.py coco继承自IMDB

class coco(IMDB):
    def __init__(self, image_set, root_path, data_path, result_path=None, mask_size=-1, binary_thresh=None, load_mask=False):
           

gt_roidb作用:返回值 gt_roidb,存储三个文件_gt_roidb.pkl, _index_roidb.pkl, _sindex_roidb.pkl, 返回值gt_roidb通过遍历image_set_index中的index,调用self._load_coco_annotation(index),也就是说, roidb其实就是每个图片的coco标注的集合,每个集合元素的形式如下

SNIPER/lib/data_utils load_data加载数据

gt_roidb实现代码

def gt_roidb(self):
        cache_file = os.path.join(self.cache_path, self.name + '_gt_roidb.pkl')
        index_file = os.path.join(self.cache_path, self.name + '_index_roidb.pkl')
        sindex_file = os.path.join(self.cache_path, self.name + '_sindex_roidb.pkl')
        if os.path.exists(cache_file) and os.path.exists(index_file):
            with open(cache_file, 'rb') as fid:
                roidb = cPickle.load(fid)
            with open(index_file, 'rb') as fid:
                self.image_set_index = cPickle.load(fid)
            print '{} gt roidb loaded from {}'.format(self.name, cache_file)
            return roidb

        gt_roidb = []
        valid_id = []
        vids = []
        ct = 0
        for index in self.image_set_index:
            roientry,flag = self._load_coco_annotation(index)
            if flag:
                gt_roidb.append(roientry)
                valid_id.append(index)
                vids.append(ct)
            ct = ct + 1
        self.image_set_index = valid_id

        with open(cache_file, 'wb') as fid:
            cPickle.dump(gt_roidb, fid, cPickle.HIGHEST_PROTOCOL)
        with open(index_file, 'wb') as fid:
            cPickle.dump(valid_id, fid, cPickle.HIGHEST_PROTOCOL)
        with open(sindex_file, 'wb') as fid:
            cPickle.dump(vids, fid, cPickle.HIGHEST_PROTOCOL)

        print 'wrote gt roidb to {}'.format(cache_file)
        return gt_roidb
           

coco.py

def _load_coco_annotation(self, index):
        def _polys2boxes(polys):
            boxes_from_polys = np.zeros((len(polys), 4), dtype=np.float32)
            for i in range(len(polys)):
                poly = polys[i]
                x0 = min(min(p[::2]) for p in poly)
                x1 = max(max(p[::2]) for p in poly)
                y0 = min(min(p[1::2]) for p in poly)
                y1 = max(max(p[1::2]) for p in poly)
                boxes_from_polys[i, :] = [x0, y0, x1, y1]
            return boxes_from_polys
        """
        coco ann: [u'segmentation', u'area', u'iscrowd', u'image_id', u'bbox', u'category_id', u'id']
        iscrowd:
            crowd instances are handled by marking their overlaps with all categories to -1
            and later excluded in training
        bbox:
            [x1, y1, w, h]
        :param index: coco image id
        :return: roidb entry
        """
        im_ann = self.coco.loadImgs(index)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = self.coco.getAnnIds(imgIds=index, iscrowd=False)
        objs = self.coco.loadAnns(annIds)

        annIds = self.coco.getAnnIds(imgIds=index, iscrowd=True)
        objsc = self.coco.loadAnns(annIds)

        # sanitize bboxes
        valid_objs = []
        for obj in objs:
            x, y, w, h = obj['bbox']
            x1 = np.max((0, x))
            y1 = np.max((0, y))
            #yujie
            #x2 = np.min((width - 1, x1 + np.max((0, w - 1))))
            #y2 = np.min((height - 1, y1 + np.max((0, h - 1))))
            x2 = np.min((width, x1 + np.max((0, w))))
            y2 = np.min((height, y1 + np.max((0, h))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)

        valid_objsc = []
        for obj in objsc:
            x, y, w, h = obj['bbox']
            x1 = np.max((0, x))
            y1 = np.max((0, y))
            #yujie
            x2 = np.min((width, x1 + np.max((0, w))))
            y2 = np.min((height, y1 + np.max((0, h))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objsc.append(obj)

        objs = valid_objs
        objc = valid_objsc
        num_objs = len(objs)
        num_objsc = len(objsc)

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

        #for ix, obj in enumerate(objsc):
        #    boxesc[ix, :] = obj['clean_bbox']

        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
            if obj['iscrowd']:
                overlaps[ix, :] = -1.0
            else:
                overlaps[ix, cls] = 1.0

        ws = boxes[:, 2] - boxes[:, 0]
        hs = boxes[:, 3] - boxes[:, 1]

        flag = True

        roi_rec = {'image': self.image_path_from_index(index),
                   'height': height,
                   'width': width,
                   'boxes': boxes,
                   'boxesc': boxesc,
                   'gt_classes': gt_classes,
                   'gt_overlaps': overlaps,
                   'max_classes': overlaps.argmax(axis=1), #[2]
                   'max_overlaps': overlaps.max(axis=1),
                   'flipped': False}
        #print '>>>>>> roi_rec'
        #print roi_rec
        if self.load_mask:
            # we only care about valid polygons
            print '>>>>>>> load mask'
            segs = []
            for obj in objs:
                if not isinstance(obj['segmentation'], list):
                    # This is a crowd box
                    segs.append([])
                else:
                    segs.append([np.array(p) for p in obj['segmentation'] if len(p)>=6])
            
            roi_rec['gt_masks'] =  segs

            # Uncomment if you need to compute gts based on segmentation masks
            # seg_boxes = _polys2boxes(segs)
            # roi_rec['mask_boxes'] = seg_boxes
        return roi_rec, flag
           
  • def merge_roidb(roidbs):
  • add_chip_data(raid, chip_meta_data_path=’’):
  • remove_small_boxes(roidb, max_scale=3, mn_size=10):
  • filter_roidb(roidb, config):
  • load_gt_segdb(dataset_name, image_set_name, root_path, dataset_path, result_path=None, flip=False):
  • def merge_segdb(segdbs):

参考博文

[1]python eval

[2]argmax

继续阅读