[b48499]: / test / test_models / test_unets.py

Download this file

63 lines (45 with data), 2.1 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
"""
"""
import pytest
import torch
from torch_ecg.model_configs import ECG_SUBTRACT_UNET_CONFIG, ECG_UNET_VANILLA_CONFIG
from torch_ecg.models.unets.ecg_subtract_unet import ECG_SUBTRACT_UNET
from torch_ecg.models.unets.ecg_unet import ECG_UNET
from torch_ecg.utils.utils_nn import adjust_cnn_filter_lengths
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@torch.no_grad()
def test_ecg_unet():
inp = torch.randn(2, 12, 5000).to(DEVICE)
fs = 400
classes = ["p", "N", "t", "i"]
config = adjust_cnn_filter_lengths(ECG_UNET_VANILLA_CONFIG, fs)
model = ECG_UNET(classes=classes, n_leads=12, config=config).to(DEVICE)
model = model.eval()
out = model(inp)
assert out.shape == model.compute_output_shape(seq_len=inp.shape[-1], batch_size=inp.shape[0])
with pytest.warns(RuntimeWarning, match="No config is provided, using default config"):
model = ECG_UNET(classes=classes, n_leads=12).to(DEVICE)
model = model.eval()
doi = model.doi
assert isinstance(doi, list)
assert all([isinstance(d, str) for d in doi]), doi
with pytest.raises(NotImplementedError, match="Implement a task-specific inference method."):
model.inference(inp)
@torch.no_grad()
def test_ecg_subtract_unet():
inp = torch.randn(2, 12, 5000).to(DEVICE)
fs = 400
classes = ["p", "N", "t", "i"]
config = adjust_cnn_filter_lengths(ECG_SUBTRACT_UNET_CONFIG, fs)
model = ECG_SUBTRACT_UNET(classes=classes, n_leads=12, config=config).to(DEVICE)
model = model.eval()
out = model(inp)
assert out.shape == model.compute_output_shape(seq_len=inp.shape[-1], batch_size=inp.shape[0])
with pytest.warns(RuntimeWarning, match="No config is provided, using default config"):
model = ECG_SUBTRACT_UNET(classes=classes, n_leads=12).to(DEVICE)
model = model.eval()
doi = model.doi
assert isinstance(doi, list)
assert all([isinstance(d, str) for d in doi]), doi
with pytest.raises(NotImplementedError, match="Implement a task-specific inference method."):
model.inference(inp)