简体   繁体   中英

Error when loading Keras model trained by tensorflow

I'm trying to load a model that I trained and saved using Tensorflow & Keras, but it's given me an error.

Python version: 3.6.6

Tensorflow version: 1.11.0

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/saving.py", line 230, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/saving.py", line 310, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
    printable_module_name='layer')
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/utils/generic_utils.py", line 173, in deserialize_keras_object
    list(custom_objects.items())))
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/sequential.py", line 339, in from_config
    custom_objects=custom_objects)
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/layers/serialization.py", line 64, in deserialize
    printable_module_name='layer')
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/utils/generic_utils.py", line 175, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/base_layer.py", line 1617, in from_config
    return cls(**config)
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/layers/advanced_activations.py", line 310, in __init__
    if max_value is not None and max_value < 0.:
TypeError: '<' not supported between instances of 'dict' and 'float'

I've also tried just saving the weights instead of the whole model, but that doesn't seem more successful:

Traceback (most recent call last): File "predict_from_NN.py", line 44, in

    model.load_weights('/home/me/Data/Out/finished_model_2_weights.hdf5.index')
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/keras/engine/network.py", line 1526, in load_weights
    checkpointable_utils.streaming_restore(status=status, session=session)
  File "/packages/tensorflow/1.11.0/Python-3.6.6/tensorflow/python/training/checkpointable/util.py", line 880, in streaming_restore
    "Streaming restore not supported from name-based checkpoints. File a "
NotImplementedError: Streaming restore not supported from name-based checkpoints. File a feature request if this limitation bothers you.

Although I'm not exactly sure why/how I'm doing a 'streaming restore', and google is not very useful in both cases.

In case it helps, here's the code for my model:

from tensorflow.python.keras.layers import Conv2D, MaxPooling2D, ReLU

from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Flatten, Activation, Dense

def cnn_model(img_rows, img_cols, img_channels):
    model = Sequential()
    model.add(Conv2D(64, (3, 3),activation='linear',kernel_initializer='he_uniform',
                     input_shape=(img_rows, img_cols, img_channels)))
    model.add(ReLU())   # add an advanced activation
    model.add(MaxPooling2D(pool_size=(5, 5)))
    model.add(Conv2D(32, (3, 3),activation='linear',kernel_initializer='he_uniform'))
    model.add(ReLU())   # add an advanced activation
    model.add(MaxPooling2D(pool_size=(3, 3)))
    model.add(Conv2D(16, (3, 3),activation='linear',kernel_initializer='he_uniform'))
    model.add(ReLU())   # add an advanced activation
    model.add(MaxPooling2D(pool_size=(3, 3)))
    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Dense(1024))
    model.add(ReLU())   # add an advanced activation
    model.add(Dense(4))
    model.add(Activation('softmax'))

    return model

And I save my model like this:

model.save(os.path.join(output_folder, model_name + '_GPU.hdf5'))

And try to load it like this:

from tensorflow.python.keras.models import load_model
model = load_model(model_file)

Have you tried saving and loading the model with the "to_json" function as described below?

from keras.models import model_from_json
[...]
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

# later...

# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

PS: I borrowed this code from here .

Try updating Keras as I also faced similar error and updating repositories helped. Update keras using github link in terminal git clone https://github.com/fchollet/keras.git . In terminal redirect to location and run sudo python3 setup.py install . Hope this helps you too.

I reproduced your code on Google Colab and it worked well.

Tf version:  2.2.0-rc3
Keras version:  2.3.1

Try to update your packages. Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM