-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
244 lines (203 loc) · 8.08 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
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
import torch
import torch.nn.functional as F
import torch.nn as nn
import numpy as np
EPS = 1e-10
def he_init_weights(module):
"""
Initialize network weights using the He (Kaiming) initialization strategy.
:param module: Network module
:type module: nn.Module
"""
if isinstance(module, (nn.Conv2d, nn.Linear)):
nn.init.kaiming_normal_(module.weight)
class DDC(nn.Module):
def __init__(self, input_dim, n_clusters):
"""
DDC clustering module
:param input_dim: Shape of inputs.
:param cfg: DDC config. See `config.defaults.DDC`
"""
super().__init__()
hidden_layers = [nn.Linear(input_dim[0], 100), nn.ReLU(), nn.BatchNorm1d(num_features=100)]
self.hidden = nn.Sequential(*hidden_layers)
self.output = nn.Sequential(nn.Linear(100, n_clusters), nn.Softmax(dim=1))
def forward(self, x):
hidden = self.hidden(x)
output = self.output(hidden)
return output, hidden
class WeightedMean(nn.Module):
"""
Weighted mean fusion.
:param cfg: Fusion config. See config.defaults.Fusion
:param input_sizes: Input shapes
"""
def __init__(self, n_views, input_sizes):
super().__init__()
self.n_views = n_views
self.weights = nn.Parameter(torch.full((self.n_views,), 1 / self.n_views), requires_grad=True)
self.output_size = self.get_weighted_sum_output_size(input_sizes)
def get_weighted_sum_output_size(self, input_sizes):
flat_sizes = [np.prod(s) for s in input_sizes]
return [flat_sizes[0]]
def forward(self, inputs):
return _weighted_sum(inputs, self.weights, normalize_weights=True)
def _weighted_sum(tensors, weights, normalize_weights=True):
if normalize_weights:
weights = F.softmax(weights, dim=0)
out = torch.sum(weights[None, None, :] * torch.stack(tensors, dim=-1), dim=-1)
return out
class Encoder(nn.Module):
def __init__(self, input_dim, feature_dim):
super(Encoder, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(input_dim, 512, bias=True),
nn.ReLU(),
nn.Linear(512, 512, bias=True),
nn.ReLU(),
nn.Linear(512, feature_dim, bias=True),
nn.ReLU(),
)
def forward(self, x):
return self.encoder(x)
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.layer = nn.Sequential(
nn.Conv2d(in_channels=1, kernel_size=5, out_channels=32),
nn.ReLU(),
nn.Conv2d(in_channels=32, kernel_size=5, out_channels=32),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=32, kernel_size=3, out_channels=32),
nn.ReLU(),
nn.Conv2d(in_channels=32, kernel_size=3, out_channels=32),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Flatten(),
)
def forward(self, x):
return self.layer(x)
class BaseMVC(nn.Module):
def __init__(self, input_size, feature_dim, class_num):
super(BaseMVC, self).__init__()
self.encoder = Encoder(input_size, feature_dim)
self.cluster_module = DDC([feature_dim], class_num)
self.apply(he_init_weights)
def forward(self, x):
z = self.encoder(x)
output, hidden = self.cluster_module(z)
return output, hidden
class SiMVC(nn.Module):
def __init__(self, view, input_size, feature_dim):
super(SiMVC, self).__init__()
self.encoders = []
self.view = view
for v in range(view):
self.encoders.append(Encoder(input_size[v], feature_dim))
self.encoders = nn.ModuleList(self.encoders)
input_sizes = []
for _ in range(view):
input_sizes.append([feature_dim])
self.fusion_module = WeightedMean(view, input_sizes=input_sizes)
def forward(self, xs):
zs = []
for v in range(self.view):
zs.append(self.encoders[v](xs[v]))
fused = self.fusion_module(zs)
return zs, fused
class SiMVCLarge(nn.Module):
def __init__(self, view, feature_dim):
super(SiMVCLarge, self).__init__()
self.encoders = []
self.view = view
for v in range(view):
self.encoders.append(ConvNet())
self.encoders = nn.ModuleList(self.encoders)
input_sizes = []
for _ in range(view):
input_sizes.append([feature_dim])
self.fusion_module = WeightedMean(view, input_sizes=input_sizes)
def forward(self, xs):
zs = []
for v in range(self.view):
zs.append(self.encoders[v](xs[v]))
fused = self.fusion_module(zs)
return zs, fused
class MVC(nn.Module):
def __init__(self, view, input_size, feature_dim, class_num):
super(MVC, self).__init__()
self.encoders = []
self.view = view
for v in range(view):
self.encoders.append(Encoder(input_size[v], feature_dim))
self.encoders = nn.ModuleList(self.encoders)
input_sizes = []
for _ in range(view):
input_sizes.append([feature_dim])
self.fusion_module = WeightedMean(view, input_sizes=input_sizes)
self.cluster_module = DDC(self.fusion_module.output_size, class_num)
self.apply(he_init_weights)
def forward(self, xs):
zs = []
for v in range(self.view):
zs.append(self.encoders[v](xs[v]))
fused = self.fusion_module(zs)
output, hidden = self.cluster_module(fused)
return output, hidden
class MVCLarge(nn.Module):
def __init__(self, view, feature_dim, class_num):
super(MVCLarge, self).__init__()
self.encoders = []
self.view = view
for v in range(view):
self.encoders.append(ConvNet())
self.encoders = nn.ModuleList(self.encoders)
input_sizes = []
for _ in range(view):
input_sizes.append([feature_dim])
self.fusion_module = WeightedMean(view, input_sizes=input_sizes)
self.cluster_module = DDC(self.fusion_module.output_size, class_num)
def forward(self, xs):
zs = []
for v in range(self.view):
zs.append(self.encoders[v](xs[v]))
fused = self.fusion_module(zs)
output, hidden = self.cluster_module(fused)
return output, hidden
class DSMVC(nn.Module):
def __init__(self, view_old, view_new, input_size, feature_dim, class_num):
super(DSMVC, self).__init__()
self.view = view_new
self.old_model = SiMVC(view_old, input_size, feature_dim)
self.new_model = SiMVC(view_new, input_size, feature_dim)
self.single = Encoder(input_size[view_new-1], feature_dim)
self.gate = WeightedMean(3, [[feature_dim], [feature_dim], [feature_dim]])
self.cluster_module = DDC([feature_dim], class_num)
self.apply(he_init_weights)
def forward(self, xs):
zs_old, fused_old = self.old_model(xs)
zs_new, fused_new = self.new_model(xs)
single = self.single(xs[self.view-1])
fused = self.gate([fused_old, fused_new, single])
output, hidden = self.cluster_module(fused)
return zs_old, zs_new, output, hidden
class DSMVCLarge(nn.Module):
def __init__(self, view_old, view_new, input_size, feature_dim, class_num):
super(DSMVCLarge, self).__init__()
self.view = view_new
self.old_model = SiMVCLarge(view_old, feature_dim)
self.new_model = SiMVCLarge(view_new, feature_dim)
self.single = ConvNet()
self.gate = WeightedMean(3, [[feature_dim], [feature_dim], [feature_dim]])
self.cluster_module = DDC([feature_dim], class_num)
self.apply(he_init_weights)
def forward(self, xs):
zs_old, fused_old = self.old_model(xs)
zs_new, fused_new = self.new_model(xs)
single = self.single(xs[self.view-1])
fused = self.gate([fused_old, fused_new, single])
output, hidden = self.cluster_module(fused)
return zs_old, zs_new, output, hidden