예제 #1
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(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,
                 planes,
                 base_width=64,
                 stride=1,
                 dilation=1,
                 norm='bn',
                 conv='normal',
                 context='none',
                 ctx_ratio=0.0625,
                 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.conv1 = nn.Conv2d(inplanes,
                               width,
                               kernel_size=1,
                               stride=str1x1,
                               bias=False)
        self.bn1 = make_norm(width, norm=norm.split('_')[-1])
        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
예제 #3
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