Skip to content

client ¤

Classes:

Name Description
DescriptionSerializer

Description serializer intended for client/server communication, NOT for sharing resource descriptions.

GradioModelAdapter

Model adapter to use the bioimage-io-gradio-runner as a backend for model inference.

GradioPredictionPipeline

Prediction pipeline to use the bioimage-io-gradio-runner as a fully remote prediction pipeline.

Attributes:

Name Type Description
SerializedSampleBlock

SerializedSampleBlock module-attribute ¤

SerializedSampleBlock = Dict[str, JsonValue]

DescriptionSerializer ¤

Description serializer intended for client/server communication, NOT for sharing resource descriptions.

This serializer only includes local files to keep the serialized package small.

Methods:

Name Description
deserialize
deserialize_from_string
serialize
serialize_to_string
serialize_to_string_and_hash

Attributes:

Name Type Description
STRING_ENCODING

STRING_ENCODING class-attribute instance-attribute ¤

STRING_ENCODING = 'ascii'

deserialize staticmethod ¤

deserialize(serialized: bytes) -> ResourceDescr
Source code in src/bioimageio/core/_description_serializer.py
51
52
53
54
55
56
57
@staticmethod
def deserialize(serialized: bytes) -> ResourceDescr:
    descr = load_description(ZipFile(BytesIO(serialized)), perform_io_checks=False)
    if isinstance(descr, InvalidDescr):
        raise ValueError(f"invalid serialized model package: {descr.get_reason()}")

    return descr

deserialize_from_string classmethod ¤

deserialize_from_string(serialized: str) -> ResourceDescr
Source code in src/bioimageio/core/_description_serializer.py
46
47
48
49
@classmethod
def deserialize_from_string(cls, serialized: str) -> ResourceDescr:
    package_bytes = base64.b64decode(serialized.encode(cls.STRING_ENCODING))
    return cls.deserialize(package_bytes)

serialize staticmethod ¤

serialize(rd: ResourceDescr) -> bytes
Source code in src/bioimageio/core/_description_serializer.py
24
25
26
27
28
@staticmethod
def serialize(rd: ResourceDescr) -> bytes:
    stream = save_bioimageio_package_to_stream(rd, local_files_only=True)
    _ = stream.seek(0)
    return stream.read()

serialize_to_string classmethod ¤

serialize_to_string(rd: ResourceDescr) -> str
Source code in src/bioimageio/core/_description_serializer.py
30
31
32
33
34
35
36
37
38
39
40
@classmethod
def serialize_to_string(cls, rd: ResourceDescr) -> str:
    package_bytes = cls.serialize(rd)

    safe_bytes = cls._get_safe_bytes(package_bytes)
    serialized_str = safe_bytes.decode(cls.STRING_ENCODING)
    if len(serialized_str) <= 2083:
        raise RuntimeError(
            "Serialized model description should be longer than 2083 characters to not be treated as a URL on the server side."
        )
    return serialized_str

serialize_to_string_and_hash classmethod ¤

serialize_to_string_and_hash(rd: ResourceDescr) -> Tuple[str, Sha256]
Source code in src/bioimageio/core/_description_serializer.py
59
60
61
62
63
64
65
66
67
68
69
@classmethod
def serialize_to_string_and_hash(cls, rd: ResourceDescr) -> Tuple[str, Sha256]:
    package_bytes = cls.serialize(rd)
    safe_bytes = cls._get_safe_bytes(package_bytes)
    serialized_str = safe_bytes.decode(cls.STRING_ENCODING)
    if len(serialized_str) <= 2083:
        raise RuntimeError(
            "Serialized model description should be longer than 2083 characters to not be treated as a URL on the server side."
        )
    sha256 = Sha256(hashlib.sha256(package_bytes).hexdigest())
    return serialized_str, sha256

GradioModelAdapter ¤

GradioModelAdapter(model_description: AnyModelDescr, *, server: Optional[str] = None)

