Example #1
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 cardinality,
                 bottleneck_width,
                 data_format="channels_last",
                 **kwargs):
        super(SEResNeXtUnit, self).__init__(**kwargs)
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        self.body = ResNeXtBottleneck(
            in_channels=in_channels,
            out_channels=out_channels,
            strides=strides,
            cardinality=cardinality,
            bottleneck_width=bottleneck_width,
            data_format=data_format,
            name="body")
        self.se = SEBlock(
            channels=out_channels,
            data_format=data_format,
            name="se")
        if self.resize_identity:
            self.identity_conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels,
                strides=strides,
                activation=None,
                data_format=data_format,
                name="identity_conv")
        self.activ = nn.ReLU()
Example #2
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 bottleneck,
                 conv1_stride,
                 data_format="channels_last",
                 **kwargs):
        super(SEResUnit, self).__init__(**kwargs)
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        if bottleneck:
            self.body = ResBottleneck(in_channels=in_channels,
                                      out_channels=out_channels,
                                      strides=strides,
                                      conv1_stride=conv1_stride,
                                      data_format=data_format,
                                      name="body")
        else:
            self.body = ResBlock(in_channels=in_channels,
                                 out_channels=out_channels,
                                 strides=strides,
                                 data_format=data_format,
                                 name="body")
        self.se = SEBlock(channels=out_channels)
        if self.resize_identity:
            self.identity_conv = conv1x1_block(in_channels=in_channels,
                                               out_channels=out_channels,
                                               strides=strides,
                                               activation=None,
                                               data_format=data_format,
                                               name="identity_conv")
        self.activ = nn.ReLU()
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 bn_eps,
                 activation,
                 tf_mode,
                 data_format="channels_last",
                 **kwargs):
        super(EffiDwsConvUnit, self).__init__(**kwargs)
        self.tf_mode = tf_mode
        self.data_format = data_format
        self.residual = (in_channels == out_channels) and (strides == 1)

        self.dw_conv = dwconv3x3_block(in_channels=in_channels,
                                       out_channels=in_channels,
                                       padding=(0 if tf_mode else 1),
                                       bn_eps=bn_eps,
                                       activation=activation,
                                       data_format=data_format,
                                       name="dw_conv")
        self.se = SEBlock(channels=in_channels,
                          reduction=4,
                          mid_activation=activation,
                          data_format=data_format,
                          name="se")
        self.pw_conv = conv1x1_block(in_channels=in_channels,
                                     out_channels=out_channels,
                                     bn_eps=bn_eps,
                                     activation=None,
                                     data_format=data_format,
                                     name="pw_conv")
