Exemplo n.º 1
0
def get_mlvl_centers(locations, strides):
    mlvl_centers = []
    for location, stride in zip(locations, strides):
        lx, ly = _tuple(location, 2)
        sw, sh = _tuple(stride, 2)
        centers = torch.zeros(lx, ly, 2)
        centers[..., 0] = (
            torch.arange(lx, dtype=torch.float).view(lx, 1).expand(lx, ly) * sw
            + sw // 2)
        centers[..., 1] = (
            torch.arange(ly, dtype=torch.float).view(1, ly).expand(lx, ly) * sh
            + sh // 2)
        mlvl_centers.append(centers)
    return mlvl_centers
 def inference(self, *xs):
     self.eval()
     with torch.no_grad():
         xs = self.forward(*xs)
     preds = self._inference(*_tuple(xs))
     self.train()
     return preds
Exemplo n.º 3
0
 def inference(self, x):
     self.eval()
     with torch.no_grad():
         preds = self.forward(x)
     dets = self._inference(*_tuple(preds))
     self.train()
     return dets
def split_levels(levels, split_at=5):
    levels = _tuple(levels)
    lo = levels[0]
    hi = levels[-1]
    assert levels == tuple(range(lo, hi + 1))
    basic_levels = tuple(range(lo, min(hi, split_at) + 1))
    extra_levels = tuple(range(max(lo, split_at + 1), hi + 1))
    return basic_levels, extra_levels
Exemplo n.º 5
0
 def region_proposal(self, x):
     cs = self.backbone(x)
     ps = self.fpn(*cs)
     ps = _tuple(ps)
     loc_p, cls_p = self.head(*ps)
     rois = self._inference(detach(loc_p), detach(cls_p))
     if self.training and self._e2e:
         return ps, rois, loc_p, cls_p
     else:
         return ps, rois
Exemplo n.º 6
0
def get_mlvl_centers(locations):
    mlvl_centers = []
    for location in locations:
        lx, ly = _tuple(location, 2)
        centers = torch.zeros(lx, ly, 2)
        centers[...,
                0] = torch.arange(lx, dtype=torch.float).view(lx, 1).expand(
                    lx, ly) + 0.5
        centers[...,
                1] = torch.arange(ly, dtype=torch.float).view(1, ly).expand(
                    lx, ly) + 0.5
        mlvl_centers.append(centers)
    return mlvl_centers
Exemplo n.º 7
0
    def __init__(self,
                 backbone,
                 num_anchors,
                 num_classes,
                 f_channels,
                 inference,
                 extra_levels=(6, )):
        super().__init__()
        self.num_classes = num_classes
        self.backbone = backbone
        self._inference = inference

        stages = backbone.out_channels

        self.extra_levels = _tuple(extra_levels)
        self.extra_layers = nn.ModuleList([])
        for l in self.extra_levels:
            self.extra_layers.append(
                Bottleneck(stages[-1], f_channels, stride=2))
            stages.append(f_channels)

        self.r_head = SSDHead(num_anchors,
                              1,
                              stages,
                              norm_layer='default',
                              lite=True)

        self.tcbs = nn.ModuleList(
            [TransferConnection(stages[-1], f_channels, last=True)])
        for c in reversed(stages[:-1]):
            self.tcbs.append(
                TransferConnection(c, f_channels, norm_layer='default'))

        self.d_head = SSDHead(num_anchors,
                              num_classes,
                              _tuple(f_channels, 3),
                              norm_layer='default',
                              lite=True)
Exemplo n.º 8
0
 def __init__(self,
              backbone,
              num_anchors=(4, 6, 6, 6, 6, 4),
              num_classes=21,
              f_channels=256,
              extra_levels=(6, 7, 8),
              pad_last=False,
              lite=False):
     super().__init__()
     extra_levels = _tuple(extra_levels)
     feature_levels = backbone.feature_levels + extra_levels
     num_anchors = _tuple(num_anchors, len(feature_levels))
     self.num_classes = num_classes
     self.backbone = backbone
     self.feature_levels = feature_levels
     self.num_anchors = num_anchors
     backbone_channels = backbone.out_channels
     head_in_channels = list(backbone_channels)
     if 6 in extra_levels:
         self.layer6 = DownBlock(backbone_channels[-1],
                                 2 * f_channels,
                                 lite=lite)
         head_in_channels.append(2 * f_channels)
     if 7 in extra_levels:
         self.layer7 = DownBlock(2 * f_channels, f_channels, lite=lite)
         head_in_channels.append(f_channels)
     if 8 in extra_levels:
         padding = 1 if pad_last else 0
         self.layer8 = DownBlock(f_channels,
                                 f_channels,
                                 padding=padding,
                                 lite=lite)
         head_in_channels.append(f_channels)
     self.head = SSDHead(num_anchors,
                         num_classes,
                         head_in_channels,
                         lite=lite)
Exemplo n.º 9
0
    def __init__(self, num_anchors, num_classes, in_channels, lite=False, concat=True):
        super().__init__()
        self.num_classes = num_classes
        self.concat = concat
        num_anchors = _tuple(num_anchors, len(in_channels))
        self.preds = nn.ModuleList([
            nn.Sequential(
                get_norm_layer('default', c),
                Conv2d(c, n * (num_classes + 4), kernel_size=3, depthwise_separable=lite, mid_norm_layer='default')
            )
            for c, n in zip(in_channels, num_anchors)
        ])

        for p in self.preds:
            get_last_conv(p).bias.data[4:].fill_(inverse_sigmoid(0.01))
def center_crop(anns, size, output_size):
    r"""
    Crops the bounding boxes of the given PIL Image at the center.

    Parameters
    ----------
    anns : ``List[Dict]``
        Sequences of annotation of objects, containing `bbox` of [l, t, w, h].
    size : ``Sequence[int]``
        Size of the original image.
    output_size : ``Union[Number, Sequence[int]]``
        Desired output size of the crop. If size is an int instead of sequence like (w, h),
        a square crop (size, size) is made.
    """
    output_size = _tuple(output_size, 2)
    output_size = tuple(int(x) for x in output_size)
    w, h = size
    th, tw = output_size
    upper = int(round((h - th) / 2.))
    left = int(round((w - tw) / 2.))
    return crop(anns, left, upper, th, tw)
    def __init__(self,
                 backbone,
                 num_classes=10,
                 num_anchors=3,
                 f_channels=256,
                 extra_levels=(6, 7),
                 num_head_layers=4):
        super().__init__()
        assert tuple(backbone.feature_levels) == (
            3, 4, 5), "Feature levels of backbone must be (3,4,5)"
        self.num_classes = num_classes
        self.backbone = backbone
        backbone_channels = backbone.out_channels
        self.extra_levels = _tuple(extra_levels)
        if 6 in extra_levels:
            self.layer6 = Conv2d(backbone_channels[-1],
                                 f_channels,
                                 kernel_size=3,
                                 stride=2,
                                 norm_layer='default')
        if 7 in extra_levels:
            assert 6 in extra_levels
            self.layer7 = nn.Sequential(
                get_activation('default'),
                Conv2d(f_channels,
                       f_channels,
                       kernel_size=3,
                       stride=2,
                       norm_layer='default'))

        self.fpn = FPN(backbone, f_channels)

        self.head = RetinaHead(f_channels,
                               num_anchors,
                               num_classes,
                               num_layers=num_head_layers)
 def forward(self, *xs):
     for module in self._modules.values():
         xs = module(*_tuple(xs))
     return xs