Exemple #1
0
    def __init__(self, network, base_size, features, num_filters, sizes, ratios,
                 steps, classes, use_1x1_transition=True, use_bn=True,
                 reduce_ratio=1.0, min_depth=128, global_pool=False, pretrained=False,
                 stds=(0.1, 0.1, 0.2, 0.2), nms_thresh=0.45, nms_topk=400, post_nms=100,
                 anchor_alloc_size=128, ctx=mx.cpu(), **kwargs):
        super(SSD, self).__init__(**kwargs)
        if network is None:
            num_layers = len(ratios)
        else:
            num_layers = len(features) + len(num_filters) + int(global_pool)
        assert len(sizes) == num_layers + 1
        sizes = list(zip(sizes[:-1], sizes[1:]))
        assert isinstance(ratios, list), "Must provide ratios as list or list of list"
        if not isinstance(ratios[0], (tuple, list)):
            ratios = ratios * num_layers  # propagate to all layers if use same ratio
        assert num_layers == len(sizes) == len(ratios), \
            "Mismatched (number of layers) vs (sizes) vs (ratios): {}, {}, {}".format(
                num_layers, len(sizes), len(ratios))
        assert num_layers > 0, "SSD require at least one layer, suggest multiple."
        self._num_layers = num_layers
        self.classes = classes
        self.nms_thresh = nms_thresh
        self.nms_topk = nms_topk
        self.post_nms = post_nms

        with self.name_scope():
            if network is None:
                # use fine-grained manually designed block as features
                self.features = features(pretrained=pretrained, ctx=ctx)
            else:
                self.features = FeatureExpander(
                    network=network, outputs=features, num_filters=num_filters,
                    use_1x1_transition=use_1x1_transition,
                    use_bn=use_bn, reduce_ratio=reduce_ratio, min_depth=min_depth,
                    global_pool=global_pool, pretrained=pretrained, ctx=ctx)
            self.class_predictors = nn.HybridSequential()
            self.box_predictors = nn.HybridSequential()
            self.anchor_generators = nn.HybridSequential()
            asz = anchor_alloc_size
            im_size = (base_size, base_size)
            for i, s, r, st in zip(range(num_layers), sizes, ratios, steps):
                anchor_generator = SSDAnchorGenerator(i, im_size, s, r, st, (asz, asz))
                self.anchor_generators.add(anchor_generator)
                asz = max(asz // 2, 16)  # pre-compute larger than 16x16 anchor map
                num_anchors = anchor_generator.num_depth
                self.class_predictors.add(ConvPredictor(num_anchors * (len(self.classes) + 1)))
                self.box_predictors.add(ConvPredictor(num_anchors * 4))
            self.bbox_decoder = NormalizedBoxCenterDecoder(stds)
            self.cls_decoder = MultiPerClassDecoder(len(self.classes) + 1, thresh=0.01)
Exemple #2
0
    def __init__(self, classes, use_1x1_transition=False, use_bn=True, reduce_ratio=1.0, min_depth=128,
                 stds=(0.1, 0.1, 0.2, 0.2), nms_thresh=0.45, nms_topk=400, post_nms=100,
                 anchor_alloc_size=128, ctx=mx.gpu(), norm_layer=nn.BatchNorm, **kwargs):
        super(JanetRes, self).__init__(**kwargs)
        self.classes = classes
        pretrained = False; global_pool = False; norm_kwargs = {}
        im_size = (300, 300)

        network = 'resnet101_v2'
        features = ['stage3_activation22', 'stage4_activation2']
        channels = [512, 512, 256, 256]
        sizes = [0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 0.9]
        ratios = [[1, 2, 1.4]] * 2 + [[1, 2, 0.8, 3, 0.8]] * 2 + [[1, 2, 1.5]] * 2
        steps = [40 / 300, 100 / 300, 120 / 300, 150 / 300, 180 / 300, 250 / 300]
        num_layers = len(features) + len(channels)

        sizes = list(zip(sizes[:-1], sizes[1:]))
        self._num_layers = num_layers
        self.classes = classes
        self.nms_thresh = nms_thresh
        self.nms_topk = nms_topk
        self.post_nms = post_nms

        with self.name_scope():
            self.features = FeatureExpander(network=network, outputs=features, num_filters=channels,
                                            use_1x1_transition=use_1x1_transition,
                                            use_bn=use_bn, reduce_ratio=reduce_ratio, min_depth=min_depth,
                                            global_pool=True, pretrained=pretrained, ctx=ctx,
                                            norm_layer=norm_layer, norm_kwargs=norm_kwargs)

            self.class_predictors = nn.HybridSequential()
            self.box_predictors = nn.HybridSequential()
            self.anchor_generators = nn.HybridSequential()
            asz = anchor_alloc_size
            for i, s, r, st in zip(range(num_layers), sizes, ratios, steps):
                anchor_generator = SSDAnchorGenerator(i, im_size, s, r, st, (asz, asz))
                self.anchor_generators.add(anchor_generator)
                asz = max(asz // 2, 16)                     # pre-compute larger than 16x16 anchor map
                num_anchors = anchor_generator.num_depth
                self.class_predictors.add(ConvPredictor(num_anchors * (len(self.classes) + 1)))
                self.box_predictors.add(ConvPredictor(num_anchors * 4))
            self.bbox_decoder = NormalizedBoxCenterDecoder(stds)
            self.cls_decoder = MultiPerClassDecoder(len(self.classes) + 1, thresh=0.01)
Exemple #3
0
    def __init__(self,
                 network,
                 base_size,
                 features,
                 ssd_filters,
                 sizes,
                 ratios,
                 steps,
                 classes,
                 use_1x1_transition=True,
                 use_bn=True,
                 reduce_ratio=1.0,
                 min_depth=128,
                 global_pool=False,
                 pretrained=False,
                 stds=(0.1, 0.1, 0.2, 0.2),
                 nms_thresh=0.3,
                 nms_topk=10000,
                 post_nms=3000,
                 anchor_alloc_size=640,
                 ctx=mx.cpu(),
                 norm_layer=SyncBatchNorm,
                 norm_kwargs=None,
                 is_fpn=False,
                 fpn_filters=256,
                 is_multitask=False,
                 use_pose=False,
                 use_keypoints=False,
                 num_keypoints=1,
                 use_embedding=False,
                 embedding_dim=128,
                 return_intermediate_features=False,
                 use_mish=False,
                 **kwargs):
        super(SSD, self).__init__(**kwargs)
        if norm_kwargs is None:
            norm_kwargs = {}
        if network is None:
            num_layers = len(ratios)
        else:
            num_layers = len(features) + len(ssd_filters) + int(global_pool)
        assert len(sizes) == len(ratios)
        assert isinstance(ratios,
                          list), "Must provide ratios as list or list of list"
        if not isinstance(ratios[0], (tuple, list)):
            ratios = ratios * num_layers  # propagate to all layers if use same ratio
        assert num_layers == len(sizes) == len(ratios), \
            "Mismatched (number of layers) vs (sizes) vs (ratios): {}, {}, {}".format(
                num_layers, len(sizes), len(ratios))
        assert num_layers > 0, "SSD require at least one layer, suggest multiple."

        self.nms_thresh = nms_thresh
        self.nms_topk = nms_topk
        self.post_nms = post_nms

        self._two_phase_run = return_intermediate_features

        with self.name_scope():
            if network is None:
                # use fine-grained manually designed block as features
                try:
                    self.features = features(pretrained=pretrained,
                                             ctx=ctx,
                                             norm_layer=norm_layer,
                                             norm_kwargs=norm_kwargs)
                except TypeError:
                    self.features = features(pretrained=pretrained, ctx=ctx)
            else:
                if is_fpn:
                    fpn_filters_list = [fpn_filters for i in range(num_layers)]
                    try:
                        self.features = FPNFeatureExpander(
                            network=network,
                            outputs=features,
                            ssd_filters=ssd_filters,
                            fpn_filters=fpn_filters_list,
                            use_bn=use_bn,
                            reduce_ratio=reduce_ratio,
                            min_depth=min_depth,
                            global_pool=global_pool,
                            pretrained=pretrained,
                            ctx=ctx,
                            norm_layer=norm_layer,
                            norm_kwargs=norm_kwargs,
                            use_mish=use_mish)

                    except TypeError:
                        self.features = FPNFeatureExpander(
                            network=network,
                            outputs=features,
                            ssd_filters=ssd_filters,
                            fpn_filters=fpn_filters_list,
                            use_bn=use_bn,
                            reduce_ratio=reduce_ratio,
                            min_depth=min_depth,
                            global_pool=global_pool,
                            pretrained=pretrained,
                            ctx=ctx,
                            use_mish=use_mish)
                else:
                    try:
                        self.features = FeatureExpander(
                            network=network,
                            outputs=features,
                            num_filters=ssd_filters,
                            use_1x1_transition=use_1x1_transition,
                            use_bn=use_bn,
                            reduce_ratio=reduce_ratio,
                            min_depth=min_depth,
                            global_pool=global_pool,
                            pretrained=pretrained,
                            ctx=ctx,
                            norm_layer=norm_layer,
                            norm_kwargs=norm_kwargs)
                    except TypeError:
                        self.features = FeatureExpander(
                            network=network,
                            outputs=features,
                            num_filters=ssd_filters,
                            use_1x1_transition=use_1x1_transition,
                            use_bn=use_bn,
                            reduce_ratio=reduce_ratio,
                            min_depth=min_depth,
                            global_pool=global_pool,
                            pretrained=pretrained,
                            ctx=ctx)

            self.detector_head = SSDDetectorHead(
                num_layers, base_size, sizes, ratios, steps, classes, stds,
                nms_thresh, nms_topk, post_nms, anchor_alloc_size,
                is_multitask, use_pose, use_keypoints, num_keypoints,
                use_embedding, embedding_dim, return_intermediate_features,
                **kwargs)