Example #1
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 use_deptwise,
                 activation,
                 data_format="channels_last",
                 **kwargs):
        super(HarDInitBlock, self).__init__(**kwargs)
        mid_channels = out_channels // 2

        self.conv1 = conv3x3_block(in_channels=in_channels,
                                   out_channels=mid_channels,
                                   strides=2,
                                   activation=activation,
                                   data_format=data_format,
                                   name="conv1")
        conv2_block_class = conv1x1_block if use_deptwise else conv3x3_block
        self.conv2 = conv2_block_class(in_channels=mid_channels,
                                       out_channels=out_channels,
                                       activation=activation,
                                       data_format=data_format,
                                       name="conv2")
        if use_deptwise:
            self.downsample = dwconv3x3_block(in_channels=out_channels,
                                              out_channels=out_channels,
                                              strides=2,
                                              activation=None,
                                              data_format=data_format,
                                              name="downsample")
        else:
            self.downsample = MaxPool2d(pool_size=3,
                                        strides=2,
                                        padding=1,
                                        data_format=data_format,
                                        name="downsample")
Example #2
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 expansion,
                 data_format="channels_last",
                 **kwargs):
        super(InvResUnit, self).__init__(**kwargs)
        self.residual = (in_channels == out_channels) and (strides == 1)
        mid_channels = in_channels * 6 if expansion else in_channels
        groups = 2

        self.conv1 = conv1x1_block(in_channels=in_channels,
                                   out_channels=mid_channels,
                                   groups=groups,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv1")
        self.c_shuffle = ChannelShuffle(channels=mid_channels,
                                        groups=groups,
                                        data_format=data_format,
                                        name="c_shuffle")
        self.conv2 = dwconv3x3_block(in_channels=mid_channels,
                                     out_channels=mid_channels,
                                     strides=strides,
                                     activation=ReLU6(),
                                     data_format=data_format,
                                     name="conv2")
        self.conv3 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   groups=groups,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv3")
    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,
                 in_channels_list,
                 out_channels_list,
                 links_list,
                 use_deptwise,
                 use_dropout,
                 downsampling,
                 activation,
                 data_format="channels_last",
                 **kwargs):
        super(HarDUnit, self).__init__(**kwargs)
        self.data_format = data_format
        self.links_list = links_list
        self.use_dropout = use_dropout
        self.downsampling = downsampling

        self.blocks = SimpleSequential(name="blocks")
        for i in range(len(links_list)):
            in_channels = in_channels_list[i]
            out_channels = out_channels_list[i]
            if use_deptwise:
                unit = invdwsconv3x3_block(in_channels=in_channels,
                                           out_channels=out_channels,
                                           pw_activation=activation,
                                           dw_activation=None,
                                           data_format=data_format,
                                           name="block{}".format(i + 1))
            else:
                unit = conv3x3_block(in_channels=in_channels,
                                     out_channels=out_channels,
                                     data_format=data_format,
                                     name="block{}".format(i + 1))
            self.blocks.add(unit)

        if self.use_dropout:
            self.dropout = nn.Dropout(rate=0.1, name="dropout")
        self.conv = conv1x1_block(in_channels=in_channels_list[-1],
                                  out_channels=out_channels_list[-1],
                                  activation=activation,
                                  data_format=data_format,
                                  name="conv")

        if self.downsampling:
            if use_deptwise:
                self.downsample = dwconv3x3_block(
                    in_channels=out_channels_list[-1],
                    out_channels=out_channels_list[-1],
                    strides=2,
                    activation=None,
                    data_format=data_format,
                    name="downsample")
            else:
                self.downsample = MaxPool2d(pool_size=2,
                                            strides=2,
                                            data_format=data_format,
                                            name="downsample")
Example #5
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 #6
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 bn_eps,
                 use_kernel3,
                 exp_factor,
                 activation="relu",
                 data_format="channels_last",
                 **kwargs):
        super(FBNetUnit, self).__init__(**kwargs)
        assert (exp_factor >= 1)
        self.residual = (in_channels == out_channels) and (strides == 1)
        self.use_exp_conv = True
        mid_channels = exp_factor * in_channels

        if self.use_exp_conv:
            self.exp_conv = conv1x1_block(in_channels=in_channels,
                                          out_channels=mid_channels,
                                          bn_eps=bn_eps,
                                          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,
                                         bn_eps=bn_eps,
                                         activation=activation,
                                         data_format=data_format,
                                         name="conv1")
        else:
            self.conv1 = dwconv5x5_block(in_channels=mid_channels,
                                         out_channels=mid_channels,
                                         strides=strides,
                                         bn_eps=bn_eps,
                                         activation=activation,
                                         data_format=data_format,
                                         name="conv1")
        self.conv2 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   bn_eps=bn_eps,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv2")
Example #7
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 bn_use_global_stats=False,
                 activation=(lambda: nn.Activation("relu")),
                 **kwargs):
        super(GhostConvBlock, self).__init__(**kwargs)
        main_out_channels = math.ceil(0.5 * out_channels)
        cheap_out_channels = out_channels - main_out_channels

        with self.name_scope():
            self.main_conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=main_out_channels,
                bn_use_global_stats=bn_use_global_stats,
                activation=activation)
            self.cheap_conv = dwconv3x3_block(
                in_channels=main_out_channels,
                out_channels=cheap_out_channels,
                bn_use_global_stats=bn_use_global_stats,
                activation=activation)
Example #8
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 activation="relu",
                 data_format="channels_last",
                 **kwargs):
        super(GhostConvBlock, self).__init__(**kwargs)
        self.data_format = data_format
        main_out_channels = math.ceil(0.5 * out_channels)
        cheap_out_channels = out_channels - main_out_channels

        self.main_conv = conv1x1_block(in_channels=in_channels,
                                       out_channels=main_out_channels,
                                       activation=activation,
                                       data_format=data_format,
                                       name="main_conv")
        self.cheap_conv = dwconv3x3_block(in_channels=main_out_channels,
                                          out_channels=cheap_out_channels,
                                          activation=activation,
                                          data_format=data_format,
                                          name="cheap_conv")
    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 #10
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")