Example #4
0
 def __init__(self,
              out_channels,
              use_bn,
              activation,
              data_format="channels_last",
              **kwargs):
     super(IbpPreBlock, self).__init__(**kwargs)
     self.conv1 = conv3x3_block(in_channels=out_channels,
                                out_channels=out_channels,
                                use_bias=(not use_bn),
                                use_bn=use_bn,
                                activation=activation,
                                data_format=data_format,
                                name="conv1")
     self.conv2 = conv3x3_block(in_channels=out_channels,
                                out_channels=out_channels,
                                use_bias=(not use_bn),
                                use_bn=use_bn,
                                activation=activation,
                                data_format=data_format,
                                name="conv2")
     self.se = SEBlock(channels=out_channels,
                       use_conv=False,
                       mid_activation=activation,
                       data_format=data_format,
                       name="se")
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 exp_factor,
                 se_factor,
                 bn_eps,
                 activation,
                 tf_mode,
                 data_format="channels_last",
                 **kwargs):
        super(EffiInvResUnit, self).__init__(**kwargs)
        self.kernel_size = kernel_size
        self.strides = strides
        self.tf_mode = tf_mode
        self.data_format = data_format
        self.residual = (in_channels == out_channels) and (strides == 1)
        self.use_se = se_factor > 0
        mid_channels = in_channels * exp_factor
        dwconv_block_fn = dwconv3x3_block if kernel_size == 3 else (
            dwconv5x5_block if kernel_size == 5 else None)

        self.conv1 = conv1x1_block(in_channels=in_channels,
                                   out_channels=mid_channels,
                                   bn_eps=bn_eps,
                                   activation=activation,
                                   data_format=data_format,
                                   name="conv1")
        self.conv2 = dwconv_block_fn(in_channels=mid_channels,
                                     out_channels=mid_channels,
                                     strides=strides,
                                     padding=(0 if tf_mode else
                                              (kernel_size // 2)),
                                     bn_eps=bn_eps,
                                     activation=activation,
                                     data_format=data_format,
                                     name="conv2")
        if self.use_se:
            self.se = SEBlock(channels=mid_channels,
                              reduction=(exp_factor * se_factor),
                              mid_activation=activation,
                              data_format=data_format,
                              name="se")
        self.conv3 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   bn_eps=bn_eps,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv3")
Example #6
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 exp_channels,
                 strides,
                 use_kernel3,
                 activation,
                 use_se,
                 data_format="channels_last",
                 **kwargs):
        super(MobileNetV3Unit, self).__init__(**kwargs)
        assert (exp_channels >= out_channels)
        self.residual = (in_channels == out_channels) and (strides == 1)
        self.use_se = use_se
        self.use_exp_conv = exp_channels != out_channels
        mid_channels = exp_channels

        if self.use_exp_conv:
            self.exp_conv = conv1x1_block(in_channels=in_channels,
                                          out_channels=mid_channels,
                                          activation=activation,
                                          data_format=data_format,
                                          name="exp_conv")
        if use_kernel3:
            self.conv1 = dwconv3x3_block(in_channels=mid_channels,
                                         out_channels=mid_channels,
                                         strides=strides,
                                         activation=activation,
                                         data_format=data_format,
                                         name="conv1")
        else:
            self.conv1 = dwconv5x5_block(in_channels=mid_channels,
                                         out_channels=mid_channels,
                                         strides=strides,
                                         activation=activation,
                                         data_format=data_format,
                                         name="conv1")
        if self.use_se:
            self.se = SEBlock(channels=mid_channels,
                              reduction=4,
                              round_mid=True,
                              out_activation="hsigmoid",
                              data_format=data_format,
                              name="se")
        self.conv2 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv2")
Example #7
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides=1,
                 use_kernel3=True,
                 exp_factor=1,
                 se_factor=0,
                 use_skip=True,
                 activation="relu",
                 data_format="channels_last",
                 **kwargs):
        super(DwsExpSEResUnit, self).__init__(**kwargs)
        assert (exp_factor >= 1)
        self.residual = (in_channels == out_channels) and (strides
                                                           == 1) and use_skip
        self.use_exp_conv = exp_factor > 1
        self.use_se = se_factor > 0
        mid_channels = exp_factor * in_channels
        dwconv_block_fn = dwconv3x3_block if use_kernel3 else dwconv5x5_block

        if self.use_exp_conv:
            self.exp_conv = conv1x1_block(in_channels=in_channels,
                                          out_channels=mid_channels,
                                          activation=activation,
                                          data_format=data_format,
                                          name="exp_conv")
        self.dw_conv = dwconv_block_fn(in_channels=mid_channels,
                                       out_channels=mid_channels,
                                       strides=strides,
                                       activation=activation,
                                       data_format=data_format,
                                       name="dw_conv")
        if self.use_se:
            self.se = SEBlock(channels=mid_channels,
                              reduction=(exp_factor * se_factor),
                              round_mid=False,
                              mid_activation=activation,
                              data_format=data_format,
                              name="se")
        self.pw_conv = conv1x1_block(in_channels=mid_channels,
                                     out_channels=out_channels,
                                     activation=None,
                                     data_format=data_format,
                                     name="pw_conv")
Example #8
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 use_se,
                 data_format="channels_last",
                 **kwargs):
        super(MobileNetV3FinalBlock, self).__init__(**kwargs)
        self.use_se = use_se

        self.conv = conv1x1_block(in_channels=in_channels,
                                  out_channels=out_channels,
                                  activation="hswish",
                                  data_format=data_format,
                                  name="conv")
        if self.use_se:
            self.se = SEBlock(channels=out_channels,
                              reduction=4,
                              round_mid=True,
                              out_activation="hsigmoid",
                              data_format=data_format,
                              name="se")
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 exp_factor,
                 se_factor,
                 mid_from_in,
                 use_skip,
                 bn_eps,
                 activation,
                 data_format="channels_last",
                 **kwargs):
        super(EffiEdgeResUnit, self).__init__(**kwargs)
        self.residual = (in_channels == out_channels) and (strides
                                                           == 1) and use_skip
        self.use_se = se_factor > 0
        mid_channels = in_channels * exp_factor if mid_from_in else out_channels * exp_factor

        self.conv1 = conv3x3_block(in_channels=in_channels,
                                   out_channels=mid_channels,
                                   bn_eps=bn_eps,
                                   activation=activation,
                                   data_format=data_format,
                                   name="conv1")
        if self.use_se:
            self.se = SEBlock(channels=mid_channels,
                              reduction=(exp_factor * se_factor),
                              mid_activation=activation,
                              data_format=data_format,
                              name="se")
        self.conv2 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   strides=strides,
                                   bn_eps=bn_eps,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv2")
