Пример #1
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")
Пример #2
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 bottleneck_factor=4,
                 data_format="channels_last",
                 **kwargs):
        super(BagNetBottleneck, self).__init__(**kwargs)
        mid_channels = out_channels // bottleneck_factor

        self.conv1 = conv1x1_block(in_channels=in_channels,
                                   out_channels=mid_channels,
                                   data_format=data_format,
                                   name="conv1")
        self.conv2 = ConvBlock(in_channels=mid_channels,
                               out_channels=mid_channels,
                               kernel_size=kernel_size,
                               strides=strides,
                               padding=0,
                               data_format=data_format,
                               name="conv2")
        self.conv3 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv3")
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 use_bias=True,
                 bottleneck_factor=2,
                 squeeze_out=False,
                 data_format="channels_last",
                 **kwargs):
        super(LwopResBottleneck, self).__init__(**kwargs)
        mid_channels = out_channels // bottleneck_factor if squeeze_out else in_channels // bottleneck_factor

        self.conv1 = conv1x1_block(in_channels=in_channels,
                                   out_channels=mid_channels,
                                   use_bias=use_bias,
                                   data_format=data_format,
                                   name="conv1")
        self.conv2 = conv3x3_block(in_channels=mid_channels,
                                   out_channels=mid_channels,
                                   strides=strides,
                                   use_bias=use_bias,
                                   data_format=data_format,
                                   name="conv2")
        self.conv3 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   use_bias=use_bias,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv3")
Пример #4
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 cardinality,
                 bottleneck_width,
                 data_format="channels_last",
                 **kwargs):
        super(SENetBottleneck, self).__init__(**kwargs)
        mid_channels = out_channels // 4
        D = int(math.floor(mid_channels * (bottleneck_width / 64.0)))
        group_width = cardinality * D
        group_width2 = group_width // 2

        self.conv1 = conv1x1_block(in_channels=in_channels,
                                   out_channels=group_width2,
                                   data_format=data_format,
                                   name="conv1")
        self.conv2 = conv3x3_block(in_channels=group_width2,
                                   out_channels=group_width,
                                   strides=strides,
                                   groups=cardinality,
                                   data_format=data_format,
                                   name="conv2")
        self.conv3 = conv1x1_block(in_channels=group_width,
                                   out_channels=out_channels,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv3")
Пример #5
0
    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")
Пример #6
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 dilations,
                 data_format="channels_last",
                 **kwargs):
        super(ESPBlock, self).__init__(**kwargs)
        num_branches = len(dilations)
        assert (out_channels % num_branches == 0)
        self.downsample = (strides != 1)
        mid_channels = out_channels // num_branches

        # dilations = [1] * len(dilations)

        self.reduce_conv = conv1x1_block(
            in_channels=in_channels,
            out_channels=mid_channels,
            groups=num_branches,
            activation=(lambda: PReLU2()),
            data_format=data_format,
            name="reduce_conv")

        self.branches = HierarchicalConcurrent(
            data_format=data_format,
            name="branches")
        for i in range(num_branches):
            self.branches.add(conv3x3(
                in_channels=mid_channels,
                out_channels=mid_channels,
                strides=strides,
                padding=dilations[i],
                dilation=dilations[i],
                groups=mid_channels,
                data_format=data_format,
                name="branch{}".format(i + 1)))

        self.merge_conv = conv1x1_block(
            in_channels=out_channels,
            out_channels=out_channels,
            groups=num_branches,
            activation=None,
            data_format=data_format,
            name="merge_conv")
        self.preactiv = PreActivation(
            in_channels=out_channels,
            data_format=data_format,
            name="preactiv")
        if not self.downsample:
            self.activ = PReLU2()
