import torch import torch.nn as nn import torch.nn.init as init import torch.nn.functional as F import torch.nn.utils.spectral_norm as SpectralNorm from math import sqrt def scale_conv_weights_fixup(conv, residual_block_count, m=2): k = conv.kernel_size[0] n = conv.out_channels scaling_factor = residual_block_count ** (-1.0 / (2 * m - 2)) sigma = sqrt(2 / (k * k * n)) * scaling_factor conv.weight.data = conv.weight.data * sigma return conv def initialize_weights(net_l, scale=1): if not isinstance(net_l, list): net_l = [net_l] for net in net_l: for m in net.modules(): if isinstance(m, nn.Conv2d): init.kaiming_normal_(m.weight, a=0, mode='fan_in') m.weight.data *= scale # for residual block if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.Linear): init.kaiming_normal_(m.weight, a=0, mode='fan_in') m.weight.data *= scale if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): init.constant_(m.weight, 1) init.constant_(m.bias.data, 0.0) def make_layer(block, n_layers): layers = [] for _ in range(n_layers): layers.append(block()) return nn.Sequential(*layers) def conv3x3(in_planes, out_planes, stride=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) def conv1x1(in_planes, out_planes, stride=1): """1x1 convolution""" return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False) class FixupBasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None): super(FixupBasicBlock, self).__init__() # Both self.conv1 and self.downsample layers downsample the input when stride != 1 self.bias1a = nn.Parameter(torch.zeros(1)) self.conv1 = conv3x3(inplanes, planes, stride) self.bias1b = nn.Parameter(torch.zeros(1)) self.relu = nn.ReLU(inplace=True) self.bias2a = nn.Parameter(torch.zeros(1)) self.conv2 = conv3x3(planes, planes) self.scale = nn.Parameter(torch.ones(1)) self.bias2b = nn.Parameter(torch.zeros(1)) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x + self.bias1a) out = self.relu(out + self.bias1b) out = self.conv2(out + self.bias2a) out = out * self.scale + self.bias2b if self.downsample is not None: identity = self.downsample(x + self.bias1a) out += identity out = self.relu(out) return out class FixupBottleneck(nn.Module): expansion = 4 def __init__(self, inplanes, planes, stride=1, downsample=None): super(FixupBottleneck, self).__init__() # Both self.conv2 and self.downsample layers downsample the input when stride != 1 self.bias1a = nn.Parameter(torch.zeros(1)) self.conv1 = conv1x1(inplanes, planes) self.bias1b = nn.Parameter(torch.zeros(1)) self.bias2a = nn.Parameter(torch.zeros(1)) self.conv2 = conv3x3(planes, planes, stride) self.bias2b = nn.Parameter(torch.zeros(1)) self.bias3a = nn.Parameter(torch.zeros(1)) self.conv3 = conv1x1(planes, planes * self.expansion) self.scale = nn.Parameter(torch.ones(1)) self.bias3b = nn.Parameter(torch.zeros(1)) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x + self.bias1a) out = self.relu(out + self.bias1b) out = self.conv2(out + self.bias2a) out = self.relu(out + self.bias2b) out = self.conv3(out + self.bias3a) out = out * self.scale + self.bias3b if self.downsample is not None: identity = self.downsample(x + self.bias1a) out += identity out = self.relu(out) return out class ResidualBlock(nn.Module): '''Residual block with BN ---Conv-BN-ReLU-Conv-+- |________________| ''' def __init__(self, nf=64): super(ResidualBlock, self).__init__() self.lrelu = nn.LeakyReLU(negative_slope=0.1, inplace=True) self.conv1 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True) self.BN1 = nn.BatchNorm2d(nf) self.conv2 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True) self.BN2 = nn.BatchNorm2d(nf) # initialization initialize_weights([self.conv1, self.conv2], 0.1) def forward(self, x): identity = x out = self.lrelu(self.BN1(self.conv1(x))) out = self.BN2(self.conv2(out)) return identity + out class ResidualBlockSpectralNorm(nn.Module): '''Residual block with Spectral Normalization. ---SpecConv-ReLU-SpecConv-+- |________________| ''' def __init__(self, nf, total_residual_blocks): super(ResidualBlockSpectralNorm, self).__init__() self.lrelu = nn.LeakyReLU(negative_slope=0.1, inplace=True) self.conv1 = SpectralNorm(nn.Conv2d(nf, nf, 3, 1, 1, bias=True)) self.conv2 = SpectralNorm(nn.Conv2d(nf, nf, 3, 1, 1, bias=True)) # Initialize first. initialize_weights([self.conv1, self.conv2], 1) # Then perform fixup scaling self.conv1 = scale_conv_weights_fixup(self.conv1, total_residual_blocks) self.conv2 = scale_conv_weights_fixup(self.conv2, total_residual_blocks) def forward(self, x): identity = x out = self.lrelu(self.conv1(x)) out = self.conv2(out) return identity + out class ResidualBlock_noBN(nn.Module): '''Residual block w/o BN ---Conv-ReLU-Conv-+- |________________| ''' def __init__(self, nf=64): super(ResidualBlock_noBN, self).__init__() self.lrelu = nn.LeakyReLU(negative_slope=0.1, inplace=True) self.conv1 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True) self.conv2 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True) # initialization initialize_weights([self.conv1, self.conv2], 0.1) def forward(self, x): identity = x out = self.lrelu(self.conv1(x)) out = self.conv2(out) return identity + out def flow_warp(x, flow, interp_mode='bilinear', padding_mode='zeros'): """Warp an image or feature map with optical flow Args: x (Tensor): size (N, C, H, W) flow (Tensor): size (N, H, W, 2), normal value interp_mode (str): 'nearest' or 'bilinear' padding_mode (str): 'zeros' or 'border' or 'reflection' Returns: Tensor: warped image or feature map """ assert x.size()[-2:] == flow.size()[1:3] B, C, H, W = x.size() # mesh grid grid_y, grid_x = torch.meshgrid(torch.arange(0, H), torch.arange(0, W)) grid = torch.stack((grid_x, grid_y), 2).float() # W(x), H(y), 2 grid.requires_grad = False grid = grid.type_as(x) vgrid = grid + flow # scale grid to [-1,1] vgrid_x = 2.0 * vgrid[:, :, :, 0] / max(W - 1, 1) - 1.0 vgrid_y = 2.0 * vgrid[:, :, :, 1] / max(H - 1, 1) - 1.0 vgrid_scaled = torch.stack((vgrid_x, vgrid_y), dim=3) output = F.grid_sample(x, vgrid_scaled, mode=interp_mode, padding_mode=padding_mode) return output