-
Notifications
You must be signed in to change notification settings - Fork 38
/
model_compat.py
160 lines (116 loc) · 7.3 KB
/
model_compat.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
import torch.nn as nn
from functions import ReverseLayerF
class DSN(nn.Module):
def __init__(self, code_size=100, n_class=10):
super(DSN, self).__init__()
self.code_size = code_size
##########################################
# private source encoder
##########################################
self.source_encoder_conv = nn.Sequential()
self.source_encoder_conv.add_module('conv_pse1', nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5,
padding=2))
self.source_encoder_conv.add_module('ac_pse1', nn.ReLU(True))
self.source_encoder_conv.add_module('pool_pse1', nn.MaxPool2d(kernel_size=2, stride=2))
self.source_encoder_conv.add_module('conv_pse2', nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5,
padding=2))
self.source_encoder_conv.add_module('ac_pse2', nn.ReLU(True))
self.source_encoder_conv.add_module('pool_pse2', nn.MaxPool2d(kernel_size=2, stride=2))
self.source_encoder_fc = nn.Sequential()
self.source_encoder_fc.add_module('fc_pse3', nn.Linear(in_features=7 * 7 * 64, out_features=code_size))
self.source_encoder_fc.add_module('ac_pse3', nn.ReLU(True))
#########################################
# private target encoder
#########################################
self.target_encoder_conv = nn.Sequential()
self.target_encoder_conv.add_module('conv_pte1', nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5,
padding=2))
self.target_encoder_conv.add_module('ac_pte1', nn.ReLU(True))
self.target_encoder_conv.add_module('pool_pte1', nn.MaxPool2d(kernel_size=2, stride=2))
self.target_encoder_conv.add_module('conv_pte2', nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5,
padding=2))
self.target_encoder_conv.add_module('ac_pte2', nn.ReLU(True))
self.target_encoder_conv.add_module('pool_pte2', nn.MaxPool2d(kernel_size=2, stride=2))
self.target_encoder_fc = nn.Sequential()
self.target_encoder_fc.add_module('fc_pte3', nn.Linear(in_features=7 * 7 * 64, out_features=code_size))
self.target_encoder_fc.add_module('ac_pte3', nn.ReLU(True))
################################
# shared encoder (dann_mnist)
################################
self.shared_encoder_conv = nn.Sequential()
self.shared_encoder_conv.add_module('conv_se1', nn.Conv2d(in_channels=3, out_channels=32, kernel_size=5,
padding=2))
self.shared_encoder_conv.add_module('ac_se1', nn.ReLU(True))
self.shared_encoder_conv.add_module('pool_se1', nn.MaxPool2d(kernel_size=2, stride=2))
self.shared_encoder_conv.add_module('conv_se2', nn.Conv2d(in_channels=32, out_channels=48, kernel_size=5,
padding=2))
self.shared_encoder_conv.add_module('ac_se2', nn.ReLU(True))
self.shared_encoder_conv.add_module('pool_se2', nn.MaxPool2d(kernel_size=2, stride=2))
self.shared_encoder_fc = nn.Sequential()
self.shared_encoder_fc.add_module('fc_se3', nn.Linear(in_features=7 * 7 * 48, out_features=code_size))
self.shared_encoder_fc.add_module('ac_se3', nn.ReLU(True))
# classify 10 numbers
self.shared_encoder_pred_class = nn.Sequential()
self.shared_encoder_pred_class.add_module('fc_se4', nn.Linear(in_features=code_size, out_features=100))
self.shared_encoder_pred_class.add_module('relu_se4', nn.ReLU(True))
self.shared_encoder_pred_class.add_module('fc_se5', nn.Linear(in_features=100, out_features=n_class))
self.shared_encoder_pred_domain = nn.Sequential()
self.shared_encoder_pred_domain.add_module('fc_se6', nn.Linear(in_features=100, out_features=100))
self.shared_encoder_pred_domain.add_module('relu_se6', nn.ReLU(True))
# classify two domain
self.shared_encoder_pred_domain.add_module('fc_se7', nn.Linear(in_features=100, out_features=2))
######################################
# shared decoder (small decoder)
######################################
self.shared_decoder_fc = nn.Sequential()
self.shared_decoder_fc.add_module('fc_sd1', nn.Linear(in_features=code_size, out_features=588))
self.shared_decoder_fc.add_module('relu_sd1', nn.ReLU(True))
self.shared_decoder_conv = nn.Sequential()
self.shared_decoder_conv.add_module('conv_sd2', nn.Conv2d(in_channels=3, out_channels=16, kernel_size=5,
padding=2))
self.shared_decoder_conv.add_module('relu_sd2', nn.ReLU())
self.shared_decoder_conv.add_module('conv_sd3', nn.Conv2d(in_channels=16, out_channels=16, kernel_size=5,
padding=2))
self.shared_decoder_conv.add_module('relu_sd3', nn.ReLU())
self.shared_decoder_conv.add_module('us_sd4', nn.Upsample(scale_factor=2))
self.shared_decoder_conv.add_module('conv_sd5', nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3,
padding=1))
self.shared_decoder_conv.add_module('relu_sd5', nn.ReLU(True))
self.shared_decoder_conv.add_module('conv_sd6', nn.Conv2d(in_channels=16, out_channels=3, kernel_size=3,
padding=1))
def forward(self, input_data, mode, rec_scheme, p=0.0):
result = []
if mode == 'source':
# source private encoder
private_feat = self.source_encoder_conv(input_data)
private_feat = private_feat.view(-1, 64 * 7 * 7)
private_code = self.source_encoder_fc(private_feat)
elif mode == 'target':
# target private encoder
private_feat = self.target_encoder_conv(input_data)
private_feat = private_feat.view(-1, 64 * 7 * 7)
private_code = self.target_encoder_fc(private_feat)
result.append(private_code)
# shared encoder
shared_feat = self.shared_encoder_conv(input_data)
shared_feat = shared_feat.view(-1, 48 * 7 * 7)
shared_code = self.shared_encoder_fc(shared_feat)
result.append(shared_code)
reversed_shared_code = ReverseLayerF.apply(shared_code, p)
domain_label = self.shared_encoder_pred_domain(reversed_shared_code)
result.append(domain_label)
if mode == 'source':
class_label = self.shared_encoder_pred_class(shared_code)
result.append(class_label)
# shared decoder
if rec_scheme == 'share':
union_code = shared_code
elif rec_scheme == 'all':
union_code = private_code + shared_code
elif rec_scheme == 'private':
union_code = private_code
rec_vec = self.shared_decoder_fc(union_code)
rec_vec = rec_vec.view(-1, 3, 14, 14)
rec_code = self.shared_decoder_conv(rec_vec)
result.append(rec_code)
return result