Example #10
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 use_kernel3,
                 exp_factor,
                 use_se,
                 bn_use_global_stats=False,
                 **kwargs):
        super(GhostExpBlock, self).__init__(**kwargs)
        self.use_dw_conv = (strides != 1)
        self.use_se = use_se
        mid_channels = int(math.ceil(exp_factor * in_channels))

        with self.name_scope():
            self.exp_conv = GhostConvBlock(
                in_channels=in_channels,
                out_channels=mid_channels,
                bn_use_global_stats=bn_use_global_stats)
            if self.use_dw_conv:
                dw_conv_class = dwconv3x3_block if use_kernel3 else dwconv5x5_block
                self.dw_conv = dw_conv_class(
                    in_channels=mid_channels,
                    out_channels=mid_channels,
                    strides=strides,
                    bn_use_global_stats=bn_use_global_stats,
                    activation=None)
            if self.use_se:
                self.se = SEBlock(channels=mid_channels,
                                  reduction=4,
                                  out_activation=HSigmoid())
            self.pw_conv = GhostConvBlock(
                in_channels=mid_channels,
                out_channels=out_channels,
                bn_use_global_stats=bn_use_global_stats,
                activation=None)
Example #11
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 use_kernel3,
                 exp_factor,
                 use_se,
                 data_format="channels_last",
                 **kwargs):
        super(GhostExpBlock, self).__init__(**kwargs)
        self.use_dw_conv = (strides != 1)
        self.use_se = use_se
        mid_channels = int(math.ceil(exp_factor * in_channels))

        self.exp_conv = GhostConvBlock(in_channels=in_channels,
                                       out_channels=mid_channels,
                                       name="exp_conv")
        if self.use_dw_conv:
            dw_conv_class = dwconv3x3_block if use_kernel3 else dwconv5x5_block
            self.dw_conv = dw_conv_class(in_channels=mid_channels,
                                         out_channels=mid_channels,
                                         strides=strides,
                                         activation=None,
                                         data_format=data_format,
                                         name="dw_conv")
        if self.use_se:
            self.se = SEBlock(channels=mid_channels,
                              reduction=4,
                              out_activation=GhostHSigmoid(),
                              data_format=data_format,
                              name="se")
        self.pw_conv = GhostConvBlock(in_channels=mid_channels,
                                      out_channels=out_channels,
                                      activation=None,
                                      data_format=data_format,
                                      name="pw_conv")
