Exception occurs when loading keras model using load_model

1,317 views
Skip to first unread message

Kishore Karunakaran

unread,
Jun 7, 2017, 12:09:59 PM6/7/17
to Keras-users
I have a keras model **model.h5**. I'm trying to convert it to a model.pb basically tensorflow format. For that I need to load the model first. When I tried to load the model using **load_model**, I'm getting this exception **ValueError: No model found in config file.**. Here is my complete code



import tensorflow as tf
from tensorflow.contrib.keras.python.keras import backend as K
from tensorflow.contrib.keras.python.keras.models import load_model, Sequential

from tensorflow.python.saved_model import builder as saved_model_builder
from tensorflow.python.saved_model import tag_constants, signature_constants
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def


sess = tf.Session()
K.set_session(sess)

# disable loading of learning nodes
K.set_learning_phase(0)

# load model
model = load_model('build/model.h5')
config = model.get_config()
weights = model.get_weights()
new_Model = Sequential.from_config(config)
new_Model.set_weights(weights)

# export current best model
export_path = "build/tensorflow_model/"
builder = saved_model_builder.SavedModelBuilder(export_path)

signature = predict_signature_def(inputs={'accelerations': new_Model.input},
outputs={'scores': new_Model.output})

with K.get_session() as sess:
builder.add_meta_graph_and_variables(sess=sess,
tags=[tag_constants.SERVING],
signature_def_map={
signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature})
builder.save()

**Exception**

2017-06-07 21:27:21.505316: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0)
Traceback (most recent call last):
  File "C:/Users/Skanda/OneDrive/Playground/Fault-Classification/FaultTypeClassification/predict.py", line 28, in <module>
    model = load_model('C:/Users/Skanda/OneDrive/Playground/Fault-Classification/FaultTypeClassification/build/model.h5')
  File "D:\Programs\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 259, in load_model
    raise ValueError('No model found in config file.')
ValueError: No model found in config file.

Daπid

unread,
Jun 7, 2017, 2:02:01 PM6/7/17
to Kishore Karunakaran, Keras-users
Your h5 file seems to contain only the weights, but not the architecture. You need  to recreate the model and load the weights into it. 

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/688bb8c0-8ae0-4181-a65a-6b2bf9719bbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kishore Karunakaran

unread,
Jun 7, 2017, 9:59:03 PM6/7/17
to Daπid, Keras-users
Hey David, Thanks Now I understand why it fails previously I was saving model weights and configuration separately.  Now I know the issue and I fixed it.
Reply all
Reply to author
Forward
0 new messages