Пример #7
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")
Пример #8
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()
Пример #9
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 data_format="channels_last",
                 **kwargs):
        super(StemBlock, self).__init__(**kwargs)
        mid1_channels = out_channels // 2
        mid2_channels = out_channels * 2

        self.first_conv = conv3x3_block(in_channels=in_channels,
                                        out_channels=out_channels,
                                        strides=2,
                                        data_format=data_format,
                                        name="first_conv")

        self.branches = Concurrent(data_format=data_format, name="branches")
        self.branches.add(
            PeleeBranch1(in_channels=out_channels,
                         out_channels=out_channels,
                         mid_channels=mid1_channels,
                         strides=2,
                         data_format=data_format,
                         name="branch1"))
        self.branches.add(
            MaxPool2d(pool_size=2,
                      strides=2,
                      padding=0,
                      data_format=data_format,
                      name="branch2"))

        self.last_conv = conv1x1_block(in_channels=mid2_channels,
                                       out_channels=out_channels,
                                       data_format=data_format,
                                       name="last_conv")
Пример #10
0
 def __init__(self,
              in_channels,
              out_channels,
              mid_channels,
              strides=1,
              use_bias=True,
              use_bn=True,
              data_format="channels_last",
              **kwargs):
     super(InceptionDouble3x3Branch, self).__init__(**kwargs)
     self.conv1 = conv1x1_block(in_channels=in_channels,
                                out_channels=mid_channels,
                                use_bias=use_bias,
                                use_bn=use_bn,
                                data_format=data_format,
                                name="conv1")
     self.conv2 = conv3x3_block(in_channels=mid_channels,
                                out_channels=out_channels,
                                use_bias=use_bias,
                                use_bn=use_bn,
                                data_format=data_format,
                                name="conv2")
     self.conv3 = conv3x3_block(in_channels=out_channels,
                                out_channels=out_channels,
                                strides=strides,
                                use_bias=use_bias,
                                use_bn=use_bn,
                                data_format=data_format,
                                name="conv3")
Пример #11
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 use_inst_norm,
                 data_format="channels_last",
                 **kwargs):
        super(IBNbResUnit, self).__init__(**kwargs)
        self.use_inst_norm = use_inst_norm
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        self.body = ResBottleneck(in_channels=in_channels,
                                  out_channels=out_channels,
                                  strides=strides,
                                  conv1_stride=False,
                                  data_format=data_format,
                                  name="body")
        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")
        if self.use_inst_norm:
            self.inst_norm = InstanceNorm(scale=True,
                                          data_format=data_format,
                                          name="inst_norm")
        self.activ = nn.ReLU()
Пример #12
0
def dark_convYxY(in_channels,
                 out_channels,
                 alpha,
                 pointwise,
                 data_format="channels_last",
                 **kwargs):
    """
    DarkNet unit.

    Parameters:
    ----------
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    alpha : float
        Slope coefficient for Leaky ReLU activation.
    pointwise : bool
        Whether use 1x1 (pointwise) convolution or 3x3 convolution.
    data_format : str, default 'channels_last'
        The ordering of the dimensions in tensors.
    """
    if pointwise:
        return conv1x1_block(in_channels=in_channels,
                             out_channels=out_channels,
                             activation=nn.LeakyReLU(alpha=alpha),
                             data_format=data_format,
                             **kwargs)
    else:
        return conv3x3_block(in_channels=in_channels,
                             out_channels=out_channels,
                             activation=nn.LeakyReLU(alpha=alpha),
                             data_format=data_format,
                             **kwargs)
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides=1,
                 use_bias=True,
                 bottleneck_factor=2,
                 squeeze_out=False,
                 activate=False,
                 data_format="channels_last",
                 **kwargs):
        super(LwopResUnit, self).__init__(**kwargs)
        self.activate = activate
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        self.body = LwopResBottleneck(in_channels=in_channels,
                                      out_channels=out_channels,
                                      strides=strides,
                                      use_bias=use_bias,
                                      bottleneck_factor=bottleneck_factor,
                                      squeeze_out=squeeze_out,
                                      data_format=data_format,
                                      name="body")
        if self.resize_identity:
            self.identity_conv = conv1x1_block(in_channels=in_channels,
                                               out_channels=out_channels,
                                               strides=strides,
                                               use_bias=use_bias,
                                               activation=None,
                                               data_format=data_format,
                                               name="identity_conv")
        if self.activate:
            self.activ = nn.ReLU()
Пример #14
0
    def __init__(self,
                 in_channels,
                 mid_channels,
                 use_bias,
                 use_bn,
                 data_format="channels_last",
                 **kwargs):
        super(LffdDetectionBlock, self).__init__(**kwargs)
        self.conv = conv1x1_block(
            in_channels=in_channels,
            out_channels=mid_channels,
            use_bias=use_bias,
            use_bn=use_bn,
            data_format=data_format,
            name="conv")

        self.branches = Concurrent(
            data_format=data_format,
            name="branches")
        self.branches.add(LffdDetectionBranch(
            in_channels=mid_channels,
            out_channels=4,
            use_bias=use_bias,
            use_bn=use_bn,
            data_format=data_format,
            name="bbox_branch"))
        self.branches.add(LffdDetectionBranch(
            in_channels=mid_channels,
            out_channels=2,
            use_bias=use_bias,
            use_bn=use_bn,
            data_format=data_format,
            name="score_branch"))
Пример #15
0
    def __init__(self, data_format="channels_last", **kwargs):
        super(TwoWayABlock, self).__init__(**kwargs)
        in_channels = 384

        self.branches = Concurrent(data_format=data_format, name="branches")
        self.branches.add(
            ConvSeqBranch(in_channels=in_channels,
                          out_channels_list=(32, 48, 64),
                          kernel_size_list=(1, 3, 3),
                          strides_list=(1, 1, 1),
                          padding_list=(0, 1, 1),
                          data_format=data_format,
                          name="branch1"))
        self.branches.add(
            ConvSeqBranch(in_channels=in_channels,
                          out_channels_list=(32, 32),
                          kernel_size_list=(1, 3),
                          strides_list=(1, 1),
                          padding_list=(0, 1),
                          data_format=data_format,
                          name="branch2"))
        self.branches.add(
            Conv1x1Branch(in_channels=in_channels,
                          out_channels=32,
                          data_format=data_format,
                          name="branch3"))
        self.conv = conv1x1_block(in_channels=128,
                                  out_channels=in_channels,
                                  activation=None,
                                  data_format=data_format,
                                  name="conv")
Пример #16
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 conv1_ibn,
                 data_format="channels_last",
                 **kwargs):
        super(IBNResBottleneck, self).__init__(**kwargs)
        mid_channels = out_channels // 4

        self.conv1 = ibn_conv1x1_block(in_channels=in_channels,
                                       out_channels=mid_channels,
                                       use_ibn=conv1_ibn,
                                       data_format=data_format,
                                       name="conv1")
        self.conv2 = conv3x3_block(in_channels=mid_channels,
                                   out_channels=mid_channels,
                                   strides=strides,
                                   data_format=data_format,
                                   name="conv2")
        self.conv3 = conv1x1_block(in_channels=mid_channels,
                                   out_channels=out_channels,
                                   activation=None,
                                   data_format=data_format,
                                   name="conv3")
Пример #17
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 stride,
                 padding=1,
                 dilation=1,
                 bottleneck=True,
                 conv1_stride=False):
        super(ResUnit, self).__init__()
        self.resize_identity = (in_channels != out_channels) or (stride != 1)

        if bottleneck:
            self.body = ResBottleneck(in_channels=in_channels,
                                      out_channels=out_channels,
                                      stride=stride,
                                      padding=padding,
                                      dilation=dilation,
                                      conv1_stride=conv1_stride)
        else:
            self.body = ResBlock(in_channels=in_channels,
                                 out_channels=out_channels,
                                 stride=stride)
        if self.resize_identity:
            self.identity_conv = conv1x1_block(in_channels=in_channels,
                                               out_channels=out_channels,
                                               stride=stride,
                                               activation=None)
        self.activ = nn.ReLU(inplace=True)
