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

add predict command #406

Merged
merged 81 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
4aba807
add predict command
FynnBe Jul 23, 2024
25aa7c0
remove unused import
FynnBe Jul 24, 2024
9146c3b
improve model_descr type check
FynnBe Jul 24, 2024
87dc749
remove commented old prediction commands
FynnBe Jul 24, 2024
b47e2b3
allow to package to folder
FynnBe Jul 24, 2024
bbfbbac
document packaging to folder
FynnBe Jul 24, 2024
dcd8334
add Get started section
FynnBe Jul 24, 2024
70a19e8
update type annotations for pyright 1.1.373
FynnBe Jul 25, 2024
abf5d20
pin spec
FynnBe Jul 25, 2024
b3402f7
use load_model_description
FynnBe Jul 25, 2024
21e24cb
improve predict command error for missing input
FynnBe Jul 25, 2024
17b4bbe
update test_bioimageio_spec_version
FynnBe Jul 25, 2024
eb9872d
include weight format in test name
FynnBe Aug 1, 2024
216bf00
make sure directory to save tensor in exists
FynnBe Aug 1, 2024
397c7a2
allow a mapping for save_sample input arg path
FynnBe Aug 1, 2024
7c78620
only calculate stats if any measures are missing
FynnBe Aug 1, 2024
3765812
add inspection helpers to get required measures
FynnBe Aug 1, 2024
4e256f3
allow axis ids to be given as strings
FynnBe Aug 1, 2024
1724589
use pydantic for stat measures and make (small) tensors json serializ…
FynnBe Aug 1, 2024
ea2cac7
rewrite CLI
FynnBe Aug 1, 2024
a0b00fb
we need a main func in main for the endpoint
FynnBe Aug 1, 2024
b97c330
update dependencies
FynnBe Aug 1, 2024
aa3e934
remove invalid alias
FynnBe Aug 1, 2024
9c98c7e
WIP update README.md
FynnBe Aug 1, 2024
3f8af93
fix typing issue
FynnBe Aug 5, 2024
d7f0a78
update tests
FynnBe Aug 5, 2024
b557940
add default path
FynnBe Aug 5, 2024
4b01578
update test_scale_range_axes
FynnBe Aug 5, 2024
2d22241
add default package path
FynnBe Aug 5, 2024
04e9f0c
read command line arguments from file
FynnBe Aug 5, 2024
8418fed
remove default path
FynnBe Aug 5, 2024
d8fb60f
reference file formats from imageio
FynnBe Aug 5, 2024
dcfd021
add cli file example
FynnBe Aug 5, 2024
6a9bb2d
complete test command
FynnBe Aug 5, 2024
bfd7c13
add output path explicitly
FynnBe Aug 5, 2024
c36fec9
bump patch version
FynnBe Aug 5, 2024
4497fbf
set output path explicitly
FynnBe Aug 5, 2024
f24b96a
black
FynnBe Aug 5, 2024
d7c4547
add conda env doc link
FynnBe Aug 6, 2024
3dfcc6c
remove clutter
FynnBe Aug 6, 2024
352422f
improve CLI
FynnBe Aug 9, 2024
4052343
log inputs
FynnBe Aug 12, 2024
b8b7f6a
pass without shorten input sequence
FynnBe Aug 12, 2024
4418a08
drop singleton batch axis when saving a tensor
FynnBe Aug 12, 2024
fe471ef
improve logging
FynnBe Aug 12, 2024
01e43fc
improve example
FynnBe Aug 12, 2024
d704e53
remove unused imports
FynnBe Aug 12, 2024
ba29ddc
improve doc strings
FynnBe Aug 12, 2024
c629a91
use argparse.RawTextHelpFormatter
FynnBe Aug 12, 2024
aa5c316
add weight_format option to predict command
FynnBe Aug 12, 2024
eec7bde
make sure example dir exists
FynnBe Aug 12, 2024
915e56c
fail for missing input samples
FynnBe Aug 12, 2024
103ca42
ignore empty initial dataset measures
FynnBe Aug 13, 2024
ee9be64
perform IO checks based on env var
FynnBe Aug 13, 2024
a0ae60c
add section on logging level
FynnBe Aug 13, 2024
5b1bd86
fix tqdm call
FynnBe Aug 13, 2024
d9fd4f6
insert singleton axis at right position
FynnBe Aug 13, 2024
83d3290
improve stat serialization
FynnBe Aug 13, 2024
d5a0814
fix tensor_custom_before_validator
FynnBe Aug 13, 2024
ca0169e
try all array permutations to match singleton requirements
FynnBe Aug 13, 2024
2605728
fix _get_array_view
FynnBe Aug 13, 2024
e2d1616
actually remove YAML_FILE
FynnBe Aug 14, 2024
090d979
improve saving with imageio
FynnBe Aug 14, 2024
7be6a7e
bump imageio to make sure v3 is available
FynnBe Aug 14, 2024
cbe02ba
simplify io with imageio.v3
FynnBe Aug 14, 2024
6c68179
allow dims to be AxisLike
FynnBe Aug 14, 2024
b9897da
improve help text formatting
FynnBe Aug 14, 2024
29de8e6
update 'Get started' section
FynnBe Aug 14, 2024
f6c3bd7
do not rule out singleton axis as easily
FynnBe Aug 16, 2024
e5d83ba
allow space and time axes to be singletons
FynnBe Aug 21, 2024
6147d4d
avoid 'ABCMeta' object is not subscriptable
FynnBe Aug 21, 2024
1d3000b
bump spec
FynnBe Aug 21, 2024
c766f12
bump spec in dev envs
FynnBe Aug 21, 2024
14f599e
add predict_sample_with_fixed_blocking
FynnBe Aug 22, 2024
bc98d65
do not convert axis id
FynnBe Aug 23, 2024
5a9c1c7
do not convert axes ids for proc ops
FynnBe Aug 23, 2024
83d6a92
expose more functions
FynnBe Aug 23, 2024
9726e60
update digest_spec
FynnBe Aug 23, 2024
2ec9e9e
AxisId is also AxisLike!
FynnBe Aug 23, 2024
88b5fca
Merge remote-tracking branch 'origin/main' into predict_cmd
FynnBe Sep 13, 2024
7306f98
remove unused dependency
FynnBe Sep 13, 2024
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
149 changes: 131 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,113 @@

