Skip to content

Commit 7146aea

Browse files
committed
update load_model_and_create_your_own
1 parent 145cc20 commit 7146aea

File tree

1 file changed

+71
-22
lines changed

1 file changed

+71
-22
lines changed

example/load_model_and_create_your_own.ipynb

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"from loguru import logger\n",
8080
"from numpy.typing import NDArray\n",
8181
"\n",
82-
"from bioimageio.spec.utils import download\n",
82+
"from bioimageio.spec.utils import get_reader\n",
8383
"\n",
8484
"# redirect stderr to stdout for better output comparison across runs with 'pytest --nbval'\n",
8585
"sys.stderr = sys.stdout\n",
@@ -388,13 +388,13 @@
388388
],
389389
"source": [
390390
"for i, cover in enumerate(model.covers):\n",
391-
" downloaded_cover = download(cover)\n",
392-
" cover_data: NDArray[Any] = imread(downloaded_cover.read()) # type: ignore\n",
391+
" cover_reader = get_reader(cover)\n",
392+
" cover_data: NDArray[Any] = imread(cover_reader.read())\n",
393393
" _ = plt.figure(figsize=(10, 10))\n",
394394
" plt.imshow(cover_data) # type: ignore\n",
395395
" plt.xticks([]) # type: ignore\n",
396396
" plt.yticks([]) # type: ignore\n",
397-
" plt.title(f\"cover image {downloaded_cover.original_file_name}\") # type: ignore\n",
397+
" plt.title(f\"cover image {cover_reader.original_file_name}\") # type: ignore\n",
398398
" plt.show()"
399399
]
400400
},
@@ -713,6 +713,55 @@
713713
" assert_never(arch)"
714714
]
715715
},
716+
{
717+
"cell_type": "markdown",
718+
"metadata": {},
719+
"source": [
720+
"### Invalid Resource Descriptions\n",
721+
"\n",
722+
"To inspect invalid (or other non-model) resource description, use `load_description`, which always returns a description object (unlike `load_model_description`, which will throw an exception if the loaded resource is not a valid model).\n",
723+
"\n",
724+
"`load_description` may return:\n",
725+
"- A valid description of a known resource type (`ModelDescr`, `DatasetDescr`, `NotebeookDescr`, or `ApplicationDescr`).\n",
726+
"- A valid description of a generic resource: `GenericDescr` with a `type` other than `'model'`, `'dataset'`, `'notebook'`, or `'application'`.\n",
727+
"- An invalid description if there were any validation errors (`InvalidDescr`).\\\n",
728+
" This can also occur with valid models when using an older version of the bioimageio.spec library. \\\n",
729+
" In these cases the validation summary includes a warning like `\"future format_version '0.5.3' treated as '0.4.10'\"`, see example below"
730+
]
731+
},
732+
{
733+
"cell_type": "code",
734+
"execution_count": 11,
735+
"metadata": {},
736+
"outputs": [
737+
{
738+
"name": "stdout",
739+
"output_type": "stream",
740+
"text": [
741+
"Description class name: InvalidDescr\n",
742+
"Type and format version attributes are always accessible (may return 'unknown'): model 0.4.10\n",
743+
"Validation status: failed\n",
744+
"Validation errors: ['Input should be a valid dictionary or instance of AttachmentsDescr', '...']\n",
745+
"Validation warnings: [\"future format_version '0.5.3' treated as '0.4.10'\"]\n"
746+
]
747+
}
748+
],
749+
"source": [
750+
"from bioimageio.spec import load_description\n",
751+
"\n",
752+
"# load the model with an older format version to force an error\n",
753+
"# (backward conversion is not supported)\n",
754+
"descr = load_description(model_source, format_version=\"0.4\")\n",
755+
"\n",
756+
"# Print a few attributes instead of rendering the full validation summary with lots of errors...\n",
757+
"# descr.validation_summary.display()\n",
758+
"print(f\"Description class name: {descr.__class__.__name__}\")\n",
759+
"print(f\"Type and format version attributes are always accessible (may return 'unknown'): {descr.type} {descr.format_version}\")\n",
760+
"print(f\"Validation status: {descr.validation_summary.status}\")\n",
761+
"print(f\"Validation errors: {[descr.validation_summary.errors[0].msg, '...']}\")\n",
762+
"print(f\"Validation warnings: {[w.msg for w in descr.validation_summary.warnings]}\")"
763+
]
764+
},
716765
{
717766
"cell_type": "markdown",
718767
"metadata": {},
@@ -727,7 +776,7 @@
727776
},
728777
{
729778
"cell_type": "code",
730-
"execution_count": 11,
779+
"execution_count": 12,
731780
"metadata": {
732781
"execution": {
733782
"iopub.execute_input": "2025-09-25T15:14:17.617583Z",
@@ -759,7 +808,7 @@
759808
"traceback": [
760809
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
761810
"\u001b[1;31mValidationError\u001b[0m Traceback (most recent call last)",
762-
"Cell \u001b[1;32mIn[11], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m ModelDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mModelDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
811+
"Cell \u001b[1;32mIn[12], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m ModelDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mModelDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
763812
"\u001b[1;31mValidationError\u001b[0m: 4 validation errors for ModelDescr:\nname\n Field required [input={'format_version': '0.5.5', 'type': 'model'}]\ninputs\n Field required [input={'format_version': '0.5.5', 'type': 'model'}]\noutputs\n Field required [input={'format_version': '0.5.5', 'type': 'model'}]\nweights\n Field required [input={'format_version': '0.5.5', 'type': 'model'}]"
764813
]
765814
}
@@ -788,7 +837,7 @@
788837
},
789838
{
790839
"cell_type": "code",
791-
"execution_count": 12,
840+
"execution_count": 13,
792841
"metadata": {
793842
"execution": {
794843
"iopub.execute_input": "2025-09-25T15:14:17.688095Z",
@@ -816,7 +865,7 @@
816865
"traceback": [
817866
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
818867
"\u001b[1;31mValidationError\u001b[0m Traceback (most recent call last)",
819-
"Cell \u001b[1;32mIn[12], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m InputTensorDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mInputTensorDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
868+
"Cell \u001b[1;32mIn[13], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m InputTensorDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mInputTensorDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
820869
"\u001b[1;31mValidationError\u001b[0m: 1 validation errors for InputTensorDescr:\naxes\n Field required [input={}]"
821870
]
822871
}
@@ -829,7 +878,7 @@
829878
},
830879
{
831880
"cell_type": "code",
832-
"execution_count": 13,
881+
"execution_count": 14,
833882
"metadata": {
834883
"execution": {
835884
"iopub.execute_input": "2025-09-25T15:14:17.770073Z",
@@ -883,7 +932,7 @@
883932
},
884933
{
885934
"cell_type": "code",
886-
"execution_count": 14,
935+
"execution_count": 15,
887936
"metadata": {
888937
"execution": {
889938
"iopub.execute_input": "2025-09-25T15:14:19.851930Z",
@@ -911,7 +960,7 @@
911960
"traceback": [
912961
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
913962
"\u001b[1;31mValidationError\u001b[0m Traceback (most recent call last)",
914-
"Cell \u001b[1;32mIn[14], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m OutputTensorDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mOutputTensorDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
963+
"Cell \u001b[1;32mIn[15], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m OutputTensorDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mOutputTensorDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
915964
"\u001b[1;31mValidationError\u001b[0m: 1 validation errors for OutputTensorDescr:\naxes\n Field required [input={}]"
916965
]
917966
}
@@ -924,7 +973,7 @@
924973
},
925974
{
926975
"cell_type": "code",
927-
"execution_count": 15,
976+
"execution_count": 16,
928977
"metadata": {
929978
"execution": {
930979
"iopub.execute_input": "2025-09-25T15:14:19.917767Z",
@@ -985,7 +1034,7 @@
9851034
},
9861035
{
9871036
"cell_type": "code",
988-
"execution_count": 16,
1037+
"execution_count": 17,
9891038
"metadata": {
9901039
"execution": {
9911040
"iopub.execute_input": "2025-09-25T15:14:22.239956Z",
@@ -1005,7 +1054,7 @@
10051054
"traceback": [
10061055
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
10071056
"\u001b[1;31mValidationError\u001b[0m Traceback (most recent call last)",
1008-
"Cell \u001b[1;32mIn[16], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m PytorchStateDictWeightsDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mPytorchStateDictWeightsDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
1057+
"Cell \u001b[1;32mIn[17], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mbioimageio\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mspec\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mv0_5\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m PytorchStateDictWeightsDescr\n\u001b[1;32m----> 3\u001b[0m _ \u001b[38;5;241m=\u001b[39m \u001b[43mPytorchStateDictWeightsDescr\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# pyright: ignore[reportCallIssue]\u001b[39;00m\n",
10091058
"\u001b[1;31mValidationError\u001b[0m: 3 validation errors for PytorchStateDictWeightsDescr:\nsource\n Field required [input={}]\narchitecture\n Field required [input={}]\npytorch_version\n Field required [input={}]"
10101059
]
10111060
}
@@ -1032,7 +1081,7 @@
10321081
},
10331082
{
10341083
"cell_type": "code",
1035-
"execution_count": 17,
1084+
"execution_count": 18,
10361085
"metadata": {
10371086
"execution": {
10381087
"iopub.execute_input": "2025-09-25T15:14:22.306568Z",
@@ -1077,7 +1126,7 @@
10771126
},
10781127
{
10791128
"cell_type": "code",
1080-
"execution_count": 18,
1129+
"execution_count": 19,
10811130
"metadata": {
10821131
"execution": {
10831132
"iopub.execute_input": "2025-09-25T15:14:27.148305Z",
@@ -1107,7 +1156,7 @@
11071156
},
11081157
{
11091158
"cell_type": "code",
1110-
"execution_count": 19,
1159+
"execution_count": 20,
11111160
"metadata": {
11121161
"execution": {
11131162
"iopub.execute_input": "2025-09-25T15:14:28.098738Z",
@@ -1137,7 +1186,7 @@
11371186
},
11381187
{
11391188
"cell_type": "code",
1140-
"execution_count": 20,
1189+
"execution_count": 21,
11411190
"metadata": {
11421191
"execution": {
11431192
"iopub.execute_input": "2025-09-25T15:14:28.858588Z",
@@ -1182,7 +1231,7 @@
11821231
},
11831232
{
11841233
"cell_type": "code",
1185-
"execution_count": 21,
1234+
"execution_count": 22,
11861235
"metadata": {
11871236
"execution": {
11881237
"iopub.execute_input": "2025-09-25T15:14:29.687571Z",
@@ -1261,7 +1310,7 @@
12611310
},
12621311
{
12631312
"cell_type": "code",
1264-
"execution_count": 22,
1313+
"execution_count": 23,
12651314
"metadata": {
12661315
"execution": {
12671316
"iopub.execute_input": "2025-09-25T15:14:32.351633Z",
@@ -1329,7 +1378,7 @@
13291378
},
13301379
{
13311380
"cell_type": "code",
1332-
"execution_count": 23,
1381+
"execution_count": 24,
13331382
"metadata": {
13341383
"execution": {
13351384
"iopub.execute_input": "2025-09-25T15:14:32.521339Z",
@@ -1564,7 +1613,7 @@
15641613
},
15651614
{
15661615
"cell_type": "code",
1567-
"execution_count": 24,
1616+
"execution_count": 25,
15681617
"metadata": {
15691618
"execution": {
15701619
"iopub.execute_input": "2025-09-25T15:15:03.940263Z",

0 commit comments

Comments
 (0)