def __init__(self, phase='train', num_classes=2):
        super(SSD_MobileNetV3, self).__init__()

        self.phase = phase
        self.num_classes = num_classes
        self.priors = Variable(priorbox('mobilenetv3'), volatile=True)

        self.conv_layer1 = nn.Sequential(
            nn.Conv2d(3, 16, 3, 2, 1, bias=False),
            nn.BatchNorm2d(16),
            HardSwish())

        '''
        input = 16
        kernel, expansion output_channel  se     nl  stride
          3        16           16        True   'RE'   2
          3        72           24        False  'RE'   2
          3        88           24        False  'RE'   1
          5        96           40        True   'HS'   2
          5       240           40        True   'HS'   1
          5       240           40        True   'HS'   1
          5       120           48        True   'HS'   1
          5       144           48        True   'HS'   1
          5       288           96        True   'HS'   2
          5       576           96        True   'HS'   1
          5       576           96        True   'HS'   1
        '''

        self.invertedresidual_layer1 = nn.Sequential(
            nn.Conv2d(16, 16, 3, 2, 1, groups=16, bias=False),
            nn.BatchNorm2d(16),
            nn.ReLU6(inplace=True),
            SqueezeAndExcite(16, reduction=4),
            nn.Conv2d(16, 16, 1, 1, 0, bias=False),
            nn.BatchNorm2d(16)
        )

        self.invertedresidual_layer2 = nn.Sequential(
            nn.Conv2d(16, 72, 1, 1, 0, bias=False),
            nn.BatchNorm2d(72),
            nn.ReLU6(inplace=True),
            nn.Conv2d(72, 72, 3, 2, 1, groups=72, bias=False),
            nn.BatchNorm2d(72),
            nn.ReLU6(inplace=True),
            nn.Conv2d(72, 24, 1, 1, 0, bias=False),
            nn.BatchNorm2d(24)
        )

        # Special
        self.invertedresidual_layer3 = nn.Sequential(
            nn.Conv2d(24, 88, 1, 1, 0, bias=False),
            nn.BatchNorm2d(88),
            nn.ReLU6(inplace=True),
            nn.Conv2d(88, 88, 3, 1, 1, groups=88, bias=False),
            nn.BatchNorm2d(88),
            nn.Conv2d(88, 24, 1, 1, 0, bias=False),
            nn.BatchNorm2d(24)
        )

        self.invertedresidual_layer4 = nn.Sequential(
            nn.Conv2d(24, 96, 1, 1, 0, bias=False),
            nn.BatchNorm2d(96),
            HardSwish(),
            nn.Conv2d(96, 96, 5, 2, 2, groups=96, bias=False),
            nn.BatchNorm2d(96),
            HardSwish(),
            SqueezeAndExcite(96, reduction=4),
            nn.Conv2d(96, 40, 1, 1, 0, bias=False),
            nn.BatchNorm2d(40)
        )

        # Special
        self.invertedresidual_layer5 = nn.Sequential(
            nn.Conv2d(40, 240, 1, 1, 0, bias=False),
            nn.BatchNorm2d(240),
            HardSwish(),
            nn.Conv2d(240, 240, 5, 1, 2, groups=240, bias=False),
            nn.BatchNorm2d(240),
            HardSwish(),
            SqueezeAndExcite(240, reduction=4),
            nn.Conv2d(240, 40, 1, 1, 0, bias=False),
            nn.BatchNorm2d(40)
        )

        # Special
        self.invertedresidual_layer6 = nn.Sequential(
            nn.Conv2d(40, 240, 1, 1, 0, bias=False),
            nn.BatchNorm2d(240),
            HardSwish(),
            nn.Conv2d(240, 240, 5, 1, 2, groups=240, bias=False),
            nn.BatchNorm2d(240),
            HardSwish(),
            SqueezeAndExcite(240, reduction=4),
            nn.Conv2d(240, 40, 1, 1, 0, bias=False),
            nn.BatchNorm2d(40)
        )

        self.invertedresidual_layer7 = nn.Sequential(
            nn.Conv2d(40, 120, 1, 1, 0, bias=False),
            nn.BatchNorm2d(120),
            HardSwish(),
            nn.Conv2d(120, 120, 5, 1, 2, groups=120, bias=False),
            nn.BatchNorm2d(120),
            HardSwish(),
            SqueezeAndExcite(120, reduction=4),
            nn.Conv2d(120, 48, 1, 1, 0, bias=False),
            nn.BatchNorm2d(48)
        )

        # Special
        self.invertedresidual_layer8 = nn.Sequential(
            nn.Conv2d(48, 144, 1, 1, 0, bias=False),
            nn.BatchNorm2d(144),
            HardSwish(),
            nn.Conv2d(144, 144, 5, 1, 2, groups=144, bias=False),
            nn.BatchNorm2d(144),
            HardSwish(),
            SqueezeAndExcite(144, reduction=4),
            nn.Conv2d(144, 48, 1, 1, 0, bias=False),
            nn.BatchNorm2d(48)
        )

        self.regression_layer1 = nn.Sequential(nn.Conv2d(48, 48,
                                                         kernel_size=3,
                                                         groups=48, stride=1, padding=1),
                                               nn.BatchNorm2d(48),
                                               nn.ReLU(),
                                               nn.Conv2d(48, 6 * 4,
                                                         kernel_size=1))

        self.confidence_layer1 = nn.Sequential(nn.Conv2d(48, 48,
                                                         kernel_size=3,
                                                         groups=48, stride=1, padding=1),
                                               nn.BatchNorm2d(48),
                                               nn.ReLU(),
                                               nn.Conv2d(48, 6 * self.num_classes,
                                                         kernel_size=1))

        self.invertedresidual_layer9 = nn.Sequential(
            nn.Conv2d(48, 288, 1, 1, 0, bias=False),
            nn.BatchNorm2d(288),
            HardSwish(),
            nn.Conv2d(288, 288, 5, 2, 2, groups=288, bias=False),
            nn.BatchNorm2d(288),
            HardSwish(),
            SqueezeAndExcite(288, reduction=4),
            nn.Conv2d(288, 96, 1, 1, 0, bias=False),
            nn.BatchNorm2d(96)
        )

        # Special
        self.invertedresidual_layer10 = nn.Sequential(
            nn.Conv2d(96, 576, 1, 1, 0, bias=False),
            nn.BatchNorm2d(576),
            HardSwish(),
            nn.Conv2d(576, 576, 5, 1, 2, groups=576, bias=False),
            nn.BatchNorm2d(576),
            HardSwish(),
            SqueezeAndExcite(576, reduction=4),
            nn.Conv2d(576, 96, 1, 1, 0, bias=False),
            nn.BatchNorm2d(96)
        )

        # Special
        self.invertedresidual_layer11 = nn.Sequential(
            nn.Conv2d(96, 576, 1, 1, 0, bias=False),
            nn.BatchNorm2d(576),
            HardSwish(),
            nn.Conv2d(576, 576, 5, 1, 2, groups=576, bias=False),
            nn.BatchNorm2d(576),
            HardSwish(),
            SqueezeAndExcite(576, reduction=4),
            nn.Conv2d(576, 96, 1, 1, 0, bias=False),
            nn.BatchNorm2d(96)
        )

        self.conv_layer2 = nn.Sequential(
            nn.Conv2d(96, 576, 1, 1, 0, bias=False),
            SqueezeAndExcite(576, reduction=4),
            nn.BatchNorm2d(576),
            HardSwish()
        )

        self.conv_layer3 = nn.Sequential(
            nn.Conv2d(576, 1280, 1, 1, 0, bias=False),
            SqueezeAndExcite(1280, reduction=4),
            nn.BatchNorm2d(1280),
            HardSwish()
        )

        self.regression_layer2 = nn.Sequential(nn.Conv2d(1280, 1280,
                                                         kernel_size=3,
                                                         groups=1280, stride=1, padding=1),
                                               nn.BatchNorm2d(1280),
                                               nn.ReLU(),
                                               nn.Conv2d(1280, 6 * 4,
                                                         kernel_size=1))

        self.confidence_layer2 = nn.Sequential(nn.Conv2d(1280, 1280,
                                                         kernel_size=3,
                                                         groups=1280, stride=1, padding=1),
                                               nn.BatchNorm2d(1280),
                                               nn.ReLU(),
                                               nn.Conv2d(1280, 6 * self.num_classes,
                                                         kernel_size=1))

        self.extra1 = nn.Sequential(nn.Conv2d(1280, 256, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(256),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(256, 256, 3, 2, 1, groups=256, bias=False),
                                    nn.BatchNorm2d(256),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(256, 512, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(512))

        self.regression_layer3 = nn.Sequential(nn.Conv2d(512, 512,
                                                         kernel_size=3,
                                                         groups=512, stride=1, padding=1),
                                               nn.BatchNorm2d(512),
                                               nn.ReLU(),
                                               nn.Conv2d(512, 6 * 4,
                                                         kernel_size=1))

        self.confidence_layer3 = nn.Sequential(nn.Conv2d(512, 512,
                                                         kernel_size=3,
                                                         groups=512, stride=1, padding=1),
                                               nn.BatchNorm2d(512),
                                               nn.ReLU(),
                                               nn.Conv2d(512, 6 * self.num_classes,
                                                         kernel_size=1))

        self.extra2 = nn.Sequential(nn.Conv2d(512, 128, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(128),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(128, 128, 3, 2, 1, groups=128, bias=False),
                                    nn.BatchNorm2d(128),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(128, 256, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(256))

        self.regression_layer4 = nn.Sequential(nn.Conv2d(256, 256,
                                                         kernel_size=3,
                                                         groups=256, stride=1, padding=1),
                                               nn.BatchNorm2d(256),
                                               nn.ReLU(),
                                               nn.Conv2d(256, 4 * 4,
                                                         kernel_size=1))

        self.confidence_layer4 = nn.Sequential(nn.Conv2d(256, 256,
                                                         kernel_size=3,
                                                         groups=256, stride=1, padding=1),
                                               nn.BatchNorm2d(256),
                                               nn.ReLU(),
                                               nn.Conv2d(256, 4 * self.num_classes,
                                                         kernel_size=1))

        self.extra3 = nn.Sequential(nn.Conv2d(256, 128, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(128),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(128, 128, 3, 2, 1, groups=128, bias=False),
                                    nn.BatchNorm2d(128),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(128, 256, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(256))

        self.regression_layer5 = nn.Sequential(nn.Conv2d(256, 256,
                                                         kernel_size=3,
                                                         groups=256, stride=1, padding=1),
                                               nn.BatchNorm2d(256),
                                               nn.ReLU(),
                                               nn.Conv2d(256, 4 * 4,
                                                         kernel_size=1))

        self.confidence_layer5 = nn.Sequential(nn.Conv2d(256, 256,
                                                         kernel_size=3,
                                                         groups=256, stride=1, padding=1),
                                               nn.BatchNorm2d(256),
                                               nn.ReLU(),
                                               nn.Conv2d(256, 4 * self.num_classes,
                                                         kernel_size=1))

        self.extra4 = nn.Sequential(nn.Conv2d(256, 64, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(64),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(64, 64, 3, 2, 1, groups=64, bias=False),
                                    nn.BatchNorm2d(64),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(64, 64, 1, 1, 0, bias=False),
                                    nn.BatchNorm2d(64))

        self.regression_layer6 = nn.Sequential(nn.Conv2d(64, 64,
                                                         kernel_size=3,
                                                         groups=64, stride=1, padding=1),
                                               nn.BatchNorm2d(64),
                                               nn.ReLU(),
                                               nn.Conv2d(64, 4 * 4,
                                                         kernel_size=1))

        self.confidence_layer6 = nn.Sequential(nn.Conv2d(64, 64,
                                                         kernel_size=3,
                                                         groups=64, stride=1, padding=1),
                                               nn.BatchNorm2d(64),
                                               nn.ReLU(),
                                               nn.Conv2d(64, 4 * self.num_classes,
                                                         kernel_size=1))

        if phase == 'test':
            self.softmax = nn.Softmax()
            self.inference_layer = InferenceLayer(top_k=200, conf_thresh=0.01, nms_thresh=0.45)
Example #2
0
    def __init__(self, phase='train', num_classes=21):
        super(SSD_VGG, self).__init__()

        self.phase = phase
        self.num_classes = num_classes
        self.priors = Variable(priorbox('vgg'), volatile=True)
        self.conv_layer1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=3, padding=1), nn.ReLU(inplace=True))

        self.conv_layer2 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, padding=1), nn.ReLU(inplace=True))

        self.maxpool_layer1 = nn.MaxPool2d(kernel_size=2, stride=2)

        self.conv_layer3 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.conv_layer4 = nn.Sequential(
            nn.Conv2d(128, 128, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.maxpool_layer2 = nn.MaxPool2d(kernel_size=2, stride=2)

        self.conv_layer5 = nn.Sequential(
            nn.Conv2d(128, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.conv_layer6 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.conv_layer7 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.maxpool_layer3 = nn.MaxPool2d(kernel_size=2,
                                           stride=2,
                                           ceil_mode=True)

        self.conv_layer8 = nn.Sequential(
            nn.Conv2d(256, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.conv_layer9 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.conv_layer10 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.l2_norm_scale = L2NormScale(512, 20)

        self.maxpool_layer4 = nn.MaxPool2d(kernel_size=2, stride=2)

        self.conv_layer11 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.conv_layer12 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.conv_layer13 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, padding=1),
            nn.ReLU(inplace=True))

        self.maxpool_layer5 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1)

        self.conv_layer14 = nn.Sequential(
            nn.Conv2d(512, 1024, kernel_size=3, padding=6, dilation=6),
            nn.ReLU(inplace=True))

        self.conv_layer15 = nn.Sequential(nn.Conv2d(1024, 1024, kernel_size=1),
                                          nn.ReLU(inplace=True))

        self.loc0 = nn.Conv2d(512, 4 * 4, kernel_size=3, padding=1)
        self.class0 = nn.Conv2d(512, 4 * num_classes, kernel_size=3, padding=1)

        self.loc1 = nn.Conv2d(1024, 6 * 4, kernel_size=3, padding=1)
        self.class1 = nn.Conv2d(1024,
                                6 * num_classes,
                                kernel_size=3,
                                padding=1)

        self.extra2 = nn.Sequential(
            nn.Conv2d(1024, 256, kernel_size=1), nn.ReLU(inplace=True),
            nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1),
            nn.ReLU(inplace=True))
        self.loc2 = nn.Conv2d(512, 6 * 4, kernel_size=3, padding=1)
        self.class2 = nn.Conv2d(512, 6 * num_classes, kernel_size=3, padding=1)

        self.extra3 = nn.Sequential(
            nn.Conv2d(512, 128, kernel_size=1), nn.ReLU(inplace=True),
            nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1),
            nn.ReLU(inplace=True))
        self.loc3 = nn.Conv2d(256, 6 * 4, kernel_size=3, padding=1)
        self.class3 = nn.Conv2d(256, 6 * num_classes, kernel_size=3, padding=1)

        self.extra4 = nn.Sequential(nn.Conv2d(256, 128, kernel_size=1),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(128, 256, kernel_size=3),
                                    nn.ReLU(inplace=True))
        self.loc4 = nn.Conv2d(256, 4 * 4, kernel_size=3, padding=1)
        self.class4 = nn.Conv2d(256, 4 * num_classes, kernel_size=3, padding=1)

        self.extra5 = nn.Sequential(nn.Conv2d(256, 128, kernel_size=1),
                                    nn.ReLU(inplace=True),
                                    nn.Conv2d(128, 256, kernel_size=3),
                                    nn.ReLU(inplace=True))
        self.loc5 = nn.Conv2d(256, 4 * 4, kernel_size=3, padding=1)
        self.class5 = nn.Conv2d(256, 4 * num_classes, kernel_size=3, padding=1)

        if phase == 'test':
            self.softmax = nn.Softmax()
            self.inference_layer = InferenceLayer(top_k=200,
                                                  conf_thresh=0.01,
                                                  nms_thresh=0.45)
    def __init__(self,
                 num_classes,
                 phase='train',
                 blocks_args=None,
                 global_params=None):
        super().__init__()
        assert isinstance(blocks_args, list), 'blocks_args should be a list'
        assert len(blocks_args) > 0, 'block args must be greater than 0'
        self._global_params = global_params
        self._blocks_args = blocks_args

        self.phase = phase
        self.num_classes = num_classes
        self.priors = Variable(priorbox('efficientnet'), volatile=True)
        bn_mom = 1 - self._global_params.batch_norm_momentum
        bn_eps = self._global_params.batch_norm_epsilon

        in_channels = 3
        out_channels = round_filters(32, self._global_params)
        self._conv_stem = Conv2dSamePadding(in_channels,
                                            out_channels,
                                            kernel_size=3,
                                            stride=2,
                                            bias=False)
        self._bn0 = nn.BatchNorm2d(num_features=out_channels,
                                   momentum=bn_mom,
                                   eps=bn_eps)

        self._blocks = nn.ModuleList([])
        for block_args in self._blocks_args:

            block_args = block_args._replace(
                input_filters=round_filters(block_args.input_filters,
                                            self._global_params),
                output_filters=round_filters(block_args.output_filters,
                                             self._global_params),
                num_repeat=round_repeats(block_args.num_repeat,
                                         self._global_params))

            self._blocks.append(MBConvBlock(block_args, self._global_params))
            if block_args.num_repeat > 1:
                block_args = block_args._replace(
                    input_filters=block_args.output_filters, stride=1)
            for _ in range(block_args.num_repeat - 1):
                self._blocks.append(
                    MBConvBlock(block_args, self._global_params))

        in_channels = block_args.output_filters  # output of final block
        out_channels = round_filters(1280, self._global_params)
        self._conv_head = Conv2dSamePadding(in_channels,
                                            out_channels,
                                            kernel_size=1,
                                            bias=False)
        self._bn1 = nn.BatchNorm2d(num_features=out_channels,
                                   momentum=bn_mom,
                                   eps=bn_eps)

        self.regression_layer1 = nn.Sequential(
            nn.Conv2d(160, 160, kernel_size=3, groups=160, stride=1,
                      padding=1), nn.BatchNorm2d(160), nn.ReLU(),
            nn.Conv2d(160, 6 * 4, kernel_size=1))

        self.confidence_layer1 = nn.Sequential(
            nn.Conv2d(160, 160, kernel_size=3, groups=160, stride=1,
                      padding=1), nn.BatchNorm2d(160), nn.ReLU(),
            nn.Conv2d(160, 6 * self.num_classes, kernel_size=1))

        self.regression_layer2 = nn.Sequential(
            nn.Conv2d(1792,
                      1792,
                      kernel_size=3,
                      groups=1792,
                      stride=1,
                      padding=1), nn.BatchNorm2d(1792), nn.ReLU(),
            nn.Conv2d(1792, 6 * 4, kernel_size=1))

        self.confidence_layer2 = nn.Sequential(
            nn.Conv2d(1792,
                      1792,
                      kernel_size=3,
                      groups=1792,
                      stride=1,
                      padding=1), nn.BatchNorm2d(1792), nn.ReLU(),
            nn.Conv2d(1792, 6 * self.num_classes, kernel_size=1))

        self.regression_layer3 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, groups=512, stride=1,
                      padding=1), nn.BatchNorm2d(512), nn.ReLU(),
            nn.Conv2d(512, 6 * 4, kernel_size=1))

        self.confidence_layer3 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, groups=512, stride=1,
                      padding=1), nn.BatchNorm2d(512), nn.ReLU(),
            nn.Conv2d(512, 6 * self.num_classes, kernel_size=1))

        self.regression_layer4 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * 4, kernel_size=1))

        self.confidence_layer4 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * self.num_classes, kernel_size=1))

        self.regression_layer5 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * 4, kernel_size=1))

        self.confidence_layer5 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * self.num_classes, kernel_size=1))

        self.regression_layer6 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, groups=64, stride=1, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 4 * 4, kernel_size=1))

        self.confidence_layer6 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, groups=64, stride=1, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(),
            nn.Conv2d(64, 4 * self.num_classes, kernel_size=1))

        self.extra1 = nn.Sequential(
            nn.Conv2d(1792, 256, 1, 1, 0, bias=False), nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, 3, 2, 1, groups=256, bias=False),
            nn.BatchNorm2d(256), nn.ReLU(inplace=True),
            nn.Conv2d(256, 512, 1, 1, 0, bias=False), nn.BatchNorm2d(512))

        self.extra2 = nn.Sequential(
            nn.Conv2d(512, 128, 1, 1, 0, bias=False), nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, 3, 2, 1, groups=128, bias=False),
            nn.BatchNorm2d(128), nn.ReLU(inplace=True),
            nn.Conv2d(128, 256, 1, 1, 0, bias=False), nn.BatchNorm2d(256))

        self.extra3 = nn.Sequential(
            nn.Conv2d(256, 128, 1, 1, 0, bias=False), nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, 3, 2, 1, groups=128, bias=False),
            nn.BatchNorm2d(128), nn.ReLU(inplace=True),
            nn.Conv2d(128, 256, 1, 1, 0, bias=False), nn.BatchNorm2d(256))

        self.extra4 = nn.Sequential(
            nn.Conv2d(256, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, 3, 2, 1, groups=64, bias=False),
            nn.BatchNorm2d(64), nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64))

        if self.phase == 'test':
            self.softmax = nn.Softmax()
            self.inference_layer = InferenceLayer(top_k=200,
                                                  conf_thresh=0.01,
                                                  nms_thresh=0.45)
