tf.keras load_model failing.

652 views
Skip to first unread message

Sreejith S

unread,
Nov 10, 2018, 12:01:46 AM11/10/18
to Discuss
Hi All,

I am trying tf.keras. Model successfully saved using save_model from tf.keras. But when i try to load the model using load_model function in tf.keras , getting below exception.

I am using TF 1.12.0 version

File "train.py", line 294, in <module>
    train_object.evaluvate_predict()
  File "train.py", line 191, in evaluvate_predict
    test_model = load_model('../model/model.h5')
  File "/home/ubuntu/srijith/dl/lib/python3.5/site-packages/tensorflow/python/keras/engine/saving.py", line 262, in load_model
    sample_weight_mode=sample_weight_mode)
  File "/home/ubuntu/srijith/dl/lib/python3.5/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "/home/ubuntu/srijith/dl/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 600, in compile
    skip_target_weighing_indices)
  File "/home/ubuntu/srijith/dl/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 134, in _set_sample_weight_attributes
    self.output_names, sample_weight_mode, skip_target_weighing_indices)
AttributeError: 'Sequential' object has no attribute 'output_names'

My code snippet using tf.keras below.

#Train, working fine
optimizer = optimizers.Adam(lr=0.001,beta_1=0.9, beta_2=0.999, epsilon=1e-08,decay=0.0011)
train_model.compile(optimizer=optimizer,
                    loss='categorical_crossentropy',
                    metrics=['accuracy'])

train_model.fit(train_set, epochs=self.args.no_epochs,
                steps_per_epoch=self.args.epochs_steps,callbacks=self.callbacks,
                validation_data=val_set, validation_steps=self.args.epochs_steps)

save_model(train_model, self.args.model_dir+"/model.h5",
                                overwrite=True, include_optimizer=True)

# Prediction part, getting in to exception
test_model = load_model('../model/model.h5')

Thank You,
Srijith

Travis Kaufman

unread,
Nov 11, 2018, 4:03:50 AM11/11/18
to Discuss
Hey Srijith,

I believe you have to use save_keras_model - as opposed to save_model - with tf 1.12.0 if you want to save Keras models directly: https://www.tensorflow.org/api_docs/python/tf/contrib/saved_model/save_keras_model

You’ll also want to use load_keras_model from the same package: https://www.tensorflow.org/api_docs/python/tf/contrib/saved_model/load_keras_model

The normal save_model/load_model calls are for Tensorflow Estimators rather than keras models, which have a different API hence the errors you were getting

Hope that helps!

Sreejith S

unread,
Nov 11, 2018, 4:12:01 AM11/11/18
to Travis Kaufman, Discuss
Hi Travis, 

Thank you for the reply.  I have used model.save and load_model from tf.keras in tf 1.13 develop version. 

Now its working fine. I will downgrade the tf version to 1.12 and update this thread according to your suggestion. 

Thank You, 
Srijith

--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To post to this group, send email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/5a431c85-0926-4de5-a906-c27cb3d1ed96%40tensorflow.org.

Sreejith S

unread,
Nov 12, 2018, 1:22:16 AM11/12/18
to Travis Kaufman, Discuss
Hi Travis,

I was able to successfully train and evaluvate/predict on tensorflow 1.12 also. 

Open sourced the project. 

Thank you
Srijith
Reply all
Reply to author
Forward
0 new messages