bioimage-io / core-bioimage-io-python

Python libraries for loading, running and packaging bioimage.io models
https://bioimage-io.github.io/core-bioimage-io-python/bioimageio/core.html
MIT License
28 stars 21 forks source link

New predict command: tiled prediction not working for model v0.4 #415

Open constantinpape opened 3 months ago

constantinpape commented 3 months ago

Tiled prediction seems to not be implemented for v0.4 models:

import bioimageio.core
import imageio.v3 as imageio
from xarray import DataArray

from bioimageio.spec.model.v0_5 import AxisId

image = imageio.imread("/home/pape/.cache/micro_sam/sample_data/livecell-2d-image.png")
model = bioimageio.core.load_description("hiding-tiger")
model.inputs[0].data_type = "float32"

input_descr = model.inputs[0]
# this still does not work for me! (It would be great to fix it)
# inputs = {input_descr.name: DataArray(image[None, None], dims=tuple(input_descr.axes))}
inputs = {input_descr.name: DataArray(image[None, None], dims=("batch", "channel", "y", "x"))}

# tile_shape = {"y": 512, "x": 512}
tile_shape = {input_descr.name: {AxisId("x"): 512, AxisId("y"): 512}}
pred = bioimageio.core.predict(model=model, inputs=inputs, input_block_shape=tile_shape)

fails with

Traceback (most recent call last):
  File "/home/pape/Work/playground/bioimageio/new-pred/check_new_pred.py", line 18, in <module>
    pred = bioimageio.core.predict(model=model, inputs=inputs, input_block_shape=tile_shape)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pape/Work/bioimageio/core-bioimage-io-python/bioimageio/core/prediction.py", line 100, in predict
    output = pp.predict_sample_with_fixed_blocking(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pape/Work/bioimageio/core-bioimage-io-python/bioimageio/core/_prediction_pipeline.py", line 208, in predict_sample_with_fixed_blocking
    self.predict_sample_block(
  File "/home/pape/Work/bioimageio/core-bioimage-io-python/bioimageio/core/_prediction_pipeline.py", line 114, in predict_sample_block
    raise NotImplementedError(
NotImplementedError: predict_sample_block not implemented for model 0.4.10
FynnBe commented 3 months ago

Yes, blocking is not implemented for model 0.4. I think it'll be best to update all relevant models in the model zoo instead of making this backwards compatible. many models, like hiding-tiger can be converted automatically, so when we use format_version="latest" or format_version="0.5" it works as is:

import collections.abc
from typing import Any

import imageio.v3 as imageio
from xarray import DataArray

import bioimageio.core
import bioimageio.spec
from bioimageio.spec.model.v0_5 import AxisId, NDArray

model = bioimageio.core.load_model("hiding-tiger", format_version="0.5")
assert isinstance(model, bioimageio.spec.model.v0_5.ModelDescr)
assert not isinstance(model.inputs[0].data, collections.abc.Sequence)
# input data type should be float32 now (after updating hiding-tiger to version 1.1)
print(model.inputs[0].data.type)

input_descr = model.inputs[0]
image: NDArray[Any] = imageio.imread(
    "/home/pape/.cache/micro_sam/sample_data/livecell-2d-image.png"
)

# this still does not work for me! (It would be great to fix it)
# inputs = {input_descr.id: DataArray(image[None, None], dims=tuple(input_descr.axes))}
# -> fix:
inputs = {
    input_descr.id: DataArray(
        image[None, None],
        dims=tuple(a.id for a in input_descr.axes),  # use id of every axis
    )
}
# alternative:
# inputs = bioimageio.core.digest_spec.load_sample_for_model(
#     model=model,
#     paths={model.inputs[0].id: Path("image path")},
# )

# tile_shape = {"y": 512, "x": 512}
tile_shape = {input_descr.id: {AxisId("x"): 512, AxisId("y"): 512}}
pred = bioimageio.core.predict(model=model, inputs=inputs, input_block_shape=tile_shape)
constantinpape commented 3 months ago

The model conversion does not work for me (which is a separate issue tied to the changes you made for this model, see message in the chat)

FynnBe commented 3 months ago

The model conversion does not work for me (which is a separate issue tied to the changes you made for this model, see message in the chat)

fixed