|
| 1 | +import torch |
| 2 | + |
| 3 | + |
| 4 | +class TransformerNet(torch.nn.Module): |
| 5 | + def __init__(self): |
| 6 | + super(TransformerNet, self).__init__() |
| 7 | + # Initial convolution layers |
| 8 | + self.conv1 = ConvLayer(3, 32, kernel_size=9, stride=1) |
| 9 | + self.in1 = torch.nn.InstanceNorm2d(32, affine=True) |
| 10 | + self.conv2 = ConvLayer(32, 64, kernel_size=3, stride=2) |
| 11 | + self.in2 = torch.nn.InstanceNorm2d(64, affine=True) |
| 12 | + self.conv3 = ConvLayer(64, 128, kernel_size=3, stride=2) |
| 13 | + self.in3 = torch.nn.InstanceNorm2d(128, affine=True) |
| 14 | + # Residual layers |
| 15 | + self.res1 = ResidualBlock(128) |
| 16 | + self.res2 = ResidualBlock(128) |
| 17 | + self.res3 = ResidualBlock(128) |
| 18 | + self.res4 = ResidualBlock(128) |
| 19 | + self.res5 = ResidualBlock(128) |
| 20 | + # Upsampling Layers |
| 21 | + self.deconv1 = UpsampleConvLayer(128, 64, kernel_size=3, stride=1, upsample=2) |
| 22 | + self.in4 = torch.nn.InstanceNorm2d(64, affine=True) |
| 23 | + self.deconv2 = UpsampleConvLayer(64, 32, kernel_size=3, stride=1, upsample=2) |
| 24 | + self.in5 = torch.nn.InstanceNorm2d(32, affine=True) |
| 25 | + self.deconv3 = ConvLayer(32, 3, kernel_size=9, stride=1) |
| 26 | + # Non-linearities |
| 27 | + self.relu = torch.nn.ReLU() |
| 28 | + |
| 29 | + def forward(self, X): |
| 30 | + y = self.relu(self.in1(self.conv1(X))) |
| 31 | + y = self.relu(self.in2(self.conv2(y))) |
| 32 | + y = self.relu(self.in3(self.conv3(y))) |
| 33 | + y = self.res1(y) |
| 34 | + y = self.res2(y) |
| 35 | + y = self.res3(y) |
| 36 | + y = self.res4(y) |
| 37 | + y = self.res5(y) |
| 38 | + y = self.relu(self.in4(self.deconv1(y))) |
| 39 | + y = self.relu(self.in5(self.deconv2(y))) |
| 40 | + y = self.deconv3(y) |
| 41 | + return y |
| 42 | + |
| 43 | + |
| 44 | +class ConvLayer(torch.nn.Module): |
| 45 | + def __init__(self, in_channels, out_channels, kernel_size, stride): |
| 46 | + super(ConvLayer, self).__init__() |
| 47 | + reflection_padding = kernel_size // 2 |
| 48 | + self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding) |
| 49 | + self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride) |
| 50 | + |
| 51 | + def forward(self, x): |
| 52 | + out = self.reflection_pad(x) |
| 53 | + out = self.conv2d(out) |
| 54 | + return out |
| 55 | + |
| 56 | + |
| 57 | +class ResidualBlock(torch.nn.Module): |
| 58 | + """ResidualBlock |
| 59 | + introduced in: https://arxiv.org/abs/1512.03385 |
| 60 | + recommended architecture: http://torch.ch/blog/2016/02/04/resnets.html |
| 61 | + """ |
| 62 | + |
| 63 | + def __init__(self, channels): |
| 64 | + super(ResidualBlock, self).__init__() |
| 65 | + self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1) |
| 66 | + self.in1 = torch.nn.InstanceNorm2d(channels, affine=True) |
| 67 | + self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1) |
| 68 | + self.in2 = torch.nn.InstanceNorm2d(channels, affine=True) |
| 69 | + self.relu = torch.nn.ReLU() |
| 70 | + |
| 71 | + def forward(self, x): |
| 72 | + residual = x |
| 73 | + out = self.relu(self.in1(self.conv1(x))) |
| 74 | + out = self.in2(self.conv2(out)) |
| 75 | + out = out + residual |
| 76 | + return out |
| 77 | + |
| 78 | + |
| 79 | +class UpsampleConvLayer(torch.nn.Module): |
| 80 | + """UpsampleConvLayer |
| 81 | + Upsamples the input and then does a convolution. This method gives better results |
| 82 | + compared to ConvTranspose2d. |
| 83 | + ref: http://distill.pub/2016/deconv-checkerboard/ |
| 84 | + """ |
| 85 | + |
| 86 | + def __init__(self, in_channels, out_channels, kernel_size, stride, upsample): |
| 87 | + super(UpsampleConvLayer, self).__init__() |
| 88 | + # self.upsample = upsample |
| 89 | + self.upsample = torch.nn.Upsample(scale_factor=2, mode='nearest') |
| 90 | + reflection_padding = kernel_size // 2 |
| 91 | + self.reflection_pad = torch.nn.ReflectionPad2d(reflection_padding) |
| 92 | + self.conv2d = torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride) |
| 93 | + |
| 94 | + def forward(self, x): |
| 95 | + x_in = x |
| 96 | + # print('upsample', self.upsample) |
| 97 | + # x_in = torch.nn.functional.interpolate(x_in, mode='nearest', scale_factor=self.upsample) |
| 98 | + # if self.upsample: |
| 99 | + # x_in = torch.nn.functional.interpolate(x_in, mode='nearest', scale_factor=self.upsample) |
| 100 | + out = self.upsample(x_in) |
| 101 | + out = self.reflection_pad(out) |
| 102 | + out = self.conv2d(out) |
| 103 | + return out |
0 commit comments