Bases: RemoteModelAdapter[SerializedSampleBlock]


              flowchart TD
              bioimageio.core.remote_backends.gradio.client.GradioModelAdapter[GradioModelAdapter]
              bioimageio.core._model_adapter.RemoteModelAdapter[RemoteModelAdapter]
              bioimageio.core._model_adapter.ModelAdapter[ModelAdapter]

                              bioimageio.core._model_adapter.RemoteModelAdapter --> bioimageio.core.remote_backends.gradio.client.GradioModelAdapter
                                bioimageio.core._model_adapter.ModelAdapter --> bioimageio.core._model_adapter.RemoteModelAdapter
                



              click bioimageio.core.remote_backends.gradio.client.GradioModelAdapter href "" "bioimageio.core.remote_backends.gradio.client.GradioModelAdapter"
              click bioimageio.core._model_adapter.RemoteModelAdapter href "" "bioimageio.core._model_adapter.RemoteModelAdapter"
              click bioimageio.core._model_adapter.ModelAdapter href "" "bioimageio.core._model_adapter.ModelAdapter"
            

Model adapter to use the bioimage-io-gradio-runner as a backend for model inference.

Note
  • This adapter requires an environment with the same gradio version as the one used on the bioimage-io-gradio-runner server.

Parameters:

Name Type Description Default

model_description ¤

AnyModelDescr

The model to run inference with.

required

server ¤

Optional[str]

The URL of a running bioimage-io-gradio-server instance (default server might not be availability/compatible).

None

Methods:

Name Description
close

Close the model adapter, freeing any resources.

forward
load
test

Run the bioimageio model test.

unload

Unload model from any devices, freeing their memory.

Attributes:

Name Type Description
model_descr AnyModelDescr
server str
Source code in src/bioimageio/core/remote_backends/gradio/client.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def __init__(
    self, model_description: AnyModelDescr, *, server: Optional[str] = None
):
    """Initialize the GradioModelAdapter.

    Note:
        - This adapter requires an environment with the same gradio version as the one used on the bioimage-io-gradio-runner server.

    Args:
        model_description: The model to run inference with.
        server: The URL of a running bioimage-io-gradio-server instance (default server might not be availability/compatible).
    """
    server = server or settings.gradio_server
    if server is None:
        raise ValueError(
            "No gradio server specified. Please provide a server URL or set the 'BIOIMAGEIO_GRADIO_SERVER' environment variable."
        )

    self._client = Client(server, httpx_kwargs={"timeout": 60})
    self._serialized_model, self._sha256 = (
        DescriptionSerializer.serialize_to_string_and_hash(model_description)
    )
    super().__init__(
        model_description, server=server, sample_serializer=GradioSampleSerializer()
    )

model_descr property ¤

model_descr: AnyModelDescr

server property ¤

server: str

close ¤

close()

Close the model adapter, freeing any resources.

Note

The moder adapter should be considered unusable afterwards.

Source code in src/bioimageio/core/_model_adapter.py
 96
 97
 98
 99
100
101
102
def close(self):
    """Close the model adapter, freeing any resources.

    Note:
        The moder adapter should be considered unusable afterwards.
    """
    self.unload()

forward ¤

forward(inputs: PerMember[Optional[Tensor]]) -> PerMember[Optional[Tensor]]
Source code in src/bioimageio/core/_model_adapter.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
def forward(
    self, inputs: PerMember[Optional[Tensor]]
) -> PerMember[Optional[Tensor]]:
    serialized_input = self._serializer.serialize_sample(
        Sample(
            members={k: v for k, v in inputs.items() if v is not None},
            stat={},
            id=None,
        )
    )
    serialized_output = self._forward_impl(serialized_input)
    output = self._serializer.deserialize_sample(serialized_output).members
    output = restore_batch_multi_index(inputs, output)
    return output

load ¤

load() -> None
Source code in src/bioimageio/core/remote_backends/gradio/client.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def load(self) -> None:
    for model_data in ("", self._serialized_model):
        try:
            result = self._client.submit(
                api_name="/load_model", model=model_data, sha256=self._sha256
            ).result()
        except Exception as e:
            if model_data:
                logger.warning(
                    "Failed to load model on server with model_data, error was: {}",
                    len(model_data),
                    e,
                )
        else:
            if result:
                break

test ¤

test() -> Optional[ValidationSummary]

Run the bioimageio model test.

