-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscriminator.py
52 lines (44 loc) · 1.68 KB
/
discriminator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import torch.nn as nn
class Discriminator(nn.Module):
def __init__(self, in_chan=3, base_chan=64, n_layers=3):
super().__init__()
self.layers = nn.ModuleList()
# Initial Convolutional Layer
self.layers.append(
nn.Sequential(
nn.Conv2d(in_chan, base_chan, kernel_size=4, stride=2, padding=2),
nn.LeakyReLU(0.2, inplace=True),
)
)
# Downsampling Layers
channels = base_chan
for _ in range(1, n_layers):
prev_channels = channels
channels = min(2 * channels, 512)
self.layers.append(
nn.Sequential(
nn.Conv2d(
prev_channels, channels, kernel_size=4, stride=2, padding=2
),
nn.InstanceNorm2d(channels, affine=False),
nn.LeakyReLU(0.2, inplace=True),
)
)
# Output Convolutional Layer
prev_channels = channels
channels = min(2 * channels, 512)
self.layers.append(
nn.Sequential(
nn.Conv2d(prev_channels, channels, kernel_size=4, stride=1, padding=2),
nn.InstanceNorm2d(channels, affine=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(channels, 1, kernel_size=4, stride=1, padding=2),
)
)
def forward(self, x):
# Will need output from every layer to calculate Feature matching Loss.
outputs = []
for layer in self.layers:
x = layer(x)
outputs.append(x)
return outputs