简体   繁体   中英

How to save and use a trained model in keras and python

we want to save a trained model that we can use it in an application. So the problem is that if we load the model, the accuracy is pretty low. see the code below.

To save the model:

    model.save("TrainedModel\\emotionDetector.h5")

To load the model:

    model = models.load_model("TrainedModel\\emotionDetector.h5")
    loss, acc = model.evaluate(X,  y, verbose=1)
    print('Restored model, accuracy: {:5.2f}%'.format(100*acc), loss)

Accuracy and loss before saving: loss: 0.2324 - accuracy: 0.9202 - val_loss: 1.3789 - val_accuracy: 0.6353

Accuracy and loss after loading: Restored model, accuracy: 36.92% Loss: nan

So we know, that there is a different between model.fit() and model.evaluate(). How can we improve this result of model.evaluate or do we have to train the model again after starting the application?

Please help us! Thanks in advance.

Depending on the Architecture of the model, some layers might not be active during evaluation. Such layers could be Dropout or BatchNormalization. Due to this results could be different.

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