0

I have a keras model where each layer has a specific name

def build_model():
  input_layer = keras.Input(shape=input_shape, name='input')
  conv1 = layers.Conv2D(32, kernel_size=(3, 3), activation="relu", name='conv1')(input_layer)
  maxpool1 = layers.MaxPooling2D(pool_size=(2, 2), name='maxpool1')(conv1)
  conv2 = layers.Conv2D(64, kernel_size=(3, 3), activation="relu", name='conv2')(maxpool1)
  maxpool2 = layers.MaxPooling2D(pool_size=(2, 2), name='maxpool2')(conv2)
  flatten = layers.Flatten(name='flatten')(maxpool2)
  dropout = layers.Dropout(0.5, name='dropout')(flatten)
  dense = layers.Dense(num_classes, activation="softmax", name='dense')(dropout)
  return keras.Model(inputs=(input_layer,), outputs=(dense,))

model = build_model()
model.compile(...)
model.fit(...)

and I would like to save the model in its entirety so that I can load it later. Saving and loading the model looks like this

model.save('path/model.h5')
new_model = tf.keras.models.load_model('path/model.h5')

Unfortunately I get the following error when I try to load the file

>>> model = keras.models.load_model('model.h5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/username/anaconda3/envs/enviromentname/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/Users/username/anaconda3/envs/enviromentname/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 166, in load_model_from_hdf5
    model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

I also tried to save only the weights but I get the same error

>>> model.save_weights('path/model_weights.h5')
>>> model.load_weights('path/model_weights.h5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/username/anaconda3/envs/enviromentname/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 181, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "/Users/username/anaconda3/envs/enviromentname/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py", line 1177, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File "/Users/username/anaconda3/envs/enviromentname/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 651, in load_weights_from_hdf5_group
    original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'

Following what has been discussed in this question I tried downgrading h5py but the error remains. How can I fix it?

EDIT: Following Dr.Snoopy answer I checked again if h5py was correcty downgraded and also matched Keras version between Google Colab and my local machine. It works now with h5py version 2.10.0

1 Answers1

0

In order to save the entire model I would use the SavedModel format, that saves your model as a .pb:

import tensorflow as tf
tf.keras.models.save_model(model, "test_model")
loded_model = tf.keras.models.load_model("test_model")
ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
  • 1
    Looking at the [documentation](https://www.tensorflow.org/api_docs/python/tf/saved_model/load?hl=en) seems to rule this solution out. Since Keras models are trackable the loaded object will not be a Keras model and thus will not have functions such as `.fit` or `.predict`. Thank you anyway for the suggestion! – Alessandro Sep 12 '22 at 13:14
  • I've updated the answer, this will allow you to use the functions from Keras. – ClaudiaR Sep 12 '22 at 13:26