Пример #18
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides,
                 body_class=ResBlock,
                 return_down=False,
                 data_format="channels_last",
                 **kwargs):
        super(DLAResBlock, self).__init__(**kwargs)
        self.return_down = return_down
        self.downsample = (strides > 1)
        self.project = (in_channels != out_channels)

        self.body = body_class(in_channels=in_channels,
                               out_channels=out_channels,
                               strides=strides,
                               data_format=data_format,
                               name="body")
        self.activ = nn.ReLU()
        if self.downsample:
            self.downsample_pool = nn.MaxPool2D(pool_size=strides,
                                                strides=strides,
                                                data_format=data_format,
                                                name="downsample_pool")
        if self.project:
            self.project_conv = conv1x1_block(in_channels=in_channels,
                                              out_channels=out_channels,
                                              activation=None,
                                              data_format=data_format,
                                              name="project_conv")
Пример #19
0
    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")
 def __init__(self,
              in_channels,
              out_channels,
              data_format="channels_last",
              **kwargs):
     super(LwopRefinementBlock, self).__init__(**kwargs)
     self.pre_conv = conv1x1_block(in_channels=in_channels,
                                   out_channels=out_channels,
                                   use_bias=True,
                                   use_bn=False,
                                   data_format=data_format,
                                   name="pre_conv")
     self.body = SimpleSequential(name="body")
     self.body.add(
         conv3x3_block(in_channels=out_channels,
                       out_channels=out_channels,
                       use_bias=True,
                       data_format=data_format,
                       name="block1"))
     self.body.add(
         conv3x3_block(in_channels=out_channels,
                       out_channels=out_channels,
                       padding=2,
                       dilation=2,
                       use_bias=True,
                       data_format=data_format,
                       name="block2"))
Пример #21
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 skip_channels,
                 mid_channels,
                 strides,
                 data_format="channels_last",
                 **kwargs):
        super(SelecSLSUnit, self).__init__(**kwargs)
        self.data_format = data_format
        self.resize = (strides == 2)
        mid2_channels = mid_channels // 2
        last_channels = 2 * mid_channels + (skip_channels if strides == 1 else 0)

        self.branch1 = conv3x3_block(
            in_channels=in_channels,
            out_channels=mid_channels,
            strides=strides,
            data_format=data_format,
            name="branch1")
        self.branch2 = SelecSLSBlock(
            in_channels=mid_channels,
            out_channels=mid2_channels,
            data_format=data_format,
            name="branch2")
        self.branch3 = SelecSLSBlock(
            in_channels=mid2_channels,
            out_channels=mid2_channels,
            data_format=data_format,
            name="branch3")
        self.last_conv = conv1x1_block(
            in_channels=last_channels,
            out_channels=out_channels,
            data_format=data_format,
            name="last_conv")
 def __init__(self,
              in_channels,
              out_channels,
              data_format="channels_last",
              **kwargs):
     super(LwopEncoderFinalBlock, self).__init__(**kwargs)
     self.pre_conv = conv1x1_block(in_channels=in_channels,
                                   out_channels=out_channels,
                                   use_bias=True,
                                   use_bn=False,
                                   data_format=data_format,
                                   name="pre_conv")
     self.body = SimpleSequential(name="body")
     for i in range(3):
         self.body.add(
             dwsconv3x3_block(in_channels=out_channels,
                              out_channels=out_channels,
                              dw_use_bn=False,
                              pw_use_bn=False,
                              dw_activation=(lambda: nn.ELU()),
                              pw_activation=(lambda: nn.ELU()),
                              data_format=data_format,
                              name="block{}".format(i + 1)))
     self.post_conv = conv3x3_block(in_channels=out_channels,
                                    out_channels=out_channels,
                                    use_bias=True,
                                    use_bn=False,
                                    data_format=data_format,
                                    name="post_conv")
