-
Notifications
You must be signed in to change notification settings - Fork 0
/
networks.py
336 lines (303 loc) · 11.3 KB
/
networks.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
'''ResNet in PyTorch.
For Pre-activation ResNet, see 'preact_resnet.py'.
Reference:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
import pdb
from torch.autograd import Variable
import numpy as np
# mlp model class
class mlpE(nn.Module):
def __init__(self, opts):
super(mlpE, self).__init__()
self.embSize = opts.nEmb
self.domain_num = opts.domain_num
self.dim = int(np.prod(opts.dim))
self.lm1 = nn.Linear(self.dim+100, self.embSize)
self.lm2 = nn.Linear(self.embSize, opts.class_num)
# self.seed = opts.seed
# self.save_name = opts.save_name
def forward(self, x, dm_indx):
dm_indx = dm_indx.view(-1, 1).repeat(1, 100)/(self.domain_num-1)
x = x.view(-1, self.dim)
x = torch.cat((dm_indx, x), dim=1)
emb = F.relu(self.lm1(x))
out = self.lm2(emb)
return out, emb
# def get_embedding_dim(self):
# return self.embSize
# def save(self, rd):
# name = self.save_name+'_'+ str(self.seed) +'_'+ str(rd) +'.pth'
# torch.save(self.state_dict(), name)
# def load(self, rd):
# name = self.save_name+'_'+ str(self.seed) +'_'+ str(rd) +'.pth'
# try:
# print('load model from {}'.format(name))
# self.load_state_dict(torch.load(name))
# print('done!')
# except:
# print('failed!')
class mlpD(nn.Module):
def __init__(self, opts):
super(mlpD, self).__init__()
self.embSize = opts.nEmb
self.lm1 = nn.Linear(self.embSize, self.embSize)
self.lm2 = nn.Linear(self.embSize, opts.domain_num)
def forward(self, emb):
layer1 = F.relu(self.lm1(emb))
out = torch.sigmoid(self.lm2(layer1))
return out
###############
class EncoderSTN_no_alpha(nn.Module):
def __init__(self, opt):
super(EncoderSTN_no_alpha, self).__init__()
nh = 256
self.domain_num = opt.domain_num
nz = opt.nz
self.conv = nn.Sequential(
nn.Conv2d(2, nh, 3, 2, 1), nn.BatchNorm2d(nh), nn.ReLU(True), nn.Dropout(opt.dropout), # 14 x 14
nn.Conv2d(nh, nh, 3, 2, 1), nn.BatchNorm2d(nh), nn.ReLU(True), nn.Dropout(opt.dropout), # 7 x 7
nn.Conv2d(nh, nh, 3, 2, 1), nn.BatchNorm2d(nh), nn.ReLU(True), nn.Dropout(opt.dropout), # 4 x 4
nn.Conv2d(nh, nz, 4, 1, 0), nn.ReLU(True), # 1 x 1
)
self.fc_pred = nn.Sequential(
nn.Conv2d(nz, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.ReLU(True),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.ReLU(True),
nnSqueeze(),
nn.Linear(nh, 10)
)
def forward(self, x, domain_index):
"""
:param x: B x 1 x 28 x 28
:param domain_index: B x 1
:return:
"""
# x = self.stn(x, u)
domain_index = domain_index.view(-1, 1, 1, 1).repeat(1, 1, 28, 28)/(self.domain_num-1)
x = torch.cat((x, domain_index), dim=1)
z = self.conv(x)
y = self.fc_pred(z)
return y, z
class Dis_softmax(nn.Module):
def __init__(self, opt):
super(Dis_softmax, self).__init__()
nh = 512
nin=opt.nz
self.net = nn.Sequential(
nn.Conv2d(nin, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nnSqueeze(),
nn.Linear(nh, opt.domain_num+1)
)
def forward(self, x):
return self.net(x)
class DiscConv(nn.Module):
def __init__(self, nin, nout):
super(DiscConv, self).__init__()
nh = 512
self.net = nn.Sequential(
nn.Conv2d(nin, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nnSqueeze(),
nn.Linear(nh, nout),
nn.Sigmoid()
)
def forward(self, x):
return self.net(x)
class Dis_6d(nn.Module):
def __init__(self, opt):
super(Dis_6d, self).__init__()
nh = 512
nin= opt.nz
self.nout = opt.domain_num
self.net ={}
self.net_para =[]
for i in range(self.nout):
self.net[i] = nn.Sequential(
nn.Conv2d(nin, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nnSqueeze(),
nn.Linear(nh, 1),
nn.Sigmoid()
)
self.net[i] = self.net[i].cuda()
self.net_para += list(self.net[i].parameters())
def forward(self, x):
out = []
for i in range(self.nout):
out_i = self.net[i](x).view(-1, 1)
out.append(out_i)
return torch.cat(out, dim=1)
###############
# mlp model class
class mlpEc(nn.Module):
def __init__(self, opts):
super(mlpEc, self).__init__()
self.embSize = opts.nEmb
self.domain_num = opts.domain_num
self.dim = int(np.prod(opts.dim))
self.lm1 = nn.Linear(self.dim+100, 2*self.embSize)
self.lm2 = nn.Linear(2*self.embSize, self.embSize)
self.lm3 = nn.Linear(self.embSize, self.embSize)
self.lm4 = nn.Linear(self.embSize, self.embSize)
self.lm5 = nn.Linear(self.embSize, opts.class_num)
# self.seed = opts.seed
# self.save_name = opts.save_name
def forward(self, x, dm_indx):
dm_indx = dm_indx.view(-1, 1).repeat(1, 100)/(self.domain_num-1)
x = x.view(-1, self.dim)
x = torch.cat((dm_indx, x), dim=1)
x = F.relu(self.lm1(x))
x = F.relu(self.lm2(x))
emb = F.relu(self.lm3(x))
x = F.relu(self.lm4(emb))
out = self.lm5(x)
return out, emb
# mlp model class
class MNIST_mlpE(nn.Module):
def __init__(self, opts):
super(MNIST_mlpE, self).__init__()
self.embSize = opts.nEmb
self.domain_num = opts.domain_num
self.dim = int(np.prod(opts.dim))
self.lm1 = nn.Linear(self.dim+100, 2*self.embSize)
self.lm2 = nn.Linear(2*self.embSize, self.embSize)
self.lm3 = nn.Linear(self.embSize, self.embSize)
self.lm4 = nn.Linear(self.embSize, self.embSize)
self.lm5 = nn.Linear(self.embSize, opts.class_num*(self.domain_num+1))
# self.seed = opts.seed
# self.save_name = opts.save_name
def forward(self, x, dm_indx):
dm_indx = dm_indx.view(-1, 1).repeat(1, 100)/(self.domain_num-1)
x = x.view(-1, self.dim)
x = torch.cat((dm_indx, x), dim=1)
x = F.relu(self.lm1(x))
x = F.relu(self.lm2(x))
emb = F.relu(self.lm3(x))
x = F.relu(self.lm4(emb))
out = self.lm5(x)
return out, emb
class MNIST_mlpD(nn.Module):
def __init__(self, opts):
super(MNIST_mlpD, self).__init__()
self.embSize = opts.nEmb
self.lm1 = nn.Linear(self.embSize, self.embSize)
self.lm2 = nn.Linear(self.embSize, self.embSize)
self.lm3 = nn.Linear(self.embSize, opts.domain_num)
def forward(self, emb):
layer1 = F.relu(self.lm1(emb))
layer2 = F.relu(self.lm2(layer1))
out = torch.sigmoid(self.lm3(layer2))
return out
class nnSqueeze(nn.Module):
def __init__(self):
super(nnSqueeze, self).__init__()
def forward(self, x):
return torch.squeeze(x)
#############
class MNIST_Encoder(nn.Module):
def __init__(self, opt):
super(MNIST_Encoder, self).__init__()
nh = 256
self.domain_num = opt.domain_num
nz = opt.nz
self.conv = nn.Sequential(
nn.Conv2d(1, nh, 3, 2, 1), nn.BatchNorm2d(nh), nn.ReLU(True), nn.Dropout(opt.dropout), # 14 x 14
nn.Conv2d(nh, nh, 3, 2, 1), nn.BatchNorm2d(nh), nn.ReLU(True), nn.Dropout(opt.dropout), # 7 x 7
nn.Conv2d(nh, nh, 3, 2, 1), nn.BatchNorm2d(nh), nn.ReLU(True), nn.Dropout(opt.dropout), # 4 x 4
nn.Conv2d(nh, nz, 4, 1, 0), nn.ReLU(True), # 1 x 1
)
self.fc_pred = nn.Sequential(
nn.Conv2d(nz, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.ReLU(True),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.ReLU(True),
nnSqueeze(),
)
self.last_layer = nn.Linear(nh, 10*(opt.domain_num+1))
def forward(self, x, domain_index):
"""
:param x: B x 1 x 28 x 28
:param domain_index: B x 1
:return:
"""
# x = self.stn(x, u)
# domain_index = domain_index.view(-1, 1, 1, 1).repeat(1, 1, 28, 28)/(self.domain_num-1)
# x = torch.cat((x, domain_index), dim=1)
z = self.conv(x)
em = self.fc_pred(z)
y = self.last_layer(em)
return y, z
def get_emb(self, x, domain_index):
"""
:param x: B x 1 x 28 x 28
:param domain_index: B x 1
:return:
"""
# x = self.stn(x, u)
# domain_index = domain_index.view(-1, 1, 1, 1).repeat(1, 1, 28, 28)/(self.domain_num-1)
# x = torch.cat((x, domain_index), dim=1)
z = self.conv(x)
em = self.fc_pred(z)
y = self.last_layer(em)
return y, em
def get_parameters(self):
return self.parameters()
class MNIST_Dis(nn.Module):
def __init__(self, opt):
super(MNIST_Dis, self).__init__()
"""Input: features condition on domain index"""
nh = 512
self.domain_num = opt.domain_num
self.dm_d = 10
nin = opt.nz+self.dm_d
self.net = nn.Sequential(
nn.Conv2d(nin, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nnSqueeze(),
nn.Linear(nh, 1),
nn.Sigmoid()
)
def forward(self, x, domain_index):
"""
:param x: B x 1 x 28 x 28
:param domain_index: B x 1
:return:
"""
# x = self.stn(x, u)
b, c, h, w = x.size()
domain_index = domain_index.view(-1, 1, 1, 1).repeat(1, self.dm_d, h, w)/(self.domain_num-1)
x = torch.cat((x, domain_index), dim=1)
return self.net(x)
def get_parameters(self):
return self.parameters()
class MNIST_Dis_1d(nn.Module):
def __init__(self, opt):
super(MNIST_Dis_1d, self).__init__()
"""Input: features condition on domain index"""
nh = 512
self.domain_num = opt.domain_num
nin = opt.nz
self.net = nn.Sequential(
nn.Conv2d(nin, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nn.Conv2d(nh, nh, 1, 1, 0), nn.BatchNorm2d(nh), nn.LeakyReLU(),
nnSqueeze(),
nn.Linear(nh, 1),
nn.Sigmoid()
)
def forward(self, x):
"""
:param x: B x 1 x 28 x 28
:param domain_index: B x 1
:return:
"""
return self.net(x)
def get_parameters(self):
return self.parameters()