question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way of exporting keras model

See original GitHub issue

Bug Description

I have faced a bug around exporting and keras model to .h5 file, loading into another keras code, and using the model. It is similar to issue #167 .

After searching, fitting, final fitting a model using Auto-Keras, I exported the model as a keras model file. Then, I loaded the model into another “pure keras” code. However, the performance of the loaded model was so much poorer than the one evaluated by an Auto-Keras code.

Reproducing Steps

I have run the sample code on the official website:

from keras.datasets import mnist
from autokeras.image_supervised import ImageClassifier

if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.reshape(x_train.shape + (1,))
    x_test = x_test.reshape(x_test.shape + (1,))

    clf = ImageClassifier(verbose=True)
    clf.fit(x_train, y_train, time_limit=12 * 60 * 60)
    clf.final_fit(x_train, y_train, x_test, y_test, retrain=True)
    y = clf.evaluate(x_test, y_test)
    print(y)

fit and final_fit finished, and the print(y) outputted 0.9926, the accuracy score. After the code written above, I added one-line code for exporting the keras model:

clf.load_searcher().load_best_model().produce_keras_model().save('my_model.h5')

Next, I loaded the model my_model.h5 into another script and evaluated again with keras evaluate function.

from keras.datasets import mnist
from keras.models import load_model
from keras.utils import to_categorical

if __name__ == '__main__':
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_test = x_test.reshape(x_test.shape + (1,))
    y_test = to_categorical(y_test)

    keras_model = load_model('my_model.h5')
    keras_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    
    score = keras_model.evaluate(x_test, y_test)
    print(score)

In this code, I added compile function because the error message was shown:

/home/morningyrp/tensorflow/venv/local/lib/python3.5/site-packages/keras/engine/saving.py:269: UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually.
  warnings.warn('No training configuration found in save file: '

The print(score) outputted [4.088045687866211, 0.1313], which means that the accuracy score was 0.1313. In spite of the same MNIST data, this result is so much poorer the result from clf.evaluate in Auto-Keras, 0.9926.

Finally, I tried to train the model again with keras codes like this:

history = keras_model.fit(x_train, y_train,\
                    batch_size=32,\
                    epochs=1000,\
                    verbose=1,\
                    validation_split=0.1)

However, the accuracy could not increase and the loss could not reduce through whole epochs.

I also tried to export the best model without final_fit of Auto-Keras, loaded it to another keras code, and trained using compile and fit function of pure keras. However, the accuracy and the loss could not improve through whole epochs.

Expected Behavior

The keras model exported by clf.load_searcher().load_best_model().produce_keras_model().save('my_model.h5') can be used correctly on another “pure keras” code. In other words, fit, evaluate, predict functions of the exported model can be used correctly in other keras codes.

Setup Details

Include the details about the versions of:

  • OS type and version: Linux (Ubuntu 16.04)
  • Python: 3.5.2
  • autokeras: 0.2.13
  • scikit-learn:
  • numpy: 1.14.5
  • keras: 2.2.2
  • scipy: 1.1.0
  • tensorflow: 1.10.1
  • pytorch: 0.4.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
AShyshkovcommented, Sep 12, 2018

You are we welcome! (I have to say, the fix was found not by me, but by my manager)

The code is exactly what we did. Just FYI, you can do this instead: keras_model.layers[:-1].activation = keras.activations.softmax

The result would be the same.

3reactions
AShyshkovcommented, Sep 12, 2018

Hey, I think we found the issue. Seem like a defect in AutoKeras. The last layer in the best model for MNIST classifier is a Dense layer with linear activation function. Training such model ends up with exploding gradients issue. Change it to softmax (or top it off with layer Activation(‘softmax’) ), retrain your model and you should be fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Save and load Keras models | TensorFlow Core
In order to save/load a model with custom-defined layers, or a subclassed model, you should overwrite the get_config and optionally from_config ...
Read more >
How to Save and Load Your Keras Deep Learning Model
You can save your model by calling the save() function on the model and specifying the filename. The example below demonstrates this by...
Read more >
Model saving & serialization APIs - Keras
Keras SavedModel uses tf.saved_model.save to save the model and all trackable objects attached to the model (e.g. layers and variables). The model config, ......
Read more >
How to save final model using keras? - python - Stack Overflow
In this case, we can simply save and load the model without re-compiling our model again. Note - This is the preferred way...
Read more >
How to export a TensorFlow 2.x Keras model to a frozen and ...
Recently, I struggled trying to export a model built with Keras and TensorFlow 2.x in the proper format to make inference with OpenCV's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found