Example #4
0
    def __init__(self, phase='train', num_classes=2):
        super(SSD_MobileNetV2, self).__init__()

        self.phase = phase
        self.num_classes = num_classes
        self.priors = Variable(priorbox('mobilenetv2'), volatile=True)

        self.conv_layer1 = nn.Sequential(nn.Conv2d(3, 32, 3, 2, 1, bias=False),
                                         nn.BatchNorm2d(32),
                                         nn.ReLU(inplace=True))

        self.invertedresidual_layer1 = nn.Sequential(
            nn.Conv2d(32, 32, 3, 1, 1, groups=32, bias=False),
            nn.BatchNorm2d(32), nn.ReLU(inplace=True),
            nn.Conv2d(32, 16, 1, 1, 0, bias=False), nn.BatchNorm2d(16))

        self.invertedresidual_layer2 = nn.Sequential(
            nn.Conv2d(16, 96, 1, 1, 0, bias=False), nn.BatchNorm2d(96),
            nn.ReLU(inplace=True),
            nn.Conv2d(96, 96, 3, 2, 1, groups=96, bias=False),
            nn.BatchNorm2d(96), nn.ReLU(inplace=True),
            nn.Conv2d(96, 24, 1, 1, 0, bias=False), nn.BatchNorm2d(24))

        self.invertedresidual_layer3 = nn.Sequential(
            nn.Conv2d(24, 144, 1, 1, 0, bias=False), nn.BatchNorm2d(144),
            nn.ReLU(inplace=True),
            nn.Conv2d(144, 144, 3, 1, 1, groups=144, bias=False),
            nn.BatchNorm2d(144), nn.ReLU(inplace=True),
            nn.Conv2d(144, 24, 1, 1, 0, bias=False), nn.BatchNorm2d(24))

        self.invertedresidual_layer4 = nn.Sequential(
            nn.Conv2d(24, 144, 1, 1, 0, bias=False), nn.BatchNorm2d(144),
            nn.ReLU(inplace=True),
            nn.Conv2d(144, 144, 3, 2, 1, groups=144, bias=False),
            nn.BatchNorm2d(144), nn.ReLU(inplace=True),
            nn.Conv2d(144, 32, 1, 1, 0, bias=False), nn.BatchNorm2d(32))

        self.invertedresidual_layer5 = nn.Sequential(
            nn.Conv2d(32, 192, 1, 1, 0, bias=False), nn.BatchNorm2d(192),
            nn.ReLU(inplace=True),
            nn.Conv2d(192, 192, 3, 1, 1, groups=192, bias=False),
            nn.BatchNorm2d(192), nn.ReLU(inplace=True),
            nn.Conv2d(192, 32, 1, 1, 0, bias=False), nn.BatchNorm2d(32))

        self.invertedresidual_layer6 = nn.Sequential(
            nn.Conv2d(32, 192, 1, 1, 0, bias=False), nn.BatchNorm2d(192),
            nn.ReLU(inplace=True),
            nn.Conv2d(192, 192, 3, 1, 1, groups=192, bias=False),
            nn.BatchNorm2d(192), nn.ReLU(inplace=True),
            nn.Conv2d(192, 32, 1, 1, 0, bias=False), nn.BatchNorm2d(32))

        self.invertedresidual_layer7 = nn.Sequential(
            nn.Conv2d(32, 192, 1, 1, 0, bias=False), nn.BatchNorm2d(192),
            nn.ReLU(inplace=True),
            nn.Conv2d(192, 192, 3, 2, 1, groups=192, bias=False),
            nn.BatchNorm2d(192), nn.ReLU(inplace=True),
            nn.Conv2d(192, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64))

        self.invertedresidual_layer8 = nn.Sequential(
            nn.Conv2d(64, 384, 1, 1, 0, bias=False), nn.BatchNorm2d(384),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, 3, 1, 1, groups=384, bias=False),
            nn.BatchNorm2d(384), nn.ReLU(inplace=True),
            nn.Conv2d(384, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64))

        self.invertedresidual_layer9 = nn.Sequential(
            nn.Conv2d(64, 384, 1, 1, 0, bias=False), nn.BatchNorm2d(384),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, 3, 1, 1, groups=384, bias=False),
            nn.BatchNorm2d(384), nn.ReLU(inplace=True),
            nn.Conv2d(384, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64))

        self.invertedresidual_layer10 = nn.Sequential(
            nn.Conv2d(64, 384, 1, 1, 0, bias=False), nn.BatchNorm2d(384),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, 3, 1, 1, groups=384, bias=False),
            nn.BatchNorm2d(384), nn.ReLU(inplace=True),
            nn.Conv2d(384, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64))

        self.invertedresidual_layer11 = nn.Sequential(
            nn.Conv2d(64, 384, 1, 1, 0, bias=False), nn.BatchNorm2d(384),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 384, 3, 1, 1, groups=384, bias=False),
            nn.BatchNorm2d(384), nn.ReLU(inplace=True),
            nn.Conv2d(384, 96, 1, 1, 0, bias=False), nn.BatchNorm2d(96))

        self.invertedresidual_layer12 = nn.Sequential(
            nn.Conv2d(96, 576, 1, 1, 0, bias=False), nn.BatchNorm2d(576),
            nn.ReLU(inplace=True),
            nn.Conv2d(576, 576, 3, 1, 1, groups=576, bias=False),
            nn.BatchNorm2d(576), nn.ReLU(inplace=True),
            nn.Conv2d(576, 96, 1, 1, 0, bias=False), nn.BatchNorm2d(96))

        self.invertedresidual_layer13 = nn.Sequential(
            nn.Conv2d(96, 576, 1, 1, 0, bias=False), nn.BatchNorm2d(576),
            nn.ReLU(inplace=True),
            nn.Conv2d(576, 576, 3, 1, 1, groups=576, bias=False),
            nn.BatchNorm2d(576), nn.ReLU(inplace=True),
            nn.Conv2d(576, 96, 1, 1, 0, bias=False), nn.BatchNorm2d(96))

        self.regression_layer1 = nn.Sequential(
            nn.Conv2d(96, 96, kernel_size=3, groups=96, stride=1, padding=1),
            nn.BatchNorm2d(96), nn.ReLU(), nn.Conv2d(96, 6 * 4, kernel_size=1))

        self.confidence_layer1 = nn.Sequential(
            nn.Conv2d(96, 96, kernel_size=3, groups=96, stride=1, padding=1),
            nn.BatchNorm2d(96), nn.ReLU(),
            nn.Conv2d(96, 6 * self.num_classes, kernel_size=1))

        self.invertedresidual_layer14 = nn.Sequential(
            nn.Conv2d(96, 576, 1, 1, 0, bias=False), nn.BatchNorm2d(576),
            nn.ReLU(inplace=True),
            nn.Conv2d(576, 576, 3, 2, 1, groups=576, bias=False),
            nn.BatchNorm2d(576), nn.ReLU(inplace=True),
            nn.Conv2d(576, 160, 1, 1, 0, bias=False), nn.BatchNorm2d(160))

        self.invertedresidual_layer15 = nn.Sequential(
            nn.Conv2d(160, 960, 1, 1, 0, bias=False), nn.BatchNorm2d(960),
            nn.ReLU(inplace=True),
            nn.Conv2d(960, 960, 3, 1, 1, groups=960, bias=False),
            nn.BatchNorm2d(960), nn.ReLU(inplace=True),
            nn.Conv2d(960, 160, 1, 1, 0, bias=False), nn.BatchNorm2d(160))

        self.invertedresidual_layer16 = nn.Sequential(
            nn.Conv2d(160, 960, 1, 1, 0, bias=False), nn.BatchNorm2d(960),
            nn.ReLU(inplace=True),
            nn.Conv2d(960, 960, 3, 1, 1, groups=960, bias=False),
            nn.BatchNorm2d(960), nn.ReLU(inplace=True),
            nn.Conv2d(960, 160, 1, 1, 0, bias=False), nn.BatchNorm2d(160))

        self.invertedresidual_layer17 = nn.Sequential(
            nn.Conv2d(160, 960, 1, 1, 0, bias=False), nn.BatchNorm2d(960),
            nn.ReLU(inplace=True),
            nn.Conv2d(960, 960, 3, 1, 1, groups=960, bias=False),
            nn.BatchNorm2d(960), nn.ReLU(inplace=True),
            nn.Conv2d(960, 320, 1, 1, 0, bias=False), nn.BatchNorm2d(320))

        self.conv_layer2 = nn.Sequential(
            nn.Conv2d(320, 1280, 1, 1, 0, bias=False), nn.BatchNorm2d(1280),
            nn.ReLU(inplace=True))

        self.regression_layer2 = nn.Sequential(
            nn.Conv2d(1280,
                      1280,
                      kernel_size=3,
                      groups=1280,
                      stride=1,
                      padding=1), nn.BatchNorm2d(1280), nn.ReLU(),
            nn.Conv2d(1280, 6 * 4, kernel_size=1))

        self.confidence_layer2 = nn.Sequential(
            nn.Conv2d(1280,
                      1280,
                      kernel_size=3,
                      groups=1280,
                      stride=1,
                      padding=1), nn.BatchNorm2d(1280), nn.ReLU(),
            nn.Conv2d(1280, 6 * self.num_classes, kernel_size=1))

        self.extra1 = nn.Sequential(
            nn.Conv2d(1280, 256, 1, 1, 0, bias=False), nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, 3, 2, 1, groups=256, bias=False),
            nn.BatchNorm2d(256), nn.ReLU(inplace=True),
            nn.Conv2d(256, 512, 1, 1, 0, bias=False), nn.BatchNorm2d(512))

        self.regression_layer3 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, groups=512, stride=1,
                      padding=1), nn.BatchNorm2d(512), nn.ReLU(),
            nn.Conv2d(512, 6 * 4, kernel_size=1))

        self.confidence_layer3 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=3, groups=512, stride=1,
                      padding=1), nn.BatchNorm2d(512), nn.ReLU(),
            nn.Conv2d(512, 6 * self.num_classes, kernel_size=1))

        self.extra2 = nn.Sequential(
            nn.Conv2d(512, 128, 1, 1, 0, bias=False), nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, 3, 2, 1, groups=128, bias=False),
            nn.BatchNorm2d(128), nn.ReLU(inplace=True),
            nn.Conv2d(128, 256, 1, 1, 0, bias=False), nn.BatchNorm2d(256))

        self.regression_layer4 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * 4, kernel_size=1))

        self.confidence_layer4 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * self.num_classes, kernel_size=1))

        self.extra3 = nn.Sequential(
            nn.Conv2d(256, 128, 1, 1, 0, bias=False), nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, 3, 2, 1, groups=128, bias=False),
            nn.BatchNorm2d(128), nn.ReLU(inplace=True),
            nn.Conv2d(128, 256, 1, 1, 0, bias=False), nn.BatchNorm2d(256))

        self.regression_layer5 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * 4, kernel_size=1))

        self.confidence_layer5 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, groups=256, stride=1,
                      padding=1), nn.BatchNorm2d(256), nn.ReLU(),
            nn.Conv2d(256, 4 * self.num_classes, kernel_size=1))

        self.extra4 = nn.Sequential(
            nn.Conv2d(256, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, 3, 2, 1, groups=64, bias=False),
            nn.BatchNorm2d(64), nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, 1, 1, 0, bias=False), nn.BatchNorm2d(64))

        self.regression_layer6 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, groups=64, stride=1, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 4 * 4, kernel_size=1))

        self.confidence_layer6 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, groups=64, stride=1, padding=1),
            nn.BatchNorm2d(64), nn.ReLU(),
            nn.Conv2d(64, 4 * self.num_classes, kernel_size=1))

        if phase == 'test':
            self.softmax = nn.Softmax()
            self.inference_layer = InferenceLayer(top_k=200,
                                                  conf_thresh=0.01,
                                                  nms_thresh=0.45)