Artificial intelligence (AI) works on prodigious amounts of data feeds to achieve cognitive abilities. Rich machine learning libraries such as Keras and TensorFlow are contributing to the dynamic artificial intelligence services. Keras and Tensorflow together support model training to build image recognition, deep video analytics, brand monitoring, facial gesture recognition, and other machine learning models.
This post highlights some common operations that you would frequently need in the Keras. First, we will understand how to save the models and use them for prediction later. Also, this post explains how to display the images from a dataset and load images from our systems to predict the class.
Training and Saving the Model
Training the models is a very slow process, nobody wants to do that every time. Fortunately, we only need to train the model once, save it and then we can load it anytime and use it to predict the new images. Keras saves the models in the .h5 format.
We loaded the parameters of the model from the saved model file and evaluated that function runs prediction over test dataset and returns accuracy of our predictions.
So far, we have demonstrated how to save the models and use them later for prediction. However, this is a comparatively easy and common task. The main task is being able to load a specific image and determine what class it belongs to.
img = test_x
test_img = img.reshape((1,784))
img_class = model.predict_classes(test_img)
prediction = img_class
classname = img_class
Here we just pick a random image. In this case at index 130 from the test set, we create the flatten copy that is reshaped.
Now that we have a prediction, we use Matplotlib to display the image and its predicted class.