Пример #1
0
    def forward(self, is_train, req, in_data, out_data, aux):
        assert self._batch_rois == -1 or self._batch_rois % self._batch_images == 0, \
            'batchimages {} must devide batch_rois {}'.format(self._batch_images, self._batch_rois)
        all_rois = in_data[0].asnumpy()
        gt_boxes = in_data[1].asnumpy()

        if self._batch_rois == -1:
            rois_per_image = all_rois.shape[0] + gt_boxes.shape[0]
            fg_rois_per_image = rois_per_image
        else:
            rois_per_image = self._batch_rois / self._batch_images
            fg_rois_per_image = np.round(self._fg_fraction * rois_per_image).astype(int)


        # Include ground-truth boxes in the set of candidate rois
        zeros = np.zeros((gt_boxes.shape[0], 1), dtype=gt_boxes.dtype)

        all_rois = np.vstack((all_rois, np.hstack((zeros, gt_boxes[:, :-1]))))
        # Sanity check: single batch only
        assert np.all(all_rois[:, 0] == 0), 'Only single item batches are supported'

        rois, labels, bbox_targets, bbox_weights = \
            sample_rois(all_rois, fg_rois_per_image, rois_per_image, self._num_classes, self._cfg, gt_boxes=gt_boxes, range_lower=self._range_lower, range_upper=self._range_upper)

        for ind, val in enumerate([rois, labels, bbox_targets, bbox_weights]):
            self.assign(out_data[ind], req[ind], val)
Пример #2
0
    def forward(self, is_train, req, in_data, out_data, aux):
        all_rois = in_data[0].asnumpy()
        gt_boxes = in_data[1].asnumpy()

        rois_per_image = int(self._batch_rois / self._batch_images)
        fg_rois_per_image =np.round(self._fg_fraction * rois_per_image).astype(int)
        zeros = np.zeros((gt_boxes.shape[0],1),dtype = gt_boxes.dtype)
        all_rois = np.vstack((all_rois,np.hstack((zeros,gt_boxes[:,:-1]))))
        #if DEBUG:
            #print(self._cfg.TRAIN.BATCH_ROIS)
        rois, labels ,bbox_targets, bbox_weights = \
            sample_rois(all_rois, fg_rois_per_image,rois_per_image,self._num_classes,self._cfg, gt_boxes = gt_boxes)

        if DEBUG:
            print("labels=", labels)
            print('num fg: {}'.format((labels > 0).sum()))
            print('num bg: {}'.format((labels == 0).sum()))
            self._count += 1
            self._fg_num += (labels > 0).sum()
            self._bg_num += (labels == 0).sum()
            print("self._count=", self._count)
            print('num fg avg: {}'.format(self._fg_num / self._count))
            print('num bg avg: {}'.format(self._bg_num / self._count))
            print('ratio: {:.3f}'.format(float(self._fg_num) / float(self._bg_num)))

        for ind, val in enumerate([rois,labels,bbox_targets,bbox_weights]):
            self.assign(out_data[ind],req[ind],val)
