| 109 | |
| 110 | |
| 111 | class ADAINDecoderBlock(nn.Module): |
| 112 | def __init__(self, input_nc, output_nc, hidden_nc, feature_nc, use_transpose=True, nonlinearity=nn.LeakyReLU(), |
| 113 | use_spect=False): |
| 114 | super(ADAINDecoderBlock, self).__init__() |
| 115 | # Attributes |
| 116 | self.actvn = nonlinearity |
| 117 | hidden_nc = min(input_nc, output_nc) if hidden_nc is None else hidden_nc |
| 118 | |
| 119 | kwargs_fine = {'kernel_size': 3, 'stride': 1, 'padding': 1} |
| 120 | if use_transpose: |
| 121 | kwargs_up = {'kernel_size': 3, 'stride': 2, 'padding': 1, 'output_padding': 1} |
| 122 | else: |
| 123 | kwargs_up = {'kernel_size': 3, 'stride': 1, 'padding': 1} |
| 124 | |
| 125 | # create conv layers |
| 126 | self.conv_0 = spectral_norm(nn.Conv2d(input_nc, hidden_nc, **kwargs_fine), use_spect) |
| 127 | if use_transpose: |
| 128 | self.conv_1 = spectral_norm(nn.ConvTranspose2d(hidden_nc, output_nc, **kwargs_up), use_spect) |
| 129 | self.conv_s = spectral_norm(nn.ConvTranspose2d(input_nc, output_nc, **kwargs_up), use_spect) |
| 130 | else: |
| 131 | self.conv_1 = nn.Sequential(spectral_norm(nn.Conv2d(hidden_nc, output_nc, **kwargs_up), use_spect), |
| 132 | nn.Upsample(scale_factor=2)) |
| 133 | self.conv_s = nn.Sequential(spectral_norm(nn.Conv2d(input_nc, output_nc, **kwargs_up), use_spect), |
| 134 | nn.Upsample(scale_factor=2)) |
| 135 | # define normalization layers |
| 136 | self.norm_0 = ADAIN(input_nc, feature_nc) |
| 137 | self.norm_1 = ADAIN(hidden_nc, feature_nc) |
| 138 | self.norm_s = ADAIN(input_nc, feature_nc) |
| 139 | |
| 140 | def forward(self, x, z): |
| 141 | x_s = self.shortcut(x, z) |
| 142 | dx = self.conv_0(self.actvn(self.norm_0(x, z))) |
| 143 | dx = self.conv_1(self.actvn(self.norm_1(dx, z))) |
| 144 | out = x_s + dx |
| 145 | return out |
| 146 | |
| 147 | def shortcut(self, x, z): |
| 148 | x_s = self.conv_s(self.actvn(self.norm_s(x, z))) |
| 149 | return x_s |
| 150 | |
| 151 | |
| 152 | def spectral_norm(module, use_spect=True): |