Skip to content

Commit

Permalink
Merge pull request #359 from bioimage-io/fix-tiling
Browse files Browse the repository at this point in the history
Fix critical bug in predict with tiling
  • Loading branch information
FynnBe authored Nov 14, 2023
2 parents cc6ff24 + 06c27fc commit 418c750
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
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

0 comments on commit 418c750

Please sign in to comment.