Пример #3
0
    def forward(self, is_train, req, in_data, out_data, aux):
        assert self._batch_rois == -1 or self._batch_rois % self._batch_images == 0, \
            'batchimages {} must devide batch_rois {}'.format(self._batch_images, self._batch_rois)
        all_rois = in_data[0].asnumpy()
        gt_boxes = in_data[1].asnumpy()

        if DEBUG:
            check_rois(all_rois, gt_boxes)

        if self._batch_rois == -1:
            rois_per_image = all_rois.shape[0] + gt_boxes.shape[0]
            fg_rois_per_image = rois_per_image
        else:
            rois_per_image = self._batch_rois / self._batch_images
            fg_rois_per_image = np.round(self._fg_fraction *
                                         rois_per_image).astype(int)

        # Include ground-truth boxes in the set of candidate rois
        zeros = np.zeros((gt_boxes.shape[0], 1), dtype=gt_boxes.dtype)

        if DEBUG:
            print('all_rois_shape: {}'.format(all_rois.shape))
            print('zeros_shape: {}'.format(zeros.shape))
            print('gt_boxes_shape: {}'.format(gt_boxes.shape))

        all_rois = np.vstack((all_rois, np.hstack((zeros, gt_boxes[:, :-1]))))
        # Sanity check: single batch only
        assert np.all(
            all_rois[:, 0] == 0), 'Only single item batches are supported'

        assert self._stage >= 1 and self._stage <= 3

        if self._stage == 1:
            rois, labels, bbox_targets, bbox_weights = \
                sample_rois(all_rois, fg_rois_per_image, rois_per_image, self._num_classes, self._cfg, gt_boxes=gt_boxes)
        elif self._stage == 2:
            rois, labels, bbox_targets, bbox_weights = \
                sample_rois_stage2(all_rois, fg_rois_per_image, rois_per_image, self._num_classes, self._cfg,
                            gt_boxes=gt_boxes)
        elif self._stage == 3:
            rois, labels, bbox_targets, bbox_weights = \
                sample_rois_stage3(all_rois, fg_rois_per_image, rois_per_image, self._num_classes, self._cfg,
                                   gt_boxes=gt_boxes)

        if DEBUG:
            print "labels=", labels
            print 'num fg: {}'.format((labels > 0).sum())
            print 'num bg: {}'.format((labels == 0).sum())
            self._count += 1
            self._fg_num += (labels > 0).sum()
            self._bg_num += (labels == 0).sum()
            print "self._count=", self._count
            print 'num fg avg: {}'.format(self._fg_num / self._count)
            print 'num bg avg: {}'.format(self._bg_num / self._count)
            print 'ratio: {:.3f}'.format(
                float(self._fg_num) / float(self._bg_num))

        for ind, val in enumerate([rois, labels, bbox_targets, bbox_weights]):
            self.assign(out_data[ind], req[ind], val)
    def forward(self, is_train, req, in_data, out_data, aux):
        all_rois = in_data[0].asnumpy()
        all_boxes = in_data[1].asnumpy()
        all_kps = None
        if config.network.PREDICT_KEYPOINTS:
            all_kps = in_data[2].asnumpy()

        all_targets = dict()
        num_images = all_boxes.shape[0]
        for i in range(num_images):
            valid_inds = np.where(all_boxes[i, :, 4] > 0)[0]
            gt_boxes = all_boxes[i, valid_inds]
            gt_kps = all_kps[i, valid_inds] if all_kps is not None else None
            roi_inds = np.where(all_rois[:, 0] == i)[0]
            if self._batch_rois == -1:
                rois_per_image = roi_inds.shape[0]  # keep same number of rois
                fg_rois_per_image = rois_per_image
            else:
                assert self._batch_rois % self._batch_images == 0, \
                        'batchimages {} must devide batch_rois {}'.format(self._batch_images, self._batch_rois)

                rois_per_image = self._batch_rois / num_images
                fg_rois_per_image = np.round(self._fg_fraction *
                                             rois_per_image).astype(int)

            # Include ground-truth boxes in the set of candidate rois
            gt_rois = np.zeros((gt_boxes.shape[0], 5), dtype=gt_boxes.dtype)
            gt_rois[:, 0] = i
            gt_rois[:, 1:5] = gt_boxes[:, :4]
            rois = np.vstack((all_rois[roi_inds], gt_rois))
            targets = sample_rois(rois,
                                  fg_rois_per_image,
                                  rois_per_image,
                                  self._num_classes,
                                  gt_boxes=gt_boxes,
                                  gt_kps=gt_kps)
            DEBUG = False  ###
            if DEBUG:
                labels = targets['label']
                print 'gt: {}\tfg: {}\tbg: {}\tignore: {}'.format(
                    valid_inds.shape[0], (labels > 0).sum(),
                    (labels == 0).sum(), (labels == -1).sum())

            for ind, k in enumerate(outputs):
                if k not in all_targets:
                    all_targets[k] = []
                all_targets[k].append(targets[k])

        for ind, k in enumerate(outputs):
            target = np.concatenate(all_targets[k], axis=0)
            self.assign(out_data[ind], req[ind], target)
    def forward(self, is_train, req, in_data, out_data, aux):
        assert self._batch_rois == -1 or self._batch_rois % self._batch_images == 0, \
            'batchimages {} must devide batch_rois {}'.format(self._batch_images, self._batch_rois)
        all_rois = in_data[0].asnumpy()
        gt_boxes = in_data[1].asnumpy()
        all_kps = None
        if self._cfg.network.PREDICT_KEYPOINTS:
            all_kps = in_data[2].asnumpy()

        if self._batch_rois == -1:
            rois_per_image = all_rois.shape[0] + gt_boxes.shape[0]
            fg_rois_per_image = rois_per_image
        else:
            rois_per_image = self._batch_rois / self._batch_images
            fg_rois_per_image = np.round(self._fg_fraction *
                                         rois_per_image).astype(int)

        # Include ground-truth boxes in the set of candidate rois
        zeros = np.zeros((gt_boxes.shape[0], 1), dtype=gt_boxes.dtype)
        all_rois = np.vstack((all_rois, np.hstack((zeros, gt_boxes[:, :-1]))))
        # Sanity check: single batch only
        assert np.all(
            all_rois[:, 0] == 0), 'Only single item batches are supported'

        sample_res = \
            sample_rois(all_rois, fg_rois_per_image, rois_per_image, self._num_classes, self._cfg, gt_boxes=gt_boxes, gt_kps=all_kps)

        if DEBUG:
            label = sample_res[1]
            print "labels=", labels
            print 'num fg: {}'.format((labels > 0).sum())
            print 'num bg: {}'.format((labels == 0).sum())
            self._count += 1
            self._fg_num += (labels > 0).sum()
            self._bg_num += (labels == 0).sum()
            print "self._count=", self._count
            print 'num fg avg: {}'.format(self._fg_num / self._count)
            print 'num bg avg: {}'.format(self._bg_num / self._count)
            print 'ratio: {:.3f}'.format(
                float(self._fg_num) / float(self._bg_num))

        for ind, val in enumerate(sample_res):
            self.assign(out_data[ind], req[ind], val)
    def forward(self, is_train, req, in_data, out_data, aux):
        assert self._batch_rois == -1 or self._batch_rois % self._batch_images == 0, \
            'batchimages {} must devide batch_rois {}'.format(self._batch_images, self._batch_rois)
        all_rois = in_data[0].asnumpy()
        gt_boxes = in_data[1].asnumpy()

        if self._batch_rois == -1:
            rois_per_image = all_rois.shape[0] + gt_boxes.shape[0]
            fg_rois_per_image = rois_per_image
        else:
            rois_per_image = self._batch_rois / self._batch_images
            fg_rois_per_image = np.round(self._fg_fraction * rois_per_image).astype(int)


        # Include ground-truth boxes in the set of candidate rois
        zeros = np.zeros((gt_boxes.shape[0], 1), dtype=gt_boxes.dtype)
        all_rois = np.vstack((all_rois, np.hstack((zeros, gt_boxes[:, :-1]))))
        # Sanity check: single batch only
        assert np.all(all_rois[:, 0] == 0), 'Only single item batches are supported'

        rois, labels, bbox_targets, bbox_weights = \
            sample_rois(all_rois, fg_rois_per_image, rois_per_image, self._num_classes, self._cfg, gt_boxes=gt_boxes)

        if DEBUG:
            print "labels=", labels
            print 'num fg: {}'.format((labels > 0).sum())
            print 'num bg: {}'.format((labels == 0).sum())
            self._count += 1
            self._fg_num += (labels > 0).sum()
            self._bg_num += (labels == 0).sum()
            print "self._count=", self._count
            print 'num fg avg: {}'.format(self._fg_num / self._count)
            print 'num bg avg: {}'.format(self._bg_num / self._count)
            print 'ratio: {:.3f}'.format(float(self._fg_num) / float(self._bg_num))

        for ind, val in enumerate([rois, labels, bbox_targets, bbox_weights]):
            self.assign(out_data[ind], req[ind], val)