Source code in src/bioimageio/core/remote_backends/gradio/client.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def test(self) -> Optional[ValidationSummary]:
    for model_data in ("", self._serialized_model):
        try:
            result = self._client.submit(
                api_name="/test_model", model=model_data, sha256=self._sha256
            ).result()
        except Exception as e:
            if model_data:
                logger.warning(
                    "Failed to test model on server with model_data, error was: {}",
                    len(model_data),
                    e,
                )
        else:
            if result:
                return ValidationSummary.model_validate_json(result)

    return None

unload ¤

unload()

Unload model from any devices, freeing their memory.

Note

The moder adapter should be considered unusable afterwards.

Source code in src/bioimageio/core/remote_backends/gradio/client.py
69
70
def unload(self):
    return super().unload()

GradioPredictionPipeline ¤

GradioPredictionPipeline(model_description: AnyModelDescr, *, server: Optional[str] = None, precomputed_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}), default_blocksize_parameter: BlocksizeParameter = 10, default_batch_size: int = 1)

Bases: RemotePredictionPipeline


              flowchart TD
              bioimageio.core.remote_backends.gradio.client.GradioPredictionPipeline[GradioPredictionPipeline]
              bioimageio.core._prediction_pipeline.RemotePredictionPipeline[RemotePredictionPipeline]
              bioimageio.core._prediction_pipeline._PredictionPipelineBase[_PredictionPipelineBase]

                              bioimageio.core._prediction_pipeline.RemotePredictionPipeline --> bioimageio.core.remote_backends.gradio.client.GradioPredictionPipeline
                                bioimageio.core._prediction_pipeline._PredictionPipelineBase --> bioimageio.core._prediction_pipeline.RemotePredictionPipeline
                



              click bioimageio.core.remote_backends.gradio.client.GradioPredictionPipeline href "" "bioimageio.core.remote_backends.gradio.client.GradioPredictionPipeline"
              click bioimageio.core._prediction_pipeline.RemotePredictionPipeline href "" "bioimageio.core._prediction_pipeline.RemotePredictionPipeline"
              click bioimageio.core._prediction_pipeline._PredictionPipelineBase href "" "bioimageio.core._prediction_pipeline._PredictionPipelineBase"
            

Prediction pipeline to use the bioimage-io-gradio-runner as a fully remote prediction pipeline.

Parameters:

Name Type Description Default

model_description ¤

AnyModelDescr

The model to run inference with.

required

server ¤

Optional[str]

The URL or Hugging Face space name of a running bioimageio gradio server instance (Note: default server might not be availabile/compatible!).

None

Methods:

Name Description
predict_sample_block

Predict a single sample block.

predict_sample_with_blocking

Predict a sample by predicting sample blocks.

predict_sample_with_blocking_yield_intermediates

Predict sample by predicting sample blocks and yield intermediate predictions if no samplewise postprocessing is included.

predict_sample_with_fixed_blocking

Predict sample with given input_block_shape.

predict_sample_with_fixed_blocking_yield_intermediates

Predict sample by predicting sample blocks of input_block_shape and yield intermediate predictions if no samplewise postprocessing is included.

predict_sample_without_blocking

Predict a whole sample at once.

Attributes:

Name Type Description
input_ids Sequence[MemberId]
model_descr AnyModelDescr
model_description AnyModelDescr
output_ids Sequence[MemberId]
pad_mode
server str
Source code in src/bioimageio/core/remote_backends/gradio/client.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def __init__(
    self,
    model_description: AnyModelDescr,
    *,
    server: Optional[str] = None,
    precomputed_statistics: Mapping[Measure, MeasureValue] = MappingProxyType({}),
    default_blocksize_parameter: BlocksizeParameter = 10,
    default_batch_size: int = 1,
):
    """
    Note:
        - This pipeline requires an environment with the same gradio version as the one used on the bioimage-io-gradio-runner server.

    Args:
        model_description: The model to run inference with.
        server: The URL or Hugging Face space name of a running bioimageio gradio server instance (Note: default server might not be availabile/compatible!).
    """
    server = server or settings.gradio_server
    if server is None:
        raise ValueError(
            "No gradio server specified. Please provide a server URL or set the 'BIOIMAGEIO_GRADIO_SERVER' environment variable."
        )

    super().__init__(
        model_description,
        server=server,
        default_blocksize_parameter=default_blocksize_parameter,
        default_batch_size=default_batch_size,
    )
    self._client = Client(self.server, httpx_kwargs={"timeout": 60})
    self._serialized_model, self._sha256 = (
        DescriptionSerializer.serialize_to_string_and_hash(model_description)
    )
    self._serializer = GradioSampleSerializer
    self._precomputed_statistics = dict(precomputed_statistics)

