| import torch | |
| import torch.nn as nn | |
| class ConvBlock(nn.Module): | |
| def __init__(self, in_channels, out_channels, down=True, use_act=True, use_norm=True, activation="relu", **kwargs): | |
| super().__init__() | |
| self.conv = nn.Sequential( | |
| nn.Conv2d(in_channels, out_channels, padding_mode="reflect", **kwargs) | |
| if down | |
| else nn.ConvTranspose2d(in_channels, out_channels, **kwargs), | |
| nn.InstanceNorm2d(out_channels) if use_norm else nn.Identity(), | |
| nn.ReLU(inplace=True) if activation == "relu" and use_act else | |
| nn.LeakyReLU(0.2, inplace=True) if activation == "leaky" and use_act else | |
| nn.Identity(), | |
| ) | |
| def forward(self, x): | |
| return self.conv(x) | |
| class ResidualBlock(nn.Module): | |
| def __init__(self, channels): | |
| super().__init__() | |
| self.block = nn.Sequential( | |
| ConvBlock(channels, channels, kernel_size=3, padding=1), | |
| ConvBlock(channels, channels, use_act=False, kernel_size=3, padding=1), | |
| ) | |
| def forward(self, x): | |
| return x + self.block(x) | |
| class Generator(nn.Module): | |
| def __init__(self, img_channels, num_features=64, num_residuals=9): | |
| super().__init__() | |
| self.initial = nn.Sequential( | |
| nn.Conv2d(img_channels, num_features, kernel_size=7, stride=1, padding=3, padding_mode="reflect"), | |
| nn.InstanceNorm2d(num_features), | |
| nn.ReLU(inplace=True), | |
| ) | |
| self.down_blocks = nn.ModuleList( | |
| [ | |
| ConvBlock(num_features, num_features * 2, kernel_size=3, stride=2, padding=1), | |
| ConvBlock(num_features * 2, num_features * 4, kernel_size=3, stride=2, padding=1), | |
| ] | |
| ) | |
| self.res_blocks = nn.Sequential( | |
| *[ResidualBlock(num_features * 4) for _ in range(num_residuals)] | |
| ) | |
| self.up_blocks = nn.ModuleList( | |
| [ | |
| ConvBlock(num_features * 4, num_features * 2, down=False, kernel_size=3, stride=2, padding=1, output_padding=1), | |
| ConvBlock(num_features * 2, num_features, down=False, kernel_size=3, stride=2, padding=1, output_padding=1), | |
| ] | |
| ) | |
| self.last = nn.Conv2d(num_features, img_channels, kernel_size=7, stride=1, padding=3, padding_mode="reflect") | |
| def forward(self, x): | |
| x = self.initial(x) | |
| for layer in self.down_blocks: | |
| x = layer(x) | |
| x = self.res_blocks(x) | |
| for layer in self.up_blocks: | |
| x = layer(x) | |
| return torch.tanh(self.last(x)) | |
| class Discriminator(nn.Module): | |
| def __init__(self, in_channels, features=[64, 128, 256, 512]): | |
| super().__init__() | |
| self.initial = nn.Sequential( | |
| nn.Conv2d(in_channels, features[0], kernel_size=4, stride=2, padding=1, padding_mode="reflect"), | |
| nn.LeakyReLU(0.2, inplace=True), | |
| ) | |
| layers = [] | |
| in_channels = features[0] | |
| for feature in features[1:]: | |
| layers.append( | |
| ConvBlock( | |
| in_channels, | |
| feature, | |
| stride=1 if feature == features[-1] else 2, | |
| kernel_size=4, | |
| padding=1, | |
| activation="leaky" | |
| ) | |
| ) | |
| in_channels = feature | |
| layers.append(nn.Conv2d(in_channels, 1, kernel_size=4, stride=1, padding=1, padding_mode="reflect")) | |
| self.model = nn.Sequential(*layers) | |
| def forward(self, x): | |
| x = self.initial(x) | |
| return torch.sigmoid(self.model(x)) | |
| def test(): | |
| img_channels = 3 | |
| img_size = 256 | |
| x = torch.randn((2, img_channels, img_size, img_size)) | |
| gen = Generator(img_channels, num_residuals=9) | |
| print(f"Generator output shape: {gen(x).shape}") | |
| disc = Discriminator(img_channels) | |
| print(f"Discriminator output shape: {disc(x).shape}") | |
| if __name__ == "__main__": | |
| test() | |