Esempio n. 1
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        self._proj_name = layer_params['proj_name']  # loki
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(proj_name=self._proj_name,
                                         scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print('feat_stride: {}'.format(
                self._feat_stride))  # python3 # print
            print('anchors:')  # python3 # print
            print(self._anchors)  # python3 # print

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)
        ## generate anchors the same way as we do in anchor target layer
        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        ## Add scale option
        self._scale = np.array(layer_params.get('scale_param', False),
                               np.float32)
        ## Add global context option
        self._global_context = np.array(
            layer_params.get('global_context', False), np.float32)

        assert self._scale == 0.0 or self._global_context == 0.0, 'Only one of them can be True'

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        ## If scale_param is True, we need to add top output
        if self._scale != 0.0 or self._global_context != 0.0:
            top[1].reshape(1, 5)
Esempio n. 3
0
    def setup(self, bottom, top):
        """
        bottom[0]: rpn score map
        bottom[1]: det score map
        bottom[2]: bbox_pred_delta    
        top[0]: ROIs
        top[1]: scores
        """
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str)
        params = layer_params['stride_scale_numcls']

        feat_stride_str, scale_str, cls_str = params.split(',')
        self._feat_stride = int(feat_stride_str)
        anchor_scale = int(scale_str)
        self._numclasses = int(cls_str)

        self._anchors = generate_anchors(base_size=cfg.MINANCHOR,
                                         scales=np.array(
                                             [anchor_scale, anchor_scale + 1]),
                                         ratios=[0.333, 0.5, 1, 2, 3])
        self._num_anchors = self._anchors.shape[0]

        # 1. Generate proposals from bbox deltas and shifted anchors

        self._ndim = cfg.TEST.BATCH_SIZE

        top[0].reshape(self._ndim, 1, 4)
        top[1].reshape(self._ndim, 1, self._numclasses)
Esempio n. 4
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))

        # added by yzh for anchor ratio setting
        ratios = layer_params.get('ratios', [0.5, 1, 2])
        self._anchors = generate_anchors(scales=np.array(anchor_scales), ratios=ratios)

        self._num_anchors = self._anchors.shape[0]
        if WSX:
            print '==== _anchors ==='
            print self._anchors
            #with open('/home/yzh/tmp/anchors.pkl','wb') as output:
            #    pickle.dump(self._anchors,output, pickle.HIGHEST_PROTOCOL)
            print self._num_anchors
            print '================'

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 5
0
    def __init__(self,
                 classifier,
                 anchor_scales=None,
                 negative_overlap=0.3,
                 positive_overlap=0.7,
                 fg_fraction=0.5,
                 batch_size=256,
                 nms_thresh=0.7,
                 min_size=16,
                 pre_nms_topN=12000,
                 post_nms_topN=2000):
        super(RPN, self).__init__()

        self.rpn_classifier = classifier

        if anchor_scales is None:
            anchor_scales = (8, 16, 32)
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        self.negative_overlap = negative_overlap
        self.positive_overlap = positive_overlap
        self.fg_fraction = fg_fraction
        self.batch_size = batch_size

        # used for both train and test
        self.nms_thresh = nms_thresh
        self.pre_nms_topN = pre_nms_topN
        self.post_nms_topN = post_nms_topN
        self.min_size = min_size
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        # layer_params = yaml.load(self.param_str_)
        # print dir(self)
        # param_str_ is wrong.  In python it's param_str.  C++ uses the extra _.
        layer_params = yaml.load(self.param_str)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        anchor_ratios = layer_params.get('ratios', ((0.5, 1, 2)))
        self._anchors = generate_anchors(ratios=anchor_ratios,
                                         scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print "anchor scales are", anchor_scales
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        # top[0].reshape(1, 5)
        top[0].reshape(300, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 7
0
    def setup(self, bottom, top):
        layer_params = yaml.load(self.param_str)
        #anchor_scales = [layer_params['scale']]

        params = layer_params['stride_scale_border_batchsize']

        feat_stride_str, scale_str, border_str, batch_str = params.split(',')
        self._feat_stride = float(feat_stride_str)
        self._scales = float(scale_str)
        self._allowed_border = float(border_str)
        self._batch = float(batch_str)
        self._anchors = generate_anchors(base_size=cfg.MINANCHOR,
                                         scales=np.array(
                                             [self._scales, self._scales + 1]),
                                         ratios=[0.333, 0.5, 1, 2, 3])
        self._num_anchors = self._anchors.shape[0]

        height, width = bottom[0].data.shape[-2:]
        if DEBUG:
            print 'AnchorTargetLayer: height', height, 'width', width

        self._ndim = cfg.TRAIN.IMS_PER_BATCH

        # labels
        top[0].reshape(self._ndim, 1, self._num_anchors * height, width)
        # bbox_targets
        top[1].reshape(self._ndim, self._num_anchors * 4, height, width)
        # bbox_inside_weights
        top[2].reshape(self._ndim, self._num_anchors * 4, height, width)
        # bbox_outside_weights
        top[3].reshape(self._ndim, self._num_anchors * 4, height, width)
Esempio n. 8
0
    def setup(self, bottom, top):
        """
        bottom[0]: rpn score map
        bottom[1]: det score map
        bottom[2]: bbox_pred_delta    
        top[0]: ROIs
        top[1]: scores
        """
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str)
        params = layer_params['stride_scale_numcls']

        feat_stride_str, scale_str, cls_str = params.split(',')
        self._feat_stride = int(feat_stride_str)
        anchor_scale = int(scale_str)
        self._numclasses = int(cls_str)

        # self._anchors = generate_anchors(base_size=self._feat_stride, scales=np.array([anchor_scale, anchor_scale * cfg.ANCHOR_SCALE_OFFSET]),
        #                                  ratios=cfg.GENERATION_ANCHOR_RATIOS)

        self._anchors = generate_anchors(
            base_size=self._feat_stride,
            scales=np.array(cfg.TRAIN.MULTI_SCALE_RPN_SCALE[anchor_scale]),
            ratios=cfg.GENERATION_ANCHOR_RATIOS)

        self._num_anchors = self._anchors.shape[0]

        # 1. Generate proposals from bbox deltas and shifted anchors

        self._ndim = cfg.TEST.BATCH_SIZE

        top[0].reshape(self._ndim, 1, 4)
        top[1].reshape(self._ndim, 1, 1)
Esempio n. 9
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        base_size = cfg.TRAIN.BASE_SIZE
        anchor_scales = np.array(cfg.TRAIN.ANCHOR_SCALES)
        anchor_ratios = np.array(cfg.TRAIN.ANCHOR_RATIOS)
        self._anchors = generate_anchors(base_size=base_size,
                                         ratios=anchor_ratios,
                                         scales=anchor_scales)
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'.format(self._anchors)

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
    def setup(self, bottom, top):
        self._anchors = generate_anchors()
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'anchors:'
            print self._anchors
            self._count = 0
            self._fg_num = 0
            self._bg_num = 0

        #layer_params = yaml.load(self.param_str_)
        layer_params = yaml.load(self.param_str)
        self._num_classes = layer_params['num_classes']

        # sampled rois (0, x1, y1, x2, y2)
        top[0].reshape(1, 5)
        # labels
        top[1].reshape(1, 1)
        # bbox_targets
        top[2].reshape(1, self._num_classes * 4)
        # bbox_inside_weights
        top[3].reshape(1, self._num_classes * 4)
        # bbox_outside_weights
        top[4].reshape(1, self._num_classes * 4)
Esempio n. 11
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        #from IPython import embed; embed()
        #anchor_scales = layer_params.get('scales', (8, 16, 32))
        anchor_ratios = layer_params.get('ratios', cfg.ANCHOR_RATIOS)
        anchor_scales = layer_params.get('scales', cfg.ANCHOR_SCALE)
        anchor_base_size = cfg.ANCHOR_BASE_SIZE
        #self._anchors = generate_anchors(base_size=anchor_base_size,)\
        #                                 scales=np.array(anchor_scales))
        self._anchors = generate_anchors(base_size=anchor_base_size,ratios=np.array(anchor_ratios),\
                                         scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
    def setup(self, bottom, top):
        anchor_scales = (8, 16, 32)
        if 'scales' in self.pythonargs:
            anchor_scales = self.pythonargs['scales']
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        self._feat_stride = self.pythonargs['feat_stride']

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = 0
        if 'allowed_border' in self.pythonargs:
            self._allowed_border = self.pythonargs['allowed_border']

        height, width = bottom[0].data.shape[-2:]

        A = self._num_anchors
        # labels
        top[0].reshape((1, 1, A * height, width))
        # bbox_targets
        top[1].reshape((1, A * 4, height, width))
        # bbox_inside_weights
        top[2].reshape((1, A * 4, height, width))
        # bbox_outside_weights
        top[3].reshape((1, A * 4, height, width))
Esempio n. 13
0
    def setup(self, bottom, top):
        layer_params = yaml.load(self.param_str)
        params = layer_params['stride_scale_border_batchsize_numcls']

        feat_stride_str, scale_str, border_str, batch_str, num_cls = params.split(
            ',')
        self._feat_stride = float(feat_stride_str)
        self._scales = float(scale_str)
        self._num_classes = int(num_cls)
        self._score_thresh = cfg.TRAIN.PROB
        self._allowed_border = float(border_str)
        self._batch = float(batch_str)

        #self._anchors = generate_anchors(base_size = cfg.MINANCHOR, scales=np.array([self._scales, self._scales + 1]), ratios = [0.333, 0.5, 1, 2, 3])
        self._anchors = generate_anchors(
            base_size=self._feat_stride,
            scales=np.array(
                [self._scales, self._scales * cfg.ANCHOR_SCALE_OFFSET]),
            ratios=cfg.GENERATION_ANCHOR_RATIOS)

        self._num_anchors = self._anchors.shape[0]

        height, width = bottom[0].data.shape[-2:]
        self._ndim = cfg.TRAIN.IMS_PER_BATCH

        # labels
        top[0].reshape(self._ndim, 1, self._num_anchors * height, width)
Esempio n. 14
0
    def __init__(self, feat_stride, scales, ratios):
        super(_ProposalLayer, self).__init__()

        self._feat_stride = feat_stride
        self._anchors = torch.from_numpy(generate_anchors(scales=np.array(scales), 
            ratios=np.array(ratios))).float()
        self._num_anchors = self._anchors.size(0)
Esempio n. 15
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        anchor_ratios = layer_params.get('ratios', ((0.5, 1, 2)))
        _base_size = layer_params.get('base_size', 16)
        self._anchors = generate_anchors(base_size=_base_size,
                                         ratios=anchor_ratios,
                                         scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 16
0
def generate_anchors_pre(height,
                         width,
                         feat_stride,
                         anchor_scales=(8, 16, 32),
                         anchor_ratios=(0.5, 1, 2),
                         base_size=16):
    """ A wrapper function to generate anchors given different scales
      Also return the number of anchors in variable 'length'
    """
    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales),
                               base_size=base_size)
    A = anchors.shape[0]
    shift_x = np.arange(0, width) * feat_stride
    shift_y = np.arange(0, height) * feat_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(),
                        shift_y.ravel())).transpose()
    K = shifts.shape[0]
    # width changes faster, so here it is H, W, C
    anchors = anchors.reshape((1, A, 4)) + \
              shifts.reshape((1, K, 4)).transpose((1, 0, 2))
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    length = np.int32(anchors.shape[0])

    return anchors, length
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)
        print 'proposal_layer parameter file is {}'.format(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors()
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors
            print 'number of anchors:'
            print self._num_anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 18
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = json.loads(self.param_str)

        self._feat_stride = layer_params['feat_stride']
        # has extra training targets, so to sample more rois
        self._has_extra = layer_params.get('has_extra', False)
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)
        if self.phase == caffe.TRAIN:
            # rois_labels
            top[1].reshape(1, )
            # rois_gt_assignments
            top[2].reshape(1, )
Esempio n. 19
0
    def setup(self, bottom, top):
        layer_params = yaml.load(self.param_str_)
        self._feat_stride = [
            int(i) for i in layer_params['feat_stride'].split(',')
        ]
        self._scales = cfg.FPNRSCALES
        self._ratios = cfg.FPNRATIOS
        #anchor_scales = layer_params.get('scales', (8, ))
        # allow boxes to sit over the edge by a small amount
        self._allowed_border = layer_params.get('allowed_border', 1000)

        s = 0
        for i in range(5):
            height, width = bottom[i].data.shape[-2:]
            s = s + height * width

            i_anchors = generate_anchors(base_size=self._feat_stride[i],
                                         ratios=self._ratios,
                                         scales=self._scales)
            A = i_anchors.shape[0]

        # labels
        top[0].reshape(1, 1, A * s)
        # bbox_targets
        top[1].reshape(1, A * 4, s)
        # bbox_inside_weights
        top[2].reshape(1, A * 4, s)
        # bbox_outside_weights
        top[3].reshape(1, A * 4, s)
Esempio n. 20
0
    def setup(self, bottom, top):
        layer_params = yaml.load(self.param_str)
        #anchor_scales = [layer_params['scale']]

        params = layer_params['stride_scale_border_batchsize']

        feat_stride_str, scale_str, border_str, batch_str = params.split(',')
        self._feat_stride = float(feat_stride_str)
        self._scales = float(scale_str)       
        self._allowed_border = float(border_str)
        self._batch = float(batch_str)
        self._anchors = generate_anchors(base_size = cfg.MINANCHOR, scales=np.array([self._scales, self._scales + 1]), ratios = [0.333, 0.5, 1, 2, 3])
        self._num_anchors = self._anchors.shape[0]
        
        
        height, width = bottom[0].data.shape[-2:]
        if DEBUG:
            print 'AnchorTargetLayer: height', height, 'width', width

        self._ndim = cfg.TRAIN.IMS_PER_BATCH

        # labels
        top[0].reshape(self._ndim, 1, self._num_anchors * height, width)
        # bbox_targets
        top[1].reshape(self._ndim, self._num_anchors * 4, height, width)
        # bbox_inside_weights
        top[2].reshape(self._ndim, self._num_anchors * 4, height, width)
        # bbox_outside_weights
        top[3].reshape(self._ndim, self._num_anchors * 4, height, width)
Esempio n. 21
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get(
            'scales', (8, 16, 32))  #original hardcode scales as (8,16,32)
        #anchor_scales = layer_params['scales']  # modified: load scales defined from prototxt
        print(
            "=================================anchor_scales in ProposalLayer:============="
            + str(anchor_scales))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 22
0
def anchor_target_layer_ws(rpn_cls_score,
                           gt_boxes,
                           num_gt_boxes,
                           im_info,
                           data,
                           _feat_stride=[
                               16,
                           ],
                           anchor_scales=[4, 8, 16, 32]):
    """
    Assign anchors to ground-truth targets. Produces anchor classification
    labels and bounding-box regression targets.
    """
    """This function is for cases with empty 'gt_boxes' (weakly supervised)"""
    _anchors = generate_anchors(scales=np.array(anchor_scales))
    _num_anchors = _anchors.shape[0]

    height, width = rpn_cls_score.shape[1:3]
    A = _num_anchors
    batch_size = rpn_cls_score.shape[0]

    rpn_labels = np.empty((batch_size, 1, A * height, width), dtype=np.float32)
    rpn_labels.fill(-1)
    rpn_bbox_targets = np.zeros((batch_size, A * 4, height, width),
                                dtype=np.float32)
    rpn_bbox_inside_weights = np.zeros((batch_size, A * 4, height, width),
                                       dtype=np.float32)
    rpn_bbox_outside_weights = np.zeros((batch_size, A * 4, height, width),
                                        dtype=np.float32)

    return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
Esempio n. 23
0
    def setup(self, bottom, gt):
        #layer_params = yaml.load(self.param_str_)
        #anchor_scales = ((0.5, 1, 2), (8, 16, 32))
        self._anchors = generate_anchors()
        #print self._anchors.shape
        self._num_anchors = self._anchors.shape[0]
        self._feat_stride = 16

        if DEBUG:
            print 'anchors:'
            print self._anchors
            print 'anchor shapes:'
            print np.hstack((
                self._anchors[:, 2::4] - self._anchors[:, 0::4],
                self._anchors[:, 3::4] - self._anchors[:, 1::4],
            ))
            self._counts = cfg.EPS
            self._sums = np.zeros((1, 4))
            self._squared_sums = np.zeros((1, 4))
            self._fg_sum = 0
            self._bg_sum = 0
            self._count = 0

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = 0
        #height, width = 34, 62
        #height, width = bottom.shape[-2:]

        if DEBUG:
            print 'AnchorTargetLayer: height', height, 'width', width

        A = self._num_anchors
Esempio n. 24
0
    def __generate_anchor(self, width, height, feat_stride):

        anchors = generate_anchors(ratios=np.array(self._anchor_ratio),
                                   scales=np.array(self._anchor_scale))
        num_anchor = anchors.shape[0]

        shift_x = tf.range(width) * feat_stride[0]
        shift_y = tf.range(height) * feat_stride[0]
        shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
        sx = tf.reshape(shift_x, shape=(-1, ))
        sy = tf.reshape(shift_y, shape=(-1, ))
        shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
        rect = tf.multiply(width, height)
        shifts = tf.transpose(tf.reshape(shifts, shape=[1, rect, 4]),
                              perm=(1, 0, 2))
        anchor_constant = tf.constant(anchors.reshape((1, num_anchor, 4)),
                                      dtype=tf.int32)

        length = num_anchor * rect
        anchors_tf = tf.cast(tf.reshape(tf.add(anchor_constant, shifts),
                                        shape=(length, 4)),
                             dtype=tf.float32)
        anchors_tf.set_shape([None, 4])
        length.set_shape([])
        self._anchors = anchors_tf
Esempio n. 25
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]  #anchor的总个数

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors
        """
         输出Top[0]: R个roi, 每一个均是 5-tuple (n, x1, y1, x2, y2),
                    其中n 代表batch index; x1, y1, x2, y2表示矩形的4个点的坐标。
     输出Top[1]: R个proposal的得分,即是一个物体的可能性。
        """
        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 26
0
    def __init__(self, feat_stride, scales):
        super(_AnchorTargetLayer, self).__init__()

        self._feat_stride = feat_stride
        self._scales = scales
        anchor_scales = scales
        self._anchors = torch.from_numpy(
            generate_anchors(scales=np.array(anchor_scales))).float()
        self._num_anchors = self._anchors.size(0)

        if DEBUG:
            print 'anchors:'
            print self._anchors
            print 'anchor shapes:'
            print np.hstack((
                self._anchors[:, 2::4] - self._anchors[:, 0::4],
                self._anchors[:, 3::4] - self._anchors[:, 1::4],
            ))
            self._counts = cfg.EPS
            self._sums = np.zeros((1, 4))
            self._squared_sums = np.zeros((1, 4))
            self._fg_sum = 0
            self._bg_sum = 0
            self._count = 0

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = 0  # default is 0
    def __init__(self, feat_stride, scales, ratios):
        super(_ProposalLayer, self).__init__()

        self._feat_stride = feat_stride
        self._anchors = torch.from_numpy(generate_anchors(scales=np.array(scales), 
            ratios=np.array(ratios))).float()
        self._num_anchors = self._anchors.size(0)
Esempio n. 28
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        if 'a_size1' in layer_params and 'a_size2' in layer_params and 'a_size3' in layer_params:
            a_size1_scale = int(layer_params['a_size1']) * (8.0 / 128.0)
            a_size2_scale = int(layer_params['a_size2']) * (8.0 / 128.0)
            a_size3_scale = int(layer_params['a_size3']) * (8.0 / 128.0)
            anchor_scales = layer_params.get(
                'scales', (a_size1_scale, a_size2_scale, a_size3_scale))
        else:
            # 8: area 128*128, 16: area 256*256, 32: area 512*512
            anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        # top[0].reshape(1, 5)
        cfg_key = str(self.phase)  # either 'TRAIN' or 'TEST'
        post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N
        top[0].reshape(post_nms_topN, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 30
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str)

        self._feat_stride = layer_params['feat_stride']
        #anchor_scales = layer_params.get('scales', (8, 16, 32))
        #self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._anchors = generate_anchors()
        self._num_anchors = self._anchors.shape[0]

        self._bbox_para_num = 5  # parameter number of bbox

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 6-tuple
        # (n, ctr_x, ctr_y, h, w, theta) specifying an image batch index n and a
        # rectangle (ctr_x, ctr_y, h, w, theta)
        top[0].reshape(1, self._bbox_para_num + 1)  # D

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1, 1)  # D
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        if cfg.CUSTOM_ANCHORS:
            self._anchors = load_custom_anchors()
        else:
            anchor_scales = layer_params.get('scales', (8, 16, 32))
            self._anchors = generate_anchors(scales=np.array(anchor_scales))

        self._num_anchors = self._anchors.shape[0]
        self._feat_stride = layer_params['feat_stride']

        print 'anchors:'
        print self._anchors
        print 'anchor shapes:'
        print np.hstack((
            self._anchors[:, 2::4] - self._anchors[:, 0::4],
            self._anchors[:, 3::4] - self._anchors[:, 1::4],
        ))

        # if DEBUG:
        #     print 'feat_stride: {}'.format(self._feat_stride)
        #     print 'anchors:'
        #     print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 32
