|
| 1 | +import torch |
| 2 | +from torch import nn |
| 3 | + |
| 4 | + |
| 5 | +class ConvBlock(nn.Module): |
| 6 | + def __init__(self, in_channels, out_channels, use_act, **kwargs): |
| 7 | + super().__init__() |
| 8 | + self.cnn = nn.Conv2d( |
| 9 | + in_channels, |
| 10 | + out_channels, |
| 11 | + **kwargs, |
| 12 | + bias=True, |
| 13 | + ) |
| 14 | + self.act = nn.LeakyReLU(0.2, inplace=True) if use_act else nn.Identity() |
| 15 | + |
| 16 | + def forward(self, x): |
| 17 | + return self.act(self.cnn(x)) |
| 18 | + |
| 19 | + |
| 20 | +class UpsampleBlock(nn.Module): |
| 21 | + def __init__(self, in_c, scale_factor=2): |
| 22 | + super().__init__() |
| 23 | + self.upsample = nn.Upsample(scale_factor=scale_factor, mode="nearest") |
| 24 | + self.conv = nn.Conv2d(in_c, in_c, 3, 1, 1, bias=True) |
| 25 | + self.act = nn.LeakyReLU(0.2, inplace=True) |
| 26 | + |
| 27 | + def forward(self, x): |
| 28 | + return self.act(self.conv(self.upsample(x))) |
| 29 | + |
| 30 | + |
| 31 | +class DenseResidualBlock(nn.Module): |
| 32 | + def __init__(self, in_channels, channels=32, residual_beta=0.2): |
| 33 | + super().__init__() |
| 34 | + self.residual_beta = residual_beta |
| 35 | + self.blocks = nn.ModuleList() |
| 36 | + |
| 37 | + for i in range(5): |
| 38 | + self.blocks.append( |
| 39 | + ConvBlock( |
| 40 | + in_channels + channels * i, |
| 41 | + channels if i <= 3 else in_channels, |
| 42 | + kernel_size=3, |
| 43 | + stride=1, |
| 44 | + padding=1, |
| 45 | + use_act=True if i <= 3 else False, |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + def forward(self, x): |
| 50 | + new_inputs = x |
| 51 | + for block in self.blocks: |
| 52 | + out = block(new_inputs) |
| 53 | + new_inputs = torch.cat([new_inputs, out], dim=1) |
| 54 | + return self.residual_beta * out + x |
| 55 | + |
| 56 | + |
| 57 | +class RRDB(nn.Module): |
| 58 | + def __init__(self, in_channels, residual_beta=0.2): |
| 59 | + super().__init__() |
| 60 | + self.residual_beta = residual_beta |
| 61 | + self.rrdb = nn.Sequential(*[DenseResidualBlock(in_channels) for _ in range(3)]) |
| 62 | + |
| 63 | + def forward(self, x): |
| 64 | + return self.rrdb(x) * self.residual_beta + x |
| 65 | + |
| 66 | + |
| 67 | +class Generator(nn.Module): |
| 68 | + def __init__(self, in_channels=3, num_channels=64, num_blocks=23): |
| 69 | + super().__init__() |
| 70 | + self.initial = nn.Conv2d( |
| 71 | + in_channels, |
| 72 | + num_channels, |
| 73 | + kernel_size=3, |
| 74 | + stride=1, |
| 75 | + padding=1, |
| 76 | + bias=True, |
| 77 | + ) |
| 78 | + self.residuals = nn.Sequential(*[RRDB(num_channels) for _ in range(num_blocks)]) |
| 79 | + self.conv = nn.Conv2d(num_channels, num_channels, kernel_size=3, stride=1, padding=1) |
| 80 | + self.upsamples = nn.Sequential( |
| 81 | + UpsampleBlock(num_channels), UpsampleBlock(num_channels), |
| 82 | + ) |
| 83 | + self.final = nn.Sequential( |
| 84 | + nn.Conv2d(num_channels, num_channels, 3, 1, 1, bias=True), |
| 85 | + nn.LeakyReLU(0.2, inplace=True), |
| 86 | + nn.Conv2d(num_channels, in_channels, 3, 1, 1, bias=True), |
| 87 | + ) |
| 88 | + |
| 89 | + def forward(self, x): |
| 90 | + initial = self.initial(x) |
| 91 | + x = self.conv(self.residuals(initial)) + initial |
| 92 | + x = self.upsamples(x) |
| 93 | + return self.final(x) |
| 94 | + |
| 95 | + |
| 96 | +class Discriminator(nn.Module): |
| 97 | + def __init__(self, in_channels=3, features=[64, 64, 128, 128, 256, 256, 512, 512]): |
| 98 | + super().__init__() |
| 99 | + blocks = [] |
| 100 | + for idx, feature in enumerate(features): |
| 101 | + blocks.append( |
| 102 | + ConvBlock( |
| 103 | + in_channels, |
| 104 | + feature, |
| 105 | + kernel_size=3, |
| 106 | + stride=1 + idx % 2, |
| 107 | + padding=1, |
| 108 | + use_act=True, |
| 109 | + ), |
| 110 | + ) |
| 111 | + in_channels = feature |
| 112 | + |
| 113 | + self.blocks = nn.Sequential(*blocks) |
| 114 | + self.classifier = nn.Sequential( |
| 115 | + nn.AdaptiveAvgPool2d((6, 6)), |
| 116 | + nn.Flatten(), |
| 117 | + nn.Linear(512 * 6 * 6, 1024), |
| 118 | + nn.LeakyReLU(0.2, inplace=True), |
| 119 | + nn.Linear(1024, 1), |
| 120 | + ) |
| 121 | + |
| 122 | + def forward(self, x): |
| 123 | + x = self.blocks(x) |
| 124 | + return self.classifier(x) |
| 125 | + |
| 126 | +def initialize_weights(model, scale=0.1): |
| 127 | + for m in model.modules(): |
| 128 | + if isinstance(m, nn.Conv2d): |
| 129 | + nn.init.kaiming_normal_(m.weight.data) |
| 130 | + m.weight.data *= scale |
| 131 | + |
| 132 | + elif isinstance(m, nn.Linear): |
| 133 | + nn.init.kaiming_normal_(m.weight.data) |
| 134 | + m.weight.data *= scale |
| 135 | + |
| 136 | + |
| 137 | +def test(): |
| 138 | + gen = Generator() |
| 139 | + disc = Discriminator() |
| 140 | + low_res = 24 |
| 141 | + x = torch.randn((5, 3, low_res, low_res)) |
| 142 | + gen_out = gen(x) |
| 143 | + disc_out = disc(gen_out) |
| 144 | + |
| 145 | + print(gen_out.shape) |
| 146 | + print(disc_out.shape) |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + test() |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
0 commit comments