Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix critical bug in predict with tiling #359

Merged
merged 3 commits into from
Nov 14, 2023
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
5 changes: 4 additions & 1 deletion bioimageio/core/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,16 @@ def check_tiling(tiling):
# override metadata defaults with provided dict
if isinstance(tiling, dict):
for key in ["halo", "tile", "scale"]:
default_tiling.update(tiling.get(key, dict()))
default_tiling[key].update(tiling.get(key, dict()))
tiling = default_tiling
check_tiling(tiling)

elif isinstance(tiling, bool) and not tiling:
raise NotImplementedError("Should be unreachable")

else:
raise ValueError(f"Invalid argument for tiling: {tiling}")

return tiling


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
packages=find_namespace_packages(exclude=["tests"]), # Required
install_requires=[
"bioimageio.spec==0.4.9.*",
"bioimageio.spec==0.4.9",
"imageio>=2.5",
"numpy",
"ruamel.yaml",
Expand Down
15 changes: 10 additions & 5 deletions tests/test_bioimageio_spec_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ def test_bioimageio_spec_version():
# get currently pinned bioimageio.spec version
meta = metadata("bioimageio.core")
req = meta["Requires-Dist"]
assert req.startswith("bioimageio.spec (==")
pmaj, pmin, ppatch, *asterisk_and_rest = req[len("bioimageio.spec (==") :].split(".")
assert asterisk_and_rest[0].startswith(
"*"
), "bioimageio.spec version should be pinned down to patch, e.g. '0.4.9.*'"
print(req)
assert req.startswith("bioimageio.spec ==")
spec_ver = req[len("bioimageio.spec ==") :]
assert spec_ver.count(".") == 2
pmaj, pmin, ppatchand_and_post = spec_ver.split(".")
assert (ppatchand_and_post.isdigit() or ppatchand_and_post[:-1].isdigit()) and (
ppatchand_and_post[-1] == "*" or ppatchand_and_post[-1].isdigit()
), "bioimageio.spec version should be pinned down to patch, e.g. '0.4.9*'"

ppatch = ppatchand_and_post[:-1] if ppatchand_and_post[-1] == "*" else ppatchand_and_post
pinned = Version(f"{pmaj}.{pmin}.{ppatch}")

assert pinned >= released, "bioimageio.spec pinned to an old version!"
20 changes: 9 additions & 11 deletions tests/test_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ def check_result():
check_result()

# test with fixed padding
predict_image(
model, in_path, out_path, padding={"x": original_shape[0], "y": original_shape[1], "mode": "fixed"}
)
predict_image(model, in_path, out_path, padding={"x": original_shape[0], "y": original_shape[1], "mode": "fixed"})
check_result()

# test with automated padding
Expand All @@ -135,7 +133,7 @@ def test_predict_image_with_padding_channel_last(stardist, tmp_path):
_test_predict_with_padding(stardist, tmp_path)


def _test_predict_image_with_tiling(model, tmp_path, exp_mean_deviation):
def _test_predict_image_with_tiling(model, tmp_path: Path, exp_mean_deviation):
from bioimageio.core.prediction import predict_image

spec = load_resource_description(model)
Expand Down Expand Up @@ -168,27 +166,27 @@ def check_result():

# prediction with tiling with the parameters above may not be suited for any model
# so we only run it for the pytorch unet2d here
def test_predict_image_with_tiling_1(unet2d_nuclei_broad_model, tmp_path):
def test_predict_image_with_tiling_1(unet2d_nuclei_broad_model, tmp_path: Path):
_test_predict_image_with_tiling(unet2d_nuclei_broad_model, tmp_path, 0.012)


def test_predict_image_with_tiling_2(unet2d_diff_output_shape, tmp_path):
_test_predict_image_with_tiling(unet2d_diff_output_shape, tmp_path, 0.012)
def test_predict_image_with_tiling_2(unet2d_diff_output_shape, tmp_path: Path):
_test_predict_image_with_tiling(unet2d_diff_output_shape, tmp_path, 0.06)


def test_predict_image_with_tiling_3(shape_change_model, tmp_path):
def test_predict_image_with_tiling_3(shape_change_model, tmp_path: Path):
_test_predict_image_with_tiling(shape_change_model, tmp_path, 0.012)


def test_predict_image_with_tiling_channel_last(stardist, tmp_path):
def test_predict_image_with_tiling_channel_last(stardist, tmp_path: Path):
_test_predict_image_with_tiling(stardist, tmp_path, 0.13)


def test_predict_image_with_tiling_fixed_output_shape(unet2d_fixed_shape, tmp_path):
def test_predict_image_with_tiling_fixed_output_shape(unet2d_fixed_shape, tmp_path: Path):
_test_predict_image_with_tiling(unet2d_fixed_shape, tmp_path, 0.025)


def test_predict_images(unet2d_nuclei_broad_model, tmp_path):
def test_predict_images(unet2d_nuclei_broad_model, tmp_path: Path):
from bioimageio.core.prediction import predict_images

n_images = 5
Expand Down
Loading