Skip to content

Commit c9bb85a

Browse files
bioimageio.core_usage notebook and requirements txt file
1 parent 506412c commit c9bb85a

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

example/bioimageio.core_usage.ipynb

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"source": [
77
"# Bioimage Model Zoo Core Example notebook\n",
88
"\n",
9-
"This notebook shows how to interact with the `bioimageio.core` programmatically to explore, load, use, and export content from the [BioImage Model Zoo](https://bioimage.io).\""
9+
"This notebook shows how to interact with the `bioimageio.core` programmatically to explore, load, use, and export content from the [BioImage Model Zoo](https://bioimage.io).\n",
10+
"\n",
11+
"For a local use of this notebook the minimum package requirements can be found in `bioimageio.core_usage_requirements.txt` located in the same folder as the notebook."
1012
]
1113
},
1214
{
@@ -20,7 +22,7 @@
2022
"cell_type": "markdown",
2123
"metadata": {},
2224
"source": [
23-
"If the notebook is being run on Google Colab, install necessary dependencies"
25+
"### 0.1. If running on Google Colab, install necessary dependencies"
2426
]
2527
},
2628
{
@@ -32,14 +34,16 @@
3234
"import os\n",
3335
"\n",
3436
"if os.getenv(\"COLAB_RELEASE_TAG\"):\n",
35-
" %pip install bioimageio.core torch onnxruntime"
37+
" %pip install bioimageio.core==0.6.7 torch==2.3.1 onnxruntime==1.18.0"
3638
]
3739
},
3840
{
3941
"cell_type": "markdown",
4042
"metadata": {},
4143
"source": [
42-
"Enable pretty validation errors"
44+
"### 0.2.Enable pretty_validation_errors\n",
45+
"\n",
46+
"This function displays validation errors in a human readable format."
4347
]
4448
},
4549
{
@@ -48,7 +52,6 @@
4852
"metadata": {},
4953
"outputs": [],
5054
"source": [
51-
"# enable pretty validation errors in ipynb\n",
5255
"from bioimageio.spec.pretty_validation_errors import (\n",
5356
" enable_pretty_validation_errors_in_ipynb,\n",
5457
")\n",
@@ -59,7 +62,7 @@
5962
"cell_type": "markdown",
6063
"metadata": {},
6164
"source": [
62-
"Load general dependencies"
65+
"### 0.3. Load general dependencies"
6366
]
6467
},
6568
{
@@ -70,26 +73,29 @@
7073
"source": [
7174
"# Load general dependencies\n",
7275
"from imageio.v2 import imread\n",
73-
"from pprint import pprint\n",
7476
"from bioimageio.spec.utils import download\n",
77+
"from pprint import pprint\n",
7578
"import matplotlib.pyplot as plt\n",
7679
"import numpy as np\n",
7780
"\n",
78-
"# function to display input and prediction output images\n",
81+
"# Function to display input and prediction output images\n",
7982
"def show_images(sample_tensor, prediction_tensor):\n",
8083
" input_array = sample_tensor.members['input0'].data\n",
84+
" \n",
8185
" # Check for the number of channels to enable display\n",
8286
" input_array = np.squeeze(input_array)\n",
8387
" if len(input_array.shape)>2:\n",
8488
" input_array = input_array[0]\n",
8589
"\n",
8690
" output_array = prediction_tensor.members['output0'].data\n",
91+
" \n",
92+
" # Check for the number of channels to enable display\n",
8793
" output_array = np.squeeze(output_array)\n",
8894
" if len(output_array.shape)>2:\n",
8995
" output_array = output_array[0]\n",
9096
"\n",
9197
" plt.figure()\n",
92-
" ax1 =plt.subplot(1,2,1)\n",
98+
" ax1 = plt.subplot(1,2,1)\n",
9399
" ax1.set_title(\"Input\")\n",
94100
" ax1.axis('off')\n",
95101
" plt.imshow(input_array)\n",
@@ -134,11 +140,11 @@
134140
"cell_type": "markdown",
135141
"metadata": {},
136142
"source": [
137-
"bioimage.io resources may be identified via their bioimage.io ID, e.g. \"affable-shark\" or the [DOI](https://doi.org/) of their [Zenodo](https://zenodo.org/) backup.\n",
143+
"`bioimage.io` resources may be identified via their bioimage.io __ID__, e.g. \"affable-shark\" or the [__DOI__](https://doi.org/) of their [__Zenodo__](https://zenodo.org/) backup.\n",
138144
"\n",
139-
"Both of these options may be version specific (\"affable-shark\" or a version specific [Zenodo](https://zenodo.org/) backup [DOI](https://doi.org/)).\n",
145+
"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",
140146
"\n",
141-
"Alternativly any RDF source may be loaded by providing a local path or URL."
147+
"Alternatively, any rdf.yaml source, single file or in a .zip, may be loaded by providing its __local path__ or __URL__."
142148
]
143149
},
144150
{
@@ -147,7 +153,7 @@
147153
"metadata": {},
148154
"outputs": [],
149155
"source": [
150-
"BMZ_MODEL_ID = \"\" #\"affable-shark\"\n",
156+
"BMZ_MODEL_ID = \"\"#\"affable-shark\"\n",
151157
"BMZ_MODEL_DOI = \"\" #\"10.5281/zenodo.6287342\"\n",
152158
"BMZ_MODEL_URL = \"https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/affable-shark/draft/files/rdf.yaml\""
153159
]
@@ -156,7 +162,8 @@
156162
"cell_type": "markdown",
157163
"metadata": {},
158164
"source": [
159-
"load_description is a function of the `bioimageio.spec` package, but as it is a sub-package of `bioimageio.core` it can be called as `bioimageio.core.load_description`.\n",
165+
"`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",
166+
"\n",
160167
"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."
161168
]
162169
},
@@ -169,16 +176,16 @@
169176
"name": "stderr",
170177
"output_type": "stream",
171178
"text": [
172-
"computing SHA256 of 2717bc821b53e7554f84f82d494f5019-zero_mean_unit_variance.ijm (result: 767f2c3a50e36365c30b9e46e57fcf82e606d337e8a48d4a2440dc512813d186): 100%|██████████| 1/1 [00:00<00:00, 1219.27it/s]\n",
173-
"computing SHA256 of 0fc5a081dd022def1829f39f58b667b5-test_input_0.npy (result: c29bd6e16e3f7856217b407ba948222b1c2a0da41922a0f79297e25588614fe2): 100%|██████████| 3/3 [00:00<00:00, 3134.76it/s]\n",
174-
"computing SHA256 of 512ac1bb6de19fba42ae180732aa2f74-sample_input_0.tif (result: a24b3c708b6ca6825494eb7c5a4d221335fb3eef5eb9d03f4108907cdaad2bf9): 100%|██████████| 1/1 [00:00<00:00, 868.03it/s] \n",
175-
"computing SHA256 of 6908a856d9b4bab0b2bacbceb17c9142-test_output_0.npy (result: 510181f38930e59e4fd8ecc03d6ea7c980eb6609759655f2d4a41fe36108d5f5): 100%|██████████| 5/5 [00:00<00:00, 5011.12it/s]\n",
176-
"computing SHA256 of 011c6c06668d71d113e21511f83b6586-sample_output_0.tif (result: e8f99aabe8405427f515eba23a49f58ba50302f57d1fdfd07026e1984f836c5e): 100%|██████████| 5/5 [00:00<00:00, 5240.26it/s]\n",
177-
"computing SHA256 of 112365584e54b0efe14453dac6c454d4-weights.onnx (result: df913b85947f5132bcdaf81d91af0963f60d44f4caf8a4fec672d96a2f327b44): 100%|██████████| 884/884 [00:00<00:00, 4757.85it/s]\n",
178-
"computing SHA256 of 136455aa974fc10d712b23af9f7d6329-unet.py (result: 7f5b15948e8e2c91f78dcff34fbf30af517073e91ba487f3edb982b948d099b3): 100%|██████████| 1/1 [00:00<00:00, 1234.34it/s]\n",
179-
"computing SHA256 of 35c39b9c4c5b926f62831f664f775d65-environment.yaml (result: e79043966078d1375f470dd4173eda70d1db66f70ceb568cf62a4fdc50d95c7f): 100%|██████████| 1/1 [00:00<00:00, 1949.93it/s]\n",
180-
"computing SHA256 of 377a31b4ba5b547fc064a1d4982a377b-weights.pt (result: 608f52cd7f5119f7a7b8272395b0c169714e8be34536eaf159820f72a1d6a5b7): 100%|██████████| 884/884 [00:00<00:00, 14829.93it/s]\n",
181-
"computing SHA256 of 921b32437eee259df0fded18e52e7c01-weights-torchscript.pt (result: 8410950508655a300793b389c815dc30b1334062fc1dadb1e15e55a93cbb99a0): 100%|██████████| 885/885 [00:00<00:00, 16039.65it/s]"
179+
"computing SHA256 of 2717bc821b53e7554f84f82d494f5019-zero_mean_unit_variance.ijm (result: 767f2c3a50e36365c30b9e46e57fcf82e606d337e8a48d4a2440dc512813d186): 100%|██████████| 1/1 [00:00<00:00, 1675.04it/s]\n",
180+
"computing SHA256 of 0fc5a081dd022def1829f39f58b667b5-test_input_0.npy (result: c29bd6e16e3f7856217b407ba948222b1c2a0da41922a0f79297e25588614fe2): 100%|██████████| 3/3 [00:00<00:00, 5748.25it/s]\n",
181+
"computing SHA256 of 512ac1bb6de19fba42ae180732aa2f74-sample_input_0.tif (result: a24b3c708b6ca6825494eb7c5a4d221335fb3eef5eb9d03f4108907cdaad2bf9): 100%|██████████| 1/1 [00:00<00:00, 2359.00it/s]\n",
182+
"computing SHA256 of 6908a856d9b4bab0b2bacbceb17c9142-test_output_0.npy (result: 510181f38930e59e4fd8ecc03d6ea7c980eb6609759655f2d4a41fe36108d5f5): 100%|██████████| 5/5 [00:00<00:00, 7302.06it/s] \n",
183+
"computing SHA256 of 011c6c06668d71d113e21511f83b6586-sample_output_0.tif (result: e8f99aabe8405427f515eba23a49f58ba50302f57d1fdfd07026e1984f836c5e): 100%|██████████| 5/5 [00:00<00:00, 7319.90it/s] \n",
184+
"computing SHA256 of 112365584e54b0efe14453dac6c454d4-weights.onnx (result: df913b85947f5132bcdaf81d91af0963f60d44f4caf8a4fec672d96a2f327b44): 100%|██████████| 884/884 [00:00<00:00, 16526.77it/s]\n",
185+
"computing SHA256 of 136455aa974fc10d712b23af9f7d6329-unet.py (result: 7f5b15948e8e2c91f78dcff34fbf30af517073e91ba487f3edb982b948d099b3): 100%|██████████| 1/1 [00:00<00:00, 1776.49it/s]\n",
186+
"computing SHA256 of 35c39b9c4c5b926f62831f664f775d65-environment.yaml (result: e79043966078d1375f470dd4173eda70d1db66f70ceb568cf62a4fdc50d95c7f): 100%|██████████| 1/1 [00:00<00:00, 2462.89it/s]\n",
187+
"computing SHA256 of 377a31b4ba5b547fc064a1d4982a377b-weights.pt (result: 608f52cd7f5119f7a7b8272395b0c169714e8be34536eaf159820f72a1d6a5b7): 100%|██████████| 884/884 [00:00<00:00, 16446.86it/s]\n",
188+
"computing SHA256 of 921b32437eee259df0fded18e52e7c01-weights-torchscript.pt (result: 8410950508655a300793b389c815dc30b1334062fc1dadb1e15e55a93cbb99a0): 100%|██████████| 885/885 [00:00<00:00, 15082.58it/s]"
182189
]
183190
},
184191
{
@@ -232,7 +239,9 @@
232239
"cell_type": "markdown",
233240
"metadata": {},
234241
"source": [
235-
"### 1.3 Inspect the model metadata"
242+
"### 1.3 Inspect the model metadata\n",
243+
"\n",
244+
"Model metadata includes author names, affiliations, license, and documentation."
236245
]
237246
},
238247
{
@@ -408,18 +417,17 @@
408417
"\n",
409418
"This test should be run before using the model to ensure that it works properly.\n",
410419
"\n",
411-
"`bioimageio.core.test_model` returns a dictionary with 'status'='passed'/'failed' and other detailed information.\n",
420+
"----\n",
412421
"\n",
413-
"A model description is validated with our format specification. \n",
414-
"To inspect the corresponding validation summary access the `validation_summary` attribute.\n",
422+
"`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",
415423
"\n",
416424
"The validation summary will indicate:\n",
417-
"- the version of the `bioimageio.spec` library used to run the validation\n",
425+
"- the versions of the `bioimageio.spec` and `bioimageio.core` libraries used to run the validation\n",
418426
"- the status of several validation steps\n",
419427
" - ✔️: Success\n",
420428
" - 🔍: information about the validation context\n",
421429
" - ⚠: Warning\n",
422-
" - ❌: Error\n"
430+
" - ❌: Error"
423431
]
424432
},
425433
{
@@ -431,8 +439,8 @@
431439
"name": "stderr",
432440
"output_type": "stream",
433441
"text": [
434-
"\u001b[32m2024-06-12 19:00:31.827\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36mbioimageio.core._resource_tests\u001b[0m:\u001b[36m_test_model_inference\u001b[0m:\u001b[36m122\u001b[0m - \u001b[1mstarting 'Reproduce test outputs from test inputs'\u001b[0m\n",
435-
"\u001b[32m2024-06-12 19:00:33.804\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36mbioimageio.core._resource_tests\u001b[0m:\u001b[36m_test_model_inference_parametrized\u001b[0m:\u001b[36m192\u001b[0m - \u001b[1mTesting inference with 4 different input tensor sizes\u001b[0m\n"
442+
"\u001b[32m2024-06-13 12:20:28.532\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36mbioimageio.core._resource_tests\u001b[0m:\u001b[36m_test_model_inference\u001b[0m:\u001b[36m122\u001b[0m - \u001b[1mstarting 'Reproduce test outputs from test inputs'\u001b[0m\n",
443+
"\u001b[32m2024-06-13 12:20:30.337\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36mbioimageio.core._resource_tests\u001b[0m:\u001b[36m_test_model_inference_parametrized\u001b[0m:\u001b[36m192\u001b[0m - \u001b[1mTesting inference with 4 different input tensor sizes\u001b[0m\n"
436444
]
437445
},
438446
{
@@ -505,9 +513,9 @@
505513
"cell_type": "markdown",
506514
"metadata": {},
507515
"source": [
508-
"`bioimageio.core` implements functionality to run a prediction with models described in the `bioimage.io` format.\n",
516+
"`bioimageio.core` implements the functionality to run a prediction with models described in the `bioimage.io` format.\n",
509517
"\n",
510-
"This includes functions to run predictions on `numpy.ndarray`s/`xarray.DataArrays` as input and convenience functions to run predictions for images stored on disc.\n",
518+
"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",
511519
"\n",
512520
"### 3.1. Load the test image and convert into a tensor"
513521
]
@@ -584,7 +592,7 @@
584592
{
585593
"data": {
586594
"text/plain": [
587-
"Sample(members={'raw': <bioimageio.core.tensor.Tensor object at 0x15729a010>}, stat=None, id='sample-from-numpy')"
595+
"Sample(members={'raw': <bioimageio.core.tensor.Tensor object at 0x147024410>}, stat=None, id='sample-from-numpy')"
588596
]
589597
},
590598
"execution_count": 11,
@@ -604,7 +612,7 @@
604612
"cell_type": "markdown",
605613
"metadata": {},
606614
"source": [
607-
"`bioimageio.core` provides a helper function `create_sample_for_model` to automatically create the `Sample` for the given model."
615+
"`bioimageio.core` provides the helper function `create_sample_for_model` to automatically create the `Sample` for the given model."
608616
]
609617
},
610618
{
@@ -622,7 +630,7 @@
622630
{
623631
"data": {
624632
"text/plain": [
625-
"Sample(members={'input0': <bioimageio.core.tensor.Tensor object at 0x157214ad0>}, stat={}, id='my_demo_sample')"
633+
"Sample(members={'input0': <bioimageio.core.tensor.Tensor object at 0x16c0e7990>}, stat={}, id='my_demo_sample')"
626634
]
627635
},
628636
"execution_count": 12,
@@ -659,7 +667,7 @@
659667
{
660668
"data": {
661669
"text/plain": [
662-
"Sample(members={'input0': <bioimageio.core.tensor.Tensor object at 0x157244e10>}, stat={}, id='test-input')"
670+
"Sample(members={'input0': <bioimageio.core.tensor.Tensor object at 0x16de5b8d0>}, stat={}, id='test-input')"
663671
]
664672
},
665673
"execution_count": 13,
@@ -787,18 +795,25 @@
787795
"### 3.3. Recover input and output tensors as numpy arrays"
788796
]
789797
},
798+
{
799+
"cell_type": "markdown",
800+
"metadata": {},
801+
"source": [
802+
"This example code shows how to recover the image information from the input and output tensors as numpy arrays."
803+
]
804+
},
790805
{
791806
"cell_type": "code",
792-
"execution_count": 19,
807+
"execution_count": 17,
793808
"metadata": {},
794809
"outputs": [
795810
{
796811
"data": {
797812
"text/plain": [
798-
"<matplotlib.image.AxesImage at 0x16b069dd0>"
813+
"<matplotlib.image.AxesImage at 0x1763ce410>"
799814
]
800815
},
801-
"execution_count": 19,
816+
"execution_count": 17,
802817
"metadata": {},
803818
"output_type": "execute_result"
804819
},
@@ -817,6 +832,7 @@
817832
"np_input_list = []\n",
818833
"np_output_list = []\n",
819834
"\n",
835+
"# iterate over the number of tensors inside the input sample\n",
820836
"for ipt in range(len(sample.members.keys())):\n",
821837
" input_array = sample.members[f\"input{ipt}\"].data\n",
822838
"\n",
@@ -828,6 +844,7 @@
828844
" np_input_list.append(input_array)\n",
829845
"\n",
830846
"\n",
847+
"# iterate over the number of tensors inside the output prediction\n",
831848
"for out in range(len(prediction.members.keys())):\n",
832849
" output_array = prediction.members[f\"output{ipt}\"].data\n",
833850
"\n",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
python == 3.11
2+
bioimageio.core == 0.6.7
3+
ipykernel == 6.29.4
4+
matplotlib == 3.9.0
5+
torch == 2.3.1
6+
onnxruntime == 1.18.0

0 commit comments

Comments
 (0)