Exemple #1
0
def match_anchors(anns,
                  mlvl_priors,
                  locations,
                  ignore_thresh=None,
                  get_label=lambda x: x['category_id'],
                  debug=False):
    loc_targets = []
    cls_targets = []
    ignores = []
    num_levels, priors_per_level = mlvl_priors.size()[:2]
    for (lx, ly), priors in zip(locations, mlvl_priors):
        loc_targets.append(priors.new_zeros((lx, ly, priors_per_level, 4)))
        cls_targets.append(
            priors.new_zeros((lx, ly, priors_per_level), dtype=torch.long))
        ignores.append(
            priors.new_zeros((lx, ly, priors_per_level), dtype=torch.uint8))

    for ann in anns:
        l, t, w, h = ann['bbox']
        x = l + w / 2
        y = t + h / 2
        size = torch.tensor([w, h])
        ious = iou_1m_with_size(size, mlvl_priors)
        max_iou, max_ind = ious.view(-1).max(dim=0)
        level, i = divmod(max_ind.item(), priors_per_level)
        if debug:
            print("[%d,%d]: %.4f" % (level, i, max_iou.item()))
        lx, ly = locations[level]
        pw, ph = mlvl_priors[level, i]
        cx, offset_x = divmod(x * lx, 1)
        cx = int(cx)
        cy, offset_y = divmod(y * ly, 1)
        cy = int(cy)
        tx = inverse_sigmoid(offset_x)
        ty = inverse_sigmoid(offset_y)
        tw = log(w / pw)
        th = log(h / ph)
        loc_targets[level][cx, cy, i] = torch.tensor([tx, ty, tw, th])
        cls_targets[level][cx, cy, i] = get_label(ann)
        ignore = ious > ignore_thresh
        for level, i in torch.nonzero(ignore):
            lx, ly = locations[level]
            cx, offset_x = divmod(x * lx, 1)
            cx = int(cx)
            cy, offset_y = divmod(y * ly, 1)
            cy = int(cy)
            ignores[level][cx, cy, i] = 1

    loc_t = torch.cat([t.view(-1, 4) for t in loc_targets], dim=0)
    cls_t = torch.cat([t.view(-1) for t in cls_targets], dim=0)
    ignore = torch.cat([t.view(-1) for t in ignores], dim=0)
    return loc_t, cls_t, ignore
    def __init__(self, num_anchors, num_classes, f_channels=256, num_layers=4, lite=False, concat=True):
        super().__init__()
        self.num_classes = num_classes
        self.concat = concat
        self.loc_head = _make_head(
            f_channels, num_layers, num_anchors * 4, lite=lite)
        self.cls_head = _make_head(
            f_channels, num_layers, num_anchors * num_classes, lite=lite)

        bias_init_constant(self.cls_head[-1], inverse_sigmoid(0.01))
Exemple #3
0
    def __init__(self, num_anchors, in_channels, f_channels=256, lite=False):
        super().__init__()
        kernel_size = 5 if lite else 3
        self.conv = Conv2d(
            in_channels, f_channels, kernel_size=kernel_size,
            norm_layer='default', activation='default', depthwise_separable=lite)
        self.loc_conv = Conv2d(f_channels, num_anchors * 4, kernel_size=1)
        self.cls_conv = Conv2d(f_channels, num_anchors * 2, kernel_size=1)

        bias_init_constant(self.cls_conv, inverse_sigmoid(0.01))
    def __init__(self, num_anchors, num_classes=2, in_channels=245, f_channels=256):
        super().__init__()
        self.num_classes = num_classes
        self.conv = DWConv2d(
            in_channels, f_channels, kernel_size=5,
            mid_norm_layer='default', norm_layer='default',
            activation='default')
        self.loc_conv = Conv2d(
            f_channels, num_anchors * 4, kernel_size=1)
        self.cls_conv = Conv2d(
            f_channels, num_anchors * num_classes, kernel_size=1)

        bias_init_constant(self.cls_conv, inverse_sigmoid(0.01))
    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))
Exemple #6
0
    def __init__(self, in_channels, num_anchors=3, num_classes=80, lite=False):
        super().__init__()
        self.num_classes = num_classes
        out_channels = num_anchors * (5 + num_classes)
        channels = in_channels
        self.conv51 = nn.Sequential(
            BasicBlock(channels[-1], channels[-1], lite=lite),
            BasicBlock(channels[-1], channels[-1], lite=lite),
            Conv2d(channels[-1],
                   channels[-1] // 2,
                   kernel_size=1,
                   norm_layer='default',
                   activation='default'),
        )
        self.conv52 = Conv2d(channels[-1] // 2,
                             channels[-1],
                             kernel_size=3,
                             norm_layer='default',
                             activation='default',
                             depthwise_separable=lite)
        self.pred5 = Conv2d(channels[-1], out_channels, kernel_size=1)

        self.lat5 = Conv2d(channels[-1] // 2,
                           channels[-1] // 4,
                           kernel_size=1,
                           norm_layer='default')

        self.conv41 = nn.Sequential(
            BasicBlock(channels[-2] + channels[-1] // 4,
                       channels[-2],
                       lite=lite),
            BasicBlock(channels[-2], channels[-2], lite=lite),
            Conv2d(channels[-2],
                   channels[-2] // 2,
                   kernel_size=1,
                   norm_layer='default',
                   activation='default'),
        )
        self.conv42 = Conv2d(channels[-2] // 2,
                             channels[-2],
                             kernel_size=3,
                             norm_layer='default',
                             activation='default',
                             depthwise_separable=lite)
        self.pred4 = Conv2d(channels[-2], out_channels, kernel_size=1)

        self.lat4 = Conv2d(channels[-2] // 2,
                           channels[-2] // 4,
                           kernel_size=1,
                           norm_layer='default')

        self.conv31 = nn.Sequential(
            BasicBlock(channels[-3] + channels[-2] // 4,
                       channels[-3],
                       lite=lite),
            BasicBlock(channels[-3], channels[-3], lite=lite),
            Conv2d(channels[-3],
                   channels[-3] // 2,
                   kernel_size=1,
                   norm_layer='default',
                   activation='default'),
        )
        self.conv32 = Conv2d(channels[-3] // 2,
                             channels[-3],
                             kernel_size=3,
                             norm_layer='default',
                             activation='default',
                             depthwise_separable=lite)
        self.pred3 = Conv2d(channels[-3], out_channels, kernel_size=1)

        get_last_conv(self.pred3).bias.data[4].fill_(inverse_sigmoid(0.01))
        get_last_conv(self.pred3).bias.data[4].fill_(inverse_sigmoid(0.01))
        get_last_conv(self.pred3).bias.data[4].fill_(inverse_sigmoid(0.01))