Python specific core utilities for bioimage.io resources (in particular models).

## Get started

To get started we recommend installing bioimageio.core with conda together with a deep
learning framework, e.g. pytorch, and run a few `bioimageio` commands to see what
bioimage.core offers.

1. install with conda (for more details on conda environments, [checkout the ])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a minor heads up to fill this link :)


```console
install -c conda-forge bioimageio.core pytorch
```

1. test a model

```console
bioimageio test powerful-chipmunk

testing powerful-chipmunk...
2024-07-24 17:10:37.470 | INFO | bioimageio.spec._internal.io_utils:open_bioimageio_yaml:112 - loading powerful-chipmunk from https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/powerful-chipmunk/1/files/rdf.yaml
Updating data from 'https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/powerful-chipmunk/1/files/rdf.yaml' to file 'C:\Users\fbeut\AppData\Local\bioimageio\bioimageio\Cache\d968304289dc978b9221e813dc757a3a-rdf.yaml'.
100%|#####################################| 2.92k/2.92k [00:00<00:00, 1.53MB/s]
computing SHA256 of 1e659a86d8dd8a7c6cfb3315f4447f5d-weights.pt (result: 3bd9c518c8473f1e35abb7624f82f3aa92f1015e66fb1f6a9d08444e1f2f5698): 100%|██████████████████████████████████████████████████████████████████████████████████████████████████| 884/884 [00:00<00:00, 1006.20it/s]
computing SHA256 of 97a83ece802cfc5ba362aa76b5f77c3a-weights-torchscript.pt (result: 4e568fd81c0ffa06ce13061327c3f673e1bac808891135badd3b0fcdacee086b): 100%|██████████████████████████████████████████████████████████████████████████████████████| 885/885 [00:00<00:00, 1229.39it/s]
2024-07-24 17:10:44.596 | INFO | bioimageio.core._resource_tests:_test_model_inference:130 - starting 'Reproduce test outputs from test inputs'
2024-07-24 17:11:00.136 | INFO | bioimageio.core._resource_tests:_test_model_inference:130 - starting 'Reproduce test outputs from test inputs'


✔️ bioimageio validation passed
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
source https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/powerful-chipmunk/1/files/rdf.yaml
format version model 0.4.10
bioimageio.spec 0.5.3post4
bioimageio.core 0.6.8



❓ location detail
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✔️ initialized ModelDescr to describe model 0.4.10

✔️ bioimageio.spec format validation model 0.4.10
🔍 context.perform_io_checks True
🔍 context.root https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/powerful-chipmunk/1/files
🔍 context.known_files.weights.pt 3bd9c518c8473f1e35abb7624f82f3aa92f1015e66fb1f6a9d08444e1f2f5698
🔍 context.known_files.weights-torchscript.pt 4e568fd81c0ffa06ce13061327c3f673e1bac808891135badd3b0fcdacee086b
🔍 context.warning_level error

✔️ Reproduce test outputs from test inputs

✔️ Reproduce test outputs from test inputs
```

1. run prediction on your data

- display the `bioimageio-predict` command interface