input_ids property ¤

input_ids: Sequence[MemberId]

model_descr property ¤

model_descr: AnyModelDescr

model_description property ¤

model_description: AnyModelDescr

output_ids property ¤

output_ids: Sequence[MemberId]

pad_mode instance-attribute ¤

pad_mode = {} if isinstance(model_descr, v0_4.ModelDescr) else {(descr.id): (descr.pad or v0_5.SymmetricPadding()) for descr in (model_descr.inputs)}

server property ¤

server: str

predict_sample_block ¤

predict_sample_block(sample_block: SampleBlock, skip_preprocessing: bool = False, skip_postprocessing: bool = False) -> SampleBlock

Predict a single sample block.

Note that this does not apply samplewise preprocessing or postprocessing steps, but only blockwise ones.

Parameters:

Name Type Description Default

sample_block ¤

SampleBlock

The sample block to predict on.

required

skip_preprocessing ¤

bool

If True, skip blockwise preprocessing steps.

False

skip_postprocessing ¤

bool

If True, skip blockwise postprocessing steps.

False
Source code in src/bioimageio/core/remote_backends/gradio/client.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def predict_sample_block(
    self,
    sample_block: SampleBlock,
    skip_preprocessing: bool = False,
    skip_postprocessing: bool = False,
) -> SampleBlock:
    if isinstance(self._model_descr, v0_4.ModelDescr):
        raise NotImplementedError(
            f"predict_sample_block not implemented for model {self._model_descr.format_version}"
        )
    else:
        assert self._block_transform is not None

    sample_block.stat.update(self._precomputed_statistics)
    output_block = self._serializer.deserialize_sample(
        _call_predict_api(
            self._client,
            self._serialized_model,
            self._sha256,
            serialized_input_sample=self._serializer.serialize_sample(
                sample_block.as_sample()
            ),
            blocksize=None,
            skip_preprocessing=skip_preprocessing,
            skip_postprocessing=skip_postprocessing,
            skip_input_padding=True,
            skip_output_cropping=True,
            batch_size=self._default_batch_size,
        )
    )
    output_meta = sample_block.get_transformed_meta(self._block_transform)
    return output_meta.with_data(output_block.members, stat=sample_block.stat)

predict_sample_with_blocking ¤

predict_sample_with_blocking(sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, ns: Optional[Union[v0_5.ParameterizedSize_N, Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N]]] = None, batch_size: Optional[int] = None) -> Sample

Predict a sample by predicting sample blocks.

Note: For fixed/known blocksizes use predict_sample_with_fixed_blocking.

Parameters:

Name Type Description Default

sample ¤

Sample

The sample to predict on.

required

skip_preprocessing ¤

bool

If True, skip all preprocessing steps.

False

skip_postprocessing ¤

bool

If True, skip all postprocessing steps.

False

ns ¤

Optional[Union[v0_5.ParameterizedSize_N, Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N]]]

Block size parameter(s) allows scaling the model's default input block size. Blocksize parameters are only applied to parameterized input axes, all other axis sizes are fixed/derived or (for output axes) data dependent. Unapplicable blocksize parameters are ignored.

None

batch_size ¤

Optional[int]

Batch size to use for prediction.

