Skip to content

Commit

Permalink
Add informational message for failed loading of nn-ensemble model
Browse files Browse the repository at this point in the history
  • Loading branch information
juhoinkinen committed Apr 11, 2024
1 parent 1ba7912 commit 7a1cfef
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
17 changes: 13 additions & 4 deletions annif/backend/nn_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import annif.corpus
import annif.parallel
import annif.util
from annif.exception import NotInitializedException, NotSupportedException
from annif.exception import (
NotInitializedException,
NotSupportedException,
OperationFailedException,
)
from annif.suggestion import SuggestionBatch, vector_to_suggestions

from . import backend, ensemble
Expand Down Expand Up @@ -129,9 +133,14 @@ def initialize(self, parallel: bool = False) -> None:
backend_id=self.backend_id,
)
self.debug("loading Keras model from {}".format(model_filename))
self._model = load_model(
model_filename, custom_objects={"MeanLayer": MeanLayer}
)
try:
self._model = load_model(
model_filename, custom_objects={"MeanLayer": MeanLayer}
)
except ValueError:
md = annif.util.get_keras_model_metadata(model_filename)
message = f"loading model from {model_filename}; model metadata: {md}"
raise OperationFailedException(message, backend_id=self.backend_id)

Check warning on line 143 in annif/backend/nn_ensemble.py

View check run for this annotation

Codecov / codecov/patch

annif/backend/nn_ensemble.py#L140-L143

Added lines #L140 - L143 were not covered by tests

def _merge_source_batches(
self,
Expand Down
14 changes: 14 additions & 0 deletions annif/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from __future__ import annotations

import glob
import json
import logging
import os
import os.path
import tempfile
import zipfile
from typing import Any, Callable

from annif import logger
Expand Down Expand Up @@ -50,6 +52,18 @@ def atomic_save(
os.rename(fn, newname)


def get_keras_model_metadata(model_file_path: str) -> dict:
"""Read metadata from Keras model files."""
try:
with zipfile.ZipFile(model_file_path, "r") as zip:
with zip.open("metadata.json") as metadata_file:
metadata_str = metadata_file.read().decode("utf-8")
metadata = json.loads(metadata_str)
return metadata
except Exception:
return dict()

Check warning on line 64 in annif/util.py

View check run for this annotation

Codecov / codecov/patch

annif/util.py#L63-L64

Added lines #L63 - L64 were not covered by tests


def cleanup_uri(uri: str) -> str:
"""remove angle brackets from a URI, if any"""
if uri.startswith("<") and uri.endswith(">"):
Expand Down
Binary file added tests/dummy-nn-model.keras
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ def test_metric_code():

for input, output in zip(inputs, outputs):
assert annif.util.metric_code(input) == output


def test_get_keras_model_metadata():
model_file_path = "tests/dummy-nn-model.keras" # nn-ensemble-model.zip"
expected_md = {"keras_version": "xx.yy.zz", "date_saved": "2024-04-11@01:01:01"}
assert annif.util.get_keras_model_metadata(model_file_path) == expected_md

0 comments on commit 7a1cfef

Please sign in to comment.