Пример #7
0
    def forward(self, is_train, req, in_data, out_data, aux):
        assert self._batch_rois == -1 or self._batch_rois % self._batch_images == 0, \
            'batchimages {} must devide batch_rois {}'.format(self._batch_images, self._batch_rois)
        all_rois = in_data[0].asnumpy()
        gt_boxes = in_data[1].asnumpy()
        im = in_data[2].asnumpy()

        if self._batch_rois == -1:
            rois_per_image = all_rois.shape[0] + gt_boxes.shape[0]
            fg_rois_per_image = rois_per_image
        else:
            rois_per_image = self._batch_rois / self._batch_images
            fg_rois_per_image = np.round(self._fg_fraction *
                                         rois_per_image).astype(int)

        # Include ground-truth boxes in the set of candidate rois
        zeros = np.zeros((gt_boxes.shape[0], 1), dtype=gt_boxes.dtype)
        all_rois = np.vstack((all_rois, np.hstack((zeros, gt_boxes[:, :-1]))))
        # Sanity check: single batch only
        assert np.all(
            all_rois[:, 0] == 0), 'Only single item batches are supported'

        rois, labels, bbox_targets, bbox_weights ,layer_indexs= \
            sample_rois(all_rois, fg_rois_per_image, rois_per_image, self._num_classes, self._cfg, gt_boxes=gt_boxes,sample_type='fpn', k0 = 4)

        vis = False
        if vis:
            ind = np.where(labels != 0)[0]
            im_shape = im.shape
            pred_boxes = bbox_pred(rois[:, 1:], bbox_targets)
            pred_boxes = clip_boxes(pred_boxes, im_shape[-2:])
            l = labels[ind]
            ro = rois[ind, 1:]
            b = bbox_targets[ind, :]
            p = pred_boxes[ind, :] * bbox_weights[ind, :]
            r = []
            for i in range(p.shape[0]):
                r.append(p[i, l[i] * 4:l[i] * 4 + 4])
            r_ = np.vstack(r)

            vis_all_detection(im, r_, l, 1)
        rois_all = np.zeros((self._batch_rois * 4, 5), dtype=rois.dtype)
        labels_all = np.ones((self._batch_rois * 4, ), dtype=labels.dtype) * -1
        bbox_targets_all = np.zeros(
            (self._batch_rois * 4, self._num_classes * 4),
            dtype=bbox_targets.dtype)
        bbox_weights_all = np.zeros(
            (self._batch_rois * 4, self._num_classes * 4),
            dtype=bbox_weights.dtype)
        for i in range(4):
            index = (layer_indexs == (i + 2))

            num_index = sum(index)
            start = self._batch_rois * i
            end = start + num_index
            index_range = range(start, end)
            rois_all[index_range, :] = rois[index, :]
            labels_all[index_range] = labels[index]
            bbox_targets_all[index_range, :] = bbox_targets[index, :]
            bbox_weights_all[index_range, :] = bbox_weights[index, :]

        if DEBUG:
            print "labels=", labels
            print 'num fg: {}'.format((labels > 0).sum())
            print 'num bg: {}'.format((labels == 0).sum())
            self._count += 1
            self._fg_num += (labels > 0).sum()
            self._bg_num += (labels == 0).sum()
            print "self._count=", self._count
            print 'num fg avg: {}'.format(self._fg_num / self._count)
            print 'num bg avg: {}'.format(self._bg_num / self._count)
            print 'ratio: {:.3f}'.format(
                float(self._fg_num) / float(self._bg_num))
        for ind, val in enumerate(
            [rois_all, labels_all, bbox_targets_all, bbox_weights_all]):
            self.assign(out_data[ind], req[0], val)
