|
17 | 17 | "cell_type": "markdown", |
18 | 18 | "metadata": {}, |
19 | 19 | "source": [ |
20 | | - "## 0. Activate human readable output error messages and load dependencies" |
| 20 | + "## Activate human readable output error messages and load dependencies" |
21 | 21 | ] |
22 | 22 | }, |
23 | 23 | { |
24 | 24 | "cell_type": "markdown", |
25 | 25 | "metadata": {}, |
26 | 26 | "source": [ |
27 | | - "### 0.1. Install necessary dependencies" |
| 27 | + "### Running local" |
28 | 28 | ] |
29 | 29 | }, |
30 | 30 | { |
31 | | - "cell_type": "code", |
32 | | - "execution_count": null, |
| 31 | + "cell_type": "markdown", |
33 | 32 | "metadata": {}, |
34 | | - "outputs": [], |
35 | 33 | "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", |
39 | 35 | "\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 | + "```" |
43 | 39 | ] |
44 | 40 | }, |
45 | 41 | { |
46 | 42 | "cell_type": "markdown", |
47 | 43 | "metadata": {}, |
48 | 44 | "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." |
52 | 53 | ] |
53 | 54 | }, |
54 | 55 | { |
|
57 | 58 | "metadata": {}, |
58 | 59 | "outputs": [], |
59 | 60 | "source": [ |
60 | | - "from bioimageio.spec.pretty_validation_errors import (\n", |
61 | | - " enable_pretty_validation_errors_in_ipynb,\n", |
62 | | - ")\n", |
| 61 | + "import os\n", |
63 | 62 | "\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" |
65 | 66 | ] |
66 | 67 | }, |
67 | 68 | { |
68 | 69 | "cell_type": "markdown", |
69 | 70 | "metadata": {}, |
70 | 71 | "source": [ |
71 | | - "### 0.3. Load general dependencies" |
| 72 | + "### Install necessary dependencies" |
72 | 73 | ] |
73 | 74 | }, |
74 | 75 | { |
|
77 | 78 | "metadata": {}, |
78 | 79 | "outputs": [], |
79 | 80 | "source": [ |
80 | | - "# Load general dependencies\n", |
| 81 | + "import torch\n", |
81 | 82 | "from pprint import pprint\n", |
82 | 83 | "\n", |
83 | 84 | "import matplotlib.pyplot as plt\n", |
|
86 | 87 | "\n", |
87 | 88 | "from bioimageio.spec.utils import download\n", |
88 | 89 | "\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", |
89 | 98 | "\n", |
90 | 99 | "# Function to display input and prediction output images\n", |
91 | 100 | "def show_images(sample_tensor, prediction_tensor):\n", |
|
119 | 128 | "cell_type": "markdown", |
120 | 129 | "metadata": {}, |
121 | 130 | "source": [ |
122 | | - "## 1. Load a model" |
| 131 | + "## Load a model" |
123 | 132 | ] |
124 | 133 | }, |
125 | 134 | { |
126 | 135 | "cell_type": "markdown", |
127 | 136 | "metadata": {}, |
128 | 137 | "source": [ |
129 | | - "### 1.1 Inspect available models in the Bioimage Model Zoo" |
| 138 | + "### Inspect available models in the Bioimage Model Zoo" |
130 | 139 | ] |
131 | 140 | }, |
132 | 141 | { |
|
140 | 149 | "cell_type": "markdown", |
141 | 150 | "metadata": {}, |
142 | 151 | "source": [ |
143 | | - "### 1.2 Load model from the BioImage Model Zoo" |
| 152 | + "### Load model from the BioImage Model Zoo" |
144 | 153 | ] |
145 | 154 | }, |
146 | 155 | { |
|
151 | 160 | "\n", |
152 | 161 | "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", |
153 | 162 | "\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)." |
155 | 166 | ] |
156 | 167 | }, |
157 | 168 | { |
|
160 | 171 | "metadata": {}, |
161 | 172 | "outputs": [], |
162 | 173 | "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\"" |
166 | 177 | ] |
167 | 178 | }, |
168 | 179 | { |
169 | 180 | "cell_type": "markdown", |
170 | 181 | "metadata": {}, |
171 | 182 | "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", |
173 | 184 | "\n", |
174 | 185 | "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." |
175 | 186 | ] |
|
180 | 191 | "metadata": {}, |
181 | 192 | "outputs": [], |
182 | 193 | "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", |
216 | 196 | "\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() " |
227 | 200 | ] |
228 | 201 | }, |
229 | 202 | { |
230 | | - "cell_type": "code", |
231 | | - "execution_count": null, |
| 203 | + "cell_type": "markdown", |
232 | 204 | "metadata": {}, |
233 | | - "outputs": [], |
234 | 205 | "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" |
243 | 207 | ] |
244 | 208 | }, |
245 | 209 | { |
246 | 210 | "cell_type": "markdown", |
247 | 211 | "metadata": {}, |
248 | 212 | "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." |
250 | 216 | ] |
251 | 217 | }, |
252 | 218 | { |
253 | 219 | "cell_type": "markdown", |
254 | 220 | "metadata": {}, |
255 | 221 | "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", |
258 | 225 | "\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", |
260 | 227 | "\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", |
262 | 229 | "\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", |
264 | 233 | "\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" |
272 | 235 | ] |
273 | 236 | }, |
274 | 237 | { |
|
277 | 240 | "metadata": {}, |
278 | 241 | "outputs": [], |
279 | 242 | "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", |
281 | 245 | "\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)" |
291 | 252 | ] |
292 | 253 | }, |
293 | 254 | { |
294 | 255 | "cell_type": "markdown", |
295 | 256 | "metadata": {}, |
296 | 257 | "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" |
302 | 259 | ] |
303 | 260 | }, |
304 | 261 | { |
|
544 | 501 | ], |
545 | 502 | "metadata": { |
546 | 503 | "kernelspec": { |
547 | | - "display_name": "bioimageio-core", |
| 504 | + "display_name": "core", |
548 | 505 | "language": "python", |
549 | 506 | "name": "python3" |
550 | 507 | }, |
|
558 | 515 | "name": "python", |
559 | 516 | "nbconvert_exporter": "python", |
560 | 517 | "pygments_lexer": "ipython3", |
561 | | - "version": "3.9.19" |
| 518 | + "version": "3.12.10" |
562 | 519 | } |
563 | 520 | }, |
564 | 521 | "nbformat": 4, |
|
0 commit comments