From 6a99519cc6721a23c41b9a6fe30c9542f0f1bc76 Mon Sep 17 00:00:00 2001 From: fynnbe Date: Wed, 17 Jul 2024 14:46:25 +0200 Subject: [PATCH 1/3] bump patch --- README.md | 3 ++- bioimageio/core/VERSION | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ce8bff2b..5ea72f0d 100644 --- a/README.md +++ b/README.md @@ -124,10 +124,11 @@ The model specification and its validation tools can be found at Date: Wed, 17 Jul 2024 16:00:47 +0200 Subject: [PATCH 2/3] prettify model adapter creation error message --- .../core/model_adapters/_model_adapter.py | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/bioimageio/core/model_adapters/_model_adapter.py b/bioimageio/core/model_adapters/_model_adapter.py index 2e61b4e6..2a2eba47 100644 --- a/bioimageio/core/model_adapters/_model_adapter.py +++ b/bioimageio/core/model_adapters/_model_adapter.py @@ -60,12 +60,17 @@ def create( ) weights = model_description.weights - errors: List[str] = [] + errors: List[Tuple[str, str]] = [] weight_format_priority_order = ( DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER if weight_format_priority_order is None else weight_format_priority_order ) + # limit weight formats to the ones present + weight_format_priority_order = [ + w for w in weight_format_priority_order if getattr(weights, w) is not None + ] + for wf in weight_format_priority_order: if wf == "pytorch_state_dict" and weights.pytorch_state_dict is not None: try: @@ -77,7 +82,7 @@ def create( devices=devices, ) except Exception as e: - errors.append(f"{wf}: {e}") + errors.append((wf, str(e))) elif ( wf == "tensorflow_saved_model_bundle" and weights.tensorflow_saved_model_bundle is not None @@ -89,7 +94,7 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append(f"{wf}: {e}") + errors.append((wf, str(e))) elif wf == "onnx" and weights.onnx is not None: try: from ._onnx_model_adapter import ONNXModelAdapter @@ -98,7 +103,7 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append(f"{wf}: {e}") + errors.append((wf, str(e))) elif wf == "torchscript" and weights.torchscript is not None: try: from ._torchscript_model_adapter import TorchscriptModelAdapter @@ -107,7 +112,7 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append(f"{wf}: {e}") + errors.append((wf, str(e))) elif wf == "keras_hdf5" and weights.keras_hdf5 is not None: # keras can either be installed as a separate package or used as part of tensorflow # we try to first import the keras model adapter using the separate package and, @@ -125,15 +130,24 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append(f"{wf}: {e}") + errors.append((wf, str(e))) assert errors - error_list = "\n - ".join(errors) - raise ValueError( - "None of the weight format specific model adapters could be created for" - + f" '{model_description.id or model_description.name}'" - + f" in this environment. Errors are:\n\n{error_list}.\n\n" - ) + if len(weight_format_priority_order) == 1: + assert len(errors) == 1 + raise ValueError( + f"The '{weight_format_priority_order[0]}' model adapter could not be created for" + + f" '{model_description.id or model_description.name}'" + + f" in this environment:\n{errors[0][1]}.\n\n" + ) + + else: + error_list = "\n - ".join(f"{wf}: {e}" for wf, e in errors) + raise ValueError( + "None of the weight format specific model adapters could be created for" + + f" '{model_description.id or model_description.name}'" + + f" in this environment. Errors are:\n\n{error_list}.\n\n" + ) @final def load(self, *, devices: Optional[Sequence[str]] = None) -> None: From 96e06b6bf94279921f4798a5e16563ec9784abaa Mon Sep 17 00:00:00 2001 From: fynnbe Date: Wed, 17 Jul 2024 16:15:53 +0200 Subject: [PATCH 3/3] include exception class name in error message --- .../core/model_adapters/_model_adapter.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/bioimageio/core/model_adapters/_model_adapter.py b/bioimageio/core/model_adapters/_model_adapter.py index 2a2eba47..4624d869 100644 --- a/bioimageio/core/model_adapters/_model_adapter.py +++ b/bioimageio/core/model_adapters/_model_adapter.py @@ -60,7 +60,7 @@ def create( ) weights = model_description.weights - errors: List[Tuple[str, str]] = [] + errors: List[Tuple[WeightsFormat, Exception]] = [] weight_format_priority_order = ( DEFAULT_WEIGHT_FORMAT_PRIORITY_ORDER if weight_format_priority_order is None @@ -82,7 +82,7 @@ def create( devices=devices, ) except Exception as e: - errors.append((wf, str(e))) + errors.append((wf, e)) elif ( wf == "tensorflow_saved_model_bundle" and weights.tensorflow_saved_model_bundle is not None @@ -94,7 +94,7 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append((wf, str(e))) + errors.append((wf, e)) elif wf == "onnx" and weights.onnx is not None: try: from ._onnx_model_adapter import ONNXModelAdapter @@ -103,7 +103,7 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append((wf, str(e))) + errors.append((wf, e)) elif wf == "torchscript" and weights.torchscript is not None: try: from ._torchscript_model_adapter import TorchscriptModelAdapter @@ -112,7 +112,7 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append((wf, str(e))) + errors.append((wf, e)) elif wf == "keras_hdf5" and weights.keras_hdf5 is not None: # keras can either be installed as a separate package or used as part of tensorflow # we try to first import the keras model adapter using the separate package and, @@ -130,22 +130,22 @@ def create( model_description=model_description, devices=devices ) except Exception as e: - errors.append((wf, str(e))) + errors.append((wf, e)) assert errors if len(weight_format_priority_order) == 1: assert len(errors) == 1 raise ValueError( - f"The '{weight_format_priority_order[0]}' model adapter could not be created for" - + f" '{model_description.id or model_description.name}'" - + f" in this environment:\n{errors[0][1]}.\n\n" + f"The '{weight_format_priority_order[0]}' model adapter could not be created" + + f" in this environment:\n{errors[0][1].__class__.__name__}({errors[0][1]}).\n\n" ) else: - error_list = "\n - ".join(f"{wf}: {e}" for wf, e in errors) + error_list = "\n - ".join( + f"{wf}: {e.__class__.__name__}({e})" for wf, e in errors + ) raise ValueError( - "None of the weight format specific model adapters could be created for" - + f" '{model_description.id or model_description.name}'" + "None of the weight format specific model adapters could be created" + f" in this environment. Errors are:\n\n{error_list}.\n\n" )