Пример #23
0
    def __init__(self,
                 channels,
                 reduction_ratio=16,
                 num_dil_convs=2,
                 dilation=4,
                 data_format="channels_last",
                 **kwargs):
        super(SpatialGate, self).__init__(**kwargs)
        mid_channels = channels // reduction_ratio

        self.init_conv = conv1x1_block(in_channels=channels,
                                       out_channels=mid_channels,
                                       strides=1,
                                       use_bias=True,
                                       data_format=data_format,
                                       name="init_conv")
        self.dil_convs = SimpleSequential(name="dil_convs")
        for i in range(num_dil_convs):
            self.dil_convs.children.append(
                conv3x3_block(in_channels=mid_channels,
                              out_channels=mid_channels,
                              strides=1,
                              padding=dilation,
                              dilation=dilation,
                              use_bias=True,
                              data_format=data_format,
                              name="conv{}".format(i + 1)))
        self.final_conv = conv1x1(in_channels=mid_channels,
                                  out_channels=1,
                                  strides=1,
                                  use_bias=True,
                                  data_format=data_format,
                                  name="final_conv")
Пример #24
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 data_format="channels_last",
                 **kwargs):
        super(BagNetUnit, self).__init__(**kwargs)
        self.data_format = data_format
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        self.body = BagNetBottleneck(in_channels=in_channels,
                                     out_channels=out_channels,
                                     kernel_size=kernel_size,
                                     strides=strides,
                                     data_format=data_format,
                                     name="body")
        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()
Пример #25
0
    def __init__(self,
                 channels,
                 init_block_channels,
                 final_block_channels,
                 classifier_mid_channels,
                 kernels3,
                 exp_factors,
                 use_se,
                 first_stride,
                 bn_use_global_stats=False,
                 in_channels=3,
                 in_size=(224, 224),
                 classes=1000,
                 dropout=0.2,
                 **kwargs):
        super(GhostNet, self).__init__(**kwargs)
        self.in_size = in_size
        self.classes = classes

        with self.name_scope():
            self.features = nn.HybridSequential(prefix="")
            self.features.add(
                conv3x3_block(in_channels=in_channels,
                              out_channels=init_block_channels,
                              strides=2,
                              bn_use_global_stats=bn_use_global_stats))
            in_channels = init_block_channels
            for i, channels_per_stage in enumerate(channels):
                stage = nn.HybridSequential(prefix="stage{}_".format(i + 1))
                with stage.name_scope():
                    for j, out_channels in enumerate(channels_per_stage):
                        strides = 2 if (j == 0) and (
                            (i != 0) or first_stride) else 1
                        use_kernel3 = kernels3[i][j] == 1
                        exp_factor = exp_factors[i][j]
                        use_se_flag = use_se[i][j] == 1
                        stage.add(
                            GhostUnit(in_channels=in_channels,
                                      out_channels=out_channels,
                                      strides=strides,
                                      use_kernel3=use_kernel3,
                                      exp_factor=exp_factor,
                                      use_se=use_se_flag,
                                      bn_use_global_stats=bn_use_global_stats))
                        in_channels = out_channels
                self.features.add(stage)
            self.features.add(
                conv1x1_block(in_channels=in_channels,
                              out_channels=final_block_channels,
                              bn_use_global_stats=bn_use_global_stats))
            in_channels = final_block_channels
            self.features.add(nn.AvgPool2D(pool_size=7, strides=1))

            self.output = nn.HybridSequential(prefix="")
            self.output.add(
                GhostClassifier(in_channels=in_channels,
                                out_channels=classes,
                                mid_channels=classifier_mid_channels,
                                dropout=dropout))
            self.output.add(nn.Flatten())