Пример #8
0
def get_rcnn_batch(roidb, cfg):
    """
    return a dict of multiple images
    :param roidb: a list of dict, whose length controls batch size
    ['images', 'flipped'] + ['gt_boxes', 'boxes', 'gt_overlap'] => ['bbox_targets']
    :return: data, label
    """
    num_images = len(roidb)
    imgs, roidb = get_image(roidb, cfg)
    im_array = tensor_vstack(imgs)

    assert cfg.TRAIN.BATCH_ROIS == -1 or cfg.TRAIN.BATCH_ROIS % cfg.TRAIN.BATCH_IMAGES == 0, \
        'BATCHIMAGES {} must divide BATCH_ROIS {}'.format(cfg.TRAIN.BATCH_IMAGES, cfg.TRAIN.BATCH_ROIS)

    if cfg.TRAIN.BATCH_ROIS == -1:
        rois_per_image = np.sum([iroidb['boxes'].shape[0] for iroidb in roidb])
        fg_rois_per_image = rois_per_image
    else:
        rois_per_image = cfg.TRAIN.BATCH_ROIS / cfg.TRAIN.BATCH_IMAGES
        fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION *
                                     rois_per_image).astype(int)

    rois_array = list()
    labels_array = list()
    bbox_targets_array = list()
    bbox_weights_array = list()

    for im_i in range(num_images):
        roi_rec = roidb[im_i]

        # infer num_classes from gt_overlaps
        num_classes = roi_rec['gt_overlaps'].shape[1]

        # label = class RoI has max overlap with
        rois = roi_rec['boxes']
        labels = roi_rec['max_classes']
        overlaps = roi_rec['max_overlaps']
        bbox_targets = roi_rec['bbox_targets']

        im_rois, labels, bbox_targets, bbox_weights = \
            sample_rois(rois, fg_rois_per_image, rois_per_image, num_classes, cfg,
                        labels, overlaps, bbox_targets)

        # project im_rois
        # do not round roi
        rois = im_rois
        batch_index = im_i * np.ones((rois.shape[0], 1))
        rois_array_this_image = np.hstack((batch_index, rois))
        rois_array.append(rois_array_this_image)

        # add labels
        labels_array.append(labels)
        bbox_targets_array.append(bbox_targets)
        bbox_weights_array.append(bbox_weights)

    rois_array = np.array(rois_array)
    labels_array = np.array(labels_array)
    bbox_targets_array = np.array(bbox_targets_array)
    bbox_weights_array = np.array(bbox_weights_array)

    data = {'data': im_array, 'rois': rois_array}
    label = {
        'label': labels_array,
        'bbox_target': bbox_targets_array,
        'bbox_weight': bbox_weights_array
    }

    return data, label