-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtests.py
More file actions
189 lines (157 loc) · 6.72 KB
/
tests.py
File metadata and controls
189 lines (157 loc) · 6.72 KB
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
from typing import List, Tuple
import unittest
import torch
from channel_attention import (
SEAttention,
SpatialAttention,
ChannelAttention,
ConvBlockAttention,
EfficientChannelAttention,
LocalAttention,
MultiSEAttention,
)
def generate_test_inputs(
batch_size: List[int], n_channels: int, seq_len: int, height: int, width: int
) -> Tuple[List[torch.Tensor], List[torch.Tensor]]:
"""
Generate random test inputs for time series and image data.
"""
time_series_inputs = [
torch.randn(batch, n_channels, seq_len) for batch in batch_size
]
image_inputs = [
torch.randn(batch, n_channels, height, width) for batch in batch_size
]
return time_series_inputs, image_inputs
class TestAttention(unittest.TestCase):
"""Unit tests for attention mechanism modules."""
batch_size = [1, 4, 16]
n_channels = 64
seq_len = 128
height = 128
width = 128
def test_SEAttention(self):
"""Test SEAttention module for both 1D and 2D inputs."""
time_series_inputs, image_inputs = generate_test_inputs(
self.batch_size, self.n_channels, self.seq_len, self.height, self.width
)
# Test SEAttention for time series (1D)
se_attention_1d = SEAttention(n_dims=1, n_channels=self.n_channels)
for x in time_series_inputs:
output = se_attention_1d(x)
self.assertEqual(output.shape, x.shape)
# Test SEAttention for images (2D)
se_attention_2d = SEAttention(n_dims=2, n_channels=self.n_channels)
for x in image_inputs:
output = se_attention_2d(x)
self.assertEqual(output.shape, x.shape)
def test_SpatialAttention(self):
"""Test SpatialAttention module for both 1D and 2D inputs."""
time_series_inputs, image_inputs = generate_test_inputs(
self.batch_size, self.n_channels, self.seq_len, self.height, self.width
)
# Test SpatialAttention for time series (1D)
sam_1d = SpatialAttention(n_dims=1)
for x in time_series_inputs:
output = sam_1d(x)
self.assertEqual(output.shape, x.shape)
# Test SpatialAttention for images (2D)
sam_2d = SpatialAttention(n_dims=2)
for x in image_inputs:
output = sam_2d(x)
self.assertEqual(output.shape, x.shape)
def test_ChannelAttention(self):
"""Test ChannelAttention module for both 1D and 2D inputs."""
time_series_inputs, image_inputs = generate_test_inputs(
self.batch_size, self.n_channels, self.seq_len, self.height, self.width
)
# Test ChannelAttention for time series (1D)
cam_1d = ChannelAttention(n_dims=1, n_channels=self.n_channels)
for x in time_series_inputs:
output = cam_1d(x)
self.assertEqual(output.shape, x.shape)
# Test ChannelAttention for images (2D)
cam_2d = ChannelAttention(n_dims=2, n_channels=self.n_channels)
for x in image_inputs:
output = cam_2d(x)
self.assertEqual(output.shape, x.shape)
def test_ConvBlockAttention(self):
"""Test ConvBlockAttention module for both 1D and 2D inputs."""
time_series_inputs, image_inputs = generate_test_inputs(
self.batch_size, self.n_channels, self.seq_len, self.height, self.width
)
# Test ConvBlockAttention for time series (1D)
cbam_1d = ConvBlockAttention(n_dims=1, n_channels=self.n_channels)
for x in time_series_inputs:
output = cbam_1d(x)
self.assertEqual(output.shape, x.shape)
# Test ConvBlockAttention for images (2D)
cbam_2d = ConvBlockAttention(n_dims=2, n_channels=self.n_channels)
for x in image_inputs:
output = cbam_2d(x)
self.assertEqual(output.shape, x.shape)
def test_EfficientChannelAttention(self) -> None:
"""Test EfficientChannelAttention module for both 1D and 2D inputs."""
time_series_inputs, image_inputs = generate_test_inputs(
self.batch_size, self.n_channels, self.seq_len, self.height, self.width
)
# Test EfficientChannelAttention for time series (1D)
eca_1d = EfficientChannelAttention(n_dims=1, kernel_size=3)
for x in time_series_inputs:
output = eca_1d(x)
self.assertEqual(output.shape, x.shape)
# Test EfficientChannelAttention for images (2D)
eca_2d = EfficientChannelAttention(n_dims=2, kernel_size=3)
for x in image_inputs:
output = eca_2d(x)
self.assertEqual(output.shape, x.shape)
def test_LocalAttention(self) -> None:
"""Test LocalAttention module for both 1D and 2D inputs."""
time_series_inputs, image_inputs = generate_test_inputs(
self.batch_size, self.n_channels, self.seq_len, self.height, self.width
)
# Test LocalAttention for time series (1D)
la_1d = LocalAttention(n_dims=1, n_channels=self.n_channels)
for x in time_series_inputs:
output = la_1d(x)
self.assertEqual(output.shape, x.shape)
# Test LocalAttention for images (2D)
la_2d = LocalAttention(n_dims=2, n_channels=self.n_channels)
for x in image_inputs:
output = la_2d(x)
self.assertEqual(output.shape, x.shape)
# Test LocalAttention for time series (1D) with speed module
la_1d = LocalAttention(n_dims=1, n_channels=self.n_channels, speed=True)
for x in time_series_inputs:
output = la_1d(x)
self.assertEqual(output.shape, x.shape)
# Test LocalAttention for images (2D) with speed module
la_2d = LocalAttention(n_dims=2, n_channels=self.n_channels, speed=True)
for x in image_inputs:
output = la_2d(x)
self.assertEqual(output.shape, x.shape)
def test_MultiSEAttention(self) -> None:
"""Test MultiSEAttention module for both 1D and 2D inputs."""
time_series_inputs, image_inputs = generate_test_inputs(
self.batch_size, self.n_channels, self.seq_len, self.height, self.width
)
# Test MultiSEAttention for time series (1D)
mse_1d = MultiSEAttention(
n_dims=1,
n_channels=self.n_channels,
n_branches=3,
)
for x in time_series_inputs:
output = mse_1d(x)
self.assertEqual(output.shape, x.shape)
# Test MultiSEAttention for images (2D)
mse_2d = MultiSEAttention(
n_dims=2,
n_channels=self.n_channels,
n_branches=4,
)
for x in image_inputs:
output = mse_2d(x)
self.assertEqual(output.shape, x.shape)
if __name__ == "__main__":
unittest.main()