Пример #26
0
 def __init__(self,
              in_channels,
              out_channels,
              avg_pool,
              use_bias,
              use_bn,
              data_format="channels_last",
              **kwargs):
     super(InceptionPoolBranch, self).__init__(**kwargs)
     if avg_pool:
         self.pool = AvgPool2d(
             pool_size=3,
             strides=1,
             padding=1,
             ceil_mode=True,
             # count_include_pad=True,
             data_format=data_format,
             name="pool")
     else:
         self.pool = MaxPool2d(pool_size=3,
                               strides=1,
                               padding=1,
                               ceil_mode=True,
                               data_format=data_format,
                               name="pool")
     self.conv = conv1x1_block(in_channels=in_channels,
                               out_channels=out_channels,
                               use_bias=use_bias,
                               use_bn=use_bn,
                               data_format=data_format,
                               name="conv")
Пример #27
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 strides=1,
                 use_bias=False,
                 bottleneck_factor=2,
                 activation="relu",
                 data_format="channels_last",
                 **kwargs):
        super(IbpResUnit, self).__init__(**kwargs)
        self.resize_identity = (in_channels != out_channels) or (strides != 1)

        self.body = IbpResBottleneck(in_channels=in_channels,
                                     out_channels=out_channels,
                                     strides=strides,
                                     use_bias=use_bias,
                                     bottleneck_factor=bottleneck_factor,
                                     activation=activation,
                                     data_format=data_format,
                                     name="body")
        if self.resize_identity:
            self.identity_conv = conv1x1_block(in_channels=in_channels,
                                               out_channels=out_channels,
                                               strides=strides,
                                               use_bias=use_bias,
                                               activation=None,
                                               data_format=data_format,
                                               name="identity_conv")
        self.activ = get_activation_layer(activation)
Пример #28
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()
Пример #29
0
 def __init__(self,
              in_channels,
              out_channels,
              kernel_size,
              strides,
              padding,
              dilation=1,
              use_bias=False,
              use_bn=True,
              bn_eps=1e-5,
              pw_activation="relu",
              dw_activation="relu",
              data_format="channels_last",
              **kwargs):
     super(InvDwsConvBlock, self).__init__(**kwargs)
     self.pw_conv = conv1x1_block(in_channels=in_channels,
                                  out_channels=out_channels,
                                  use_bias=use_bias,
                                  use_bn=use_bn,
                                  bn_eps=bn_eps,
                                  activation=pw_activation,
                                  data_format=data_format,
                                  name="pw_conv")
     self.dw_conv = dwconv_block(in_channels=out_channels,
                                 out_channels=out_channels,
                                 kernel_size=kernel_size,
                                 strides=strides,
                                 padding=padding,
                                 dilation=dilation,
                                 use_bias=use_bias,
                                 use_bn=use_bn,
                                 bn_eps=bn_eps,
                                 activation=dw_activation,
                                 data_format=data_format,
                                 name="dw_conv")
Пример #30
0
 def __init__(self,
              in_channels_list,
              out_channels_list,
              data_format="channels_last",
              **kwargs):
     super(HRFinalBlock, self).__init__(**kwargs)
     self.inc_blocks = SimpleSequential(name="inc_blocks")
     for i, in_channels_i in enumerate(in_channels_list):
         self.inc_blocks.add(ResUnit(
             in_channels=in_channels_i,
             out_channels=out_channels_list[i],
             strides=1,
             bottleneck=True,
             data_format=data_format,
             name="inc_blocks/block{}".format(i + 1)))
     self.down_blocks = SimpleSequential(name="down_blocks")
     for i in range(len(in_channels_list) - 1):
         self.down_blocks.add(conv3x3_block(
             in_channels=out_channels_list[i],
             out_channels=out_channels_list[i + 1],
             strides=2,
             use_bias=True,
             data_format=data_format,
             name="down_blocks/block{}".format(i + 1)))
     self.final_layer = conv1x1_block(
         in_channels=1024,
         out_channels=2048,
         strides=1,
         use_bias=True,
         data_format=data_format,
         name="final_layer")