示例#1
0
def generate_anchors_global(feat_stride,
                            height,
                            width,
                            anchor_scales=(8, 16, 32),
                            anchor_ratios=(0.5, 1, 2)):

    anchors = generate_anchors(base_size=feat_stride,
                               ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))
    # 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 (A*K, 4) shifted anchors
    A = anchors.shape[0]
    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)).astype(np.float32, copy=False)

    return anchors
示例#2
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)
    def __init__(self, cfg, feat_stride, scales, ratios):
        super(AnchorTargetLayer, self).__init__()

        self.cfg = cfg

        self._feat_stride = feat_stride
        self._scales = scales
        anchor_scales = scales
        self._anchors = torch.from_numpy(
            generate_anchors(base_size=cfg.FEAT_STRIDE[0],
                             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
    def __init__(self, cfg):
        super(AnchorTargetLayer, self).__init__()

        self.cfg = cfg

        self.scales = np.array(cfg.ANCHOR_SCALES)
        self.ratios = np.array(cfg.ANCHOR_RATIOS)
        self._feat_stride = cfg.FEAT_STRIDE[0]
        base_anchors = generate_anchors(base_size=self._feat_stride,
                                        scales=self.scales,
                                        ratios=self.ratios)
        self._anchors = torch.from_numpy(base_anchors).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
示例#5
0
def generate_anchors_global(feat_strides,
                            heights,
                            widths,
                            anchor_scales=(8, 16, 32),
                            anchor_ratios=(0.5, 1, 2)):
    assert len(feat_strides) == len(heights)
    assert len(heights) == len(widths)

    anchors_list = list()
    for index in range(len(feat_strides)):
        anchors = generate_anchors(base_size=feat_strides[index],
                                   ratios=np.array(anchor_ratios),
                                   scales=np.array([anchor_scales[index]]))
        anchors_list.append(anchors)

    num_anchors = np.asarray([anchors.shape[0] for anchors in anchors_list])

    def global_anchors(height, width, feat_stride, anchors):
        # 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 (A*K, 4) shifted anchors
        A = anchors.shape[0]
        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)).astype(np.float32, copy=False)
        return anchors

    global_anchors_list = list()
    for index in range(len(feat_strides)):
        anchors = global_anchors(height=heights[index],
                                 width=widths[index],
                                 feat_stride=feat_strides[index],
                                 anchors=anchors_list[index])
        global_anchors_list.append(anchors)

    anchors = np.concatenate(global_anchors_list, axis=0)
    return anchors, num_anchors
示例#6
0
    def grid_anchors(self, featmap_size, stride=16, device='cuda'):
        base_anchors = torch.from_numpy(generate_anchors(base_size=cfg.FEAT_STRIDE[0], scales=np.array(stride),
                                                         ratios=np.array(1.0))).float()

        feat_h, feat_w = featmap_size
        shift_x = torch.arange(0, feat_w, device=device) * stride
        shift_y = torch.arange(0, feat_h, device=device) * stride
        shift_xx, shift_yy = self._meshgrid(shift_x, shift_y)
        shifts = torch.stack([shift_xx, shift_yy, shift_xx, shift_yy], dim=-1)
        shifts = shifts.type_as(base_anchors)
        # first feat_w elements correspond to the first row of shifts
        # add A anchors (1, A, 4) to K shifts (K, 1, 4) to get
        # shifted anchors (K, A, 4), reshape to (K*A, 4)

        all_anchors = base_anchors[None, :, :] + shifts[:, None, :]
        all_anchors = all_anchors.view(-1, 4)
        # first A rows correspond to A anchors of (0, 0) in feature map,
        # then (0, 1), (0, 2), ...
        return all_anchors