def __init__(self,
              inplanes,
              planes,
              bn_norm,
              with_ibn=False,
              with_se=False,
              stride=1,
              downsample=None,
              reduction=16):
     super(BasicBlock, self).__init__()
     self.conv1 = nn.Conv2d(inplanes,
                            planes,
                            kernel_size=3,
                            stride=stride,
                            padding=1,
                            bias=False)
     if with_ibn:
         self.bn1 = IBN(planes, bn_norm)
     else:
         self.bn1 = get_norm(bn_norm, planes)
     self.conv2 = nn.Conv2d(planes,
                            planes,
                            kernel_size=3,
                            stride=1,
                            padding=1,
                            bias=False)
     self.bn2 = get_norm(bn_norm, planes)
     self.relu = nn.ReLU(inplace=True)
     if with_se:
         self.se = SELayer(planes, reduction)
     else:
         self.se = nn.Identity()
     self.downsample = downsample
     self.stride = stride
    def _make_layer(self,
                    block,
                    planes,
                    blocks,
                    stride=1,
                    bn_norm="BN",
                    with_ibn=False,
                    with_se=False):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes,
                          planes * block.expansion,
                          kernel_size=1,
                          stride=stride,
                          bias=False),
                get_norm(bn_norm, planes * block.expansion),
            )

        layers = []
        layers.append(
            block(self.inplanes, planes, bn_norm, with_ibn, with_se, stride,
                  downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(
                block(self.inplanes, planes, bn_norm, with_ibn, with_se))

        return nn.Sequential(*layers)
    def __init__(self, last_stride, bn_norm, with_ibn, with_se, with_nl, block,
                 layers, non_layers):
        self.inplanes = 64
        super().__init__()
        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = get_norm(bn_norm, 64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        # self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True)
        self.layer1 = self._make_layer(block, 64, layers[0], 1, bn_norm,
                                       with_ibn, with_se)
        self.layer2 = self._make_layer(block, 128, layers[1], 2, bn_norm,
                                       with_ibn, with_se)
        self.layer3 = self._make_layer(block, 256, layers[2], 2, bn_norm,
                                       with_ibn, with_se)
        self.layer4 = self._make_layer(block,
                                       512,
                                       layers[3],
                                       last_stride,
                                       bn_norm,
                                       with_se=with_se)

        self.random_init()

        # fmt: off
        if with_nl: self._build_nonlocal(layers, non_layers, bn_norm)
        else:
            self.NL_1_idx = self.NL_2_idx = self.NL_3_idx = self.NL_4_idx = []
Esempio n. 4
0
 def __init__(self, in_channels, out_channels, bn_norm, stride=1):
     super(Conv1x1Linear, self).__init__()
     self.conv = nn.Conv2d(in_channels,
                           out_channels,
                           1,
                           stride=stride,
                           padding=0,
                           bias=False)
     self.bn = get_norm(bn_norm, out_channels)
Esempio n. 5
0
 def __init__(self, in_channels, out_channels, bn_norm, stride=1, groups=1):
     super(Conv1x1, self).__init__()
     self.conv = nn.Conv2d(in_channels,
                           out_channels,
                           1,
                           stride=stride,
                           padding=0,
                           bias=False,
                           groups=groups)
     self.bn = get_norm(bn_norm, out_channels)
     self.relu = nn.ReLU(inplace=True)
Esempio n. 6
0
 def __init__(self, in_channels, out_channels, bn_norm):
     super(LightConv3x3, self).__init__()
     self.conv1 = nn.Conv2d(in_channels,
                            out_channels,
                            1,
                            stride=1,
                            padding=0,
                            bias=False)
     self.conv2 = nn.Conv2d(out_channels,
                            out_channels,
                            3,
                            stride=1,
                            padding=1,
                            bias=False,
                            groups=out_channels)
     self.bn = get_norm(bn_norm, out_channels)
     self.relu = nn.ReLU(inplace=True)
Esempio n. 7
0
 def __init__(self,
              in_channels,
              out_channels,
              kernel_size,
              bn_norm,
              stride=1,
              padding=0,
              groups=1,
              IN=False):
     super(ConvLayer, self).__init__()
     self.conv = nn.Conv2d(in_channels,
                           out_channels,
                           kernel_size,
                           stride=stride,
                           padding=padding,
                           bias=False,
                           groups=groups)
     if IN:
         self.bn = nn.InstanceNorm2d(out_channels, affine=True)
     else:
         self.bn = get_norm(bn_norm, out_channels)
     self.relu = nn.ReLU(inplace=True)
    def __init__(self,
                 inplanes,
                 planes,
                 bn_norm,
                 with_ibn=False,
                 stride=1,
                 downsample=None,
                 radix=1,
                 cardinality=1,
                 bottleneck_width=64,
                 avd=False,
                 avd_first=False,
                 dilation=1,
                 is_first=False,
                 rectified_conv=False,
                 rectify_avg=False,
                 dropblock_prob=0.0,
                 last_gamma=False):
        super(Bottleneck, self).__init__()
        group_width = int(planes * (bottleneck_width / 64.)) * cardinality
        self.conv1 = nn.Conv2d(inplanes,
                               group_width,
                               kernel_size=1,
                               bias=False)
        if with_ibn:
            self.bn1 = IBN(group_width, bn_norm)
        else:
            self.bn1 = get_norm(bn_norm, group_width)
        self.dropblock_prob = dropblock_prob
        self.radix = radix
        self.avd = avd and (stride > 1 or is_first)
        self.avd_first = avd_first

        if self.avd:
            self.avd_layer = nn.AvgPool2d(3, stride, padding=1)
            stride = 1

        if radix > 1:
            self.conv2 = SplAtConv2d(group_width,
                                     group_width,
                                     kernel_size=3,
                                     stride=stride,
                                     padding=dilation,
                                     dilation=dilation,
                                     groups=cardinality,
                                     bias=False,
                                     radix=radix,
                                     rectify=rectified_conv,
                                     rectify_avg=rectify_avg,
                                     norm_layer=bn_norm,
                                     dropblock_prob=dropblock_prob)
        elif rectified_conv:
            from rfconv import RFConv2d
            self.conv2 = RFConv2d(group_width,
                                  group_width,
                                  kernel_size=3,
                                  stride=stride,
                                  padding=dilation,
                                  dilation=dilation,
                                  groups=cardinality,
                                  bias=False,
                                  average_mode=rectify_avg)
            self.bn2 = get_norm(bn_norm, group_width)
        else:
            self.conv2 = nn.Conv2d(group_width,
                                   group_width,
                                   kernel_size=3,
                                   stride=stride,
                                   padding=dilation,
                                   dilation=dilation,
                                   groups=cardinality,
                                   bias=False)
            self.bn2 = get_norm(bn_norm, group_width)

        self.conv3 = nn.Conv2d(group_width,
                               planes * 4,
                               kernel_size=1,
                               bias=False)
        self.bn3 = get_norm(bn_norm, planes * 4)

        if last_gamma:
            from torch.nn.init import zeros_
            zeros_(self.bn3.weight)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.dilation = dilation
        self.stride = stride
    def _make_layer(self,
                    block,
                    planes,
                    blocks,
                    stride=1,
                    bn_norm="BN",
                    with_ibn=False,
                    dilation=1,
                    dropblock_prob=0.0,
                    is_first=True):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            down_layers = []
            if self.avg_down:
                if dilation == 1:
                    down_layers.append(
                        nn.AvgPool2d(kernel_size=stride,
                                     stride=stride,
                                     ceil_mode=True,
                                     count_include_pad=False))
                else:
                    down_layers.append(
                        nn.AvgPool2d(kernel_size=1,
                                     stride=1,
                                     ceil_mode=True,
                                     count_include_pad=False))
                down_layers.append(
                    nn.Conv2d(self.inplanes,
                              planes * block.expansion,
                              kernel_size=1,
                              stride=1,
                              bias=False))
            else:
                down_layers.append(
                    nn.Conv2d(self.inplanes,
                              planes * block.expansion,
                              kernel_size=1,
                              stride=stride,
                              bias=False))
            down_layers.append(get_norm(bn_norm, planes * block.expansion))
            downsample = nn.Sequential(*down_layers)

        layers = []
        if dilation == 1 or dilation == 2:
            layers.append(
                block(self.inplanes,
                      planes,
                      bn_norm,
                      with_ibn,
                      stride,
                      downsample=downsample,
                      radix=self.radix,
                      cardinality=self.cardinality,
                      bottleneck_width=self.bottleneck_width,
                      avd=self.avd,
                      avd_first=self.avd_first,
                      dilation=1,
                      is_first=is_first,
                      rectified_conv=self.rectified_conv,
                      rectify_avg=self.rectify_avg,
                      dropblock_prob=dropblock_prob,
                      last_gamma=self.last_gamma))
        elif dilation == 4:
            layers.append(
                block(self.inplanes,
                      planes,
                      bn_norm,
                      with_ibn,
                      stride,
                      downsample=downsample,
                      radix=self.radix,
                      cardinality=self.cardinality,
                      bottleneck_width=self.bottleneck_width,
                      avd=self.avd,
                      avd_first=self.avd_first,
                      dilation=2,
                      is_first=is_first,
                      rectified_conv=self.rectified_conv,
                      rectify_avg=self.rectify_avg,
                      dropblock_prob=dropblock_prob,
                      last_gamma=self.last_gamma))
        else:
            raise RuntimeError("=> unknown dilation size: {}".format(dilation))

        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(
                block(self.inplanes,
                      planes,
                      bn_norm,
                      with_ibn,
                      radix=self.radix,
                      cardinality=self.cardinality,
                      bottleneck_width=self.bottleneck_width,
                      avd=self.avd,
                      avd_first=self.avd_first,
                      dilation=dilation,
                      rectified_conv=self.rectified_conv,
                      rectify_avg=self.rectify_avg,
                      dropblock_prob=dropblock_prob,
                      last_gamma=self.last_gamma))

        return nn.Sequential(*layers)
    def __init__(self,
                 last_stride,
                 bn_norm,
                 with_ibn,
                 with_nl,
                 block,
                 layers,
                 non_layers,
                 radix=1,
                 groups=1,
                 bottleneck_width=64,
                 dilated=False,
                 dilation=1,
                 deep_stem=False,
                 stem_width=64,
                 avg_down=False,
                 rectified_conv=False,
                 rectify_avg=False,
                 avd=False,
                 avd_first=False,
                 final_drop=0.0,
                 dropblock_prob=0,
                 last_gamma=False):
        self.cardinality = groups
        self.bottleneck_width = bottleneck_width
        # ResNet-D params
        self.inplanes = stem_width * 2 if deep_stem else 64
        self.avg_down = avg_down
        self.last_gamma = last_gamma
        # ResNeSt params
        self.radix = radix
        self.avd = avd
        self.avd_first = avd_first

        super().__init__()
        self.rectified_conv = rectified_conv
        self.rectify_avg = rectify_avg
        if rectified_conv:
            from rfconv import RFConv2d
            conv_layer = RFConv2d
        else:
            conv_layer = nn.Conv2d
        conv_kwargs = {'average_mode': rectify_avg} if rectified_conv else {}
        if deep_stem:
            self.conv1 = nn.Sequential(
                conv_layer(3,
                           stem_width,
                           kernel_size=3,
                           stride=2,
                           padding=1,
                           bias=False,
                           **conv_kwargs),
                get_norm(bn_norm, stem_width),
                nn.ReLU(inplace=True),
                conv_layer(stem_width,
                           stem_width,
                           kernel_size=3,
                           stride=1,
                           padding=1,
                           bias=False,
                           **conv_kwargs),
                get_norm(bn_norm, stem_width),
                nn.ReLU(inplace=True),
                conv_layer(stem_width,
                           stem_width * 2,
                           kernel_size=3,
                           stride=1,
                           padding=1,
                           bias=False,
                           **conv_kwargs),
            )
        else:
            self.conv1 = conv_layer(3,
                                    64,
                                    kernel_size=7,
                                    stride=2,
                                    padding=3,
                                    bias=False,
                                    **conv_kwargs)
        self.bn1 = get_norm(bn_norm, self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block,
                                       64,
                                       layers[0],
                                       1,
                                       bn_norm,
                                       with_ibn=with_ibn,
                                       is_first=False)
        self.layer2 = self._make_layer(block,
                                       128,
                                       layers[1],
                                       2,
                                       bn_norm,
                                       with_ibn=with_ibn)
        if dilated or dilation == 4:
            self.layer3 = self._make_layer(block,
                                           256,
                                           layers[2],
                                           1,
                                           bn_norm,
                                           with_ibn=with_ibn,
                                           dilation=2,
                                           dropblock_prob=dropblock_prob)
            self.layer4 = self._make_layer(block,
                                           512,
                                           layers[3],
                                           1,
                                           bn_norm,
                                           with_ibn=with_ibn,
                                           dilation=4,
                                           dropblock_prob=dropblock_prob)
        elif dilation == 2:
            self.layer3 = self._make_layer(block,
                                           256,
                                           layers[2],
                                           2,
                                           bn_norm,
                                           with_ibn=with_ibn,
                                           dilation=1,
                                           dropblock_prob=dropblock_prob)
            self.layer4 = self._make_layer(block,
                                           512,
                                           layers[3],
                                           1,
                                           bn_norm,
                                           with_ibn=with_ibn,
                                           dilation=2,
                                           dropblock_prob=dropblock_prob)
        else:
            self.layer3 = self._make_layer(block,
                                           256,
                                           layers[2],
                                           2,
                                           bn_norm,
                                           with_ibn=with_ibn,
                                           dropblock_prob=dropblock_prob)
            self.layer4 = self._make_layer(block,
                                           512,
                                           layers[3],
                                           last_stride,
                                           bn_norm,
                                           with_ibn=with_ibn,
                                           dropblock_prob=dropblock_prob)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

        # fmt: off
        if with_nl: self._build_nonlocal(layers, non_layers, bn_norm)
        else:
            self.NL_1_idx = self.NL_2_idx = self.NL_3_idx = self.NL_4_idx = []
    def __init__(self,
                 in_channels,
                 channels,
                 kernel_size,
                 stride=(1, 1),
                 padding=(0, 0),
                 dilation=(1, 1),
                 groups=1,
                 bias=True,
                 radix=2,
                 reduction_factor=4,
                 rectify=False,
                 rectify_avg=False,
                 norm_layer=None,
                 num_splits=1,
                 dropblock_prob=0.0,
                 **kwargs):
        super(SplAtConv2d, self).__init__()
        padding = _pair(padding)
        self.rectify = rectify and (padding[0] > 0 or padding[1] > 0)
        self.rectify_avg = rectify_avg
        inter_channels = max(in_channels * radix // reduction_factor, 32)
        self.radix = radix
        self.cardinality = groups
        self.channels = channels
        self.dropblock_prob = dropblock_prob
        if self.rectify:
            from rfconv import RFConv2d
            self.conv = RFConv2d(in_channels,
                                 channels * radix,
                                 kernel_size,
                                 stride,
                                 padding,
                                 dilation,
                                 groups=groups * radix,
                                 bias=bias,
                                 average_mode=rectify_avg,
                                 **kwargs)
        else:
            self.conv = Conv2d(in_channels,
                               channels * radix,
                               kernel_size,
                               stride,
                               padding,
                               dilation,
                               groups=groups * radix,
                               bias=bias,
                               **kwargs)
        self.use_bn = norm_layer is not None
        if self.use_bn:
            self.bn0 = get_norm(norm_layer, channels * radix, num_splits)
        self.relu = ReLU(inplace=True)
        self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
        if self.use_bn:
            self.bn1 = get_norm(norm_layer, inter_channels, num_splits)
        self.fc2 = Conv2d(inter_channels,
                          channels * radix,
                          1,
                          groups=self.cardinality)

        self.rsoftmax = rSoftMax(radix, groups)