forked from Jo0o0Hyung/Dual-Attention-for-VAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attention_Module.py
160 lines (132 loc) · 5.67 KB
/
Attention_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
import torch
import torch.nn as nn
class TemporalAttention(nn.Module):
def __init__(self, kernel_size=11):
super(TemporalAttention, self).__init__()
self.kernel_size = kernel_size
self.conv1 = nn.Sequential(
nn.Conv1d(in_channels=3, out_channels=5, kernel_size=self.kernel_size, padding=5),
nn.BatchNorm1d(5),
nn.ReLU()
)
self.conv2 = nn.Sequential(
nn.Conv1d(in_channels=5, out_channels=5, kernel_size=self.kernel_size, padding=5),
nn.BatchNorm1d(5),
nn.ReLU()
)
self.conv3 = nn.Sequential(
nn.Conv1d(in_channels=5, out_channels=5, kernel_size=self.kernel_size, padding=5),
nn.BatchNorm1d(5),
nn.ReLU()
)
self.conv4 = nn.Sequential(
nn.Conv1d(in_channels=5, out_channels=1, kernel_size=self.kernel_size, padding=5),
nn.BatchNorm1d(1)
)
def forward(self, x):
x_avg = torch.mean(x, dim=-1).unsqueeze(dim=1)
x_std = torch.std(x, dim=-1).unsqueeze(dim=1)
x_max, _ = torch.max(x, dim=-1)
x_max = x_max.unsqueeze(dim=1)
x_total = torch.cat((x_avg, x_std, x_max), dim=1)
output = self.conv1(x_total)
output = self.conv2(output)
output = self.conv3(output)
output = self.conv4(output)
output = torch.transpose(output, 2, 1)
output = output.expand_as(x)
return output
class FrequentialAttention(nn.Module):
def __init__(self, sequential_length, kernel_size=21):
super(FrequentialAttention, self).__init__()
self.sequential_length = sequential_length
self.kernel_size = kernel_size
self.conv1 = nn.Sequential(
nn.Conv1d(in_channels=3, out_channels=5, kernel_size=self.kernel_size, padding=10),
nn.BatchNorm1d(5),
nn.ReLU()
)
self.conv2 = nn.Sequential(
nn.Conv1d(in_channels=5, out_channels=5, kernel_size=self.kernel_size, padding=10),
nn.BatchNorm1d(5),
nn.ReLU()
)
self.conv3 = nn.Sequential(
nn.Conv1d(in_channels=5, out_channels=5, kernel_size=self.kernel_size, padding=10),
nn.BatchNorm1d(5),
nn.ReLU()
)
self.conv4 = nn.Sequential(
nn.Conv1d(in_channels=5, out_channels=1, kernel_size=self.kernel_size, padding=10),
nn.BatchNorm1d(1)
)
def forward(self, x):
# Training
x_sub = x[:, :self.sequential_length, :]
x_sub_avg = torch.mean(x_sub, dim=1).unsqueeze(dim=1)
x_sub_std = torch.std(x_sub, dim=1).unsqueeze(dim=1)
x_sub_max, _ = torch.max(x_sub, dim=1)
x_sub_max = x_sub_max.unsqueeze(dim=1)
x_sub_total = torch.cat((x_sub_avg, x_sub_std, x_sub_max), dim=1)
output = self.conv1(x_sub_total)
output = self.conv2(output)
output = self.conv3(output)
output = self.conv4(output)
output = output.expand_as(x_sub)
# Validation or Test
if x.size()[1] > self.sequential_length:
iter_idx = (x.size()[1] // self.sequential_length) + 1
for i in range(1, iter_idx):
if i == iter_idx-1:
x_sub = x[:, i * self.sequential_length:, :]
if x_sub.size()[1] == 0:
break
x_sub_avg = torch.mean(x_sub, dim=1).unsqueeze(dim=1)
x_sub_std = torch.std(x_sub, dim=1).unsqueeze(dim=1)
x_sub_max, _ = torch.max(x_sub, dim=1)
x_sub_max = x_sub_max.unsqueeze(dim=1)
x_sub_total = torch.cat((x_sub_avg, x_sub_std, x_sub_max), dim=1)
output_sub = self.conv1(x_sub_total)
output_sub = self.conv2(output_sub)
output_sub = self.conv3(output_sub)
output_sub = self.conv4(output_sub)
output_sub = output_sub.expand_as(x_sub)
output = torch.cat((output, output_sub), dim=1)
else:
x_sub = x[:, i*self.sequential_length:(i+1)*self.sequential_length, :]
x_sub_avg = torch.mean(x_sub, dim=1).unsqueeze(dim=1)
x_sub_std = torch.std(x_sub, dim=1).unsqueeze(dim=1)
x_sub_max, _ = torch.max(x_sub, dim=1)
x_sub_max = x_sub_max.unsqueeze(dim=1)
x_sub_total = torch.cat((x_sub_avg, x_sub_std, x_sub_max), dim=1)
output_sub = self.conv1(x_sub_total)
output_sub = self.conv2(output_sub)
output_sub = self.conv3(output_sub)
output_sub = self.conv4(output_sub)
output_sub = output_sub.expand_as(x_sub)
output = torch.cat((output, output_sub), dim=1)
return output
class Dual_Attention_1(nn.Module):
def __init__(self):
super(Dual_Attention_1, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=3, kernel_size=7, padding=3),
nn.BatchNorm2d(3),
nn.ReLU()
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=3, kernel_size=7, padding=3),
nn.BatchNorm2d(3),
nn.ReLU()
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=1, kernel_size=7, padding=3),
nn.BatchNorm2d(1)
)
def forward(self, x):
x = x.unsqueeze(dim=1)
x = self.conv1(x)
x = self.conv2(x)
output = self.conv3(x)
output = output.squeeze(dim=1)
return output