Skip to content

Commit 3f163a0

Browse files
committed
add explanation
1 parent a089a3f commit 3f163a0

File tree

1 file changed

+69
-112
lines changed

1 file changed

+69
-112
lines changed

example/model_usage.ipynb

Lines changed: 69 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,39 @@
1717
"cell_type": "markdown",
1818
"metadata": {},
1919
"source": [
20-
"## 0. Activate human readable output error messages and load dependencies"
20+
"## Activate human readable output error messages and load dependencies"
2121
]
2222
},
2323
{
2424
"cell_type": "markdown",
2525
"metadata": {},
2626
"source": [
27-
"### 0.1. Install necessary dependencies"
27+
"### Running local"
2828
]
2929
},
3030
{
31-
"cell_type": "code",
32-
"execution_count": null,
31+
"cell_type": "markdown",
3332
"metadata": {},
34-
"outputs": [],
3533
"source": [
36-
"try:\n",
37-
" import matplotlib\n",
38-
" import torch\n",
34+
"If you are running the notebook locally you need to pip install the following dependencies into your local environment. Make sure to restart your notebook kernel after installing dependencies.\n",
3935
"\n",
40-
" import bioimageio.core\n",
41-
"except ImportError:\n",
42-
" %pip install bioimageio.core==0.6.7 torch==2.3.1 matplotlib==3.9.0"
36+
"```console\n",
37+
"pip install bioimageio.core==0.8.0 torch==2.5.1 matplotlib==3.10.1\n",
38+
"```"
4339
]
4440
},
4541
{
4642
"cell_type": "markdown",
4743
"metadata": {},
4844
"source": [
49-
"### 0.2.Enable pretty_validation_errors\n",
50-
"\n",
51-
"This function displays validation errors in a human readable format."
45+
"### Running in colab"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"If you are running this notebook through colab, there is no need to install dependencies manually, just run the following cell."
5253
]
5354
},
5455
{
@@ -57,18 +58,18 @@
5758
"metadata": {},
5859
"outputs": [],
5960
"source": [
60-
"from bioimageio.spec.pretty_validation_errors import (\n",
61-
" enable_pretty_validation_errors_in_ipynb,\n",
62-
")\n",
61+
"import os\n",
6362
"\n",
64-
"enable_pretty_validation_errors_in_ipynb()"
63+
"# install dependencies if running in colab\n",
64+
"if os.getenv(\"COLAB_RELEASE_TAG\"):\n",
65+
" %pip install bioimageio.core==0.8.0 torch==2.5.1 matplotlib==3.10.1"
6566
]
6667
},
6768
{
6869
"cell_type": "markdown",
6970
"metadata": {},
7071
"source": [
71-
"### 0.3. Load general dependencies"
72+
"### Install necessary dependencies"
7273
]
7374
},
7475
{
@@ -77,7 +78,7 @@
7778
"metadata": {},
7879
"outputs": [],
7980
"source": [
80-
"# Load general dependencies\n",
81+
"import torch\n",
8182
"from pprint import pprint\n",
8283
"\n",
8384
"import matplotlib.pyplot as plt\n",
@@ -86,6 +87,14 @@
8687
"\n",
8788
"from bioimageio.spec.utils import download\n",
8889
"\n",
90+
"import bioimageio.core\n",
91+
"\n",
92+
"from bioimageio.spec.pretty_validation_errors import (\n",
93+
" enable_pretty_validation_errors_in_ipynb,\n",
94+
")\n",
95+
"\n",
96+
"# Improve readiblity of validation errors\n",
97+
"enable_pretty_validation_errors_in_ipynb()\n",
8998
"\n",
9099
"# Function to display input and prediction output images\n",
91100
"def show_images(sample_tensor, prediction_tensor):\n",
@@ -119,14 +128,14 @@
119128
"cell_type": "markdown",
120129
"metadata": {},
121130
"source": [
122-
"## 1. Load a model"
131+
"## Load a model"
123132
]
124133
},
125134
{
126135
"cell_type": "markdown",
127136
"metadata": {},
128137
"source": [
129-
"### 1.1 Inspect available models in the Bioimage Model Zoo"
138+
"### Inspect available models in the Bioimage Model Zoo"
130139
]
131140
},
132141
{
@@ -140,7 +149,7 @@
140149
"cell_type": "markdown",
141150
"metadata": {},
142151
"source": [
143-
"### 1.2 Load model from the BioImage Model Zoo"
152+
"### Load model from the BioImage Model Zoo"
144153
]
145154
},
146155
{
@@ -151,7 +160,9 @@
151160
"\n",
152161
"Both of these options may be version specific (\"affable-shark/1\" or a version specific [__Zenodo__](https://zenodo.org/) backup [__DOI__](https://doi.org/)).\n",
153162
"\n",
154-
"Alternatively, any rdf.yaml source, single file or in a .zip, may be loaded by providing its __local path__ or __URL__."
163+
"Alternatively, any rdf.yaml source, single file or in a .zip, may be loaded by providing its __local path__ or __URL__.\n",
164+
"\n",
165+
"**Note** -- For more detailed information about loading a model, inspecting a model's metadata and creating your own model visit [bioimageio.spec package example notebook](https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb)."
155166
]
156167
},
157168
{
@@ -160,16 +171,16 @@
160171
"metadata": {},
161172
"outputs": [],
162173
"source": [
163-
"BMZ_MODEL_ID = \"\" # \"affable-shark\"\n",
164-
"BMZ_MODEL_DOI = \"\" # \"10.5281/zenodo.6287342\"\n",
165-
"BMZ_MODEL_URL = \"https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/affable-shark/draft/files/rdf.yaml\""
174+
"model_source = \"affable-shark\"\n",
175+
"#model_source = \"10.5281/zenodo.6287342\"\n",
176+
"#model_source = \"https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/affable-shark/draft/files/rdf.yaml\""
166177
]
167178
},
168179
{
169180
"cell_type": "markdown",
170181
"metadata": {},
171182
"source": [
172-
"`load_description` is a function of the `bioimageio.spec` package, but as it is a sub-package of `bioimageio.core` it can also be called from it by `bioimageio.core.load_description`.\n",
183+
"`load_model_description` is a function of the `bioimageio.spec` package, but as it is a sub-package of `bioimageio.core` it can also be called from it by `bioimageio.core.load_model_description`.\n",
173184
"\n",
174185
"To learn more about the functionalities of the `bioimageio.spec` package, see the [bioimageio.spec package example notebook](https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb), also available as a [Google Colab](https://colab.research.google.com/github/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb) notebook."
175186
]
@@ -180,95 +191,47 @@
180191
"metadata": {},
181192
"outputs": [],
182193
"source": [
183-
"from bioimageio.core import load_description\n",
184-
"\n",
185-
"# Load the model description\n",
186-
"# ------------------------------------------------------------------------------\n",
187-
"if BMZ_MODEL_ID != \"\":\n",
188-
" model = load_description(BMZ_MODEL_ID)\n",
189-
" print(\n",
190-
" f\"\\nThe model '{model.name}' with ID '{BMZ_MODEL_ID}' has been correctly loaded.\"\n",
191-
" )\n",
192-
"elif BMZ_MODEL_DOI != \"\":\n",
193-
" model = load_description(BMZ_MODEL_DOI)\n",
194-
" print(\n",
195-
" f\"\\nThe model '{model.name}' with DOI '{BMZ_MODEL_DOI}' has been correctly loaded.\"\n",
196-
" )\n",
197-
"elif BMZ_MODEL_URL != \"\":\n",
198-
" model = load_description(BMZ_MODEL_URL)\n",
199-
" print(\n",
200-
" f\"\\nThe model '{model.name}' with URL '{BMZ_MODEL_URL}' has been correctly loaded.\"\n",
201-
" )\n",
202-
"else:\n",
203-
" print(\"\\nPlease specify a model ID, DOI or URL\")\n",
204-
"\n",
205-
"if \"draft\" in BMZ_MODEL_ID or \"draft\" in BMZ_MODEL_DOI or \"draft\" in BMZ_MODEL_URL:\n",
206-
" print(\n",
207-
" f\"\\nThis is the DRAFT version of '{model.name}'. \\nDraft versions have not been reviewed by the Bioimage Model Zoo Team and may contain harmful code. Run with caution.\"\n",
208-
" )"
209-
]
210-
},
211-
{
212-
"cell_type": "markdown",
213-
"metadata": {},
214-
"source": [
215-
"### 1.3 Inspect the model metadata\n",
194+
"from bioimageio.core import load_model_description\n",
195+
"from bioimageio.core import test_model\n",
216196
"\n",
217-
"Let's inspect all the model metadata. For a step-by-step inspection refer to [bioimageio.spec package example notebook](https://github.com/bioimage-io/spec-bioimage-io/blob/main/example/load_model_and_create_your_own.ipynb)."
218-
]
219-
},
220-
{
221-
"cell_type": "code",
222-
"execution_count": null,
223-
"metadata": {},
224-
"outputs": [],
225-
"source": [
226-
"pprint(model)"
197+
"model = load_model_description(model_source)\n",
198+
"test_summary = test_model(model) #Test model to check environment compatibility \n",
199+
"assert test_summary.status == \"passed\", test_summary.display() "
227200
]
228201
},
229202
{
230-
"cell_type": "code",
231-
"execution_count": null,
203+
"cell_type": "markdown",
232204
"metadata": {},
233-
"outputs": [],
234205
"source": [
235-
"print(f\"\\n Covers of the model '{model.name}' are: \")\n",
236-
"for cover in model.covers:\n",
237-
" cover_data = imread(download(cover).path)\n",
238-
" plt.figure(figsize=(10, 10))\n",
239-
" plt.imshow(cover_data)\n",
240-
" plt.xticks([])\n",
241-
" plt.yticks([])\n",
242-
" plt.show()"
206+
"## Running a prediction"
243207
]
244208
},
245209
{
246210
"cell_type": "markdown",
247211
"metadata": {},
248212
"source": [
249-
"## 2. Test the model"
213+
"`bioimageio.core` implements the functionality to run a prediction with models described in the `bioimage.io` format.\n",
214+
"\n",
215+
"This includes functions to run predictions on `numpy.ndarray`/`xarray.DataArray` as input and convenience functions to run predictions for images stored on disc."
250216
]
251217
},
252218
{
253219
"cell_type": "markdown",
254220
"metadata": {},
255221
"source": [
256-
"The `bioimageio.core.test_model` function can be used to fully test the model.\n",
257-
"This is done by running the predicition on the test input(s) and checking that they agree with the test output(s) provided in the model documentation.\n",
222+
"### `predict` function \n",
223+
"\n",
224+
"The most direct way to run a model prediction is using the `bioimageio.core` convenience function `predict`, to which you pass the model you want to predict with and the input you want to predict on, along with optional arguments with default values. The input can be a range of types including; `numpy.ndarray`, `xarray.DataArray`, bioimageio `Tensor` or `pathlib.Path` to data. The union of all these types are contained within the `TensorSource` object.\n",
258225
"\n",
259-
"This test should be run before using the model to ensure that it works properly.\n",
226+
"`bioimageio.core.Tensors/xarray.DataArrays` are like numpy arrays, but they have annotated axes.\n",
260227
"\n",
261-
"----\n",
228+
"However, sometimes a model requires multiple inputs to be passed in at once (e.g a raw data input and a mask input). To easily facilitate this inputs can be passed in as a mapping between input id and `TensorSource`. For example defining mulitple `numpy.ndarray` inputs could be done manually done as follows:\n",
262229
"\n",
263-
"`bioimageio.core.test_model` returns a validation dictionary with 'status'='passed'/'failed' and other detailed information that can be inspected by calling `.display()` on it.\n",
230+
"```console\n",
231+
"inputs = {\"raw_input\": np.array([1,2,3]), \"mask_input\": np.array()}\n",
232+
"```\n",
264233
"\n",
265-
"The validation summary will indicate:\n",
266-
"- the versions of the `bioimageio.spec` and `bioimageio.core` libraries used to run the validation\n",
267-
"- the status of several validation steps\n",
268-
" - ✔️: Success\n",
269-
" - 🔍: information about the validation context\n",
270-
" - ⚠: Warning\n",
271-
" - ❌: Error"
234+
"To make things more convinent multiple inputs can instead be passed via a bioimageio `Sample` object"
272235
]
273236
},
274237
{
@@ -277,28 +240,22 @@
277240
"metadata": {},
278241
"outputs": [],
279242
"source": [
280-
"from bioimageio.core import test_model\n",
243+
"from bioimageio.core import predict # , predict_many\n",
244+
"from bioimageio.core import Sample\n",
281245
"\n",
282-
"test_summary = test_model(model)\n",
283-
"test_summary.display()"
284-
]
285-
},
286-
{
287-
"cell_type": "markdown",
288-
"metadata": {},
289-
"source": [
290-
"## 3. Running a prediction"
246+
"# predict_many(model=model, inputs=[sample])\n",
247+
"\n",
248+
"prediction: Sample = predict(model=model, inputs=sample)\n",
249+
"\n",
250+
"# show the prediction result\n",
251+
"show_images(sample, prediction)"
291252
]
292253
},
293254
{
294255
"cell_type": "markdown",
295256
"metadata": {},
296257
"source": [
297-
"`bioimageio.core` implements the functionality to run a prediction with models described in the `bioimage.io` format.\n",
298-
"\n",
299-
"This includes functions to run predictions on `numpy.ndarray`/`xarray.DataArray` as input and convenience functions to run predictions for images stored on disc.\n",
300-
"\n",
301-
"### 3.1. Load the test image and convert into a tensor"
258+
"### Load the test image and convert into a tensor"
302259
]
303260
},
304261
{
@@ -544,7 +501,7 @@
544501
],
545502
"metadata": {
546503
"kernelspec": {
547-
"display_name": "bioimageio-core",
504+
"display_name": "core",
548505
"language": "python",
549506
"name": "python3"
550507
},
@@ -558,7 +515,7 @@
558515
"name": "python",
559516
"nbconvert_exporter": "python",
560517
"pygments_lexer": "ipython3",
561-
"version": "3.9.19"
518+
"version": "3.12.10"
562519
}
563520
},
564521
"nbformat": 4,

0 commit comments

Comments
 (0)