0
    def setup(self, bottom, top):
        layer_params = yaml.load(self.param_str_)
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]
        self._feat_stride = layer_params['feat_stride']

        if DEBUG:
            print 'anchors:'
            print self._anchors
            print 'anchor shapes:'
            print np.hstack((
                self._anchors[:, 2::4] - self._anchors[:, 0::4],
                self._anchors[:, 3::4] - self._anchors[:, 1::4],
            ))
            self._counts = cfg.EPS
            self._sums = np.zeros((1, 4))
            self._squared_sums = np.zeros((1, 4))
            self._fg_sum = 0
            self._bg_sum = 0
            self._count = 0

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = layer_params.get('allowed_border', 0)

        height, width = bottom[0].data.shape[-2:]
        if DEBUG:
            print 'AnchorTargetLayer: height', height, 'width', width

        A = self._num_anchors
        # labels
        top[0].reshape(1, 1, A * height, width)
        # bbox_targets
        top[1].reshape(1, A * 4, height, width)
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        p = int(np.log2(self._feat_stride) + 1)
        if p == 5:
            anchor_scales = layer_params.get('scales', (8, 16, 32))
        if p == 4:
            anchor_scales = layer_params.get('scales', (4, 8, 16))
        if p == 3:
            anchor_scales = layer_params.get('scales', (2, 4, 8))
        if p == 2:
            anchor_scales = layer_params.get('scales', (1, 2, 4))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
    def __init__(self, feat_stride, scales, ratios):
        super(_AnchorTargetLayer, self).__init__()

        self._feat_stride = feat_stride
        self._scales = scales
        anchor_scales = scales
        self._anchors = torch.from_numpy(generate_anchors(scales=np.array(anchor_scales), ratios=np.array(ratios))).float()
        self._num_anchors = self._anchors.size(0)

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = 0  # default is 0
Esempio n. 35
0
    def setup(self, bottom, top):
        layer_params = json.loads(self.param_str_)
        self._scales = layer_params['scales']
        self._base_size = layer_params['base_size']
        self._anchors = generate_anchors(base_size=self._base_size,scales=np.array(self._scales))
        self._num_anchors = self._anchors.shape[0]
        self._feat_stride = layer_params['feat_stride']

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = layer_params.get('allowed_border', 0)

        height, width = bottom[0].data.shape[-2:]
        if DEBUG:
            print 'GenerateAnchorLayer: height', height, 'width', width

        A = self._num_anchors
        # bbox_anchors
        top[0].reshape(height * width * A, 4)
Esempio n. 36
0
    def setup(self, bottom, top):
        self._feat_stride = self.pythonargs['feat_stride']

        anchor_scales = (8, 16, 32)
        if 'scales' in self.pythonargs:
            anchor_scales = self.pythonargs['scales']
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape((1, 5))

        self._output_scores = 'output_scores' in self.pythonargs

        # scores blob: holds scores for R regions of interest
        if self._output_scores:
            top[1].reshape((1, 1, 1, 1))
Esempio n. 37
0
    def setup(self, bottom, top):
        layer_params = yaml.load(self.param_str)
        params = layer_params['stride_scale_border_batchsize_numcls']

        feat_stride_str, scale_str, border_str, batch_str, num_cls = params.split(',')
        self._feat_stride = float(feat_stride_str)
        self._scales = float(scale_str)       
        self._num_classes = int(num_cls)
        self._score_thresh = cfg.TRAIN.PROB  
        self._allowed_border = float(border_str)
        self._batch = float(batch_str)
              
        self._anchors = generate_anchors(base_size = cfg.MINANCHOR, scales=np.array([self._scales, self._scales + 1]), ratios = [0.333, 0.5, 1, 2, 3])
        self._num_anchors = self._anchors.shape[0]  
        
        height, width = bottom[0].data.shape[-2:]
        self._ndim = cfg.TRAIN.IMS_PER_BATCH

        # labels
        top[0].reshape(self._ndim, 1, self._num_anchors * height, width)
Esempio n. 38
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str_)

        self._feat_stride = layer_params['feat_stride']
        self._anchors     = generate_anchors(cfg.TRAIN.RPN_BASE_SIZE, cfg.TRAIN.RPN_ASPECTS, cfg.TRAIN.RPN_SCALES)
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 39
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        layer_params = yaml.load(self.param_str)

        self._feat_stride = layer_params['feat_stride']
        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'feat_stride: {}'.format(self._feat_stride)
            print 'anchors:'
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 40
0
    def setup(self, bottom, top):
        self._anchors = generate_anchors()
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print 'anchors:'
            print self._anchors
            print 'anchor shapes:'
            print np.hstack((
                self._anchors[:, 2::4] - self._anchors[:, 0::4],
                self._anchors[:, 3::4] - self._anchors[:, 1::4],
            ))
            self._counts = cfg.EPS
            self._sums = np.zeros((1, 4))
            self._squared_sums = np.zeros((1, 4))
            self._fg_sum = 0
            self._bg_sum = 0
            self._count = 0

        #layer_params = yaml.load(self.param_str_)
        layer_params = yaml.load(self.param_str)
        self._feat_stride = layer_params['feat_stride']

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = layer_params.get('allowed_border', 0)

        height, width = bottom[0].data.shape[-2:]
        if DEBUG:
            print 'AnchorTargetLayer: height', height, 'width', width

        A = self._num_anchors
        # labels
        top[0].reshape(1, 1, A * height, width)
        # bbox_targets
        top[1].reshape(1, A * 4, height, width)
        # bbox_inside_weights
        top[2].reshape(1, A * 4, height, width)
        # bbox_outside_weights
        top[3].reshape(1, A * 4, height, width)
