-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmodels.py
449 lines (387 loc) · 17.4 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
'''
Description:
Author: voicebeer
Date: 2020-09-09 00:06:57
LastEditTime: 2021-03-25 03:27:41
'''
import torch.nn as nn
import torch.nn.functional as F
import torch
import numpy as np
import utils
class CFE(nn.Module):
def __init__(self):
super(CFE, self).__init__()
self.module = nn.Sequential(
nn.Linear(310, 256),
# nn.BatchNorm1d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.LeakyReLU(negative_slope=0.01, inplace=True),
nn.Linear(256, 128),
# nn.BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.LeakyReLU(negative_slope=0.01, inplace=True),
nn.Linear(128, 64),
# nn.BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.LeakyReLU(negative_slope=0.01, inplace=True),
)
def forward(self, x):
x = self.module(x)
return x
def pretrained_CFE(pretrained=False):
model = CFE()
if pretrained:
pass
return model
class pre_trained_MLP(nn.Module):
def __init__(self):
super(pre_trained_MLP, self).__init__()
self.module = nn.Sequential(
nn.Linear(310, 256),
nn.BatchNorm1d(256, eps=1e-05, momentum=0.1,
affine=True, track_running_stats=True),
nn.LeakyReLU(negative_slope=0.01, inplace=True),
nn.Linear(256, 128),
nn.LeakyReLU(negative_slope=0.01, inplace=True),
nn.Linear(128, 64),
nn.LeakyReLU(negative_slope=0.01, inplace=True),
nn.Linear(64, 4)
)
def forward(self, x):
x = self.module(x)
return x
class DSFE(nn.Module):
def __init__(self):
super(DSFE, self).__init__()
self.module = nn.Sequential(
nn.Linear(64, 32),
# nn.ReLU(inplace=True),
nn.BatchNorm1d(32, eps=1e-05, momentum=0.1,
affine=True, track_running_stats=True),
nn.LeakyReLU(negative_slope=0.01, inplace=True),
# nn.LeakyReLU(negative_slope=0.01, inplace=True),
)
def forward(self, x):
x = self.module(x)
return x
class MSMDAERNet_tsne(nn.Module):
def __init__(self, pretrained=False, number_of_source=14, number_of_category=4):
super(MSMDAERNet_tsne, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
# for i in range(1, number_of_source):
# exec('self.DSFE' + str(i) + '=DSFE()')
# exec('self.cls_fc_DSC' + str(i) + '=nn.Linear(32,' + str(number_of_category) + ')')
for i in range(number_of_source):
exec('self.DSFE' + str(i) + '=DSFE()')
exec('self.cls_fc_DSC' + str(i) +
'=nn.Linear(32,' + str(number_of_category) + ')')
def forward(self, data_src, number_of_source, data_tgt=0, label_src=0, mark=0):
'''
description: take one source data and the target data in every forward operation.
the mmd loss is calculated between the source data and the target data (both after the DSFE)
the discrepency loss is calculated between all the classifiers' results (test on the target data)
the cls loss is calculated between the ground truth label and the prediction of the mark-th classifier
之所以target data每一条线都要过一遍是因为要计算discrepency loss, mmd和cls都只要mark-th那条线就行
param {type}:
mark: int, the order of the current source
data_src: take one source data each time
number_of_source: int
label_Src: corresponding label
data_tgt: target data
return {type}
'''
mmd_loss = 0
disc_loss = 0
data_tgt_DSFE = []
if self.training == True:
# common feature extractor
data_src_CFE = self.sharedNet(data_src)
data_tgt_CFE = self.sharedNet(data_tgt)
# Each domian specific feature extractor
# to extract the domain specific feature of target data
for i in range(number_of_source):
DSFE_name = 'self.DSFE' + str(i)
data_tgt_DSFE_i = eval(DSFE_name)(data_tgt_CFE)
data_tgt_DSFE.append(data_tgt_DSFE_i)
# Use the specific feature extractor
# to extract the source data, and calculate the mmd loss
DSFE_name = 'self.DSFE' + str(mark)
data_src_DSFE = eval(DSFE_name)(data_src_CFE)
# mmd_loss += utils.mmd(data_src_DSFE, data_tgt_DSFE[mark])
mmd_loss += utils.mmd_linear(data_src_DSFE, data_tgt_DSFE[mark])
# discrepency loss
for i in range(len(data_tgt_DSFE)):
if i != mark:
disc_loss += torch.mean(torch.abs(
F.softmax(data_tgt_DSFE[mark], dim=1) -
F.softmax(data_tgt_DSFE[i], dim=1)
))
# domain specific classifier and cls_loss
DSC_name = 'self.cls_fc_DSC' + str(mark)
pred_src = eval(DSC_name)(data_src_DSFE)
cls_loss = F.nll_loss(F.log_softmax(
pred_src, dim=1), label_src.squeeze())
return cls_loss, mmd_loss, disc_loss
else:
data_CFE = self.sharedNet(data_src)
pred = []
feature_DSFE = []
for i in range(number_of_source):
DSFE_name = 'self.DSFE' + str(i)
DSC_name = 'self.cls_fc_DSC' + str(i)
feature_DSFE_i = eval(DSFE_name)(data_CFE)
feature_DSFE.append(feature_DSFE_i)
pred.append(eval(DSC_name)(feature_DSFE_i))
return pred, feature_DSFE
class MSMDAERNet(nn.Module):
def __init__(self, pretrained=False, number_of_source=15, number_of_category=4):
super(MSMDAERNet, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
# for i in range(1, number_of_source):
# exec('self.DSFE' + str(i) + '=DSFE()')
# exec('self.cls_fc_DSC' + str(i) + '=nn.Linear(32,' + str(number_of_category) + ')')
for i in range(number_of_source):
exec('self.DSFE' + str(i) + '=DSFE()')
exec('self.cls_fc_DSC' + str(i) +
'=nn.Linear(32,' + str(number_of_category) + ')')
def forward(self, data_src, number_of_source, data_tgt=0, label_src=0, mark=0):
'''
description: take one source data and the target data in every forward operation.
the mmd loss is calculated between the source data and the target data (both after the DSFE)
the discrepency loss is calculated between all the classifiers' results (test on the target data)
the cls loss is calculated between the ground truth label and the prediction of the mark-th classifier
之所以target data每一条线都要过一遍是因为要计算discrepency loss, mmd和cls都只要mark-th那条线就行
param {type}:
mark: int, the order of the current source
data_src: take one source data each time
number_of_source: int
label_Src: corresponding label
data_tgt: target data
return {type}
'''
mmd_loss = 0
disc_loss = 0
data_tgt_DSFE = []
if self.training == True:
# common feature extractor
data_src_CFE = self.sharedNet(data_src)
data_tgt_CFE = self.sharedNet(data_tgt)
# Each domian specific feature extractor
# to extract the domain specific feature of target data
for i in range(number_of_source):
DSFE_name = 'self.DSFE' + str(i)
data_tgt_DSFE_i = eval(DSFE_name)(data_tgt_CFE)
data_tgt_DSFE.append(data_tgt_DSFE_i)
# Use the specific feature extractor
# to extract the source data, and calculate the mmd loss
DSFE_name = 'self.DSFE' + str(mark)
data_src_DSFE = eval(DSFE_name)(data_src_CFE)
# mmd_loss += utils.mmd(data_src_DSFE, data_tgt_DSFE[mark])
mmd_loss += utils.mmd_linear(data_src_DSFE, data_tgt_DSFE[mark])
# discrepency loss
for i in range(len(data_tgt_DSFE)):
if i != mark:
disc_loss += torch.mean(torch.abs(
F.softmax(data_tgt_DSFE[mark], dim=1) -
F.softmax(data_tgt_DSFE[i], dim=1)
))
# domain specific classifier and cls_loss
DSC_name = 'self.cls_fc_DSC' + str(mark)
pred_src = eval(DSC_name)(data_src_DSFE)
cls_loss = F.nll_loss(F.log_softmax(
pred_src, dim=1), label_src.squeeze())
return cls_loss, mmd_loss, disc_loss
else:
data_CFE = self.sharedNet(data_src)
pred = []
for i in range(number_of_source):
DSFE_name = 'self.DSFE' + str(i)
DSC_name = 'self.cls_fc_DSC' + str(i)
feature_DSFE_i = eval(DSFE_name)(data_CFE)
pred.append(eval(DSC_name)(feature_DSFE_i))
return pred
class MEERNtmp(nn.Module):
def __init__(self, pretrained=False, number_of_source=15, number_of_category=4):
super(MEERNtmp, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
# for i in range(1, number_of_source):
# exec('self.DSFE' + str(i) + '=DSFE()')
# exec('self.cls_fc_DSC' + str(i) + '=nn.Linear(32,' + str(number_of_category) + ')')
for i in range(number_of_source):
exec('self.DSFE' + str(i) + '=DSFE()')
exec('self.cls_fc_DSC' + str(i) +
'=nn.Linear(32,' + str(number_of_category) + ')')
def forward(self, data_src, number_of_source, data_tgt=0, label_src=0, mark=0):
mmd_loss = 0
disc_loss = 0
data_tgt_DSFE = []
if self.training == True:
# common feature extractor
data_src_CFE = self.sharedNet(data_src)
data_tgt_CFE = self.sharedNet(data_tgt)
# Each domian specific feature extractor
# to extract the domain specific feature of target data
for i in range(number_of_source):
DSFE_name = 'self.DSFE' + str(i)
data_tgt_DSFE_i = eval(DSFE_name)(data_tgt_CFE)
data_tgt_DSFE.append(data_tgt_DSFE_i)
# Use the specific feature extractor
# to extract the source data, and calculate the mmd loss
DSFE_name = 'self.DSFE' + str(mark)
data_src_DSFE = eval(DSFE_name)(data_src_CFE)
# mmd_loss += utils.mmd(data_src_DSFE, data_tgt_DSFE[mark])
mmd_loss += utils.mmd_linear(data_src_DSFE, data_tgt_DSFE[mark])
# discrepency loss
for i in range(len(data_tgt_DSFE)):
if i != mark:
disc_loss += torch.mean(torch.abs(
F.softmax(data_tgt_DSFE[mark], dim=1) -
F.softmax(data_tgt_DSFE[i], dim=1)
))
# domain specific classifier and cls_loss
DSC_name = 'self.cls_fc_DSC' + str(mark)
pred_src = eval(DSC_name)(data_src_DSFE)
cls_loss = F.nll_loss(F.log_softmax(
pred_src, dim=1), label_src.squeeze())
return cls_loss, mmd_loss, disc_loss, data_src_DSFE
else:
data_CFE = self.sharedNet(data_src)
pred = []
for i in range(number_of_source):
DSFE_name = 'self.DSFE' + str(i)
DSC_name = 'self.cls_fc_DSC' + str(i)
feature_DSFE_i = eval(DSFE_name)(data_CFE)
pred.append(eval(DSC_name)(feature_DSFE_i))
return pred
class DDC(nn.Module):
def __init__(self, pretrained=False, number_of_category=4):
super(DDC, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
# self.DSFE = DSFE()
self.cls_fc = nn.Linear(64, number_of_category)
def forward(self, data_src, data_tgt=0):
loss = 0
data_src_feature = self.sharedNet(data_src)
# data_src_feature = self.DSFE(data_src_feature)
if self.training == True:
data_tgt_feature = self.sharedNet(data_tgt)
# data_tgt_feature = self.DSFE(data_tgt_feature)
# loss = utils.mmd_linear(data_src_feature, data_tgt_feature)
loss += utils.mmd_rbf_accelerate(data_src_feature,
data_tgt_feature)
data_src_cls = self.cls_fc(data_src_feature)
return data_src_cls, loss
class DAN(nn.Module):
def __init__(self, pretrained=False, number_of_category=4):
super(DAN, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
# self.DSFE = DSFE()
self.cls_fc = nn.Linear(64, number_of_category)
def forward(self, data_src, data_tgt=0):
loss = 0
data_src_feature = self.sharedNet(data_src)
# data_src_feature = self.DSFE(data_src_feature)
if self.training == True:
data_tgt_feature = self.sharedNet(data_tgt)
loss += utils.mmd(data_src_feature, data_tgt_feature)
data_src_cls = self.cls_fc(data_src_feature)
return data_src_cls, loss
class DAN_tsne(nn.Module):
def __init__(self, pretrained=False, number_of_category=4):
super(DAN_tsne, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
# self.DSFE = DSFE()
self.cls_fc = nn.Linear(64, number_of_category)
def forward(self, data_src, data_tgt=0):
loss = 0
data_src_feature = self.sharedNet(data_src)
# data_src_feature = self.DSFE(data_src_feature)
if self.training == True:
data_tgt_feature = self.sharedNet(data_tgt)
loss += utils.mmd(data_src_feature, data_tgt_feature)
data_src_cls = self.cls_fc(data_src_feature)
return data_src_cls, loss, data_src_feature
class DeepCoraltmp(nn.Module):
def __init__(self, pretrained=False, number_of_category=4):
super(DeepCoraltmp, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
# self.DSFE = DSFE()
self.cls_fc = nn.Linear(64, number_of_category)
def forward(self, data_src, data_tgt=0):
loss = 0
data_src = self.sharedNet(data_src)
# data_Src_feature = self.DSFE(data_src_feature)
if self.training == True:
data_tgt = self.sharedNet(data_tgt)
loss += utils.CORAL(data_src, data_tgt)
data_src_cls = self.cls_fc(data_src)
return data_src_cls, loss, data_src, data_tgt
class DeepCoral(nn.Module):
def __init__(self, pretrained=False, number_of_category=4):
super(DeepCoral, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
self.cls_fc = nn.Linear(64, number_of_category)
def forward(self, data_src, data_tgt=0):
loss = 0
data_src_feature = self.sharedNet(data_src)
if self.training == True:
data_tgt_feature = self.sharedNet(data_tgt)
loss += utils.CORAL(data_src_feature, data_tgt_feature)
data_src_cls = self.cls_fc(data_src_feature)
return data_src_cls, loss
class DANN(nn.Module):
def __init__(self, pretrained=False, number_of_category=4):
super(DANN, self).__init__()
self.sharedNet = pretrained_CFE(pretrained=pretrained)
self.cls_fc = nn.Linear(64, number_of_category)
self.domain_fc = AdversarialNetwork(in_feature=64)
def forward(self, data):
data = self.sharedNet(data)
clabel_pred = self.cls_fc(data)
dlabel_pred = self.domain_fc(
AdversarialLayer(high_value=1.0).apply(data))
return clabel_pred, dlabel_pred
class AdversarialNetwork(nn.Module):
def __init__(self, in_feature):
super(AdversarialNetwork, self).__init__()
self.ad_layer1 = nn.Linear(in_feature, 32)
self.ad_layer2 = nn.Linear(32, 32)
self.ad_layer3 = nn.Linear(32, 1)
self.ad_layer1.weight.data.normal_(0, 0.01)
self.ad_layer2.weight.data.normal_(0, 0.01)
self.ad_layer3.weight.data.normal_(0, 0.3)
self.ad_layer1.bias.data.fill_(0.0)
self.ad_layer2.bias.data.fill_(0.0)
self.ad_layer3.bias.data.fill_(0.0)
self.relu1 = nn.LeakyReLU()
self.relu2 = nn.LeakyReLU()
self.dropout1 = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.5)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.ad_layer1(x)
x = self.relu1(x)
x = self.dropout1(x)
x = self.ad_layer2(x)
x = self.relu2(x)
x = self.dropout2(x)
x = self.ad_layer3(x)
x = self.sigmoid(x)
return x
class AdversarialLayer(torch.autograd.Function):
def __init__(self, high_value=1.0):
self.iter_num = 0
self.alpha = 10
self.low = 0.0
self.high = high_value
self.max_iter = 2000.0
@staticmethod
def forward(ctx, input):
iter_num = 0
iter_num += 1
output = input * 1.0
# ctx.save_for_backward(output)
return output
@staticmethod
def backward(ctx, gradOutput):
coeff = np.float(2.0 * (high - low) / (1.0 +
np.exp(alpha*iter_num / max_iter)) - (high - low) + low)
return -coeff * gradOutput