Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a Keras trained model saved in a HDF5 file to make predictions?

I recently got started with neural networks. I built a handwritten character prediction model using the extended MNIST dataset, sklearn, Keras, numpy and pandas. The main goal is to take and/or upload pictures of handwritten text/characters and the model should be able to guess.

After finishing the training phase, the model was saved in the file my_model.h5. At this point, how could I use this trained model (specifically, the my_model.h5) in a Python program that receives as input images and should produce a prediction?

like image 638
Thorvald Avatar asked Mar 06 '20 18:03

Thorvald


People also ask

How do you connect model input data with predictions for machine learning?

To give inputs to a machine learning model, you have to create a NumPy array, where you have to input the values of the features you used to train your machine learning model. Then we can use that array in the model. predict() method, and at the end, it will give the predicted value as an output based on the inputs.


1 Answers

There are different ways to load a (trained) model from a file in Keras and TensorFlow.

The Keras documentation provides a snippet that shows how to load a model.

from keras.models import load_model

# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

After having loaded the model, you can use model.predict.

You integrate this code into your application as you wish.

like image 94
nbro Avatar answered Oct 18 '22 09:10

nbro