```console
> bioimageio predict -h
usage: bioimageio predict [-h] [--inputs {str,Sequence[str]}] [--outputs {str,Sequence[str]}] [--overwrite bool]
[--blockwise bool] [--stats Path]
SOURCE

bioimageio-predict - Run inference on your data with a bioimage.io model.

positional arguments:
SOURCE Url/path to a bioimageio.yaml/rdf.yaml file or a bioimage.io resource identifier, e.g.
'affable-shark'

optional arguments:
-h, --help show this help message and exit
--inputs {str,Sequence[str]}
model inputs Either a single path/glob pattern including `{tensor_id}` to be used for all
model inputs, or a list of paths/glob patterns for each model input respectively. For models
with a single input a single path/glob pattern with `{tensor_id}` is also accepted.
(default: model_inputs/*/{tensor_id}.*)
--outputs {str,Sequence[str]}
output paths analog to `inputs` (default: outputs_{model_id}/{sample_id}/{tensor_id}.npy)
--overwrite bool allow overwriting existing output files (default: False)
--blockwise bool process inputs blockwise (default: False)
--stats Path path to dataset statistics (will be written if it does not exist, but the model requires
statistical dataset measures) (default: model_inputs\dataset_statistics.json)
```

- locate your input data
- predict away!

```console
bioimageio predict affable-shark
```

- for convenience the command line arguments may be given in a `bioimageio-cli.json` or `bioimageio-cli.yaml` file.
The YAML file takes priority over the JSON file.
Addtional command line arguments take the highest priority.

```yaml
# bioimageio-cli.yaml
inputs: inputs/*_{tensor_id}.h5
outputs: outputs_{model_id}/{sample_id}_{tensor_id}.h5
overwrite: true
blockwise: true
stats: inputs/dataset_statistics.json
```

```console
bioimageio predict affable-shark
```

## Installation

### Via Mamba/Conda
Expand All @@ -23,7 +130,7 @@ If you do not install any additional deep learning libraries, you will only be a
functionality, but not any functionality for model prediction.
To install additional deep learning libraries use:

* Pytorch/Torchscript:
- Pytorch/Torchscript:

CPU installation (if you don't have an nvidia graphics card):

Expand All @@ -39,15 +146,15 @@ To install additional deep learning libraries use:

Note that the pytorch installation instructions may change in the future. For the latest instructions please refer to [pytorch.org](https://pytorch.org/).

* Tensorflow
- Tensorflow

Currently only CPU version supported

```console
mamba install -c conda-forge bioimageio.core tensorflow
```

* ONNXRuntime
- ONNXRuntime

Currently only cpu version supported

Expand Down Expand Up @@ -121,7 +228,7 @@ In addition bioimageio.core provides functionality to convert model weight forma

To get an overview of this functionality, check out these example notebooks:

* [model creation/loading with bioimageio.spec](https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb)
- [model creation/loading with bioimageio.spec](https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb)

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

Expand All @@ -131,47 +238,53 @@ The model specification and its validation tools can be found at <https://github

## Changelog

### 0.6.9

- improve bioimageio command line interface (details in #157)
- add `predict` command
- package command input `path` is now required

### 0.6.8

* testing model inference will now check all weight formats
- testing model inference will now check all weight formats
(previously only the first one for which model adapter creation succeeded had been checked)
* fix predict with blocking (Thanks @thodkatz)
- fix predict with blocking (Thanks @thodkatz)

### 0.6.7

* `predict()` argument `inputs` may be sample
- `predict()` argument `inputs` may be sample

### 0.6.6

* add aliases to match previous API more closely
- add aliases to match previous API more closely

### 0.6.5

* improve adapter error messages
- improve adapter error messages

### 0.6.4

* add `bioimageio validate-format` command
* improve error messages and display of command results
- add `bioimageio validate-format` command
- improve error messages and display of command results

### 0.6.3

* Fix [#386](https://github.com/bioimage-io/core-bioimage-io-python/issues/386)
* (in model inference testing) stop assuming model inputs are tileable
- Fix [#386](https://github.com/bioimage-io/core-bioimage-io-python/issues/386)
- (in model inference testing) stop assuming model inputs are tileable

### 0.6.2

* Fix [#384](https://github.com/bioimage-io/core-bioimage-io-python/issues/384)
- Fix [#384](https://github.com/bioimage-io/core-bioimage-io-python/issues/384)

### 0.6.1

* Fix [#378](https://github.com/bioimage-io/core-bioimage-io-python/pull/378) (with [#379](https://github.com/bioimage-io/core-bioimage-io-python/pull/379))*
- Fix [#378](https://github.com/bioimage-io/core-bioimage-io-python/pull/378) (with [#379](https://github.com/bioimage-io/core-bioimage-io-python/pull/379))*

### 0.6.0

* add compatibility with new bioimageio.spec 0.5 (0.5.2post1)
* improve interfaces
- add compatibility with new bioimageio.spec 0.5 (0.5.2post1)
- improve interfaces

### 0.5.10

* [Fix critical bug in predict with tiling](https://github.com/bioimage-io/core-bioimage-io-python/pull/359)
- [Fix critical bug in predict with tiling](https://github.com/bioimage-io/core-bioimage-io-python/pull/359)
2 changes: 1 addition & 1 deletion bioimageio/core/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.6.8"
"version": "0.6.9"
}
8 changes: 7 additions & 1 deletion bioimageio/core/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from bioimageio.core.commands import main
from bioimageio.core.cli import Bioimageio


def main():
cli = Bioimageio() # pyright: ignore[reportCallIssue]
cli.run()


if __name__ == "__main__":
main()
12 changes: 6 additions & 6 deletions bioimageio/core/_prediction_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def __init__(
postprocessing: List[Processing],
model_adapter: ModelAdapter,
default_ns: Union[
v0_5.ParameterizedSize.N,
Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize.N],
v0_5.ParameterizedSize_N,
Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
] = 10,
default_batch_size: int = 1,
) -> None:
Expand Down Expand Up @@ -186,8 +186,8 @@ def predict_sample_with_blocking(
skip_postprocessing: bool = False,
ns: Optional[
Union[
v0_5.ParameterizedSize.N,
Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize.N],
v0_5.ParameterizedSize_N,
Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
]
] = None,
batch_size: Optional[int] = None,
Expand Down Expand Up @@ -310,8 +310,8 @@ def create_prediction_pipeline(
),
model_adapter: Optional[ModelAdapter] = None,
ns: Union[
v0_5.ParameterizedSize.N,
Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize.N],
v0_5.ParameterizedSize_N,
Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
] = 10,
**deprecated_kwargs: Any,
) -> PredictionPipeline:
Expand Down
16 changes: 8 additions & 8 deletions bioimageio/core/_resource_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import traceback
import warnings
from itertools import product
from typing import Dict, Hashable, List, Literal, Optional, Set, Tuple, Union
from typing import Dict, Hashable, List, Literal, Optional, Sequence, Set, Tuple, Union

import numpy as np
from loguru import logger
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_description(
*,
format_version: Union[Literal["discover", "latest"], str] = "discover",
weight_format: Optional[WeightsFormat] = None,
devices: Optional[List[str]] = None,
devices: Optional[Sequence[str]] = None,
decimal: int = 4,
expected_type: Optional[str] = None,
) -> ValidationSummary:
Expand All @@ -73,7 +73,7 @@ def load_description_and_test(
*,
format_version: Union[Literal["discover", "latest"], str] = "discover",
weight_format: Optional[WeightsFormat] = None,
devices: Optional[List[str]] = None,
devices: Optional[Sequence[str]] = None,
decimal: int = 4,
expected_type: Optional[str] = None,
) -> Union[ResourceDescr, InvalidDescr]:
Expand Down Expand Up @@ -123,10 +123,10 @@ def load_description_and_test(
def _test_model_inference(
model: Union[v0_4.ModelDescr, v0_5.ModelDescr],
weight_format: WeightsFormat,
devices: Optional[List[str]],
devices: Optional[Sequence[str]],
decimal: int,
) -> None:
test_name = "Reproduce test outputs from test inputs"
test_name = f"Reproduce test outputs from test inputs ({weight_format})"
logger.info("starting '{}'", test_name)
error: Optional[str] = None
tb: List[str] = []
Expand Down Expand Up @@ -182,15 +182,15 @@ def _test_model_inference(
def _test_model_inference_parametrized(
model: v0_5.ModelDescr,
weight_format: WeightsFormat,
devices: Optional[List[str]],
devices: Optional[Sequence[str]],
) -> None:
if not any(
isinstance(a.size, v0_5.ParameterizedSize)
for ipt in model.inputs
for a in ipt.axes
):
# no parameterized sizes => set n=0
ns: Set[v0_5.ParameterizedSize.N] = {0}
ns: Set[v0_5.ParameterizedSize_N] = {0}
else:
ns = {0, 1, 2}

Expand All @@ -209,7 +209,7 @@ def _test_model_inference_parametrized(
# no batch axis
batch_sizes = {1}

test_cases: Set[Tuple[v0_5.ParameterizedSize.N, BatchSize]] = {
test_cases: Set[Tuple[v0_5.ParameterizedSize_N, BatchSize]] = {
(n, b) for n, b in product(sorted(ns), sorted(batch_sizes))
}
logger.info(
Expand Down
Loading
Loading