Esempio n. 41
0
    def setup(self, bottom, top):
        # parse the layer parameter string, which must be valid YAML
        # layer_params = yaml.load(self.param_str_)
        layer_params = yaml.load(self.param_str)

        self._feat_stride = layer_params["feat_stride"]
        self._anchors = generate_anchors()
        self._num_anchors = self._anchors.shape[0]

        if DEBUG:
            print "feat_stride: {}".format(self._feat_stride)
            print "anchors:"
            print self._anchors

        # rois blob: holds R regions of interest, each is a 5-tuple
        # (n, x1, y1, x2, y2) specifying an image batch index n and a
        # rectangle (x1, y1, x2, y2)
        top[0].reshape(1, 5)

        # scores blob: holds scores for R regions of interest
        if len(top) > 1:
            top[1].reshape(1, 1, 1, 1)
Esempio n. 42
0
    def setup(self, bottom, top):
        # layer_params = yaml.load(self.param_str_)

        layer_params = dict()
        layer_params['feat_stride'] = 16
        layer_params['scales'] = (8, 16, 32)
        layer_params['allowed_border'] = 0
        self.RPN_NEGATIVE_OVERLAP = 0.3
        self.RPN_POSITIVE_OVERLAP = 0.7
        self.RPN_FG_FRACTION = 0.5
        self.RPN_BATCHSIZE = 256
        self.EPS = 1e-14
        self.RPN_BBOX_INSIDE_WEIGHTS = 1
        self.RPN_POSITIVE_WEIGHT = -1

        anchor_scales = layer_params.get('scales', (8, 16, 32))
        self._anchors = generate_anchors(scales=np.array(anchor_scales))
        self._num_anchors = self._anchors.shape[0]
        self._feat_stride = layer_params['feat_stride']

        if DEBUG:
            # print 'anchors:'
            # print self._anchors
            # print 'anchor shapes:'
            # print np.hstack((
            #     self._anchors[:, 2::4] - self._anchors[:, 0::4],
            #     self._anchors[:, 3::4] - self._anchors[:, 1::4],
            # ))
            self._counts = self.EPS
            self._sums = np.zeros((1, 4))
            self._squared_sums = np.zeros((1, 4))
            self._fg_sum = 0
            self._bg_sum = 0
            self._count = 0

        # allow boxes to sit over the edge by a small amount
        self._allowed_border = layer_params.get('allowed_border', 0)
Esempio n. 43
0
    def reshape(self, bottom, top):
        """Reshaping happens during the call to forward."""
        pass

def _filter_boxes(boxes, min_size):
    """Remove all boxes with any side smaller than min_size."""
    ws = boxes[:, 3] # D
    hs = boxes[:, 2] # D
    keep = np.where((ws >= min_size) & (hs >= min_size))[0]
    return keep


if __name__ == "__main__":
        anchor_scales = (8, 16, 32)
        _anchors = generate_anchors(scales=np.array(anchor_scales))
        _num_anchors = _anchors.shape[0]
        _feat_stride = 16
	print _anchors

	_bbox_para_num = 5

        height, width = (10,10)
        
        A = _num_anchors
        # labels
        #top0 = np.zeros((1, 6))

        # im_info
        im_info = [160,160,1]
Esempio n. 44
0
 def __init__(self, feat_stride=16):
     self.feat_stride = feat_stride
     self.anchors = generate_anchors()
     self.n_anchors = self.anchors.shape[0]
     self.allowed_border = 0
 def __init__(self, feat_stride=16):
     self.feat_stride = 16
     self.anchors = generate_anchors()
     self.num_anchors = self.anchors.shape[0]
     self.train = False
Esempio n. 46
0
def proposal_layer(rpn_cls_prob_reshape,rpn_bbox_pred,im_info,cfg_key,_feat_stride = [16,],anchor_scales = [8, 16, 32]):
    # Algorithm:
    #
    # for each (H, W) location i
    #   generate A anchor boxes centered on cell i
    #   apply predicted bbox deltas at cell i to each of the A anchors
    # clip predicted boxes to image
    # remove predicted boxes with either height or width < threshold
    # sort all (proposal, score) pairs by score from highest to lowest
    # take top pre_nms_topN proposals before NMS
    # apply NMS with threshold 0.7 to remaining proposals
    # take after_nms_topN proposals after NMS
    # return the top proposals (-> RoIs top, scores top)
    #layer_params = yaml.load(self.param_str_)
    _anchors = generate_anchors(scales=np.array(anchor_scales))
    _num_anchors = _anchors.shape[0]
    rpn_cls_prob_reshape = np.transpose(rpn_cls_prob_reshape,[0,3,1,2])
    rpn_bbox_pred = np.transpose(rpn_bbox_pred,[0,3,1,2])
    #rpn_cls_prob_reshape = np.transpose(np.reshape(rpn_cls_prob_reshape,[1,rpn_cls_prob_reshape.shape[0],rpn_cls_prob_reshape.shape[1],rpn_cls_prob_reshape.shape[2]]),[0,3,2,1])
    #rpn_bbox_pred = np.transpose(rpn_bbox_pred,[0,3,2,1])
    im_info = im_info[0]

    assert rpn_cls_prob_reshape.shape[0] == 1, \
        'Only single item batches are supported'
    # cfg_key = str(self.phase) # either 'TRAIN' or 'TEST'
    #cfg_key = 'TEST'
    pre_nms_topN  = cfg[cfg_key].RPN_PRE_NMS_TOP_N
    post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N
    nms_thresh    = cfg[cfg_key].RPN_NMS_THRESH
    min_size      = cfg[cfg_key].RPN_MIN_SIZE

    # the first set of _num_anchors channels are bg probs
    # the second set are the fg probs, which we want
    scores = rpn_cls_prob_reshape[:, _num_anchors:, :, :]
    bbox_deltas = rpn_bbox_pred
    #im_info = bottom[2].data[0, :]

    if DEBUG:
        print 'im_size: ({}, {})'.format(im_info[0], im_info[1])
        print 'scale: {}'.format(im_info[2])

    # 1. Generate proposals from bbox deltas and shifted anchors
    height, width = scores.shape[-2:]

    if DEBUG:
        print 'score map size: {}'.format(scores.shape)

    # Enumerate all shifts
    shift_x = np.arange(0, width) * _feat_stride
    shift_y = np.arange(0, height) * _feat_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(),
                        shift_x.ravel(), shift_y.ravel())).transpose()

    # Enumerate all shifted anchors:
    #
    # add A anchors (1, A, 4) to
    # cell K shifts (K, 1, 4) to get
    # shift anchors (K, A, 4)
    # reshape to (K*A, 4) shifted anchors
    A = _num_anchors
    K = shifts.shape[0]
    anchors = _anchors.reshape((1, A, 4)) + \
              shifts.reshape((1, K, 4)).transpose((1, 0, 2))
    anchors = anchors.reshape((K * A, 4))

    # Transpose and reshape predicted bbox transformations to get them
    # into the same order as the anchors:
    #
    # bbox deltas will be (1, 4 * A, H, W) format
    # transpose to (1, H, W, 4 * A)
    # reshape to (1 * H * W * A, 4) where rows are ordered by (h, w, a)
    # in slowest to fastest order
    bbox_deltas = bbox_deltas.transpose((0, 2, 3, 1)).reshape((-1, 4))

    # Same story for the scores:
    #
    # scores are (1, A, H, W) format
    # transpose to (1, H, W, A)
    # reshape to (1 * H * W * A, 1) where rows are ordered by (h, w, a)
    scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1))

    # Convert anchors into proposals via bbox transformations
    proposals = bbox_transform_inv(anchors, bbox_deltas)

    # 2. clip predicted boxes to image
    proposals = clip_boxes(proposals, im_info[:2])

    # 3. remove predicted boxes with either height or width < threshold
    # (NOTE: convert min_size to input image scale stored in im_info[2])
    keep = _filter_boxes(proposals, min_size * im_info[2])
    proposals = proposals[keep, :]
    scores = scores[keep]

    # 4. sort all (proposal, score) pairs by score from highest to lowest
    # 5. take top pre_nms_topN (e.g. 6000)
    order = scores.ravel().argsort()[::-1]
    if pre_nms_topN > 0:
        order = order[:pre_nms_topN]
    proposals = proposals[order, :]
    scores = scores[order]

    # 6. apply nms (e.g. threshold = 0.7)
    # 7. take after_nms_topN (e.g. 300)
    # 8. return the top proposals (-> RoIs top)
    keep = nms(np.hstack((proposals, scores)), nms_thresh)
    if post_nms_topN > 0:
        keep = keep[:post_nms_topN]
    proposals = proposals[keep, :]
    scores = scores[keep]
    # Output rois blob
    # Our RPN implementation only supports a single input image, so all
    # batch inds are 0
    batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32)
    blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False)))
    return blob
