-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathtest_ProjectionHeads.py
287 lines (272 loc) · 12.2 KB
/
test_ProjectionHeads.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
import unittest
import torch
from lightly.loss import DINOLoss
from lightly.models.modules.heads import (
BarlowTwinsProjectionHead,
BYOLPredictionHead,
BYOLProjectionHead,
DenseCLProjectionHead,
DINOProjectionHead,
MMCRProjectionHead,
MoCoProjectionHead,
MSNProjectionHead,
NNCLRPredictionHead,
NNCLRProjectionHead,
SimCLRProjectionHead,
SimSiamPredictionHead,
SimSiamProjectionHead,
SwaVProjectionHead,
SwaVPrototypes,
TiCoProjectionHead,
VicRegLLocalProjectionHead,
)
class TestProjectionHeads(unittest.TestCase):
def setUp(self) -> None:
self.n_features = [
(8, 16, 32),
(8, 32, 16),
(16, 8, 32),
(16, 32, 8),
(32, 8, 16),
(32, 16, 8),
]
self.swavProtoypes = [(8, 16, [32, 64, 128])]
self.heads = [
BarlowTwinsProjectionHead,
BYOLProjectionHead,
BYOLPredictionHead,
DenseCLProjectionHead,
DINOProjectionHead,
MoCoProjectionHead,
MSNProjectionHead,
MMCRProjectionHead,
NNCLRProjectionHead,
NNCLRPredictionHead,
SimCLRProjectionHead,
SimSiamProjectionHead,
SimSiamPredictionHead,
SwaVProjectionHead,
TiCoProjectionHead,
VicRegLLocalProjectionHead,
]
def test_single_projection_head(self, device: str = "cpu", seed: int = 0) -> None:
for head_cls in self.heads:
for in_features, hidden_features, out_features in self.n_features:
torch.manual_seed(seed)
if head_cls == DINOProjectionHead:
bottleneck_features = hidden_features
head = head_cls(
in_features, hidden_features, bottleneck_features, out_features
)
elif head_cls == SimCLRProjectionHead:
head = head_cls(
in_features, hidden_features, out_features, batch_norm=False
)
else:
head = head_cls(in_features, hidden_features, out_features)
head = head.eval()
head = head.to(device)
for batch_size in [1, 2]:
msg = (
f"head: {head_cls}"
+ f"d_in, d_h, d_out = "
+ f"{in_features}x{hidden_features}x{out_features}"
)
with self.subTest(msg=msg):
x = torch.torch.rand((batch_size, in_features)).to(device)
with torch.no_grad():
y = head(x)
self.assertEqual(y.shape[0], batch_size)
self.assertEqual(y.shape[1], out_features)
@unittest.skipUnless(torch.cuda.is_available(), "skip")
def test_single_projection_head_cuda(self, seed: int = 0) -> None:
self.test_single_projection_head(device="cuda", seed=seed)
def test_swav_prototypes(self, device: str = "cpu", seed: int = 0) -> None:
for in_features, _, n_prototypes in self.n_features:
torch.manual_seed(seed)
prototypes = SwaVPrototypes(in_features, n_prototypes)
prototypes = prototypes.eval()
prototypes = prototypes.to(device)
for batch_size in [1, 2]:
msg = (
"prototypes d_in, n_prototypes = "
+ f"{in_features} x {n_prototypes}"
)
with self.subTest(msg=msg):
x = torch.torch.rand((batch_size, in_features)).to(device)
with torch.no_grad():
y = prototypes(x)
self.assertEqual(y.shape[0], batch_size)
self.assertEqual(y.shape[1], n_prototypes)
def test_swav_frozen_prototypes(self, seed: int = 0) -> None:
criterion = torch.nn.L1Loss()
linear_layer = torch.nn.Linear(8, 8, bias=False)
prototypes = SwaVPrototypes(
input_dim=8, n_prototypes=8, n_steps_frozen_prototypes=2
)
optimizer = torch.optim.SGD(prototypes.parameters(), lr=0.01)
torch.manual_seed(seed)
in_features = torch.rand(4, 8, device="cpu")
target_features = torch.ones(4, 8, device="cpu")
for step in range(4):
out_features = linear_layer(in_features)
out_features = prototypes.forward(out_features, step)
loss = criterion(out_features, target_features)
loss.backward()
optimizer.step()
optimizer.zero_grad()
if step == 0:
loss0 = loss
if step <= 2:
self.assertEqual(loss, loss0)
if step > 2:
self.assertNotEqual(loss, loss0)
def test_swav_multi_prototypes(self, device: str = "cpu", seed: int = 0) -> None:
for in_features, _, n_prototypes in self.swavProtoypes:
torch.manual_seed(seed)
prototypes = SwaVPrototypes(in_features, n_prototypes)
prototypes = prototypes.eval()
prototypes = prototypes.to(device)
for batch_size in [1, 2]:
msg = (
"prototypes d_in, n_prototypes = "
+ f"{in_features} x {n_prototypes}"
)
with self.subTest(msg=msg):
x = torch.torch.rand((batch_size, in_features)).to(device)
with torch.no_grad():
y = prototypes(x)
for layerNum, prototypeSize in enumerate(n_prototypes):
self.assertEqual(y[layerNum].shape[0], batch_size)
self.assertEqual(y[layerNum].shape[1], prototypeSize)
@unittest.skipUnless(torch.cuda.is_available(), "skip")
def test_swav_prototypes_cuda(self, device: str = "cuda", seed: int = 0) -> None:
self.test_swav_prototypes(device=device, seed=seed)
def test_dino_projection_head(self, device: str = "cpu", seed: int = 0) -> None:
input_dim, hidden_dim, output_dim = self.n_features[0]
for bottleneck_dim in [8, 16, 32]:
for batch_norm in [False, True]:
torch.manual_seed(seed)
head = DINOProjectionHead(
input_dim=input_dim,
hidden_dim=hidden_dim,
output_dim=output_dim,
bottleneck_dim=bottleneck_dim,
batch_norm=batch_norm,
)
head = head.eval()
head = head.to(device)
for batch_size in [1, 2]:
msg = (
f"bottleneck_dim={bottleneck_dim}, " f"batch_norm={batch_norm}"
)
with self.subTest(msg=msg):
x = torch.torch.rand((batch_size, input_dim)).to(device)
with torch.no_grad():
y = head(x)
self.assertEqual(y.shape[0], batch_size)
self.assertEqual(y.shape[1], output_dim)
@unittest.skipUnless(torch.cuda.is_available(), "skip")
def test_dino_projection_head_cuda(
self, device: str = "cuda", seed: int = 0
) -> None:
self.test_dino_projection_head(device=device, seed=seed)
def test_dino_projection_head_freeze_last_layer(self, seed: int = 0) -> None:
"""Test if freeze last layer cancels backprop."""
torch.manual_seed(seed)
for norm_last_layer in [False, True]:
for freeze_last_layer in range(-1, 3):
head = DINOProjectionHead(
input_dim=4,
hidden_dim=4,
output_dim=4,
bottleneck_dim=4,
freeze_last_layer=freeze_last_layer,
norm_last_layer=norm_last_layer,
)
optimizer = torch.optim.SGD(head.parameters(), lr=1)
criterion = DINOLoss(output_dim=4)
# Store initial weights of last layer
initial_data = [
param.data.detach().clone()
for param in head.last_layer.parameters()
]
for epoch in range(5):
with self.subTest(
f"norm_last_layer={norm_last_layer}, "
f"freeze_last_layer={freeze_last_layer}, "
f"epoch={epoch}"
):
views = [torch.rand((3, 4)) for _ in range(2)]
teacher_out = [head(view) for view in views]
student_out = [head(view) for view in views]
loss = criterion(teacher_out, student_out, epoch=epoch)
optimizer.zero_grad()
loss.backward()
head.cancel_last_layer_gradients(current_epoch=epoch)
optimizer.step()
params = head.last_layer.parameters()
# Verify that weights have (not) changed depending on epoch.
for param, init_data in zip(params, initial_data):
if param.requires_grad:
are_same = torch.allclose(param.data, init_data)
if epoch >= freeze_last_layer:
self.assertFalse(are_same)
else:
self.assertTrue(are_same)
def test_simclr_projection_head_multiple_layers(
self, device: str = "cpu", seed: int = 0
) -> None:
for in_features, hidden_features, out_features in self.n_features:
for num_layers in range(2, 5):
for batch_norm in [True, False]:
torch.manual_seed(seed)
head = SimCLRProjectionHead(
in_features,
hidden_features,
out_features,
num_layers,
batch_norm,
)
head = head.eval()
head = head.to(device)
for batch_size in [1, 2]:
msg = (
f"head: SimCLRProjectionHead"
+ f"d_in, d_h, d_out = "
+ f"{in_features}x{hidden_features}x{out_features}"
)
with self.subTest(msg=msg):
x = torch.torch.rand((batch_size, in_features)).to(device)
with torch.no_grad():
y = head(x)
self.assertEqual(y.shape[0], batch_size)
self.assertEqual(y.shape[1], out_features)
def test_moco_projection_head_multiple_layers(
self, device: str = "cpu", seed: int = 0
) -> None:
for in_features, hidden_features, out_features in self.n_features:
for num_layers in range(2, 5):
for batch_norm in [True, False]:
torch.manual_seed(seed)
head = MoCoProjectionHead(
in_features,
hidden_features,
out_features,
num_layers,
batch_norm,
)
head = head.eval()
head = head.to(device)
for batch_size in [1, 2]:
msg = (
f"head: MoCoProjectionHead"
+ f"d_in, d_h, d_out = "
+ f"{in_features}x{hidden_features}x{out_features}"
)
with self.subTest(msg=msg):
x = torch.torch.rand((batch_size, in_features)).to(device)
with torch.no_grad():
y = head(x)
self.assertEqual(y.shape[0], batch_size)
self.assertEqual(y.shape[1], out_features)