Skip to content

Commit fc3a03f

Browse files
committed
Merge remote-tracking branch 'Tomaz-Vieira/fix_v4_pre_and_post_processing' into fix_torch_load
2 parents 03d4940 + 8919900 commit fc3a03f

File tree

3 files changed

+222
-114
lines changed

3 files changed

+222
-114
lines changed

README.md

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -262,49 +262,22 @@ bioimage.core has to offer:
262262

263263
## Installation
264264

265-
### Via Mamba/Conda
265+
### Via Conda
266266

267267
The `bioimageio.core` package can be installed from conda-forge via
268268

269269
```console
270-
mamba install -c conda-forge bioimageio.core
270+
conda install -c conda-forge bioimageio.core
271271
```
272272

273273
If you do not install any additional deep learning libraries, you will only be able to use general convenience
274-
functionality, but not any functionality for model prediction.
275-
To install additional deep learning libraries use:
274+
functionality, but not any functionality depending on model prediction.
275+
To install additional deep learning libraries add `pytorch`, `onnxruntime`, `keras` or `tensorflow`.
276276

277-
- Pytorch/Torchscript:
278-
279-
CPU installation (if you don't have an nvidia graphics card):
280-
281-
```console
282-
mamba install -c pytorch -c conda-forge bioimageio.core pytorch torchvision cpuonly
283-
```
284-
285-
GPU installation (for cuda 11.6, please choose the appropriate cuda version for your system):
286-
287-
```console
288-
mamba install -c pytorch -c nvidia -c conda-forge bioimageio.core pytorch torchvision pytorch-cuda=11.8
289-
```
290-
291-
Note that the pytorch installation instructions may change in the future. For the latest instructions please refer to [pytorch.org](https://pytorch.org/).
292-
293-
- Tensorflow
294-
295-
Currently only CPU version supported
296-
297-
```console
298-
mamba install -c conda-forge bioimageio.core tensorflow
299-
```
300-
301-
- ONNXRuntime
302-
303-
Currently only cpu version supported
304-
305-
```console
306-
mamba install -c conda-forge bioimageio.core onnxruntime
307-
```
277+
Deeplearning frameworks to consider installing alongside `bioimageio.core`:
278+
- [Pytorch/Torchscript](https://pytorch.org/get-started/locally/)
279+
- [TensorFlow](https://www.tensorflow.org/install)
280+
- [ONNXRuntime](https://onnxruntime.ai/docs/install/#python-installs)
308281

309282
### Via pip
310283

@@ -315,17 +288,33 @@ The package is also available via pip
315288
pip install "bioimageio.core[onnx,pytorch]"
316289
```
317290

318-
### Set up Development Environment
291+
## 🐍 Use in Python
319292

320-
To set up a development conda environment run the following commands:
293+
`bioimageio.core` is a python package that implements prediction with bioimageio models
294+
including standardized pre- and postprocessing operations.
295+
These models are described by---and can be loaded with---the bioimageio.spec package.
321296

322-
```console
323-
mamba env create -f dev/env.yaml
324-
mamba activate core
325-
pip install -e . --no-deps
326-
```
297+
In addition bioimageio.core provides functionality to convert model weight formats.
327298

328-
There are different environment files available that only install tensorflow or pytorch as dependencies.
299+
### Documentation
300+
[Here you find the bioimageio.core documentation.](https://bioimage-io.github.io/core-bioimage-io-python/bioimageio/core.html)
301+
302+
#### Presentations
303+
- [Create a model from scratch](https://bioimage-io.github.io/core-bioimage-io-python/presentations/create_ambitious_sloth.slides.html) ([source](https://github.com/bioimage-io/core-bioimage-io-python/tree/main/presentations))
304+
305+
#### Examples
306+
<dl>
307+
<dt>Notebooks that save and load resource descriptions and validate their format (using <a href="https://bioimage-io.github.io/core-bioimage-io-python/bioimageio/spec.html">bioimageio.spec</a>, a dependency of bioimageio.core)</dt>
308+
<dd><a href="https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb">load_model_and_create_your_own.ipynb</a> <a target="_blank" href="https://colab.research.google.com/github/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb">
309+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
310+
</a></dd>
311+
<dd><a href="https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/dataset_creation.ipynb">dataset_creation.ipynb</a> <a target="_blank" href="https://colab.research.google.com/github/bioimage-io/spec-bioimage-io/blob/main/example/dataset_creation.ipynb">
312+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
313+
</a></dd>
314+
<dt>Use the described resources in Python with <a href="https://bioimage-io.github.io/core-bioimage-io-python/bioimageio/core.html">bioimageio.core</a></dt>
315+
<dd><a href="https://github.com/bioimage-io/core-bioimage-io-python/blob/main/example/model_usage.ipynb">model_usage.ipynb</a><a target="_blank" href="https://colab.research.google.com/github/bioimage-io/core-bioimage-io-python/blob/main/example/model_usage.ipynb">
316+
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
317+
</dl>
329318

330319
## 💻 Use the Command Line Interface
331320

@@ -336,6 +325,9 @@ You can list all the available commands via:
336325
bioimageio
337326
```
338327

328+
For examples see [Get started](#get-started).
329+
330+
339331
### CLI inputs from file
340332

341333
For convenience the command line options (not arguments) may be given in a `bioimageio-cli.json`
@@ -350,28 +342,25 @@ blockwise: true
350342
stats: inputs/dataset_statistics.json
351343
```
352344
353-
## 🐍 Use in Python
354345
355-
`bioimageio.core` is a python package that implements prediction with bioimageio models
356-
including standardized pre- and postprocessing operations.
357-
These models are described by---and can be loaded with---the bioimageio.spec package.
346+
## Set up Development Environment
358347
359-
In addition bioimageio.core provides functionality to convert model weight formats.
348+
To set up a development conda environment run the following commands:
360349
361-
To get an overview of this functionality, check out these example notebooks:
350+
```console
351+
conda env create -f dev/env.yaml
352+
conda activate core
353+
pip install -e . --no-deps
354+
```
362355

363-
- [model creation/loading with bioimageio.spec](https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb)
356+
There are different environment files available that only install tensorflow or pytorch as dependencies, see [dev folder](https://github.com/bioimage-io/core-bioimage-io-python/tree/main/dev).
364357

365-
and the [developer documentation](https://bioimage-io.github.io/core-bioimage-io-python/bioimageio/core.html).
366358

367359
## Logging level
368360

369361
`bioimageio.spec` and `bioimageio.core` use [loguru](https://github.com/Delgan/loguru) for logging, hence the logging level
370362
may be controlled with the `LOGURU_LEVEL` environment variable.
371363

372-
## Model Specification
373-
374-
The model specification and its validation tools can be found at <https://github.com/bioimage-io/spec-bioimage-io>.
375364

376365
## Changelog
377366

bioimageio/core/proc_ops.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from typing_extensions import Self, assert_never
1818

1919
from bioimageio.spec.model import v0_4, v0_5
20+
from bioimageio.spec.model.v0_5 import TensorId
21+
from bioimageio.spec.model.v0_5 import (
22+
_convert_proc, # pyright: ignore [reportPrivateUsage]
23+
)
2024

2125
from ._op_base import BlockedOperator, Operator
2226
from .axis import AxisId, PerAxis
@@ -699,3 +703,113 @@ def get_proc_class(proc_spec: ProcDescr):
699703
return ZeroMeanUnitVariance
700704
else:
701705
assert_never(proc_spec)
706+
707+
708+
def preproc_v4_to_processing(
709+
inp: v0_4.InputTensorDescr,
710+
proc_spec: v0_4.PreprocessingDescr,
711+
) -> Processing:
712+
member_id = TensorId(str(inp.name))
713+
if isinstance(proc_spec, v0_4.BinarizeDescr):
714+
return Binarize.from_proc_descr(proc_spec, member_id)
715+
elif isinstance(proc_spec, v0_4.ClipDescr):
716+
return Clip.from_proc_descr(proc_spec, member_id)
717+
elif isinstance(proc_spec, v0_4.ScaleLinearDescr):
718+
return ScaleLinear.from_proc_descr(proc_spec, member_id)
719+
elif isinstance(proc_spec, v0_4.ScaleRangeDescr):
720+
return ScaleRange.from_proc_descr(proc_spec, member_id)
721+
elif isinstance(proc_spec, v0_4.SigmoidDescr):
722+
return Sigmoid.from_proc_descr(proc_spec, member_id)
723+
elif isinstance(proc_spec, v0_4.ZeroMeanUnitVarianceDescr):
724+
if proc_spec.kwargs.mode == "fixed":
725+
axes = inp.axes
726+
v5_proc_spec = _convert_proc(proc_spec, axes)
727+
assert isinstance(
728+
v5_proc_spec, v0_5.FixedZeroMeanUnitVarianceDescr
729+
) # FIXME
730+
return FixedZeroMeanUnitVariance.from_proc_descr(v5_proc_spec, member_id)
731+
else:
732+
return ZeroMeanUnitVariance.from_proc_descr(proc_spec, member_id)
733+
else:
734+
assert_never(proc_spec)
735+
736+
737+
def postproc_v4_to_processing(
738+
inp: v0_4.OutputTensorDescr,
739+
proc_spec: v0_4.PostprocessingDescr,
740+
) -> Processing:
741+
member_id = TensorId(str(inp.name))
742+
if isinstance(proc_spec, v0_4.BinarizeDescr):
743+
return Binarize.from_proc_descr(proc_spec, member_id)
744+
if isinstance(proc_spec, v0_4.ScaleMeanVarianceDescr):
745+
return ScaleMeanVariance.from_proc_descr(proc_spec, member_id)
746+
elif isinstance(proc_spec, v0_4.ClipDescr):
747+
return Clip.from_proc_descr(proc_spec, member_id)
748+
elif isinstance(proc_spec, v0_4.ScaleLinearDescr):
749+
return ScaleLinear.from_proc_descr(proc_spec, member_id)
750+
elif isinstance(proc_spec, v0_4.ScaleRangeDescr):
751+
return ScaleRange.from_proc_descr(proc_spec, member_id)
752+
elif isinstance(proc_spec, v0_4.SigmoidDescr):
753+
return Sigmoid.from_proc_descr(proc_spec, member_id)
754+
elif isinstance(proc_spec, v0_4.ZeroMeanUnitVarianceDescr):
755+
if proc_spec.kwargs.mode == "fixed":
756+
axes = inp.axes
757+
v5_proc_spec = _convert_proc(proc_spec, axes)
758+
assert isinstance(
759+
v5_proc_spec, v0_5.FixedZeroMeanUnitVarianceDescr
760+
) # FIXME
761+
return FixedZeroMeanUnitVariance.from_proc_descr(v5_proc_spec, member_id)
762+
else:
763+
return ZeroMeanUnitVariance.from_proc_descr(proc_spec, member_id)
764+
else:
765+
assert_never(proc_spec)
766+
767+
768+
def preproc_v5_to_processing(
769+
inp: v0_5.InputTensorDescr,
770+
proc_spec: v0_5.PreprocessingDescr,
771+
) -> Processing:
772+
if isinstance(proc_spec, v0_5.BinarizeDescr):
773+
return Binarize.from_proc_descr(proc_spec, inp.id)
774+
elif isinstance(proc_spec, v0_5.ClipDescr):
775+
return Clip.from_proc_descr(proc_spec, inp.id)
776+
elif isinstance(proc_spec, v0_5.ScaleLinearDescr):
777+
return ScaleLinear.from_proc_descr(proc_spec, inp.id)
778+
elif isinstance(proc_spec, v0_5.ScaleRangeDescr):
779+
return ScaleRange.from_proc_descr(proc_spec, inp.id)
780+
elif isinstance(proc_spec, v0_5.SigmoidDescr):
781+
return Sigmoid.from_proc_descr(proc_spec, inp.id)
782+
elif isinstance(proc_spec, v0_5.EnsureDtypeDescr):
783+
return EnsureDtype.from_proc_descr(proc_spec, inp.id)
784+
elif isinstance(proc_spec, v0_5.ZeroMeanUnitVarianceDescr):
785+
return ZeroMeanUnitVariance.from_proc_descr(proc_spec, inp.id)
786+
elif isinstance(proc_spec, v0_5.FixedZeroMeanUnitVarianceDescr):
787+
return FixedZeroMeanUnitVariance.from_proc_descr(proc_spec, inp.id)
788+
else:
789+
assert_never(proc_spec)
790+
791+
792+
def postproc_v5_to_processing(
793+
inp: v0_5.OutputTensorDescr,
794+
proc_spec: v0_5.PostprocessingDescr,
795+
) -> Processing:
796+
if isinstance(proc_spec, v0_5.BinarizeDescr):
797+
return Binarize.from_proc_descr(proc_spec, inp.id)
798+
if isinstance(proc_spec, v0_5.ScaleMeanVarianceDescr):
799+
return ScaleMeanVariance.from_proc_descr(proc_spec, inp.id)
800+
elif isinstance(proc_spec, v0_5.ClipDescr):
801+
return Clip.from_proc_descr(proc_spec, inp.id)
802+
elif isinstance(proc_spec, v0_5.ScaleLinearDescr):
803+
return ScaleLinear.from_proc_descr(proc_spec, inp.id)
804+
elif isinstance(proc_spec, v0_5.ScaleRangeDescr):
805+
return ScaleRange.from_proc_descr(proc_spec, inp.id)
806+
elif isinstance(proc_spec, v0_5.SigmoidDescr):
807+
return Sigmoid.from_proc_descr(proc_spec, inp.id)
808+
elif isinstance(proc_spec, v0_5.EnsureDtypeDescr):
809+
return EnsureDtype.from_proc_descr(proc_spec, inp.id)
810+
elif isinstance(proc_spec, v0_5.ZeroMeanUnitVarianceDescr):
811+
return ZeroMeanUnitVariance.from_proc_descr(proc_spec, inp.id)
812+
elif isinstance(proc_spec, v0_5.FixedZeroMeanUnitVarianceDescr):
813+
return FixedZeroMeanUnitVariance.from_proc_descr(proc_spec, inp.id)
814+
else:
815+
assert_never(proc_spec)

0 commit comments

Comments
 (0)