Skip to content

Commit

Permalink
prettify model adapter creation error message
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnBe committed Jul 17, 2024
1 parent 6a99519 commit 0dc1c63
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions bioimageio/core/model_adapters/_model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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:
Expand Down

0 comments on commit 0dc1c63

Please sign in to comment.