-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
134 lines (107 loc) · 4.32 KB
/
model.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import logging
from torch import nn
class View(nn.Module):
def __init__(self, output_shape):
super().__init__()
self.output_shape = output_shape
def forward(self, x):
batch_size, *_ = x.shape
return x.view((batch_size, *self.output_shape))
class Flatten(View):
def __init__(self):
super().__init__((-1,))
class GeneratorCNN(nn.Module):
def __init__(self, gan_noise_size, gan_output_size):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(gan_noise_size, 128),
nn.LeakyReLU(negative_slope=0.2),
nn.BatchNorm1d(128, momentum=0.99, eps=0.001),
View((2, 8, 8)),
nn.ZeroPad2d((0, 1, 0, 1)),
nn.ConvTranspose2d(in_channels=2, out_channels=32, kernel_size=2, padding=1),
nn.LeakyReLU(negative_slope=0.2),
nn.BatchNorm2d(32, momentum=0.99, eps=0.001),
nn.ConvTranspose2d(in_channels=32, out_channels=16, kernel_size=3, padding=1),
nn.LeakyReLU(negative_slope=0.2),
nn.BatchNorm2d(16, momentum=0.99, eps=0.001),
Flatten(),
nn.Linear(1024, gan_output_size),
nn.Tanh()
)
def forward(self, x):
return self.layers(x)
class GeneratorFC(nn.Module):
def __init__(self, gan_noise_size, gan_output_size):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(gan_noise_size, 64),
nn.LeakyReLU(negative_slope=0.2),
nn.BatchNorm1d(64, momentum=0.99, eps=0.001),
nn.Linear(64, 128),
nn.LeakyReLU(negative_slope=0.2),
nn.BatchNorm1d(128, momentum=0.99, eps=0.001),
nn.Linear(128, 64),
nn.LeakyReLU(negative_slope=0.2),
nn.BatchNorm1d(64, momentum=0.99, eps=0.001),
nn.Linear(64, gan_output_size),
nn.Tanh()
)
def forward(self, x):
return self.layers(x)
class DiscriminatorCNN(nn.Module):
def __init__(self, gan_output_size):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(gan_output_size, 128),
View((2, 8, 8)),
nn.LeakyReLU(negative_slope=0.2),
nn.Conv2d(in_channels=2, out_channels=64, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2),
nn.Conv2d(in_channels=64, out_channels=32, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2),
nn.Conv2d(in_channels=32, out_channels=16, kernel_size=3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2),
Flatten(),
nn.LeakyReLU(negative_slope=0.2),
nn.Dropout(0.2),
nn.Linear(1024, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.layers(x)
class DiscriminatorFC(nn.Module):
def __init__(self, gan_output_size):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(gan_output_size, 64),
nn.LeakyReLU(negative_slope=0.2),
nn.Linear(64, 64),
nn.LeakyReLU(negative_slope=0.2),
nn.Linear(64, 128),
nn.LeakyReLU(negative_slope=0.2),
nn.Linear(128, 64),
nn.LeakyReLU(negative_slope=0.2),
nn.Linear(64, 64),
nn.LeakyReLU(negative_slope=0.2),
nn.Linear(64, 32),
nn.Dropout(0.2),
nn.Linear(32, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.layers(x)
def get_models(args, n_features, device):
logging.info(f'using {args.architecture} architecture')
if args.architecture == 'cnn':
generator = GeneratorCNN(args.gan_noise_size, n_features).to(device)
discriminator = DiscriminatorCNN(n_features).to(device)
elif args.architecture == 'fc':
generator = GeneratorFC(args.gan_noise_size, n_features).to(device)
discriminator = DiscriminatorFC(n_features).to(device)
else:
raise ValueError
learning_params = lambda module: sum(p.numel() for p in module.parameters() if p.requires_grad)
logging.info(f'generator learning parameters count: {learning_params(generator)}')
logging.info(f'discriminator learning parameters count: {learning_params(discriminator)}')
return generator, discriminator