None
Source code in src/bioimageio/core/_prediction_pipeline.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
def predict_sample_with_blocking(
    self,
    sample: Sample,
    skip_preprocessing: bool = False,
    skip_postprocessing: bool = False,
    ns: Optional[
        Union[
            v0_5.ParameterizedSize_N,
            Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
        ]
    ] = None,
    batch_size: Optional[int] = None,
) -> Sample:
    """Predict a sample by predicting sample blocks.

    Note: For fixed/known blocksizes use `predict_sample_with_fixed_blocking`.

    Args:
        sample: The sample to predict on.
        skip_preprocessing: If `True`, skip all preprocessing steps.
        skip_postprocessing: If `True`, skip all postprocessing steps.
        ns: Block size parameter(s) allows scaling the model's default input block size.
          Blocksize parameters are only applied to parameterized input axes, all other axis sizes are fixed/derived or (for output axes) data dependent.
          Unapplicable blocksize parameters are ignored.
        batch_size: Batch size to use for prediction.
    """

    output = None
    for output in self.predict_sample_with_blocking_yield_intermediates(
        sample,
        skip_preprocessing=skip_preprocessing,
        skip_postprocessing=skip_postprocessing,
        ns=ns,
        batch_size=batch_size,
    )[1]:
        pass

    assert output is not None, (
        "No blocks were predicted, cannot return final sample."
    )
    return output.sample

predict_sample_with_blocking_yield_intermediates ¤

predict_sample_with_blocking_yield_intermediates(sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, ns: Optional[Union[v0_5.ParameterizedSize_N, Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N]]] = None, batch_size: Optional[int] = None) -> Tuple[int, Iterable[IntermediatePrediction]]

Predict sample by predicting sample blocks and yield intermediate predictions if no samplewise postprocessing is included. Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs). For preceding prediction pipelines ns and batch_size are shared, but pre- and postprocessing are never skipped in preceding pipelines.

Returns:

Type Description
Tuple[int, Iterable[IntermediatePrediction]]

