def __init__(self, backbone, num_classes, sync_bn=False): super(Decoder, self).__init__() if backbone == 'resnet' or backbone == 'drn': low_feature_size = 256 elif backbone == 'xception': low_feature_size = 128 elif backbone == 'mobilenet': low_feature_size = 24 elif backbone == 'inception': low_feature_size = 192 else: raise NotImplementedError self.conv1 = nn.Conv2d(low_feature_size, 48, 1, bias=False) self.bn1 = BatchNorm(48, sync_bn) self.relu = nn.ReLU() # here 304 = 256 + 48, is the sum size of low level feature and output feature self.last_conv = nn.Sequential( nn.Conv2d(304, 256, kernel_size=3, stride=1, padding=1, bias=False), BatchNorm(256, sync_bn), nn.ReLU(), nn.Dropout(0.5), nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=False), BatchNorm(256, sync_bn), nn.ReLU(), nn.Dropout(0.1), nn.Conv2d(256, num_classes, kernel_size=1, stride=1)) initial_weight(self.modules())
def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None, sync_bn=False): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = BatchNorm(planes, sync_bn) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes, dilation=dilation, padding=dilation) self.bn2 = BatchNorm(planes, sync_bn) self.downsample = downsample self.stride = stride
def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None, sync_bn=False): super(Bottleneck, self).__init__() self.conv1 = conv1x1(inplanes, planes) self.bn1 = BatchNorm(planes, sync_bn) self.conv2 = conv3x3(planes, planes, stride, dilation=dilation, padding=dilation) self.bn2 = BatchNorm(planes, sync_bn) self.conv3 = conv1x1(planes, planes * self.expansion) self.bn3 = BatchNorm(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride
def __init__(self, backbone, output_scale, sync_bn=False): super(ASPP, self).__init__() if backbone == 'drn': inplanes = 512 elif backbone == 'mobilenet': inplanes = 320 else: inplanes = 2048 if output_scale == 16: dilations = [1, 6, 12, 18] elif output_scale == 8: dilations = [1, 12, 24, 36] else: raise NotImplementedError self.layer1 = ASPPBlock(inplanes, 256, 1, padding=0, dilation=dilations[0], sync_bn=sync_bn) self.layer2 = ASPPBlock(inplanes, 256, 3, padding=dilations[1], dilation=dilations[1], sync_bn=sync_bn) self.layer3 = ASPPBlock(inplanes, 256, 3, padding=dilations[2], dilation=dilations[2], sync_bn=sync_bn) self.layer4 = ASPPBlock(inplanes, 256, 3, padding=dilations[3], dilation=dilations[3], sync_bn=sync_bn) self.global_avg_pool = nn.Sequential( nn.AdaptiveAvgPool2d((1, 1)), nn.Conv2d(inplanes, 256, 1, 1, bias=False), BatchNorm(256, sync_bn), nn.ReLU()) self.conv1 = nn.Conv2d(256 * 5, 256, 1, bias=False) self.bn1 = BatchNorm(256, sync_bn) self.relu = nn.ReLU() self.dropout = nn.Dropout(0.5) initial_weight(self.modules())
def __init__(self, block, layers, output_scale=16, sync_bn=False, zero_init_residual=False): super(ResNet, self).__init__() self.inplanes = 64 if output_scale == 16: strides = [1, 2, 2, 1] dilations = [1, 1, 1, 2] elif output_scale == 8: strides = [1, 2, 1, 1] dilations = [1, 1, 2, 4] else: raise NotImplementedError self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = BatchNorm(64, sync_bn) 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], sync_bn=sync_bn) self.layer2 = self._make_layer(block, 128, layers[1], stride=strides[1], dilation=dilations[1], sync_bn=sync_bn) self.layer3 = self._make_layer(block, 256, layers[2], stride=strides[2], dilation=dilations[2], sync_bn=sync_bn) self.layer4 = self._make_layer(block, 512, layers[3], stride=strides[3], dilation=dilations[3], sync_bn=sync_bn) initial_weight(self.modules()) # Zero-initialize the last BN in each residual branch, # so that the residual branch starts with zeros, and each residual block behaves like an identity. # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677 if zero_init_residual: for m in self.modules(): if isinstance(m, Bottleneck): nn.init.constant_(m.bn3.weight, 0) elif isinstance(m, BasicBlock): nn.init.constant_(m.bn2.weight, 0)
def _make_layer(self, block, planes, blocks, stride=1, dilation=1, sync_bn=False): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( conv1x1(self.inplanes, planes * block.expansion, stride), BatchNorm(planes * block.expansion, sync_bn), ) layers = [] layers.append(block(self.inplanes, planes, stride, dilation, downsample, sync_bn)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, dilation=dilation, sync_bn=sync_bn)) return nn.Sequential(*layers)
def __init__(self, inplanes, planes, kernel, padding, dilation, sync_bn=False): super(ASPPBlock, self).__init__() self.atrous_conv = nn.Conv2d(inplanes, planes, kernel_size=kernel, stride=1, padding=padding, dilation=dilation, bias=False) self.bn = BatchNorm(planes, sync_bn) # what will happen if use LeakyReLU self.relu = nn.ReLU()
def __init__(self, in_channels, out_channels, sync_bn, **kwargs): super(BasicConv2d, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) self.bn = BatchNorm(out_channels, sync_bn)