示例#1
0
 def __init__(self,
              inplanes,
              planes,
              groups,
              reduction,
              stride=1,
              downsample=None,
              base_width=4):
     super(SEResNeXtBottleneck, self).__init__()
     width = math.floor(planes * (base_width / 64)) * groups
     self.conv1 = nn.Conv2d(inplanes,
                            width,
                            kernel_size=1,
                            bias=False,
                            stride=1)
     self.bn1 = SyncBN2d(width)
     self.conv2 = nn.Conv2d(width,
                            width,
                            kernel_size=3,
                            stride=stride,
                            padding=1,
                            groups=groups,
                            bias=False)
     self.bn2 = SyncBN2d(width)
     self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False)
     self.bn3 = SyncBN2d(planes * 4)
     self.relu = nn.ReLU(inplace=True)
     self.se_module = SEModule(planes * 4, reduction=reduction)
     self.downsample = downsample
     self.stride = stride
示例#2
0
 def __init__(self,
              inplanes,
              planes,
              groups,
              reduction,
              stride=1,
              downsample=None):
     super(SEResNetBottleneck, self).__init__()
     self.conv1 = nn.Conv2d(inplanes,
                            planes,
                            kernel_size=1,
                            bias=False,
                            stride=stride)
     self.bn1 = SyncBN2d(planes)
     self.conv2 = nn.Conv2d(planes,
                            planes,
                            kernel_size=3,
                            padding=1,
                            groups=groups,
                            bias=False)
     self.bn2 = SyncBN2d(planes)
     self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
     self.bn3 = SyncBN2d(planes * 4)
     self.relu = nn.ReLU(inplace=True)
     self.se_module = SEModule(planes * 4, reduction=reduction)
     self.downsample = downsample
     self.stride = stride
示例#3
0
文件: unet_ae.py 项目: Alisirs/MECNet
 def __init__(self, in_channels, out_channels, stride=1, downsample=None):
     super(Bottleneck, self).__init__()
     channels = out_channels // 4
     self.conv1 = nn.Conv2d(in_channels,
                            channels,
                            kernel_size=1,
                            bias=False)
     self.bn1 = SyncBN2d(channels)
     self.conv2 = nn.Conv2d(channels,
                            channels,
                            kernel_size=3,
                            stride=stride,
                            padding=1,
                            bias=False)
     self.bn2 = SyncBN2d(channels)
     self.conv3 = nn.Conv2d(channels,
                            out_channels,
                            kernel_size=1,
                            bias=False)
     self.bn3 = SyncBN2d(out_channels)
     self.relu = nn.ReLU(inplace=True)
     self.downsample = downsample
     if (self.downsample is None) and (in_channels != out_channels):
         self.downsample = nn.Sequential(
             nn.Conv2d(in_channels, out_channels, kernel_size=1,
                       bias=False), SyncBN2d(out_channels))
     self.stride = stride
 def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None):
     super(BasicBlock, self).__init__()
     self.conv1 = conv3x3(inplanes, planes, stride, dilation)
     self.bn1 = SyncBN2d(planes)
     self.relu = nn.ReLU(inplace=True)
     self.conv2 = conv3x3(planes, planes)
     self.bn2 = SyncBN2d(planes)
     self.downsample = downsample
     self.stride = stride
示例#5
0
    def __init__(self,
                 in_channels,
                 num_classes,
                 backend='resnet18',
                 pool_scales=(1, 2, 3, 6),
                 pretrained='imagenet'):
        '''
        :param in_channels:
        :param num_classes:
        :param backend: only support resnet, otherwise need to have low_features for aux
                        and high_features methods for out
        '''
        super(PSPNet, self).__init__()
        self.in_channes = in_channels
        self.num_classes = num_classes

        if hasattr(backend, 'low_features') and hasattr(backend, 'high_features') \
                and hasattr(backend, 'lastconv_channel'):
            self.backend = backend
        elif 'resnet' in backend:
            ## in the future, I may retrain on os=16 (now without os)
            self.backend = ResnetBackend(backend,
                                         os=None,
                                         pretrained=pretrained)
        elif 'resnext' in backend:
            self.backend = ResnetBackend(backend,
                                         os=None,
                                         pretrained=pretrained)
        else:
            raise NotImplementedError

        self.pp_out_channel = 256
        self.pyramid_pooling = PPBlock(self.backend.lastconv_channel,
                                       out_channel=self.pp_out_channel,
                                       pool_scales=pool_scales)

        self.cbr_last = nn.Sequential(
            nn.Conv2d(self.backend.lastconv_channel +
                      self.pp_out_channel * len(pool_scales),
                      self.pp_out_channel,
                      kernel_size=3,
                      padding=1,
                      bias=False), SyncBN2d(self.pp_out_channel),
            nn.ReLU(inplace=True), nn.Dropout2d(0.1),
            nn.Conv2d(self.pp_out_channel, num_classes, kernel_size=1))
        self.cbr_deepsup = nn.Sequential(
            nn.Conv2d(self.backend.lastconv_channel // 2,
                      self.backend.lastconv_channel // 4,
                      kernel_size=3,
                      padding=1,
                      bias=False),
            SyncBN2d(self.backend.lastconv_channel // 4),
            nn.ReLU(inplace=True), nn.Dropout2d(0.1),
            nn.Conv2d(self.backend.lastconv_channel // 4,
                      num_classes,
                      kernel_size=1))
 def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None):
     super(Bottleneck, self).__init__()
     self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
     self.bn1 = SyncBN2d(planes)
     self.conv2 = conv3x3(planes, planes, stride=stride, dilation=dilation)
     self.bn2 = SyncBN2d(planes)
     self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
     self.bn3 = SyncBN2d(planes * self.expansion)
     self.relu = nn.ReLU(inplace=True)
     self.downsample = downsample
     self.stride = stride
