-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
86 lines (66 loc) · 2.45 KB
/
conftest.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
"""
Common testing.
"""
from pathlib import Path
import dataclasses
import numpy as np
from PIL import Image
import pytest
import torch
import stereomatch
@pytest.fixture
def sample_stereo_pair():
"""
Fixture with sample stereo pair to use during testing.
"""
image_base_dir = (Path(__file__).parent.parent /
"tests/data/middleburry/teddy/")
target_size = (512, 256)
left_image = torch.from_numpy(
np.array(Image.open(image_base_dir /
"im2.png").convert('L').resize(target_size))).float() / 255.0
right_image = torch.from_numpy(
np.array(Image.open(image_base_dir /
"im6.png").convert('L').resize(target_size))).float() / 255.0
return left_image, right_image
@dataclasses.dataclass
class CostFixture:
"""
Input for aggregation and disparity reduce methods.
Attributes:
volume: A cost volume with shape [HxWxD], where D is the maximum disparity.
left_image: The left image that some methods like SGM uses to adapt its calculations.
"""
volume: torch.Tensor
left_image: torch.Tensor
def to(self, device):
"""
Returns a new instance with its tensor attributes to the given device.
"""
return CostFixture(self.volume.to(device), self.left_image.to(device))
@pytest.fixture
def ssd_cost():
"""
Fixture for aggregation and disparity reduce methods to have a common SSD cost volume to test.
"""
cache_file = (Path(__file__).parent /
"test_cache/cost_volume_teddy.torch")
image_base_dir = (Path(__file__).parent.parent /
"tests/data/middleburry/teddy/")
left_image = torch.from_numpy(
np.array(Image.open(image_base_dir / "im2.png").convert('L')))
if cache_file.exists():
return CostFixture(volume=torch.load(str(cache_file)),
left_image=left_image)
right_image = torch.from_numpy(
np.array(Image.open(image_base_dir / "im6.png").convert('L')))
cost_volume = stereomatch.cost.SSD(128)(left_image, right_image)
cache_file.parent.mkdir(exist_ok=True, parents=True)
torch.save(cost_volume, str(cache_file))
return CostFixture(volume=cost_volume, left_image=left_image)
def pytest_configure():
"""
Configures global variables used during testing.
"""
pytest.STM_TEST_OUTPUT_PATH = Path(__file__).parent / "test-result"
pytest.STM_MAX_DISPARITY = 32