| 111 | |
| 112 | |
| 113 | class DiscriminatorBlock(nn.Module): |
| 114 | def __init__(self, input_channels, filters, downsample=True, antialiased_downsample=True): |
| 115 | super().__init__() |
| 116 | self.conv_res = nn.Conv2d(input_channels, filters, 1, stride=(2 if downsample else 1)) |
| 117 | |
| 118 | self.net = nn.Sequential( |
| 119 | nn.Conv2d(input_channels, filters, 3, padding=1), |
| 120 | leaky_relu(), |
| 121 | nn.Conv2d(filters, filters, 3, padding=1), |
| 122 | leaky_relu(), |
| 123 | ) |
| 124 | |
| 125 | self.maybe_blur = Blur() if antialiased_downsample else None |
| 126 | |
| 127 | self.downsample = ( |
| 128 | nn.Sequential( |
| 129 | Rearrange("b c (h p1) (w p2) -> b (c p1 p2) h w", p1=2, p2=2), nn.Conv2d(filters * 4, filters, 1) |
| 130 | ) |
| 131 | if downsample |
| 132 | else None |
| 133 | ) |
| 134 | |
| 135 | def forward(self, x): |
| 136 | res = self.conv_res(x) |
| 137 | |
| 138 | x = self.net(x) |
| 139 | |
| 140 | if exists(self.downsample): |
| 141 | if exists(self.maybe_blur): |
| 142 | x = self.maybe_blur(x, space_only=True) |
| 143 | |
| 144 | x = self.downsample(x) |
| 145 | |
| 146 | x = (x + res) * (2**-0.5) |
| 147 | return x |
| 148 | |
| 149 | |
| 150 | class Discriminator(nn.Module): |