Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion sdks/python/apache_beam/ml/inference/tensorflow_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ class ModelType(enum.Enum):


def _load_model(model_uri, custom_weights, load_model_args):
model = tf.keras.models.load_model(hub.resolve(model_uri), **load_model_args)
try:
model = tf.keras.models.load_model(
hub.resolve(model_uri), **load_model_args)
except Exception as e:
raise ValueError(
"Unable to load the TensorFlow model: {exception}. Make sure you've \
saved the model with TF2 format. Check out the list of TF2 Models on \
TensorFlow Hub - https://tfhub.dev/s?subtype=module,placeholder&tf-version=tf2." # pylint: disable=line-too-long
.format(exception=e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use f-strings instead of format? also can we remove the \ at the end?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this is merged already. NVM then.

if custom_weights:
model.load_weights(custom_weights)
return model
Expand Down Expand Up @@ -156,6 +164,7 @@ def load_model(self) -> tf.Module:
"Callable create_model_fn must be passed"
"with ModelType.SAVED_WEIGHTS")
return _load_model_from_weights(self._create_model_fn, self._model_uri)

return _load_model(
self._model_uri, self._custom_weights, self._load_model_args)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ def test_predict_keyed_tensor(self):
for actual, expected in zip(inferences, expected_predictions):
self.assertTrue(_compare_tensor_prediction_result(actual[1], expected[1]))

def test_load_model_exception(self):
with self.assertRaises(ValueError):
tensorflow_inference._load_model(
"https://tfhub.dev/google/imagenet/mobilenet_v1_075_192/quantops/classification/3", # pylint: disable=line-too-long
None, {})


@pytest.mark.uses_tf
class TFRunInferenceTestWithMocks(unittest.TestCase):
Expand Down