示例#7
0
 def __init__(self, in_channel, out_channel):
     super(unetConv2d, self).__init__()
     self.conv = nn.Sequential(
         nn.Conv2d(in_channel,
                   out_channel,
                   kernel_size=3,
                   padding=1,
                   bias=False), SyncBN2d(out_channel),
         nn.ReLU(inplace=True),
         nn.Conv2d(out_channel,
                   out_channel,
                   kernel_size=3,
                   padding=1,
                   bias=False), SyncBN2d(out_channel),
         nn.ReLU(inplace=True))
 def __init__(self, block, layers, output_stride=16):
     if output_stride == 16:
         dilations = [1, 1, 1, 2]
         strides = [1, 2, 2, 1]
     elif output_stride == 8:
         dilations = [1, 1, 2, 4]
         strides = [1, 2, 1, 1]
     elif output_stride is None:
         dilations = [1, 1, 1, 1]
         strides = [1, 2, 2, 2]
     else:
         raise Warning("output_stride must be 8 or 16 or None!")
     self.inplanes = 64
     super(ResNet, self).__init__()
     self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
                            bias=False)
     self.bn1 = SyncBN2d(64)
     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], stride=strides[0], dilation=dilations[0])
     self.layer2 = self._make_layer(block, 128, layers[1], stride=strides[1], dilation=dilations[1])
     self.layer3 = self._make_layer(block, 256, layers[2], stride=strides[2], dilation=dilations[2])
     self.layer4 = self._make_layer(block, 512, layers[3], stride=strides[3], dilation=dilations[3])
     # self.LS = nn.LogSoftmax(dim=1)
     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, SyncBN2d):
             m.weight.data.fill_(1)
             m.bias.data.zero_()
示例#9
0
    def _make_layer(self,
                    block,
                    planes,
                    blocks,
                    groups,
                    reduction,
                    stride=1,
                    downsample_kernel_size=1,
                    downsample_padding=0):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes,
                          planes * block.expansion,
                          kernel_size=downsample_kernel_size,
                          stride=stride,
                          padding=downsample_padding,
                          bias=False),
                SyncBN2d(planes * block.expansion),
            )

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

        return nn.Sequential(*layers)
 def __init__(self,
              in_channel=2048,
              out_channel=256,
              pool_scales=(1, 2, 3, 6)):
     '''
     :param in_channel: default 2048 for resnet50 backend
     :param out_channel: default 256 for resnet50 backend
     :param pool_scales: default scales [1,2,3,6]
     '''
     super(PPBlock, self).__init__()
     self.pp_bra1 = nn.Sequential(
         OrderedDict([('pool1', nn.AdaptiveAvgPool2d(pool_scales[0])),
                      ('conv1',
                       nn.Conv2d(in_channel,
                                 out_channel,
                                 kernel_size=1,
                                 bias=False)),
                      ('bn1', SyncBN2d(out_channel)),
                      ('relu1', nn.ReLU(inplace=True))]))
     self.pp_bra2 = nn.Sequential(
         OrderedDict([('pool2', nn.AdaptiveAvgPool2d(pool_scales[1])),
                      ('conv2',
                       nn.Conv2d(in_channel,
                                 out_channel,
                                 kernel_size=1,
                                 bias=False)),
                      ('bn2', SyncBN2d(out_channel)),
                      ('relu2', nn.ReLU(inplace=True))]))
     self.pp_bra3 = nn.Sequential(
         OrderedDict([('pool3', nn.AdaptiveAvgPool2d(pool_scales[2])),
                      ('conv3',
                       nn.Conv2d(in_channel,
                                 out_channel,
                                 kernel_size=1,
                                 bias=False)),
                      ('bn3', SyncBN2d(out_channel)),
                      ('relu3', nn.ReLU(inplace=True))]))
     self.pp_bra4 = nn.Sequential(
         OrderedDict([('pool4', nn.AdaptiveAvgPool2d(pool_scales[3])),
                      ('conv4',
                       nn.Conv2d(in_channel,
                                 out_channel,
                                 kernel_size=1,
                                 bias=False)),
                      ('bn4', SyncBN2d(out_channel)),
                      ('relu4', nn.ReLU(inplace=True))]))