Tuple of number of prediction steps and an iterator of predicted intermediate samples with the last predicted block, All samples, but the last one, are intermediate samples with more and more blocks predicted. In case samplewise postprocessing needs to be applied, no intermediate results are yielded, but only the final sample after all blocks are predicted and postprocessed. In case of preceding prediction pipelines (model inputs depend on another model's outputs), intermediate results initially do not include the final output tensors at all.

Source code in src/bioimageio/core/_prediction_pipeline.py
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def predict_sample_with_blocking_yield_intermediates(
    self,
    sample: Sample,
    skip_preprocessing: bool = False,
    skip_postprocessing: bool = False,
    ns: Optional[
        Union[
            v0_5.ParameterizedSize_N,
            Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
        ]
    ] = None,
    batch_size: Optional[int] = None,
) -> Tuple[int, Iterable[IntermediatePrediction]]:
    """Predict `sample` by predicting sample blocks and yield intermediate predictions if no samplewise postprocessing is included.
    Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs).
    For preceding prediction pipelines `ns` and `batch_size` are shared, but pre- and postprocessing are never skipped in preceding pipelines.

    Returns:
        Tuple of number of prediction steps and an iterator of predicted intermediate samples with the last predicted block,
        All samples, but the last one, are intermediate samples with more and more blocks predicted.
        In case samplewise postprocessing needs to be applied, no intermediate results are yielded, but only the final sample after all blocks are predicted and postprocessed.
        In case of preceding prediction pipelines (model inputs depend on another model's outputs), intermediate results initially do not include the final output tensors at all.
    """

    total_prediction_steps = 0
    iterable_intermediates = ()
    for pp in self._get_preceding_prediction_pipelines_for_sample(sample):
        pp_steps, pp_intermediates = (
            pp.predict_sample_with_blocking_yield_intermediates(
                sample,
                ns=ns,
                batch_size=batch_size,
                skip_preprocessing=False,
                skip_postprocessing=False,
            )
        )
        total_prediction_steps += pp_steps
        iterable_intermediates = chain(iterable_intermediates, pp_intermediates)

    if isinstance(self._model_descr, v0_4.ModelDescr):
        raise NotImplementedError(
            "`predict_sample_with_blocking` not implemented for v0_4.ModelDescr"
            + f" {self._model_descr.name}."
            + " Consider using `predict_sample_with_fixed_blocking`"
        )

    ns = ns or self._default_blocksize_parameter
    if isinstance(ns, int):
        ns = {
            (ipt.id, a.id): ns
            for ipt in self._model_descr.inputs
            for a in ipt.axes
            if isinstance(a.size, v0_5.ParameterizedSize)
        }
    input_block_shape = self._model_descr.get_tensor_sizes(
        ns, batch_size or self._default_batch_size
    ).inputs

    steps, intermediates = (
        self._predict_sample_with_fixed_blocking_yield_intermediates_impl(
            sample,
            input_block_shape=input_block_shape,
            skip_preprocessing=skip_preprocessing,
            skip_postprocessing=skip_postprocessing,
        )
    )
    total_prediction_steps += steps
    iterable_intermediates = chain(iterable_intermediates, intermediates)
    return total_prediction_steps, iterable_intermediates

predict_sample_with_fixed_blocking ¤

predict_sample_with_fixed_blocking(sample: Sample, input_block_shape: PerMember[PerAxis[int]], skip_preprocessing: bool = False, skip_postprocessing: bool = False) -> Sample

Predict sample with given input_block_shape.

Note
  • input_block_shape is expected to be a valid input shape for the model.
  • Use predict_sample_with_blocking if you want to control block sizes via generic block size parameters rather than fixed block shapes.

Parameters:

Name Type Description Default

sample ¤

Sample

The sample to predict on.

required

input_block_shape ¤

PerMember[PerAxis[int]]

Mapping of input member id to mapping of axis id to block size for that axis.

required

skip_preprocessing ¤

bool

If True, skip all preprocessing steps.

False

skip_postprocessing ¤

bool

If True, skip all postprocessing steps.

False
Source code in src/bioimageio/core/_prediction_pipeline.py
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
def predict_sample_with_fixed_blocking(
    self,
    sample: Sample,
    input_block_shape: PerMember[PerAxis[int]],
    skip_preprocessing: bool = False,
    skip_postprocessing: bool = False,
) -> Sample:
    """Predict `sample` with given `input_block_shape`.

    Note:
        - `input_block_shape` is expected to be a valid input shape for the model.
        - Use `predict_sample_with_blocking` if you want to control block sizes via generic block size parameters rather than fixed block shapes.

    Args:
        sample: The sample to predict on.
        input_block_shape: Mapping of input member id to mapping of axis id to block size for that axis.
        skip_preprocessing: If `True`, skip all preprocessing steps.
        skip_postprocessing: If `True`, skip all postprocessing steps.
    """
    intermediate = None
    for (
        intermediate
    ) in self._predict_sample_with_fixed_blocking_yield_intermediates_impl(
        sample,
        input_block_shape=input_block_shape,
        skip_preprocessing=skip_preprocessing,
        skip_postprocessing=skip_postprocessing,
    )[1]:
        pass

    assert intermediate is not None, (
        "No blocks were predicted, cannot return final sample."
    )
    return intermediate.sample

predict_sample_with_fixed_blocking_yield_intermediates ¤

predict_sample_with_fixed_blocking_yield_intermediates(sample: Sample, input_block_shape: PerMember[PerAxis[int]], *, skip_preprocessing: bool = False, skip_postprocessing: bool = False, fill_value: float = float('nan')) -> Tuple[int, Iterable[IntermediatePrediction]]

Predict sample by predicting sample blocks of input_block_shape and yield intermediate predictions if no samplewise postprocessing is included. Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs). For preceding prediction pipelines input_block_shape and fill_value are shared, but pre- and postprocessing are never skipped in preceding pipelines.

Returns:

Type Description
Tuple[int, Iterable[IntermediatePrediction]]

