Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions tests/test_handler_metrics_saver_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_content(self):
self._run(tempdir)

def _run(self, tempdir):
my_rank = dist.get_rank()
fnames = ["aaa" * 300, "bbb" * 301, "ccc" * 302]

metrics_saver = MetricsSaver(
Expand All @@ -46,15 +47,15 @@ def _val_func(engine, batch):

engine = Engine(_val_func)

if dist.get_rank() == 0:
if my_rank == 0:
data = [{"image_meta_dict": {"filename_or_obj": [fnames[0]]}}]

@engine.on(Events.EPOCH_COMPLETED)
def _save_metrics0(engine):
engine.state.metrics = {"metric1": 1, "metric2": 2}
engine.state.metric_details = {"metric3": torch.tensor([[1, 2]]), "metric4": torch.tensor([[5, 6]])}

if dist.get_rank() == 1:
if my_rank == 1:
# different ranks have different data length
data = [
{"image_meta_dict": {"filename_or_obj": [fnames[1]]}},
Expand All @@ -79,7 +80,7 @@ def _all_gather(engine):
metrics_saver.attach(engine)
engine.run(data, max_epochs=1)

if dist.get_rank() == 0:
Comment thread
wyli marked this conversation as resolved.
if my_rank == 0:
# check the metrics.csv and content
self.assertTrue(os.path.exists(os.path.join(tempdir, "metrics.csv")))
with open(os.path.join(tempdir, "metrics.csv")) as f:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration_determinism.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def setUp(self):
def tearDown(self):
set_determinism(seed=None)

@TimedCall(seconds=150)
@TimedCall(seconds=150, skip_timing=not torch.cuda.is_available())
def test_training(self):
set_determinism(seed=0)
loss, step = run_test(device=self.device)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration_stn.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def setUp(self):
def tearDown(self):
set_determinism(seed=None)

@TimedCall(seconds=100)
@TimedCall(seconds=100, skip_timing=not torch.cuda.is_available())
def test_training(self):
"""
check that the quality AffineTransform backpropagation
Expand Down
32 changes: 16 additions & 16 deletions tests/test_savitzky_golay_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
torch.Tensor([1.0]).unsqueeze(0).unsqueeze(0), # Input data: Single value
torch.Tensor([1 / 3]).unsqueeze(0).unsqueeze(0), # Expected output: With a window length of 3 and polyorder 1
# output should be equal to mean of 0, 1 and 0 = 1/3 (because input will be zero-padded and a linear fit performed)
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

TEST_CASE_1D = [
Expand All @@ -35,21 +35,21 @@
.unsqueeze(0)
.unsqueeze(0), # Expected output: zero padded, so linear interpolation
# over length-3 windows will result in output of [2/3, 1, 2/3].
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

TEST_CASE_2D_AXIS_2 = [
{"window_length": 3, "order": 1}, # along default axis (2, first spatial dim)
torch.ones((3, 2)).unsqueeze(0).unsqueeze(0),
torch.Tensor([[2 / 3, 2 / 3], [1.0, 1.0], [2 / 3, 2 / 3]]).unsqueeze(0).unsqueeze(0),
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

TEST_CASE_2D_AXIS_3 = [
{"window_length": 3, "order": 1, "axis": 3}, # along axis 3 (second spatial dim)
torch.ones((2, 3)).unsqueeze(0).unsqueeze(0),
torch.Tensor([[2 / 3, 1.0, 2 / 3], [2 / 3, 1.0, 2 / 3]]).unsqueeze(0).unsqueeze(0),
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

# Replicated-padding trivial tests
Expand All @@ -59,29 +59,29 @@
torch.Tensor([1.0]).unsqueeze(0).unsqueeze(0), # Input data: Single value
torch.Tensor([1.0]).unsqueeze(0).unsqueeze(0), # Expected output: With a window length of 3 and polyorder 1
# output will be equal to mean of [1, 1, 1] = 1 (input will be nearest-neighbour-padded and a linear fit performed)
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

TEST_CASE_1D_REP = [
{"window_length": 3, "order": 1, "mode": "replicate"},
torch.Tensor([1.0, 1.0, 1.0]).unsqueeze(0).unsqueeze(0), # Input data
torch.Tensor([1.0, 1.0, 1.0]).unsqueeze(0).unsqueeze(0), # Expected output: zero padded, so linear interpolation
# over length-3 windows will result in output of [2/3, 1, 2/3].
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

TEST_CASE_2D_AXIS_2_REP = [
{"window_length": 3, "order": 1, "mode": "replicate"}, # along default axis (2, first spatial dim)
torch.ones((3, 2)).unsqueeze(0).unsqueeze(0),
torch.Tensor([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]]).unsqueeze(0).unsqueeze(0),
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

TEST_CASE_2D_AXIS_3_REP = [
{"window_length": 3, "order": 1, "axis": 3, "mode": "replicate"}, # along axis 3 (second spatial dim)
torch.ones((2, 3)).unsqueeze(0).unsqueeze(0),
torch.Tensor([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]).unsqueeze(0).unsqueeze(0),
1e-15, # absolute tolerance
1e-6, # absolute tolerance
]

# Sine smoothing
Expand All @@ -101,38 +101,38 @@ class TestSavitzkyGolayCPU(unittest.TestCase):
@parameterized.expand(
[TEST_CASE_SINGLE_VALUE, TEST_CASE_1D, TEST_CASE_2D_AXIS_2, TEST_CASE_2D_AXIS_3, TEST_CASE_SINE_SMOOTH]
)
def test_value(self, arguments, image, expected_data, atol):
def test_value(self, arguments, image, expected_data, atol, rtol=1e-5):
result = SavitzkyGolayFilter(**arguments)(image)
np.testing.assert_allclose(result, expected_data, atol=atol)
np.testing.assert_allclose(result, expected_data, atol=atol, rtol=rtol)


class TestSavitzkyGolayCPUREP(unittest.TestCase):
@parameterized.expand(
[TEST_CASE_SINGLE_VALUE_REP, TEST_CASE_1D_REP, TEST_CASE_2D_AXIS_2_REP, TEST_CASE_2D_AXIS_3_REP]
)
def test_value(self, arguments, image, expected_data, atol):
def test_value(self, arguments, image, expected_data, atol, rtol=1e-5):
result = SavitzkyGolayFilter(**arguments)(image)
np.testing.assert_allclose(result, expected_data, atol=atol)
np.testing.assert_allclose(result, expected_data, atol=atol, rtol=rtol)


@skip_if_no_cuda
class TestSavitzkyGolayGPU(unittest.TestCase):
@parameterized.expand(
[TEST_CASE_SINGLE_VALUE, TEST_CASE_1D, TEST_CASE_2D_AXIS_2, TEST_CASE_2D_AXIS_3, TEST_CASE_SINE_SMOOTH]
)
def test_value(self, arguments, image, expected_data, atol):
def test_value(self, arguments, image, expected_data, atol, rtol=1e-5):
result = SavitzkyGolayFilter(**arguments)(image.to(device="cuda"))
np.testing.assert_allclose(result.cpu(), expected_data, atol=atol)
np.testing.assert_allclose(result.cpu(), expected_data, atol=atol, rtol=rtol)


@skip_if_no_cuda
class TestSavitzkyGolayGPUREP(unittest.TestCase):
@parameterized.expand(
[TEST_CASE_SINGLE_VALUE_REP, TEST_CASE_1D_REP, TEST_CASE_2D_AXIS_2_REP, TEST_CASE_2D_AXIS_3_REP]
)
def test_value(self, arguments, image, expected_data, atol):
def test_value(self, arguments, image, expected_data, atol, rtol=1e-5):
result = SavitzkyGolayFilter(**arguments)(image.to(device="cuda"))
np.testing.assert_allclose(result.cpu(), expected_data, atol=atol)
np.testing.assert_allclose(result.cpu(), expected_data, atol=atol, rtol=rtol)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from functools import partial
from subprocess import PIPE, Popen
from typing import Callable, Optional, Tuple
from urllib.error import HTTPError, URLError
from urllib.error import ContentTooShortError, HTTPError, URLError

import numpy as np
import torch
Expand Down