Пример #1
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width=64,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 radix=1,
                 stride_3x3=False,
                 downsample=None):
        super(BasicBlock, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))
        assert context in ['none', 'se', 'gcb']
        width = int(planes * (base_width / 64.))

        self.conv1 = conv_op(inplanes,
                             width,
                             kernel_size=3,
                             stride=stride,
                             dilation=dilation,
                             padding=dilation,
                             bias=False)
        self.bn1 = make_norm(width, norm=norm, an_k=10 if planes < 256 else 20)
        self.conv2 = conv_op(width,
                             width,
                             kernel_size=3,
                             stride=1,
                             dilation=dilation,
                             padding=dilation,
                             bias=False)
        self.bn2 = make_norm(width, norm=norm, an_k=10 if planes < 256 else 20)

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(width, int(width * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(width, int(width * ctx_ratio))
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Пример #2
0
 def __init__(self, inplanes, outplanes, stride=1, dilation=1, kernel=3, groups=(1, 1), t=6, norm='bn', se_ratio=0,
              activation=nn.ReLU6):
     super(LinearBottleneck, self).__init__()
     padding = (dilation * kernel - dilation) // 2
     self.stride = stride
     self.inplanes, self.outplanes, innerplanes = int(inplanes), int(outplanes), int(inplanes * abs(t))
     self.t = t
     if self.t != 1:
         self.conv1 = nn.Conv2d(self.inplanes, innerplanes, kernel_size=1, padding=0, stride=1, groups=groups[0],
                                bias=False)
         self.bn1 = make_norm(innerplanes, norm=norm)
     self.conv2 = nn.Conv2d(innerplanes, innerplanes, kernel_size=kernel, padding=padding, stride=stride,
                            dilation=dilation, groups=innerplanes, bias=False)
     self.bn2 = make_norm(innerplanes, norm=norm)
     self.se = ops.SeConv2d(innerplanes, int(self.inplanes * se_ratio), activation) if se_ratio else None
     self.conv3 = nn.Conv2d(innerplanes, self.outplanes, kernel_size=1, padding=0, stride=1, groups=groups[1],
                            bias=False)
     self.bn3 = make_norm(self.outplanes, norm=norm)
     try:
         self.activation = activation(inplace=True)
     except:
         self.activation = activation()
Пример #3
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width=64,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 radix=1,
                 stride_3x3=False,
                 downsample=None):
        super(Bottleneck, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))
        (str1x1, str3x3) = (1, stride) if stride_3x3 else (stride, 1)
        width = int(planes * (base_width / 64.))
        self.radix = radix

        self.conv1 = nn.Conv2d(inplanes,
                               width,
                               kernel_size=1,
                               stride=str1x1,
                               bias=False)
        self.bn1 = make_norm(width, norm=norm.split('_')[-1])

        if radix > 1 and (str3x3 > 1 or dilation > 1):
            self.avd_layer = nn.AvgPool2d(3, str3x3, padding=1)
            str3x3 = 1
        else:
            self.avd_layer = None
        if radix > 1:
            self.conv2 = ops.SplAtConv2d(width,
                                         width,
                                         kernel_size=3,
                                         stride=str3x3,
                                         padding=dilation,
                                         dilation=dilation,
                                         bias=False,
                                         radix=radix)
        else:
            self.conv2 = conv_op(width,
                                 width,
                                 kernel_size=3,
                                 stride=str3x3,
                                 dilation=dilation,
                                 padding=dilation,
                                 bias=False)
            self.bn2 = make_norm(width,
                                 norm=norm,
                                 an_k=10 if planes < 256 else 20)
        self.conv3 = nn.Conv2d(width,
                               planes * self.expansion,
                               kernel_size=1,
                               bias=False)
        self.bn3 = make_norm(planes * self.expansion, norm=norm.split('_')[-1])

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(planes * self.expansion,
                                    int(planes * self.expansion * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(
                planes * self.expansion,
                int(planes * self.expansion * ctx_ratio))
        elif context == 'nonlocal':
            self.ctx = ops.NonLocal2d(planes * self.expansion,
                                      int(planes * self.expansion * ctx_ratio),
                                      planes * self.expansion,
                                      use_gn=True)
        elif context == 'msa':
            self.ctx = ops.MS_NonLocal2d(planes * self.expansion,
                                         int(planes * self.expansion *
                                             ctx_ratio),
                                         planes * self.expansion,
                                         use_gn=True)
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Пример #4
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width=64,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 stride_3x3=False,
                 downsample=None):
        super(AlignedBottleneck, self).__init__()
        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))
        width = int(planes * (base_width / 64.))

        self.conv1_1 = nn.Conv2d(inplanes,
                                 width,
                                 kernel_size=1,
                                 stride=1,
                                 padding=0,
                                 bias=False)
        self.bn1_1 = make_norm(width, norm=norm.split('_')[-1])
        self.conv1_2 = conv_op(width,
                               width,
                               kernel_size=3,
                               stride=stride,
                               dilation=dilation,
                               padding=dilation,
                               bias=False)
        self.conv2_1 = nn.Conv2d(inplanes,
                                 width // 2,
                                 kernel_size=1,
                                 stride=1,
                                 padding=0,
                                 bias=False)
        self.bn2_1 = make_norm(width // 2, norm=norm.split('_')[-1])
        self.conv2_2 = conv_op(width // 2,
                               width // 2,
                               kernel_size=3,
                               stride=stride,
                               dilation=dilation,
                               padding=dilation,
                               bias=False)
        self.bn2_2 = make_norm(width // 2,
                               norm=norm,
                               an_k=10 if planes < 256 else 20)
        self.conv2_3 = conv_op(width // 2,
                               width // 2,
                               kernel_size=3,
                               stride=1,
                               dilation=dilation,
                               padding=dilation,
                               bias=False)
        self.bn_concat = make_norm(width + (width // 2),
                                   norm=norm,
                                   an_k=10 if planes < 256 else 20)

        self.conv = nn.Conv2d(width + (width // 2),
                              planes * self.expansion,
                              kernel_size=1,
                              stride=1,
                              padding=0,
                              bias=False)
        self.bn = make_norm(planes * self.expansion, norm=norm.split('_')[-1])

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(planes * self.expansion,
                                    int(planes * self.expansion * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(
                planes * self.expansion,
                int(planes * self.expansion * ctx_ratio))
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
Пример #5
0
    def __init__(self,
                 inplanes,
                 planes,
                 base_width,
                 cardinality,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 downsample=None):
        """ Constructor
        Args:
            inplanes: input channel dimensionality
            planes: output channel dimensionality
            base_width: base width.
            cardinality: num of convolution groups.
            stride: conv stride. Replaces pooling layer.
        """
        super(Bottleneck, self).__init__()

        D = int(math.floor(planes * (base_width / 64.0)))
        C = cardinality

        if conv == 'normal':
            conv_op = nn.Conv2d
        elif conv == 'deform':
            conv_op = ops.DeformConvPack
        elif conv == 'deformv2':
            conv_op = ops.ModulatedDeformConvPack
        else:
            raise ValueError(
                '{} type conv operation is not supported.'.format(conv))

        self.conv1 = nn.Conv2d(inplanes,
                               D * C,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.bn1 = make_norm(D * C, norm=norm)
        self.conv2 = conv_op(D * C,
                             D * C,
                             kernel_size=3,
                             stride=stride,
                             dilation=dilation,
                             padding=dilation,
                             groups=C,
                             bias=False)
        self.bn2 = make_norm(D * C, norm=norm)
        self.conv3 = nn.Conv2d(D * C,
                               planes * 4,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.bn3 = make_norm(planes * 4, norm=norm)

        if context == 'none':
            self.ctx = None
        elif context == 'se':
            self.ctx = ops.SeConv2d(planes * 4, int(planes * 4 * ctx_ratio))
        elif context == 'gcb':
            self.ctx = ops.GlobalContextBlock(planes * 4,
                                              int(planes * 4 * ctx_ratio))
        else:
            raise ValueError(
                '{} type context operation is not supported.'.format(context))

        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample