-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
270 lines (220 loc) · 9.09 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: UTF-8 -*-
import torch
import torch.nn as nn
import layers
def get_encoder(dataset_type, img_type, dim_zs, dim_zc):
if img_type == 'rgb':
img_dim = 3
elif img_type == 'grayscale':
img_dim = 1
elif img_type == 'sobel':
img_dim = 2
else:
raise ValueError
if dataset_type in ['MNIST', 'FashionMNIST']:
return Encoder28(img_dim, dim_zs, dim_zc, return_act=True)
elif dataset_type in ['CIFAR10']:
return Encoder32(img_dim, 128, dim_zs, dim_zc, return_act=True, norm_type='bn', momentum=0.1)
elif dataset_type in ['STL10', 'ImageNet10']:
return Encoder96(img_dim, 64, dim_zs, dim_zc, return_act=True, norm_type='bn', momentum=0.1)
else:
raise NotImplementedError
def get_critic(dim_zs, dim_zc):
return Critic(dim_zs, dim_zc)
def get_discriminator(dataset_type, dim_zs, dim_zc):
if dataset_type in ['MNIST', 'FashionMNIST']:
return Discriminator(5 * 5 * 128, dim_zs + dim_zc)
elif dataset_type in ['CIFAR10']:
return Discriminator(4 * 4 * 128 * 4, dim_zs + dim_zc)
elif dataset_type in ['STL10', 'ImageNet10']:
return Discriminator(6 * 6 * 64 * 8, dim_zs + dim_zc)
else:
raise NotImplementedError
class Encoder28(nn.Module):
def __init__(self, img_dim, dim_zs=30, dim_zc=10, return_act=False):
super(Encoder28, self).__init__()
self.dim_zs = dim_zs
self.return_act = return_act
self.conv = nn.Sequential(
nn.Conv2d(img_dim, 64, 4, stride=2, bias=False),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, stride=2, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
)
self.fc = nn.Sequential(
nn.Linear(5 * 5 * 128, 1024, bias=False),
nn.BatchNorm1d(1024),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(1024, dim_zs + dim_zc, bias=True),
)
self.reset_parameters()
def reset_parameters(self):
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm1d) or isinstance(m, nn.GroupNorm):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
def forward(self, x):
# x: [b, img_dim, 28, 28]
b = x.size(0)
out = self.conv(x)
out = out.view(b, -1)
if self.return_act:
# [b, 128 * 5 * 5]
act = out
out = self.fc(out)
zs, zc_logit = out[:, :self.dim_zs], out[:, self.dim_zs:]
if self.return_act:
return zs, zc_logit, act
else:
return zs, zc_logit
class Encoder32(nn.Module):
def __init__(self, img_dim, num_channels, dim_zs=30, dim_zc=10, return_act=False, norm_type='bn', **kwargs):
super(Encoder32, self).__init__()
self.dim_zs = dim_zs
self.return_act = return_act
# [32, 32] -> [16, 16]
self.block1 = layers.OptimizedResBlockDown(img_dim, num_channels, norm_type=norm_type, **kwargs)
# [16, 16] -> [8, 8]
self.block2 = layers.ResBlock(num_channels, num_channels * 2, sample_type='down', norm_type=norm_type, **kwargs)
# [8, 8] -> [4, 4]
self.block3 = layers.ResBlock(num_channels * 2, num_channels * 4, sample_type='down', norm_type=norm_type,
**kwargs)
self.block4 = layers.ResBlock(num_channels * 4, num_channels * 4, sample_type='none', norm_type=norm_type,
**kwargs)
# [4, 4] -> [1, 1]
self.conv5 = nn.Sequential(
layers.Norm2dLayer(num_channels * 4, norm_type=norm_type, **kwargs),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=4),
nn.Conv2d(num_channels * 4, dim_zs + dim_zc, kernel_size=1, bias=True)
)
self.reset_parameters()
def reset_parameters(self):
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.GroupNorm):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
def forward(self, x):
# x: [b, img_dim, 32, 32]
b = x.size(0)
out = self.block1(x)
out = self.block2(out)
out = self.block3(out)
if self.return_act:
# [b, num_channels * 4 * 4 * 4]
act = out.view(b, -1)
out = self.block4(out)
out = self.conv5(out)
c = out.size(1)
out = out.view(b, c)
zs, zc_logit = out[:, :self.dim_zs], out[:, self.dim_zs:]
if self.return_act:
return zs, zc_logit, act
else:
return zs, zc_logit
class Encoder96(nn.Module):
def __init__(self, img_dim, num_channels, dim_zs=30, dim_zc=10, return_act=False, norm_type='bn', **kwargs):
super(Encoder96, self).__init__()
self.dim_zs = dim_zs
self.return_act = return_act
# [96, 96] -> [48, 48]
self.block1 = layers.OptimizedResBlockDown(img_dim, num_channels, norm_type=norm_type, **kwargs)
# [48, 48] -> [24, 24]
self.block2 = layers.ResBlock(num_channels, num_channels * 2, sample_type='down', norm_type=norm_type, **kwargs)
# [24, 24] -> [12, 12]
self.block3 = layers.ResBlock(num_channels * 2, num_channels * 4, sample_type='down', norm_type=norm_type,
**kwargs)
# [12, 12] -> [6, 6]
self.block4 = layers.ResBlock(num_channels * 4, num_channels * 8, sample_type='down', norm_type=norm_type,
**kwargs)
self.block5 = layers.ResBlock(num_channels * 8, num_channels * 8, sample_type='none', norm_type=norm_type,
**kwargs)
# [6, 6] -> [1, 1]
self.conv6 = nn.Sequential(
layers.Norm2dLayer(num_channels * 8, norm_type=norm_type, **kwargs),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=6),
nn.Conv2d(num_channels * 8, dim_zs + dim_zc, kernel_size=1, bias=True)
)
self.reset_parameters()
def reset_parameters(self):
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.GroupNorm):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
def forward(self, x):
# x: [b, img_dim, 96, 96]
b = x.size(0)
out = self.block1(x)
out = self.block2(out)
out = self.block3(out)
out = self.block4(out)
if self.return_act:
# [b, num_channels * 8 * 6 * 6]
act = out.view(b, -1)
out = self.block5(out)
out = self.conv6(out)
c = out.size(1)
out = out.view(b, c)
zs, zc_logit = out[:, :self.dim_zs], out[:, self.dim_zs:]
if self.return_act:
return zs, zc_logit, act
else:
return zs, zc_logit
class Critic(nn.Module):
def __init__(self, dim_zs=30, dim_zc=10):
super(Critic, self).__init__()
self.fc = nn.Sequential(
nn.Linear(dim_zs + dim_zc, 1024, bias=True),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(1024, 512, bias=True),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(512, 1, bias=True),
)
self.reset_parameters()
def reset_parameters(self):
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
def forward(self, z):
# z: [b, dim_zs + dim_zc]
out = self.fc(z)
return out
class Discriminator(nn.Module):
def __init__(self, x_channels, z_channels):
super(Discriminator, self).__init__()
self.net = nn.Sequential(
nn.Linear(x_channels + z_channels, 1024, bias=True),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(1024, 512, bias=True),
nn.LeakyReLU(0.2, inplace=True),
nn.Linear(512, 1, bias=True),
)
self.reset_parameters()
def reset_parameters(self):
for m in self.modules():
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.zeros_(m.bias)
def forward(self, x, z):
# x: [b, x_channels]
# z: [b, z_channels]
logit = self.net(torch.cat([x, z], dim=1))
return logit