def anchor_target_layer(rpn_cls_score, gt_boxes, im_info, data, _feat_stride = [16,], anchor_scales = [4 ,8, 16, 32]):
    """
    Assign anchors to ground-truth targets. Produces anchor classification
    labels and bounding-box regression targets.
    """
    _anchors = generate_anchors(scales=np.array(anchor_scales))
    _num_anchors = _anchors.shape[0]

    if DEBUG:
        print 'anchors:'
        print _anchors
        print 'anchor shapes:'
        print np.hstack((
            _anchors[:, 2::4] - _anchors[:, 0::4],
            _anchors[:, 3::4] - _anchors[:, 1::4],
        ))
        _counts = cfg.EPS
        _sums = np.zeros((1, 4))
        _squared_sums = np.zeros((1, 4))
        _fg_sum = 0
        _bg_sum = 0
        _count = 0

    # allow boxes to sit over the edge by a small amount
    _allowed_border =  0
    # map of shape (..., H, W)
    #height, width = rpn_cls_score.shape[1:3]

    im_info = im_info[0]

    # Algorithm:
    #
    # for each (H, W) location i
    #   generate 9 anchor boxes centered on cell i
    #   apply predicted bbox deltas at cell i to each of the 9 anchors
    # filter out-of-image anchors
    # measure GT overlap

    assert rpn_cls_score.shape[0] == 1, \
        'Only single item batches are supported'

    # map of shape (..., H, W)
    height, width = rpn_cls_score.shape[1:3]

    if DEBUG:
        print 'AnchorTargetLayer: height', height, 'width', width
        print ''
        print 'im_size: ({}, {})'.format(im_info[0], im_info[1])
        print 'scale: {}'.format(im_info[2])
        print 'height, width: ({}, {})'.format(height, width)
        print 'rpn: gt_boxes.shape', gt_boxes.shape
        print 'rpn: gt_boxes', gt_boxes

    # 1. Generate proposals from bbox deltas and shifted anchors
    shift_x = np.arange(0, width) * _feat_stride
    shift_y = np.arange(0, height) * _feat_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(),
                        shift_x.ravel(), shift_y.ravel())).transpose()
    # add A anchors (1, A, 4) to
    # cell K shifts (K, 1, 4) to get
    # shift anchors (K, A, 4)
    # reshape to (K*A, 4) shifted anchors
    A = _num_anchors
    K = shifts.shape[0]
    all_anchors = (_anchors.reshape((1, A, 4)) +
                   shifts.reshape((1, K, 4)).transpose((1, 0, 2)))
    all_anchors = all_anchors.reshape((K * A, 4))
    total_anchors = int(K * A)

    # only keep anchors inside the image
    inds_inside = np.where(
        (all_anchors[:, 0] >= -_allowed_border) &
        (all_anchors[:, 1] >= -_allowed_border) &
        (all_anchors[:, 2] < im_info[1] + _allowed_border) &  # width
        (all_anchors[:, 3] < im_info[0] + _allowed_border)    # height
    )[0]

    if DEBUG:
        print 'total_anchors', total_anchors
        print 'inds_inside', len(inds_inside)

    # keep only inside anchors
    anchors = all_anchors[inds_inside, :]
    if DEBUG:
        print 'anchors.shape', anchors.shape

    # label: 1 is positive, 0 is negative, -1 is dont care
    labels = np.empty((len(inds_inside), ), dtype=np.float32)
    labels.fill(-1)

    # overlaps between the anchors and the gt boxes
    # overlaps (ex, gt)
    overlaps = bbox_overlaps(
        np.ascontiguousarray(anchors, dtype=np.float),
        np.ascontiguousarray(gt_boxes, dtype=np.float))
    argmax_overlaps = overlaps.argmax(axis=1)
    max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]
    gt_argmax_overlaps = overlaps.argmax(axis=0)
    gt_max_overlaps = overlaps[gt_argmax_overlaps,
                               np.arange(overlaps.shape[1])]
    gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]

    if not cfg.TRAIN.RPN_CLOBBER_POSITIVES:
        # assign bg labels first so that positive labels can clobber them
        labels[max_overlaps < cfg.TRAIN.RPN_NEGATIVE_OVERLAP] = 0

    # fg label: for each gt, anchor with highest overlap
    labels[gt_argmax_overlaps] = 1

    # fg label: above threshold IOU
    labels[max_overlaps >= cfg.TRAIN.RPN_POSITIVE_OVERLAP] = 1

    if cfg.TRAIN.RPN_CLOBBER_POSITIVES:
        # assign bg labels last so that negative labels can clobber positives
        labels[max_overlaps < cfg.TRAIN.RPN_NEGATIVE_OVERLAP] = 0

    # subsample positive labels if we have too many
    num_fg = int(cfg.TRAIN.RPN_FG_FRACTION * cfg.TRAIN.RPN_BATCHSIZE)
    fg_inds = np.where(labels == 1)[0]
    if len(fg_inds) > num_fg:
        disable_inds = npr.choice(
            fg_inds, size=(len(fg_inds) - num_fg), replace=False)
        labels[disable_inds] = -1

    # subsample negative labels if we have too many
    num_bg = cfg.TRAIN.RPN_BATCHSIZE - np.sum(labels == 1)
    bg_inds = np.where(labels == 0)[0]
    if len(bg_inds) > num_bg:
        disable_inds = npr.choice(
            bg_inds, size=(len(bg_inds) - num_bg), replace=False)
        labels[disable_inds] = -1
        #print "was %s inds, disabling %s, now %s inds" % (
            #len(bg_inds), len(disable_inds), np.sum(labels == 0))

    bbox_targets = np.zeros((len(inds_inside), 4), dtype=np.float32)
    bbox_targets = _compute_targets(anchors, gt_boxes[argmax_overlaps, :])

    bbox_inside_weights = np.zeros((len(inds_inside), 4), dtype=np.float32)
    bbox_inside_weights[labels == 1, :] = np.array(cfg.TRAIN.RPN_BBOX_INSIDE_WEIGHTS)

    bbox_outside_weights = np.zeros((len(inds_inside), 4), dtype=np.float32)
    if cfg.TRAIN.RPN_POSITIVE_WEIGHT < 0:
        # uniform weighting of examples (given non-uniform sampling)
        num_examples = np.sum(labels >= 0)
        positive_weights = np.ones((1, 4)) * 1.0 / num_examples
        negative_weights = np.ones((1, 4)) * 1.0 / num_examples
    else:
        assert ((cfg.TRAIN.RPN_POSITIVE_WEIGHT > 0) &
                (cfg.TRAIN.RPN_POSITIVE_WEIGHT < 1))
        positive_weights = (cfg.TRAIN.RPN_POSITIVE_WEIGHT /
                            np.sum(labels == 1))
        negative_weights = ((1.0 - cfg.TRAIN.RPN_POSITIVE_WEIGHT) /
                            np.sum(labels == 0))
    bbox_outside_weights[labels == 1, :] = positive_weights
    bbox_outside_weights[labels == 0, :] = negative_weights

    if DEBUG:
        _sums += bbox_targets[labels == 1, :].sum(axis=0)
        _squared_sums += (bbox_targets[labels == 1, :] ** 2).sum(axis=0)
        _counts += np.sum(labels == 1)
        means = _sums / _counts
        stds = np.sqrt(_squared_sums / _counts - means ** 2)
        print 'means:'
        print means
        print 'stdevs:'
        print stds

    # map up to original set of anchors
    labels = _unmap(labels, total_anchors, inds_inside, fill=-1)
    bbox_targets = _unmap(bbox_targets, total_anchors, inds_inside, fill=0)
    bbox_inside_weights = _unmap(bbox_inside_weights, total_anchors, inds_inside, fill=0)
    bbox_outside_weights = _unmap(bbox_outside_weights, total_anchors, inds_inside, fill=0)

    if DEBUG:
        print 'rpn: max max_overlap', np.max(max_overlaps)
        print 'rpn: num_positive', np.sum(labels == 1)
        print 'rpn: num_negative', np.sum(labels == 0)
        _fg_sum += np.sum(labels == 1)
        _bg_sum += np.sum(labels == 0)
        _count += 1
        print 'rpn: num_positive avg', _fg_sum / _count
        print 'rpn: num_negative avg', _bg_sum / _count

    # labels
    #pdb.set_trace()
    labels = labels.reshape((1, height, width, A)).transpose(0, 3, 1, 2)
    labels = labels.reshape((1, 1, A * height, width))
    rpn_labels = labels

    # bbox_targets
    bbox_targets = bbox_targets \
        .reshape((1, height, width, A * 4)).transpose(0, 3, 1, 2)

    rpn_bbox_targets = bbox_targets
    # bbox_inside_weights
    bbox_inside_weights = bbox_inside_weights \
        .reshape((1, height, width, A * 4)).transpose(0, 3, 1, 2)
    #assert bbox_inside_weights.shape[2] == height
    #assert bbox_inside_weights.shape[3] == width

    rpn_bbox_inside_weights = bbox_inside_weights

    # bbox_outside_weights
    bbox_outside_weights = bbox_outside_weights \
        .reshape((1, height, width, A * 4)).transpose(0, 3, 1, 2)
    #assert bbox_outside_weights.shape[2] == height
    #assert bbox_outside_weights.shape[3] == width

    rpn_bbox_outside_weights = bbox_outside_weights

    return rpn_labels,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights
Esempio n. 48
0
    def forward(self, bottom, top):
        cfg_key = str(self.phase) # either 'TRAIN' or 'TEST'
        pre_nms_topN  = cfg[cfg_key].RPN_PRE_NMS_TOP_N
        post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N
        nms_thresh    = cfg[cfg_key].RPN_NMS_THRESH
        min_size = self._min_sizes
        # the first set of _num_anchors channels are bg probs
        # the second set are the fg probs, which we want
    
        im_info = bottom[0].data[0, :]
        batch_size = bottom[1].data.shape[0]
        if batch_size > 1:
            raise ValueError("Sorry, multiple images each device is not implemented")

        cls_prob_dict = {
            'stride64': bottom[10].data,
            'stride32': bottom[9].data,
            'stride16': bottom[8].data,
            'stride8': bottom[7].data,
            'stride4': bottom[6].data,
        }
        bbox_pred_dict = {
            'stride64': bottom[5].data,
            'stride32': bottom[4].data,
            'stride16': bottom[3].data,
            'stride8': bottom[2].data,
            'stride4': bottom[1].data,
        }
      
        proposal_list = []
        score_list = []
        for s in self._feat_stride:
            stride = int(s)
            sub_anchors = generate_anchors(base_size=stride, scales=self._scales, ratios=self._ratios)
    
            scores = cls_prob_dict['stride' + str(s)][:, self._num_anchors:, :, :]
            bbox_deltas = bbox_pred_dict['stride' + str(s)]
          
            # 1. Generate proposals from bbox_deltas and shifted anchors
            # use real image size instead of padded feature map sizes
            height, width = int(im_info[0] / stride), int(im_info[1] / stride)

            # Enumerate all shifts
            shift_x = np.arange(0, width) * stride
            shift_y = np.arange(0, height) * stride
            shift_x, shift_y = np.meshgrid(shift_x, shift_y)
            shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()

            # Enumerate all shifted anchors:
            #
            # add A anchors (1, A, 4) to
            # cell K shifts (K, 1, 4) to get
            # shift anchors (K, A, 4)
            # reshape to (K*A, 4) shifted anchors
            A = self._num_anchors
            K = shifts.shape[0]
    
            anchors = sub_anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
            anchors = anchors.reshape((K * A, 4))

            # Transpose and reshape predicted bbox transformations to get them
            # into the same order as the anchors:
            #
            # bbox deltas will be (1, 4 * A, H, W) format
            # transpose to (1, H, W, 4 * A)
            # reshape to (1 * H * W * A, 4) where rows are ordered by (h, w, a)
            # in slowest to fastest order
            bbox_deltas = _clip_pad(bbox_deltas, (height, width))
            bbox_deltas = bbox_deltas.transpose((0, 2, 3, 1)).reshape((-1, 4))

            # Same story for the scores:
            #
            # scores are (1, A, H, W) format
            # transpose to (1, H, W, A)
            # reshape to (1 * H * W * A, 1) where rows are ordered by (h, w, a)
            scores = _clip_pad(scores, (height, width))
            scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1))

            # Convert anchors into proposals via bbox transformations
            proposals = bbox_transform_inv(anchors, bbox_deltas)

            # 2. clip predicted boxes to image
            proposals = clip_boxes(proposals, im_info[:2])

            # 3. remove predicted boxes with either height or width < threshold
            # (NOTE: convert min_size to input image scale stored in im_info[2])
            keep = _filter_boxes(proposals, min_size * im_info[2])
            proposals = proposals[keep, :]
            scores = scores[keep]

            proposal_list.append(proposals)
            score_list.append(scores)

        proposals = np.vstack(proposal_list)
        scores = np.vstack(score_list)

        # 4. sort all (proposal, score) pairs by score from highest to lowest
        # 5. take top pre_nms_topN (e.g. 6000)
        order = scores.ravel().argsort()[::-1]
        if pre_nms_topN > 0:
            order = order[:pre_nms_topN]
        proposals = proposals[order, :]
        scores = scores[order]

        # 6. apply nms (e.g. threshold = 0.7)
        # 7. take after_nms_topN (e.g. 300)
        # 8. return the top proposals (-> RoIs top)
        det = np.hstack((proposals, scores)).astype(np.float32)
        keep = nms(det,nms_thresh)
        if post_nms_topN > 0:
            keep = keep[:post_nms_topN]
        # pad to ensure output size remains unchanged
        if len(keep) < post_nms_topN:
            pad = npr.choice(keep, size=post_nms_topN - len(keep))
            keep = np.hstack((keep, pad))

        # pad to ensure output size remains unchanged
        if len(keep) < post_nms_topN:
            try:
                pad = npr.choice(keep, size=post_nms_topN - len(keep))
            except:
                proposals = np.zeros((post_nms_topN, 4), dtype=np.float32)
                proposals[:,2] = 16
                proposals[:,3] = 16
                batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32)
                blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False)))
                top[0].reshape(*(blob.shape))
                top[0].data[...] = blob
                return      
            keep = np.hstack((keep, pad))
           
        proposals = proposals[keep, :]
        scores = scores[keep]

        # Output rois array
        # Our RPN implementation only supports a single input image, so all
        # batch inds are 0
        batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32)
        blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False)))
        # if is_train:
    
        top[0].reshape(*(blob.shape))
        top[0].data[...] = blob
