forked from JayceeLee/BAGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
200 lines (159 loc) · 7.93 KB
/
models.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import torch
import torch.nn as nn
# Generator
class Decoder(nn.Module):
def __init__(self, z_dim, c_dim, gf_dim):
super(Decoder, self).__init__()
self.convTrans0 = nn.ConvTranspose2d(z_dim, gf_dim*8, 4, 1, 0, bias=False)
self.bn0 = nn.BatchNorm2d(gf_dim*8)
self.relu0 = nn.ReLU(inplace=True)
self.convTrans1 = nn.ConvTranspose2d(gf_dim*8, gf_dim*4, 4, 2, 1, bias=False)
self.bn1 = nn.BatchNorm2d(gf_dim*4)
self.relu1 = nn.ReLU(inplace=True)
self.convTrans2 = nn.ConvTranspose2d(gf_dim*4, gf_dim*2, 4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(gf_dim*2)
self.relu2 = nn.ReLU(inplace=True)
self.convTrans3 = nn.ConvTranspose2d(gf_dim*2, gf_dim, 4, 2, 1, bias=False)
self.bn3 = nn.BatchNorm2d(gf_dim)
self.relu3 = nn.ReLU(inplace=True)
self.convTrans4 = nn.ConvTranspose2d(gf_dim, c_dim, 4, 2, 1, bias=False)
self.tanh = nn.Tanh()
for m in self.modules():
if isinstance(m, nn.ConvTranspose2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, z):
h0 = self.relu0(self.bn0(self.convTrans0(z)))
h1 = self.relu1(self.bn1(self.convTrans1(h0)))
h2 = self.relu2(self.bn2(self.convTrans2(h1)))
h3 = self.relu3(self.bn3(self.convTrans3(h2)))
h4 = self.convTrans4(h3)
output = self.tanh(h4)
return output # (c_dim, 64, 64)
# Discriminator
class Encoder(nn.Module):
def __init__(self, z_dim, c_dim, df_dim):
super(Encoder, self).__init__()
self.df_dim = df_dim
self.conv0 = nn.Conv2d(c_dim, df_dim, 4, 2, 1, bias=False)
self.relu0 = nn.LeakyReLU(0.2, inplace=True)
self.conv1 = nn.Conv2d(df_dim, df_dim*2, 4, 2, 1, bias=False)
self.bn1 = nn.BatchNorm2d(df_dim*2)
self.relu1 = nn.LeakyReLU(0.2, inplace=True)
self.conv2 = nn.Conv2d(df_dim*2, df_dim*4, 4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(df_dim*4)
self.relu2 = nn.LeakyReLU(0.2, inplace=True)
self.conv3 = nn.Conv2d(df_dim*4, df_dim*8, 4, 2, 1, bias=False)
self.bn3 = nn.BatchNorm2d(df_dim*8)
self.relu3 = nn.LeakyReLU(0.2, inplace=True)
self.fc_z1 = nn.Linear(df_dim*8*4*4, z_dim)
self.fc_z2 = nn.Linear(df_dim*8*4*4, z_dim)
#self.conv4 = nn.Conv2d(df_dim*8, 1, 4, 1, 0, bias=False)
for m in self.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, input):
h0 = self.relu0(self.conv0(input))
h1 = self.relu1(self.bn1(self.conv1(h0)))
h2 = self.relu2(self.bn2(self.conv2(h1)))
h3 = self.relu3(self.bn3(self.conv3(h2)))
mu = self.fc_z1(h3.view(-1, self.df_dim*8*4*4)) # (1, 128*8*4*4)
sigma = self.fc_z2(h3.view(-1, self.df_dim*8*4*4))
return mu,sigma # by squeeze, get just float not float Tenosor
class Generator(nn.Module):
def __init__(self, z_dim, c_dim, gf_dim):
super(Generator, self).__init__()
self.convTrans0 = nn.ConvTranspose2d(z_dim, gf_dim*8, 4, 1, 0, bias=False)
self.bn0 = nn.BatchNorm2d(gf_dim*8)
self.relu0 = nn.ReLU(inplace=True)
self.convTrans1 = nn.ConvTranspose2d(gf_dim*8, gf_dim*4, 4, 2, 1, bias=False)
self.bn1 = nn.BatchNorm2d(gf_dim*4)
self.relu1 = nn.ReLU(inplace=True)
self.convTrans2 = nn.ConvTranspose2d(gf_dim*4, gf_dim*2, 4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(gf_dim*2)
self.relu2 = nn.ReLU(inplace=True)
self.convTrans3 = nn.ConvTranspose2d(gf_dim*2, gf_dim, 4, 2, 1, bias=False)
self.bn3 = nn.BatchNorm2d(gf_dim)
self.relu3 = nn.ReLU(inplace=True)
self.convTrans4 = nn.ConvTranspose2d(gf_dim, c_dim, 4, 2, 1, bias=False)
self.tanh = nn.Tanh()
def forward(self, z):
h0 = self.relu0(self.bn0(self.convTrans0(z)))
h1 = self.relu1(self.bn1(self.convTrans1(h0)))
h2 = self.relu2(self.bn2(self.convTrans2(h1)))
h3 = self.relu3(self.bn3(self.convTrans3(h2)))
h4 = self.convTrans4(h3)
output = self.tanh(h4)
return output # (c_dim, 64, 64)
class _ganLogits(nn.Module):
'''
Layer of the GAN logits of the discriminator
The layer gets class logits as inputs and calculates GAN logits to
differentiate real and fake images in a numerical stable way
'''
def __init__(self, num_classes):
'''
:param num_classes: Number of real data classes (10 for SVHN)
'''
super(_ganLogits, self).__init__()
self.num_classes = num_classes
def forward(self, class_logits):
'''
:param class_logits: Unscaled log probabilities of house numbers
'''
# Set gan_logits such that P(input is real | input) = sigmoid(gan_logits).
# Keep in mind that class_logits gives you the probability distribution over all the real
# classes and the fake class. You need to work out how to transform this multiclass softmax
# distribution into a binary real-vs-fake decision that can be described with a sigmoid.
# Numerical stability is very important.
# You'll probably need to use this numerical stability trick:
# log sum_i exp a_i = m + log sum_i exp(a_i - m).
# This is numerically stable when m = max_i a_i.
# (It helps to think about what goes wrong when...
# 1. One value of a_i is very large
# 2. All the values of a_i are very negative
# This trick and this value of m fix both those cases, but the naive implementation and
# other values of m encounter various problems)
real_class_logits, fake_class_logits = torch.split(class_logits, self.num_classes, dim=1)
fake_class_logits = torch.squeeze(fake_class_logits)
max_val, _ = torch.max(real_class_logits, 1, keepdim=True)
stable_class_logits = real_class_logits - max_val
max_val = torch.squeeze(max_val)
gan_logits = torch.log(torch.sum(torch.exp(stable_class_logits), 1)) + max_val - fake_class_logits
return gan_logits # [128]
class Discriminator(nn.Module):
def __init__(self, z_dim, c_dim, df_dim, class_num):
super(Discriminator, self).__init__()
self.df_dim = df_dim
self.conv0 = nn.Conv2d(c_dim, df_dim, 4, 2, 1, bias=False)
self.relu0 = nn.LeakyReLU(0.2, inplace=True)
self.conv1 = nn.Conv2d(df_dim, df_dim*2, 4, 2, 1, bias=False)
self.bn1 = nn.BatchNorm2d(df_dim*2)
self.relu1 = nn.LeakyReLU(0.2, inplace=True)
self.conv2 = nn.Conv2d(df_dim*2, df_dim*4, 4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(df_dim*4)
self.relu2 = nn.LeakyReLU(0.2, inplace=True)
self.conv3 = nn.Conv2d(df_dim*4, df_dim*8, 4, 2, 1, bias=False)
self.bn3 = nn.BatchNorm2d(df_dim*8)
self.relu3 = nn.LeakyReLU(0.2, inplace=True)
#self.fc_z = nn.Linear(df_dim*8*4*4, z_dim)
self.fc_aux = nn.Linear(df_dim*8*4*4, class_num+1)
self.softmax = nn.LogSoftmax()
for m in self.modules():
if isinstance(m, nn.Linear):
m.weight.data.normal_(0.0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, input):
h0 = self.relu0(self.conv0(input))
h1 = self.relu1(self.bn1(self.conv1(h0)))
h2 = self.relu2(self.bn2(self.conv2(h1)))
h3 = self.relu3(self.bn3(self.conv3(h2)))
#cl = self.class_logistics(h3.view(-1, self.df_dim*8*4*4))
#gl = self.gan_logistics(cl)
output = self.softmax(self.fc_aux(h3.view(-1, self.df_dim*8*4*4)))
return h3, output
#return h3, output.view(-1,1).squeeze(1) # by squeeze, get just float not float Tenosor