Skip to content

Commit d1b46c9

Browse files
Merge pull request #149 from bioimage-io/extend-cli-tests
Add more tests for weight_format
2 parents 65a1aad + 502d45b commit d1b46c9

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

tests/test_cli.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_cli_test_model(unet2d_nuclei_broad_model):
1616
assert ret.returncode == 0
1717

1818

19-
def test_cli_test_model_with_specific_weight_format(unet2d_nuclei_broad_model):
19+
def test_cli_test_model_with_weight_format(unet2d_nuclei_broad_model):
2020
ret = subprocess.run(
2121
["bioimageio", "test-model", unet2d_nuclei_broad_model, "--weight-format", "pytorch_state_dict"]
2222
)
@@ -28,25 +28,34 @@ def test_cli_test_resource(unet2d_nuclei_broad_model):
2828
assert ret.returncode == 0
2929

3030

31-
def test_cli_test_resource_with_specific_weight_format(unet2d_nuclei_broad_model):
31+
def test_cli_test_resource_with_weight_format(unet2d_nuclei_broad_model):
3232
ret = subprocess.run(
3333
["bioimageio", "test-model", unet2d_nuclei_broad_model, "--weight-format", "pytorch_state_dict"]
3434
)
3535
assert ret.returncode == 0
3636

3737

38-
def test_cli_predict_image(unet2d_nuclei_broad_model, tmp_path):
39-
spec = load_resource_description(unet2d_nuclei_broad_model)
38+
def _test_cli_predict_image(model, tmp_path, extra_kwargs=None):
39+
spec = load_resource_description(model)
4040
in_path = spec.test_inputs[0]
4141
out_path = tmp_path.with_suffix(".npy")
42-
ret = subprocess.run(
43-
["bioimageio", "predict-image", unet2d_nuclei_broad_model, "--inputs", str(in_path), "--outputs", str(out_path)]
44-
)
42+
cmd = ["bioimageio", "predict-image", model, "--inputs", str(in_path), "--outputs", str(out_path)]
43+
if extra_kwargs is not None:
44+
cmd.extend(extra_kwargs)
45+
ret = subprocess.run(cmd)
4546
assert ret.returncode == 0
4647
assert out_path.exists()
4748

4849

49-
def test_cli_predict_images(unet2d_nuclei_broad_model, tmp_path):
50+
def test_cli_predict_image(unet2d_nuclei_broad_model, tmp_path):
51+
_test_cli_predict_image(unet2d_nuclei_broad_model, tmp_path)
52+
53+
54+
def test_cli_predict_image_with_weight_format(unet2d_nuclei_broad_model, tmp_path):
55+
_test_cli_predict_image(unet2d_nuclei_broad_model, tmp_path, ["--weight-format", "pytorch_state_dict"])
56+
57+
58+
def _test_cli_predict_images(model, tmp_path, extra_kwargs=None):
5059
n_images = 3
5160
shape = (1, 1, 128, 128)
5261
expected_shape = (1, 1, 128, 128)
@@ -64,14 +73,25 @@ def test_cli_predict_images(unet2d_nuclei_broad_model, tmp_path):
6473
expected_outputs.append(out_folder / f"im-{i}.npy")
6574

6675
input_pattern = str(in_folder / "*.npy")
67-
ret = subprocess.run(["bioimageio", "predict-images", unet2d_nuclei_broad_model, input_pattern, str(out_folder)])
76+
cmd = ["bioimageio", "predict-images", model, input_pattern, str(out_folder)]
77+
if extra_kwargs is not None:
78+
cmd.extend(extra_kwargs)
79+
ret = subprocess.run(cmd)
6880
assert ret.returncode == 0
6981

7082
for out_path in expected_outputs:
7183
assert out_path.exists()
7284
assert np.load(out_path).shape == expected_shape
7385

7486

87+
def test_cli_predict_images(unet2d_nuclei_broad_model, tmp_path):
88+
_test_cli_predict_images(unet2d_nuclei_broad_model, tmp_path)
89+
90+
91+
def test_cli_predict_images_with_weight_format(unet2d_nuclei_broad_model, tmp_path):
92+
_test_cli_predict_images(unet2d_nuclei_broad_model, tmp_path, ["--weight-format", "pytorch_state_dict"])
93+
94+
7595
def test_torch_to_torchscript(unet2d_nuclei_broad_model, tmp_path):
7696
out_path = tmp_path.with_suffix(".pt")
7797
ret = subprocess.run(

tests/test_prediction.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ def test_predict_image(unet2d_fixed_shape_or_not, tmpdir):
3939
assert_array_almost_equal(res, exp, decimal=4)
4040

4141

42+
def test_predict_image_with_weight_format(unet2d_fixed_shape_or_not, tmpdir):
43+
from bioimageio.core.prediction import predict_image
44+
45+
spec = load_resource_description(unet2d_fixed_shape_or_not)
46+
assert isinstance(spec, Model)
47+
inputs = spec.test_inputs
48+
49+
outputs = [Path(tmpdir) / f"out{i}.npy" for i in range(len(spec.test_outputs))]
50+
predict_image(unet2d_fixed_shape_or_not, inputs, outputs, weight_format="pytorch_state_dict")
51+
for out_path in outputs:
52+
assert out_path.exists()
53+
54+
result = [np.load(str(p)) for p in outputs]
55+
expected = [np.load(str(p)) for p in spec.test_outputs]
56+
for res, exp in zip(result, expected):
57+
assert_array_almost_equal(res, exp, decimal=4)
58+
59+
4260
def test_predict_image_with_padding(unet2d_fixed_shape_or_not, tmp_path):
4361
any_model = unet2d_fixed_shape_or_not # todo: replace 'unet2d_fixed_shape_or_not' with 'any_model'
4462
from bioimageio.core.prediction import predict_image

0 commit comments

Comments
 (0)