From 563743805f15d7b904fbc8fc26eb9acb3aaaca83 Mon Sep 17 00:00:00 2001 From: Tobias Hermann Date: Mon, 18 Mar 2024 16:58:14 +0100 Subject: [PATCH] Assert correct model type when converting, issue #413 (#414) --- README.md | 2 +- keras_export/convert_model.py | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b3eae37c..6b8737eb 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ Would you like to build/train a model using Keras/Python? And would you like to Usage ----- -1) Use Keras/Python to build (`model.compile(...)`), train (`model.fit(...)`) and test (`model.evaluate(...)`) your model as usual. Then save it to a single HDF5 file using `model.save('....keras')`. The `image_data_format` in your model must be `channels_last`, which is the default when using the TensorFlow backend. Models created with a different `image_data_format` and other backends are not supported. +1) Use Keras/Python to build (`model.compile(...)`), train (`model.fit(...)`) and test (`model.evaluate(...)`) your model as usual. Then save it to a single file using `model.save('....keras')`. The `image_data_format` in your model must be `channels_last`, which is the default when using the TensorFlow backend. Models created with a different `image_data_format` and other backends are not supported. 2) Now convert it to the frugally-deep file format with `keras_export/convert_model.py` diff --git a/keras_export/convert_model.py b/keras_export/convert_model.py index 66fa9259..7b5c7443 100755 --- a/keras_export/convert_model.py +++ b/keras_export/convert_model.py @@ -887,6 +887,9 @@ def workaround_cudnn_not_found_problem(): for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) +def assert_model_type(model): + import keras + assert type(model) in [keras.src.models.sequential.Sequential, keras.src.models.functional.Functional] def convert(in_path, out_path, no_tests=False): """Convert any (h5-)stored Keras model to the frugally-deep model format."""