-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.py
387 lines (298 loc) · 11.3 KB
/
module.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
"""
特征处理模型集合 (Feature Processing Models)
本模块实现了一系列用于特征处理的深度学习模型:
1. CNN: 用于特征提取的卷积神经网络
2. Classifier系列: 用于特征分类的多个分类器模型
3. RandMix: 用于数据增强的随机混合模块
4. AdaIN1d: 用于特征归一化的一维自适应实例归一化层
5. Masker: 用于类别特征掩码的模块
6. 其他工具函数: 包括对比学习损失(CLUB)等
"""
import torch
from torch import nn
import torch.nn.functional as F
from typing import Tuple, Dict, List, Optional
class CNN(nn.Module):
"""卷积神经网络模型,用于特征提取
Args:
n_classes (int): 分类类别数
"""
def __init__(self, n_classes: int):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv1d(1, 16, 32),
nn.BatchNorm1d(16),
nn.ReLU(),
nn.MaxPool1d(2)
)
self.conv2 = nn.Sequential(
nn.Conv1d(16, 32, 3),
nn.BatchNorm1d(32),
nn.ReLU(),
nn.MaxPool1d(2)
)
self.feature_layer = nn.Sequential(
nn.Conv1d(32, 64, 3),
nn.BatchNorm1d(64),
nn.ReLU(),
nn.MaxPool1d(4)
)
self.conv4 = nn.Sequential(
nn.Conv1d(64, 128, 3),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.MaxPool1d(4)
)
self.conv5_branches = nn.ModuleDict({
'branch1': nn.Sequential(
nn.Conv1d(128, 128, 3),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.MaxPool1d(2)
),
'branch2': nn.Sequential(
nn.Conv1d(128, 128, 5),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.MaxPool1d(2)
),
'branch3': nn.Sequential(
nn.Conv1d(128, 128, 9),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.MaxPool1d(2)
)
})
# Channel mask
self.channel_mask = nn.Parameter(torch.randn(2, 32, 1))
self.fc_layers = nn.Sequential(
nn.Linear(128 * 6, 128),
nn.Linear(128, n_classes)
)
def forward_first_layer(self, x: torch.Tensor, tau: float) -> Tuple[torch.Tensor, torch.Tensor]:
"""Forward propagation layer 1, returning domain invariant and domain specific features
Args:
x: 输入张量
tau: 温度参数
Returns:
f_invariant: domain invariant features
f_specific: domain specific features
"""
x1 = self.conv1(x)
x2 = self.conv2(x1)
channel_weights = torch.softmax(self.channel_mask / tau, dim=0)
f_invariant = x2 * channel_weights[0].view(1, *channel_weights[0].shape)
f_specific = x2 * channel_weights[1].view(1, *channel_weights[1].shape)
return f_invariant, f_specific
def forward(self, x: torch.Tensor, tau: float) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
"""模型的前向传播
Args:
x: 输入张量
tau: 温度参数
Returns:
features
channel_weights: Weighting of the channel mask
f_invariant: domain invariant features
f_specific: domain specific features
"""
x1 = self.conv1(x)
x2 = self.conv2(x1)
channel_weights = torch.softmax(self.channel_mask / tau, dim=0)
content_features = x2 * channel_weights[0].view(1, *channel_weights[0].shape)
style_features = x2 * channel_weights[1].view(1, *channel_weights[1].shape)
x3 = self.feature_layer(content_features)
x4 = self.conv4(x3)
x5 = self.conv5_branches['branch1'](x4)
features = x5.view(x5.size(0), -1)
return features, channel_weights, content_features, style_features
class Classifier(nn.Module):
"""基础分类器模型
Args:
n_classes: 分类类别数
"""
def __init__(self, n_classes: int):
super().__init__()
self.feature_extractor = nn.Sequential(
nn.Linear(128 * 6, 256),
nn.ReLU(),
nn.Linear(256, 128),
nn.ReLU()
)
self.classifier = nn.Linear(128, n_classes)
def forward(self, x: torch.Tensor) -> torch.Tensor:
features = self.feature_extractor(x)
return self.classifier(features)
class Classifier_ad(nn.Module):
"""adversarial classifier
"""
def __init__(self, n_classes):
super(Classifier_ad, self).__init__()
self.fc = nn.Sequential(nn.Linear(128 * 6, 128 * 2))
self.fc1 = nn.Sequential(nn.Linear(128 * 2, 128))
self.out = nn.Linear(128, n_classes)
def forward(self, x):
fea = self.fc(x)
fea = self.fc1(fea)
label = self.out(fea)
return label
class Classifier_all(nn.Module):
def __init__(self, n_classes):
super(Classifier_all, self).__init__()
self.fc = nn.Sequential(nn.Linear(128 * 6, 128 * 2))
self.fc1 = nn.Sequential(nn.Linear(128 * 2, 128))
self.out = nn.Linear(128, n_classes)
def forward(self, x):
fea = self.fc(x)
fea = self.fc1(fea)
label = self.out(fea)
return label
class Projector(nn.Module):
def __init__(self, output_size=1024):
super(Projector, self).__init__()
self.conv = nn.Conv1d(32, 64, 3)
self.bn = nn.BatchNorm1d(64)
self.pool = nn.MaxPool1d(kernel_size=4)
self.conv1 = nn.Conv1d(64, 128, 3)
self.bn1 = nn.BatchNorm1d(128)
self.pool1 = nn.MaxPool1d(kernel_size=4)
self.fc = nn.Linear(3904, output_size)
self.p_logvar = nn.Sequential(nn.Linear(3904, 1024))
self.p_mu = nn.Sequential(nn.Linear(3904, 1024))
self.fc1 = nn.Linear(output_size, 128)
def forward(self, x, train=True):
end_points = {}
x = self.conv(x)
x = self.bn(x)
x = F.relu(x)
x = self.pool(x)
x = x.view(x.size(0), -1)
logvar = self.p_logvar(x)
mu = self.p_mu(x)
end_points['logvar'] = logvar
end_points['mu'] = mu
if train:
x = reparametrize(mu, logvar)
else:
x = mu
end_points['Embedding'] = x
return x, end_points
class Masker(nn.Module):
def __init__(self, in_dim=128 * 6, num_classes=128 * 6, middle=256 * 6, k=256):
super(Masker, self).__init__()
self.in_dim = in_dim
self.num_classes = num_classes
self.k = k
self.layers = nn.Sequential(
nn.Dropout(0.5),
nn.Linear(in_dim, middle),
nn.BatchNorm1d(middle, affine=True),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(middle, middle),
nn.BatchNorm1d(middle, affine=True),
nn.ReLU(inplace=True),
nn.Linear(middle, num_classes))
self.bn = nn.BatchNorm1d(num_classes, affine=False)
def forward(self, f):
mask = self.bn(self.layers(f))
z = torch.zeros_like(mask)
for _ in range(self.k):
mask = F.gumbel_softmax(mask, dim=1, tau=0.1, hard=False)
z = torch.maximum(mask, z)
return z
class AdaIN1d(nn.Module):
"""一维自适应实例归一化层
Args:
style_dim: 风格维度
num_features: 特征数量
"""
def __init__(self, style_dim: int, num_features: int):
super().__init__()
self.norm = nn.InstanceNorm1d(num_features, affine=False)
self.style_transform = nn.Linear(style_dim, num_features * 2)
def forward(self, x: torch.Tensor, style: torch.Tensor) -> torch.Tensor:
style_params = self.style_transform(style)
style_params = style_params.view(style_params.size(0), style_params.size(1), 1)
gamma, beta = torch.chunk(style_params, chunks=2, dim=1)
normalized = self.norm(x)
return (1 + gamma) * normalized + beta
class RandMix(nn.Module):
"""随机混合增强模块
Args:
noise_lv: 噪声水平
"""
def __init__(self, noise_lv: float):
super().__init__()
self.zdim = 3
self.noise_lv = noise_lv
# AdaIN层
self.adain_layers = nn.ModuleList([
AdaIN1d(self.zdim, 1) for _ in range(4)
])
# 空间变换层
self.spatial_transforms = nn.ModuleList([
nn.ModuleDict({
'down': nn.Conv1d(1, 1, 2 * i + 3),
'up': nn.ConvTranspose1d(1, 1, 2 * i + 3)
}) for i in range(4)
])
self.mixing_weights = [0.2, 0.2, 0.2, 0.2, 0.2] # Fixed blend weights
# self.random_weights = torch.randn(5) # Random weighting
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""前向传播
Args:
x: 输入张量
Returns:
mixed: 混合后的特征
"""
original = x
x = x + torch.randn_like(x) * self.noise_lv * 0.001
# 空间变换
spatial_features = []
for i, (transform, adain) in enumerate(zip(self.spatial_transforms, self.adain_layers)):
down = transform['down'](x)
style = torch.randn(len(down), self.zdim, device=x.device)
transformed = adain(down, style)
spatial_features.append(torch.relu(transform['up'](transformed)))
# 特征混合
mixed_features = sum(w * f for w, f in zip(self.mixing_weights[:4], spatial_features))
original_weighted = self.mixing_weights[4] * original
mixed = mixed_features + original_weighted
return mixed
def loglikeli(mu: torch.Tensor, logvar: torch.Tensor, y_samples: torch.Tensor) -> torch.Tensor:
"""计算对数似然
Args:
mu: 均值
logvar: 对数方差
y_samples: 样本
Returns:
log_likelihood: 对数似然值
"""
return (-(mu - y_samples) ** 2 / logvar.exp() - logvar).mean()
def reparametrize(mu: torch.Tensor, logvar: torch.Tensor, factor: float = 0.2) -> torch.Tensor:
"""重参数化技巧
Args:
mu: 均值
logvar: 对数方差
factor: 缩放因子
Returns:
重参数化后的样本
"""
std = logvar.div(2).exp()
eps = std.data.new(std.size()).normal_()
return mu + factor * std * eps
def club(mu: torch.Tensor, logvar: torch.Tensor, y_samples: torch.Tensor) -> torch.Tensor:
"""计算CLUB (Contrastive Log-ratio Upper Bound)
Args:
mu: 均值
logvar: 对数方差
y_samples: 样本
Returns:
upper_bound: CLUB上界
"""
sample_size = y_samples.shape[0]
random_index = torch.randperm(sample_size).long()
positive = -(mu - y_samples) ** 2 / logvar.exp()
negative = -(mu - y_samples[random_index]) ** 2 / logvar.exp()
upper_bound = (positive.sum(dim=-1) - negative.sum(dim=-1)).mean()
return upper_bound / 2.