Esempio n. 49
0
def imdb_rpn_compute_stats(net, imdb, anchor_scales=(8,16,32),
                           feature_stride=16):
    raw_anchors = generate_anchors(scales=np.array(anchor_scales))
    print raw_anchors.shape
    sums = 0
    squred_sums = 0
    counts = 0
    roidb = filter_roidb(imdb.roidb)
    # Compute a map of input image size and output feature map blob
    map_w = {}
    map_h = {}
    for i in xrange(50, cfg.TRAIN.MAX_SIZE + 10):
        blobs = {
            'data': np.zeros((1, 3, i, i)),
            'im_info': np.asarray([[i, i, 1.0]])
        }
        net.blobs['data'].reshape(*(blobs['data'].shape))
        net.blobs['im_info'].reshape(*(blobs['im_info'].shape))
        blobs_out = net.forward(
            data=blobs['data'].astype(np.float32, copy=False),
            im_info=blobs['im_info'].astype(np.float32, copy=False))
        height, width = net.blobs['rpn/output'].data.shape[-2:]
        map_w[i] = width
        map_h[i] = height

    for i in xrange(len(roidb)):
        if not i % 5000:
            print 'computing %d/%d' % (i, imdb.num_images)
        im = None
        if cfg.TRAIN.FORMAT == 'pickle':
            with open(roidb[i]['image'], 'rb') as f:
                im = cPickle.load(f)
        else:
            im = cv2.imread(roidb[i]['image'])

        im_data, im_info = _get_image_blob(im)
        gt_boxes = roidb[i]['boxes']
        gt_boxes = gt_boxes * im_info[0, 2]
        height = map_h[im_data.shape[2]]
        width = map_w[im_data.shape[3]]
        # 1. Generate proposals from bbox deltas and shifted anchors
        shift_x = np.arange(0, width) * feature_stride
        shift_y = np.arange(0, height) * feature_stride
        shift_x, shift_y = np.meshgrid(shift_x, shift_y)
        shifts = np.vstack((shift_x.ravel(), shift_y.ravel(),
                            shift_x.ravel(), shift_y.ravel())).transpose()
        # add A anchors (1, A, 4) to
        # cell K shifts (K, 1, 4) to get
        # shift anchors (K, A, 4)
        # reshape to (K*A, 4) shifted anchors
        A = raw_anchors.shape[0]
        K = shifts.shape[0]
        all_anchors = (raw_anchors.reshape((1, A, 4)) +
                       shifts.reshape((1, K, 4)).transpose((1, 0, 2)))
        all_anchors = all_anchors.reshape((K * A, 4))

        # only keep anchors inside the image
        inds_inside = np.where(
            (all_anchors[:, 0] >= 0) &
            (all_anchors[:, 1] >= 0) &
            (all_anchors[:, 2] < im_info[0, 1]) &  # width
            (all_anchors[:, 3] < im_info[0, 0])  # height
        )[0]

        # keep only inside anchors
        anchors = all_anchors[inds_inside, :]

        overlaps = bbox_overlaps(
            np.ascontiguousarray(anchors, dtype=np.float),
            np.ascontiguousarray(gt_boxes, dtype=np.float))

        # There are 2 types of bbox targets
        # 1. anchor whose overlaps with gt is greater than RPN_POSITIVE_OVERLAP
        argmax_overlaps = overlaps.argmax(axis=1)
        max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]
        fg_inds = np.where(max_overlaps >= cfg.TRAIN.RPN_POSITIVE_OVERLAP)[0]
        # 2. anchors which best match certain gt
        gt_argmax_overlaps = overlaps.argmax(axis=0)
        gt_max_overlaps = overlaps[gt_argmax_overlaps,
                                   np.arange(overlaps.shape[1])]
        gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]
        fg_inds = np.unique(np.hstack((fg_inds, gt_argmax_overlaps)))
        gt_rois = gt_boxes[argmax_overlaps, :]

        anchors = anchors[fg_inds, :]
        gt_rois = gt_rois[fg_inds, :]
        targets = bbox_transform(anchors, gt_rois[:, :4]).astype(np.float32, copy=False)
        sums += targets.sum(axis=0)
        squred_sums += (targets ** 2).sum(axis=0)
        counts += targets.shape[0]

    means = sums / counts
    stds = np.sqrt(squred_sums / counts - means ** 2)
    print means
    print stds
    return means, stds