-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathtest_arch_vanilla.py
More file actions
82 lines (69 loc) · 2.39 KB
/
test_arch_vanilla.py
File metadata and controls
82 lines (69 loc) · 2.39 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
"""Unit test package for vanilla CNN within toolbox."""
import numpy as np
import pytest
import torch
from tiatoolbox.models.architecture.vanilla import CNNModel, TimmModel
from tiatoolbox.models.models_abc import model_to
ON_GPU = False
RNG = np.random.default_rng() # Numpy Random Generator
device = "cuda" if ON_GPU else "cpu"
def test_functional() -> None:
"""Test for creating backbone."""
backbones = [
"alexnet",
"resnet18",
"resnet34",
"resnet50",
"resnet101",
"resnext50_32x4d",
"resnext101_32x8d",
"wide_resnet50_2",
"wide_resnet101_2",
"densenet121",
"densenet161",
"densenet169",
"densenet201",
"googlenet",
"mobilenet_v2",
"mobilenet_v3_large",
"mobilenet_v3_small",
]
assert CNNModel.postproc([1, 2]) == 1
b = 4
h = w = 512
samples = torch.from_numpy(RNG.random((b, h, w, 3)))
# Dummy entry, will generate ValueError if "try" fails without running the loop.
backbone = "empty"
try:
for backbone in backbones:
model = CNNModel(backbone, num_classes=1)
model_ = model_to(device=device, model=model)
model.infer_batch(model_, samples, device=device)
except ValueError as exc:
msg = f"Model {backbone} failed."
raise AssertionError(msg) from exc
# skipcq
with pytest.raises(ValueError, match=r".*Backbone.*not supported.*"):
CNNModel("shiny_model_to_crash", num_classes=2)
def test_timm_functional() -> None:
"""Test for creating backbone."""
backbones = [
"efficientnet_b0",
]
assert TimmModel.postproc([1, 2]) == 1
b = 4
h = w = 224
samples = torch.from_numpy(RNG.random((b, h, w, 3)))
# Dummy entry, will generate ValueError if "try" fails without running the loop.
backbone = "empty"
try:
for backbone in backbones:
model = TimmModel(backbone=backbone, num_classes=1, pretrained=False)
model_ = model_to(device=device, model=model)
model.infer_batch(model_, samples, device=device)
except ValueError as exc:
msg = f"Model {backbone} failed."
raise AssertionError(msg) from exc
# skipcq
with pytest.raises(ValueError, match=r".*Backbone.*not supported.*"):
TimmModel(backbone="shiny_model_to_crash", num_classes=2, pretrained=False)