Tuple of number of prediction steps and an iterator of predicted intermediate samples with the last predicted block, All samples, but the last one, are intermediate samples with more and more blocks predicted. In case samplewise postprocessing needs to be applied, no intermediate results are yielded, but only the final sample after all blocks are predicted and postprocessed. In case of preceding prediction pipelines (model inputs depend on another model's outputs), intermediate results initially do not include the final output tensors at all.

Source code in src/bioimageio/core/_prediction_pipeline.py
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
def predict_sample_with_fixed_blocking_yield_intermediates(
    self,
    sample: Sample,
    input_block_shape: PerMember[PerAxis[int]],
    *,
    skip_preprocessing: bool = False,
    skip_postprocessing: bool = False,
    fill_value: float = float("nan"),
) -> Tuple[int, Iterable[IntermediatePrediction]]:
    """Predict `sample` by predicting sample blocks of `input_block_shape` and yield intermediate predictions if no samplewise postprocessing is included.
    Also yields intermediate predictions if there are preceding prediction pipelines (model inputs depend on another model's outputs).
    For preceding prediction pipelines `input_block_shape` and `fill_value` are shared, but pre- and postprocessing are never skipped in preceding pipelines.

    Returns:
        Tuple of number of prediction steps and an iterator of predicted intermediate samples with the last predicted block,
        All samples, but the last one, are intermediate samples with more and more blocks predicted.
        In case samplewise postprocessing needs to be applied, no intermediate results are yielded, but only the final sample after all blocks are predicted and postprocessed.
        In case of preceding prediction pipelines (model inputs depend on another model's outputs), intermediate results initially do not include the final output tensors at all.
    """
    total_prediction_steps = 0
    iterable_intermediates = ()
    for pp in self._get_preceding_prediction_pipelines_for_sample(sample):
        pp_steps, pp_intermediates = (
            pp._predict_sample_with_fixed_blocking_yield_intermediates_impl(
                sample,
                input_block_shape=input_block_shape,
                skip_preprocessing=False,
                skip_postprocessing=False,
                fill_value=fill_value,
            )
        )
        total_prediction_steps += pp_steps
        iterable_intermediates = chain(iterable_intermediates, pp_intermediates)

    pp_steps, pp_intermediates = (
        self._predict_sample_with_fixed_blocking_yield_intermediates_impl(
            sample,
            input_block_shape=input_block_shape,
            skip_preprocessing=skip_preprocessing,
            skip_postprocessing=skip_postprocessing,
            fill_value=fill_value,
        )
    )
    total_prediction_steps += pp_steps
    iterable_intermediates = chain(iterable_intermediates, pp_intermediates)
    return total_prediction_steps, iterable_intermediates

predict_sample_without_blocking ¤

predict_sample_without_blocking(sample: Sample, skip_preprocessing: bool = False, skip_postprocessing: bool = False, skip_input_padding: bool = False, skip_output_cropping: bool = False) -> Sample

Predict a whole sample at once.

Note

The sample's tensor shapes have to match the model's input tensor description. If that is not the case, consider predict_sample_with_blocking

Parameters:

Name Type Description Default

sample ¤

Sample

input sample

required

skip_preprocessing ¤

bool

if True, skip all preprocessing steps (except for any preceding prediction pipeline).

False

skip_postprocessing ¤

bool

if True, skip all postprocessing steps (except for any preceding prediction pipeline).

False

skip_input_padding ¤

bool

if True, skip padding the input sample according to the model's (optional) output halos.

False

skip_output_cropping ¤

bool

if True, skip cropping any output halos from the model output.

False
Source code in src/bioimageio/core/_prediction_pipeline.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def predict_sample_without_blocking(
    self,
    sample: Sample,
    skip_preprocessing: bool = False,
    skip_postprocessing: bool = False,
    skip_input_padding: bool = False,
    skip_output_cropping: bool = False,
) -> Sample:
    """Predict a whole sample at once.

    Note:
        The sample's tensor shapes have to match the model's input tensor description.
        If that is not the case, consider `predict_sample_with_blocking`

    Args:
        sample: input sample
        skip_preprocessing: if `True`, skip all preprocessing steps (except for any preceding prediction pipeline).
        skip_postprocessing: if `True`, skip all postprocessing steps (except for any preceding prediction pipeline).
        skip_input_padding: if `True`, skip padding the input sample according to the model's (optional) output halos.
        skip_output_cropping: if `True`, skip cropping any output halos from the model output.
    """
    for pp in self._get_preceding_prediction_pipelines_for_sample(sample):
        sample = pp._predict_sample_without_blocking_impl(
            sample,
            skip_input_padding=skip_input_padding,
            skip_output_cropping=skip_output_cropping,
        )

    return self._predict_sample_without_blocking_impl(
        sample,
        skip_preprocessing=skip_preprocessing,
        skip_postprocessing=skip_postprocessing,
        skip_input_padding=skip_input_padding,
        skip_output_cropping=skip_output_cropping,
    )