示例#11
0
    def __init__(self, inp, oup, stride, dilation, expand_ratio):
        super(InvertedResidual, self).__init__()
        self.stride = stride
        assert stride in [1, 2]

        hidden_dim = round(inp * expand_ratio)
        self.use_res_connect = self.stride == 1 and inp == oup

        if expand_ratio == 1:
            self.conv = nn.Sequential(
                # dw
                nn.Conv2d(hidden_dim,
                          hidden_dim,
                          3,
                          stride,
                          dilation,
                          dilation,
                          groups=hidden_dim,
                          bias=False),
                SyncBN2d(hidden_dim),
                nn.ReLU6(inplace=True),
                # pw-linear
                nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                SyncBN2d(oup),
            )
        else:
            self.conv = nn.Sequential(
                # pw
                nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
                SyncBN2d(hidden_dim),
                nn.ReLU6(inplace=True),
                # dw
                nn.Conv2d(hidden_dim,
                          hidden_dim,
                          3,
                          stride,
                          dilation,
                          dilation,
                          groups=hidden_dim,
                          bias=False),
                SyncBN2d(hidden_dim),
                nn.ReLU6(inplace=True),
                # pw-linear
                nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                SyncBN2d(oup),
            )
示例#12
0
文件: unet_ae.py 项目: Alisirs/MECNet
    def __init__(self,
                 in_channels,
                 num_classes,
                 backend='resnet18',
                 pretrained='imagenet'):
        super(UnetResnetAE, self).__init__()
        self.in_channes = in_channels
        self.num_classes = num_classes
        if 'resne' in backend:
            self.encoder = ResnetBackend(backend, pretrained)
        else:
            raise NotImplementedError

        if backend in ['resnet18', 'resnet34']:
            block = BasicBlock
        else:
            block = Bottleneck

        inter_channel = self.encoder.lastconv_channel

        self.center = nn.Sequential(
            nn.Conv2d(inter_channel, 512, kernel_size=3, padding=1,
                      bias=False), SyncBN2d(512), nn.ReLU(inplace=True),
            nn.Conv2d(512, 256, kernel_size=3, padding=1, bias=False),
            SyncBN2d(256), nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2))

        self.decoder5 = Decoder(block, 256 + inter_channel, 512, 64)
        self.decoder4 = Decoder(block, 64 + inter_channel // 2, 256, 64)
        self.decoder3 = Decoder(block, 64 + inter_channel // 4, 128, 64)
        self.decoder2 = Decoder(block, 64 + inter_channel // 8, 64, 64)
        self.decoder1 = Decoder(block, 64, 32, 64)

        self.cbr_last = nn.Sequential(
            nn.Conv2d(64 * 6, 64, kernel_size=3, padding=1, bias=False),
            SyncBN2d(64), nn.ReLU(inplace=True),
            nn.Conv2d(64, self.num_classes, kernel_size=1))
示例#13
0
 def __init__(self,
              block,
              layers,
              groups,
              reduction,
              dropout_p=0.2,
              inplanes=128,
              input_3x3=True,
              downsample_kernel_size=3,
              downsample_padding=1,
              num_classes=1000):
     """
     Parameters
     ----------
     block (nn.Module): Bottleneck class.
         - For SENet154: SEBottleneck
         - For SE-ResNet compare_models: SEResNetBottleneck
         - For SE-ResNeXt compare_models:  SEResNeXtBottleneck
     layers (list of ints): Number of residual blocks for 4 layers of the
         network (layer1...layer4).
     groups (int): Number of groups for the 3x3 convolution in each
         bottleneck block.
         - For SENet154: 64
         - For SE-ResNet compare_models: 1
         - For SE-ResNeXt compare_models:  32
     reduction (int): Reduction ratio for Squeeze-and-Excitation modules.
         - For all compare_models: 16
     dropout_p (float or None): Drop probability for the Dropout layer.
         If `None` the Dropout layer is not used.
         - For SENet154: 0.2
         - For SE-ResNet compare_models: None
         - For SE-ResNeXt compare_models: None
     inplanes (int):  Number of input channels for layer1.
         - For SENet154: 128
         - For SE-ResNet compare_models: 64
         - For SE-ResNeXt compare_models: 64
     input_3x3 (bool): If `True`, use three 3x3 convolutions instead of
         a single 7x7 convolution in layer0.
         - For SENet154: True
         - For SE-ResNet compare_models: False
         - For SE-ResNeXt compare_models: False
     downsample_kernel_size (int): Kernel size for downsampling convolutions
         in layer2, layer3 and layer4.
         - For SENet154: 3
         - For SE-ResNet compare_models: 1
         - For SE-ResNeXt compare_models: 1
     downsample_padding (int): Padding for downsampling convolutions in
         layer2, layer3 and layer4.
         - For SENet154: 1
         - For SE-ResNet compare_models: 0
         - For SE-ResNeXt compare_models: 0
     num_classes (int): Number of outputs in `last_linear` layer.
         - For all compare_models: 1000
     """
     super(SENet, self).__init__()
     self.inplanes = inplanes
     if input_3x3:
         layer0_modules = [
             ('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,
                                 bias=False)),
             ('bn1', SyncBN2d(64)),
             ('relu1', nn.ReLU(inplace=True)),
             ('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,
                                 bias=False)),
             ('bn2', SyncBN2d(64)),
             ('relu2', nn.ReLU(inplace=True)),
             ('conv3',
              nn.Conv2d(64, inplanes, 3, stride=1, padding=1, bias=False)),
             ('bn3', SyncBN2d(inplanes)),
             ('relu3', nn.ReLU(inplace=True)),
         ]
     else:
         layer0_modules = [
             ('conv1',
              nn.Conv2d(3,
                        inplanes,
                        kernel_size=7,
                        stride=2,
                        padding=3,
                        bias=False)),
             ('bn1', SyncBN2d(inplanes)),
             ('relu1', nn.ReLU(inplace=True)),
         ]
     # To preserve compatibility with Caffe weights `ceil_mode=True`
     # is used instead of `padding=1`.
     layer0_modules.append(('pool', nn.MaxPool2d(3,
                                                 stride=2,
                                                 ceil_mode=True)))
     self.layer0 = nn.Sequential(OrderedDict(layer0_modules))
     self.layer1 = self._make_layer(block,
                                    planes=64,
                                    blocks=layers[0],
                                    groups=groups,
                                    reduction=reduction,
                                    downsample_kernel_size=1,
                                    downsample_padding=0)
     self.layer2 = self._make_layer(
         block,
         planes=128,
         blocks=layers[1],
         stride=2,
         groups=groups,
         reduction=reduction,
         downsample_kernel_size=downsample_kernel_size,
         downsample_padding=downsample_padding)
     self.layer3 = self._make_layer(
         block,
         planes=256,
         blocks=layers[2],
         stride=2,
         groups=groups,
         reduction=reduction,
         downsample_kernel_size=downsample_kernel_size,
         downsample_padding=downsample_padding)
     self.layer4 = self._make_layer(
         block,
         planes=512,
         blocks=layers[3],
         stride=2,
         groups=groups,
         reduction=reduction,
         downsample_kernel_size=downsample_kernel_size,
         downsample_padding=downsample_padding)
     self.avg_pool = nn.AvgPool2d(7, stride=1)
     self.dropout = nn.Dropout(dropout_p) if dropout_p is not None else None
     self.last_linear = nn.Linear(512 * block.expansion, num_classes)
示例#14
0
def conv_1x1_bn(inp, oup):
    return nn.Sequential(nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
                         SyncBN2d(oup), nn.ReLU6(inplace=True))
示例#15
0
def conv_bn(inp, oup, stride):
    return nn.Sequential(nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
                         SyncBN2d(oup), nn.ReLU6(inplace=True))
    def __init__(self, in_channel=2048, out_channel=256, os=16):
        '''
        :param in_channel: default 2048 for resnet101
        :param out_channel: default 256 for resnet101
        :param os: 16 or 8
        '''
        super(ASPPBlock, self).__init__()
        if os == 16:
            rates = [1, 6, 12, 18]
        elif os == 8:
            rates = [1, 12, 24, 36]
        else:
            raise NotImplementedError

        self.gave_pool = nn.Sequential(
            OrderedDict([('gavg', nn.AdaptiveAvgPool2d(rates[0])),
                         ('conv0_1',
                          nn.Conv2d(in_channel,
                                    out_channel,
                                    kernel_size=1,
                                    bias=False)),
                         ('bn0_1', SyncBN2d(out_channel)),
                         ('relu0_1', nn.ReLU(inplace=True))]))
        self.conv1_1 = nn.Sequential(
            OrderedDict([('conv0_2',
                          nn.Conv2d(in_channel,
                                    out_channel,
                                    kernel_size=1,
                                    bias=False)),
                         ('bn0_2', SyncBN2d(out_channel)),
                         ('relu0_2', nn.ReLU(inplace=True))]))
        self.aspp_bra1 = nn.Sequential(
            OrderedDict([('conv1_1',
                          nn.Conv2d(in_channel,
                                    out_channel,
                                    kernel_size=3,
                                    padding=rates[1],
                                    dilation=rates[1],
                                    bias=False)),
                         ('bn1_1', SyncBN2d(out_channel)),
                         ('relu1_1', nn.ReLU(inplace=True))]))
        self.aspp_bra2 = nn.Sequential(
            OrderedDict([('conv1_2',
                          nn.Conv2d(in_channel,
                                    out_channel,
                                    kernel_size=3,
                                    padding=rates[2],
                                    dilation=rates[2],
                                    bias=False)),
                         ('bn1_2', SyncBN2d(out_channel)),
                         ('relu1_2', nn.ReLU(inplace=True))]))
        self.aspp_bra3 = nn.Sequential(
            OrderedDict([('conv1_3',
                          nn.Conv2d(in_channel,
                                    out_channel,
                                    kernel_size=3,
                                    padding=rates[3],
                                    dilation=rates[3],
                                    bias=False)),
                         ('bn1_3', SyncBN2d(out_channel)),
                         ('relu1_3', nn.ReLU(inplace=True))]))
        self.aspp_catdown = nn.Sequential(
            OrderedDict([('conv_down',
                          nn.Conv2d(5 * out_channel,
                                    out_channel,
                                    kernel_size=1,
                                    bias=False)),
                         ('bn_down', SyncBN2d(out_channel)),
                         ('relu_down', nn.ReLU(inplace=True)),
                         ('drop_out', nn.Dropout(.1))]))
示例#17
0
    def __init__(self,
                 in_channels,
                 num_classes,
                 backend='resnet18',
                 os=16,
                 pretrained='imagenet'):
        '''
        :param in_channels:
        :param num_classes:
        :param backend: only support resnet, otherwise need to have low_features
                        and high_features methods for out
        '''
        super(DeepLabv3_plus, self).__init__()
        self.in_channes = in_channels
        self.num_classes = num_classes
        if hasattr(backend, 'low_features') and hasattr(backend, 'high_features') \
                and hasattr(backend, 'lastconv_channel'):
            self.backend = backend
        elif 'resnet' in backend:
            self.backend = ResnetBackend(backend, os, pretrained)
        elif 'resnext' in backend:
            self.backend = ResnetBackend(backend,
                                         os=None,
                                         pretrained=pretrained)
        elif 'mobilenet' in backend:
            self.backend = MobileNetBackend(backend,
                                            os=os,
                                            pretrained=pretrained)
        elif 'shufflenet' in backend:
            self.backend = ShuffleNetBackend(backend,
                                             os=os,
                                             pretrained=pretrained)
        else:
            raise NotImplementedError

        if hasattr(self.backend, 'interconv_channel'):
            self.aspp_out_channel = self.backend.interconv_channel
        else:
            self.aspp_out_channel = self.backend.lastconv_channel // 8

        self.aspp_pooling = ASPPBlock(self.backend.lastconv_channel,
                                      self.aspp_out_channel, os)

        self.cbr_low = nn.Sequential(
            nn.Conv2d(self.aspp_out_channel,
                      self.aspp_out_channel // 5,
                      kernel_size=1,
                      bias=False), SyncBN2d(self.aspp_out_channel // 5),
            nn.ReLU(inplace=True))
        self.cbr_last = nn.Sequential(
            nn.Conv2d(self.aspp_out_channel + self.aspp_out_channel // 5,
                      self.aspp_out_channel,
                      kernel_size=3,
                      padding=1,
                      bias=False), SyncBN2d(self.aspp_out_channel),
            nn.ReLU(inplace=True),
            nn.Conv2d(self.aspp_out_channel,
                      self.aspp_out_channel,
                      kernel_size=3,
                      padding=1,
                      bias=False), SyncBN2d(self.aspp_out_channel),
            nn.ReLU(inplace=True),
            nn.Conv2d(self.aspp_out_channel, self.num_classes, kernel_size=1))