-
Notifications
You must be signed in to change notification settings - Fork 12
/
model.py
209 lines (181 loc) · 8.09 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
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
201
202
203
204
205
206
207
208
209
import torch
class ConvBlock(torch.nn.Module):
def __init__(self, input_size, output_size, kernel_size=4, stride=2, padding=1, activation=True, batch_norm=True):
super(ConvBlock, self).__init__()
self.conv = torch.nn.Conv2d(input_size, output_size, kernel_size, stride, padding)
self.activation = activation
self.lrelu = torch.nn.LeakyReLU(0.2, True)
self.batch_norm = batch_norm
self.bn = torch.nn.BatchNorm2d(output_size)
def forward(self, x):
if self.activation:
out = self.conv(self.lrelu(x))
else:
out = self.conv(x)
if self.batch_norm:
return self.bn(out)
else:
return out
class DeconvBlock(torch.nn.Module):
def __init__(self, input_size, output_size, kernel_size=4, stride=2, padding=1, batch_norm=True, dropout=False):
super(DeconvBlock, self).__init__()
self.deconv = torch.nn.ConvTranspose2d(input_size, output_size, kernel_size, stride, padding)
self.bn = torch.nn.BatchNorm2d(output_size)
self.drop = torch.nn.Dropout(0.5)
self.relu = torch.nn.ReLU(True)
self.batch_norm = batch_norm
self.dropout = dropout
def forward(self, x):
if self.batch_norm:
out = self.bn(self.deconv(self.relu(x)))
else:
out = self.deconv(self.relu(x))
if self.dropout:
return self.drop(out)
else:
return out
class Generator(torch.nn.Module):
def __init__(self, input_dim, num_filter, output_dim):
super(Generator, self).__init__()
# Encoder
self.conv1 = ConvBlock(input_dim, num_filter, activation=False, batch_norm=False)
self.conv2 = ConvBlock(num_filter, num_filter * 2)
self.conv3 = ConvBlock(num_filter * 2, num_filter * 4)
self.conv4 = ConvBlock(num_filter * 4, num_filter * 8)
self.conv5 = ConvBlock(num_filter * 8, num_filter * 8)
self.conv6 = ConvBlock(num_filter * 8, num_filter * 8)
self.conv7 = ConvBlock(num_filter * 8, num_filter * 8)
self.conv8 = ConvBlock(num_filter * 8, num_filter * 8, batch_norm=False)
# Decoder
self.deconv1 = DeconvBlock(num_filter * 8, num_filter * 8, dropout=True)
self.deconv2 = DeconvBlock(num_filter * 8 * 2, num_filter * 8, dropout=True)
self.deconv3 = DeconvBlock(num_filter * 8 * 2, num_filter * 8, dropout=True)
self.deconv4 = DeconvBlock(num_filter * 8 * 2, num_filter * 8)
self.deconv5 = DeconvBlock(num_filter * 8 * 2, num_filter * 4)
self.deconv6 = DeconvBlock(num_filter * 4 * 2, num_filter * 2)
self.deconv7 = DeconvBlock(num_filter * 2 * 2, num_filter)
self.deconv8 = DeconvBlock(num_filter * 2, output_dim, batch_norm=False)
def forward(self, x):
# Encoder
enc1 = self.conv1(x)
enc2 = self.conv2(enc1)
enc3 = self.conv3(enc2)
enc4 = self.conv4(enc3)
enc5 = self.conv5(enc4)
enc6 = self.conv6(enc5)
enc7 = self.conv7(enc6)
enc8 = self.conv8(enc7)
# Decoder with skip-connections
dec1 = self.deconv1(enc8)
dec1 = torch.cat([dec1, enc7], 1)
dec2 = self.deconv2(dec1)
dec2 = torch.cat([dec2, enc6], 1)
dec3 = self.deconv3(dec2)
dec3 = torch.cat([dec3, enc5], 1)
dec4 = self.deconv4(dec3)
dec4 = torch.cat([dec4, enc4], 1)
dec5 = self.deconv5(dec4)
dec5 = torch.cat([dec5, enc3], 1)
dec6 = self.deconv6(dec5)
dec6 = torch.cat([dec6, enc2], 1)
dec7 = self.deconv7(dec6)
dec7 = torch.cat([dec7, enc1], 1)
dec8 = self.deconv8(dec7)
out = torch.nn.Tanh()(dec8)
return out
def normal_weight_init(self, mean=0.0, std=0.02):
for m in self.children():
if isinstance(m, ConvBlock):
torch.nn.init.normal(m.conv.weight, mean, std)
if isinstance(m, DeconvBlock):
torch.nn.init.normal(m.deconv.weight, mean, std)
class Generator128(torch.nn.Module):
def __init__(self, input_dim, num_filter, output_dim):
super(Generator128, self).__init__()
# Encoder
self.conv1 = ConvBlock(input_dim, num_filter, activation=False, batch_norm=False)
self.conv2 = ConvBlock(num_filter, num_filter * 2)
self.conv3 = ConvBlock(num_filter * 2, num_filter * 4)
self.conv4 = ConvBlock(num_filter * 4, num_filter * 8)
self.conv5 = ConvBlock(num_filter * 8, num_filter * 8)
self.conv6 = ConvBlock(num_filter * 8, num_filter * 8)
self.conv7 = ConvBlock(num_filter * 8, num_filter * 8, batch_norm=False)
# Decoder
self.deconv1 = DeconvBlock(num_filter * 8, num_filter * 8, dropout=True)
self.deconv2 = DeconvBlock(num_filter * 8 * 2, num_filter * 8, dropout=True)
self.deconv3 = DeconvBlock(num_filter * 8 * 2, num_filter * 8, dropout=True)
self.deconv4 = DeconvBlock(num_filter * 8 * 2, num_filter * 4)
self.deconv5 = DeconvBlock(num_filter * 4 * 2, num_filter * 2)
self.deconv6 = DeconvBlock(num_filter * 2 * 2, num_filter)
self.deconv7 = DeconvBlock(num_filter * 2, output_dim, batch_norm=False)
def forward(self, x):
# Encoder
enc1 = self.conv1(x)
enc2 = self.conv2(enc1)
enc3 = self.conv3(enc2)
enc4 = self.conv4(enc3)
enc5 = self.conv5(enc4)
enc6 = self.conv6(enc5)
enc7 = self.conv7(enc6)
# Decoder with skip-connections
dec1 = self.deconv1(enc7)
dec1 = torch.cat([dec1, enc6], 1)
dec2 = self.deconv2(dec1)
dec2 = torch.cat([dec2, enc5], 1)
dec3 = self.deconv3(dec2)
dec3 = torch.cat([dec3, enc4], 1)
dec4 = self.deconv4(dec3)
dec4 = torch.cat([dec4, enc3], 1)
dec5 = self.deconv5(dec4)
dec5 = torch.cat([dec5, enc2], 1)
dec6 = self.deconv6(dec5)
dec6 = torch.cat([dec6, enc1], 1)
dec7 = self.deconv7(dec6)
out = torch.nn.Tanh()(dec7)
return out
def normal_weight_init(self, mean=0.0, std=0.02):
for m in self.children():
if isinstance(m, ConvBlock):
torch.nn.init.normal(m.conv.weight, mean, std)
if isinstance(m, DeconvBlock):
torch.nn.init.normal(m.deconv.weight, mean, std)
class Discriminator(torch.nn.Module):
def __init__(self, input_dim, num_filter, output_dim):
super(Discriminator, self).__init__()
self.conv1 = ConvBlock(input_dim, num_filter, activation=False, batch_norm=False)
self.conv2 = ConvBlock(num_filter, num_filter * 2)
self.conv3 = ConvBlock(num_filter * 2, num_filter * 4)
self.conv4 = ConvBlock(num_filter * 4, num_filter * 8, stride=1)
self.conv5 = ConvBlock(num_filter * 8, output_dim, stride=1, batch_norm=False)
def forward(self, x, label):
x = torch.cat([x, label], 1)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
out = torch.nn.Sigmoid()(x)
return out
def normal_weight_init(self, mean=0.0, std=0.02):
for m in self.children():
if isinstance(m, ConvBlock):
torch.nn.init.normal(m.conv.weight, mean, std)
class Discriminator128(torch.nn.Module):
def __init__(self, input_dim, num_filter, output_dim):
super(Discriminator128, self).__init__()
self.conv1 = ConvBlock(input_dim, num_filter, activation=False, batch_norm=False)
self.conv2 = ConvBlock(num_filter, num_filter * 2)
self.conv3 = ConvBlock(num_filter * 2, num_filter * 4, stride=1)
self.conv4 = ConvBlock(num_filter * 4, output_dim, stride=1, batch_norm=False)
def forward(self, x, label):
x = torch.cat([x, label], 1)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
out = torch.nn.Sigmoid()(x)
return out
def normal_weight_init(self, mean=0.0, std=0.02):
for m in self.children():
if isinstance(m, ConvBlock):
torch.nn.init.normal(m.conv.weight, mean, std)