-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_alignnet.py
More file actions
55 lines (46 loc) · 1.34 KB
/
test_alignnet.py
File metadata and controls
55 lines (46 loc) · 1.34 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
import torch
from alignit.models.alignnet import AlignNet
def test_alignnet_forward_shapes_cpu():
model = AlignNet(
backbone_name="resnet18",
backbone_weights=None,
use_vector_input=False,
output_dim=7,
)
model.eval()
x = torch.randn(2, 3, 3, 64, 64) # B=2, N=3 views
with torch.no_grad():
y = model(x)
assert y.shape == (2, 7)
def test_alignnet_with_vector_input():
model = AlignNet(
backbone_name="resnet18",
backbone_weights=None,
use_vector_input=True,
output_dim=7,
)
model.eval()
x = torch.randn(1, 2, 3, 64, 64)
vecs = [torch.randn(5)]
with torch.no_grad():
y = model(x, vecs)
assert y.shape == (1, 7)
def test_alignnet_performance():
model = AlignNet(
backbone_name="efficientnet_b0",
backbone_weights=None,
use_vector_input=True,
output_dim=7,
)
model.eval()
x = torch.randn(1, 3, 3, 224, 224) # B=1, N=3 views
vecs = [torch.randn(5)]
with torch.no_grad():
import time
start_time = time.time()
for _ in range(10):
y = model(x, vecs)
elapsed_time = (time.time() - start_time) / 10
elapsed_time_ms = elapsed_time * 1000
print(f"Forward pass took {elapsed_time_ms:.2f} ms")
assert elapsed_time < 0.5