Example #12
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 downsample,
                 use_se,
                 use_residual,
                 data_format="channels_last",
                 **kwargs):
        super(ShuffleUnit, self).__init__(**kwargs)
        self.data_format = data_format
        self.downsample = downsample
        self.use_se = use_se
        self.use_residual = use_residual
        mid_channels = out_channels // 2

        self.compress_conv1 = conv1x1(
            in_channels=(in_channels if self.downsample else mid_channels),
            out_channels=mid_channels,
            data_format=data_format,
            name="compress_conv1")
        self.compress_bn1 = BatchNorm(
            # in_channels=mid_channels,
            data_format=data_format,
            name="compress_bn1")
        self.dw_conv2 = depthwise_conv3x3(
            channels=mid_channels,
            strides=(2 if self.downsample else 1),
            data_format=data_format,
            name="dw_conv2")
        self.dw_bn2 = BatchNorm(
            # in_channels=mid_channels,
            data_format=data_format,
            name="dw_bn2")
        self.expand_conv3 = conv1x1(in_channels=mid_channels,
                                    out_channels=mid_channels,
                                    data_format=data_format,
                                    name="expand_conv3")
        self.expand_bn3 = BatchNorm(
            # in_channels=mid_channels,
            data_format=data_format,
            name="expand_bn3")
        if self.use_se:
            self.se = SEBlock(channels=mid_channels,
                              data_format=data_format,
                              name="se")
        if downsample:
            self.dw_conv4 = depthwise_conv3x3(channels=in_channels,
                                              strides=2,
                                              data_format=data_format,
                                              name="dw_conv4")
            self.dw_bn4 = BatchNorm(
                # in_channels=in_channels,
                data_format=data_format,
                name="dw_bn4")
            self.expand_conv5 = conv1x1(in_channels=in_channels,
                                        out_channels=mid_channels,
                                        data_format=data_format,
                                        name="expand_conv5")
            self.expand_bn5 = BatchNorm(
                # in_channels=mid_channels,
                data_format=data_format,
                name="expand_bn5")

        self.activ = nn.ReLU()
        self.c_shuffle = ChannelShuffle(channels=out_channels,
                                        groups=2,
                                        data_format=data_format,
                                        name="c_shuffle")
    def __init__(self,
                 in_channels,
                 out_channels,
                 downsample,
                 use_se,
                 use_residual,
                 shuffle_group_first,
                 data_format="channels_last",
                 **kwargs):
        super(ShuffleUnit, self).__init__(**kwargs)
        self.data_format = data_format
        self.downsample = downsample
        self.use_se = use_se
        self.use_residual = use_residual
        mid_channels = out_channels // 2
        in_channels2 = in_channels // 2
        assert (in_channels % 2 == 0)

        y2_in_channels = (in_channels if downsample else in_channels2)
        y2_out_channels = out_channels - y2_in_channels

        self.conv1 = conv1x1_block(in_channels=y2_in_channels,
                                   out_channels=mid_channels,
                                   data_format=data_format,
                                   name="conv1")
        self.dconv = dwconv3x3_block(in_channels=mid_channels,
                                     out_channels=mid_channels,
                                     strides=(2 if self.downsample else 1),
                                     activation=None,
                                     data_format=data_format,
                                     name="dconv")
        self.conv2 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=y2_out_channels,
                                   data_format=data_format,
                                   name="conv2")
        if self.use_se:
            self.se = SEBlock(channels=y2_out_channels,
                              data_format=data_format,
                              name="se")
        if downsample:
            self.shortcut_dconv = dwconv3x3_block(in_channels=in_channels,
                                                  out_channels=in_channels,
                                                  strides=2,
                                                  activation=None,
                                                  data_format=data_format,
                                                  name="shortcut_dconv")
            self.shortcut_conv = conv1x1_block(in_channels=in_channels,
                                               out_channels=in_channels,
                                               data_format=data_format,
                                               name="shortcut_conv")

        if shuffle_group_first:
            self.c_shuffle = ChannelShuffle(channels=out_channels,
                                            groups=2,
                                            data_format=data_format,
                                            name="c_shuffle")
        else:
            self.c_shuffle = ChannelShuffle2(channels=out_channels,
                                             groups=2,
                                             data_format=data_format,
                                             name="c_shuffle")
Example #14
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 exp_kernel_count,
                 conv1_kernel_count,
                 conv2_kernel_count,
                 exp_factor,
                 se_factor,
                 activation,
                 data_format="channels_last",
                 **kwargs):
        super(MixUnit, self).__init__(**kwargs)
        assert (exp_factor >= 1)
        assert (se_factor >= 0)
        self.residual = (in_channels == out_channels) and (strides == 1)
        self.use_se = se_factor > 0
        mid_channels = exp_factor * in_channels
        self.use_exp_conv = exp_factor > 1

        if self.use_exp_conv:
            if exp_kernel_count == 1:
                self.exp_conv = conv1x1_block(in_channels=in_channels,
                                              out_channels=mid_channels,
                                              activation=activation,
                                              data_format=data_format,
                                              name="exp_conv")
            else:
                self.exp_conv = mixconv1x1_block(in_channels=in_channels,
                                                 out_channels=mid_channels,
                                                 kernel_count=exp_kernel_count,
                                                 activation=activation,
                                                 data_format=data_format,
                                                 name="exp_conv")
        if conv1_kernel_count == 1:
            self.conv1 = dwconv3x3_block(in_channels=mid_channels,
                                         out_channels=mid_channels,
                                         strides=strides,
                                         activation=activation,
                                         data_format=data_format,
                                         name="conv1")
        else:
            self.conv1 = MixConvBlock(
                in_channels=mid_channels,
                out_channels=mid_channels,
                kernel_size=[3 + 2 * i for i in range(conv1_kernel_count)],
                strides=strides,
                padding=[1 + i for i in range(conv1_kernel_count)],
                groups=mid_channels,
                activation=activation,
                data_format=data_format,
                name="conv1")
        if self.use_se:
            self.se = SEBlock(channels=mid_channels,
                              reduction=(exp_factor * se_factor),
                              round_mid=False,
                              mid_activation=activation,
                              data_format=data_format,
                              name="se")
        if conv2_kernel_count == 1:
            self.conv2 = conv1x1_block(in_channels=mid_channels,
                                       out_channels=out_channels,
                                       activation=None,
                                       data_format=data_format,
                                       name="conv2")
        else:
            self.conv2 = mixconv1x1_block(in_channels=mid_channels,
                                          out_channels=out_channels,
                                          kernel_count=conv2_kernel_count,
                                          activation=None,
                                          data_format=data_format,
                                          name="conv2")