You can take the train keras model and apply it to new data and the model will be able to generalize and accurately predict data that it’s not seen before.
So say that you had a model that was able to classify images of cats and dogs. So it was able to label whether or not an image of a cat or dog.
In this tutorial, we’ll be demonstrating how to predict an image on a trained keras model. So our goal has been to build a CNN that can identify whether a given image is an image of a cat or an image of a dog and save the model as an HDF5 file. So at a later point, you might want to take this model and give it your own images of cats and dogs and you want to see if it’s able to accurately label those images as the respective true labels of cats or dogs.
Get an example dataset
import os import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from keras.models import load_model from keras.preprocessing import image import tensorflow_datasets as tfds tfds.disable_progress_bar() SPLIT_WEIGHTS = (8, 1, 1) splits = tfds.Split.TRAIN.subsplit(weighted=SPLIT_WEIGHTS) (raw_train, raw_validation, raw_test), metadata = tfds.load( 'cats_vs_dogs', split=list(splits), with_info=True, as_supervised=True) IMG_SIZE = 160 # All images will be resized to 160x160 def format_example(image, label): image = tf.cast(image, tf.float32) image = (image/127.5) - 1 image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE)) return image, label train = raw_train.map(format_example) validation = raw_validation.map(format_example) test = raw_test.map(format_example) BATCH_SIZE = 32 SHUFFLE_BUFFER_SIZE = 1000 train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE) validation_batches = validation.batch(BATCH_SIZE) test_batches = test.batch(BATCH_SIZE)
Define a model
Let’s build a model we’ll use to demonstrate the saving and loading model.
model = tf.keras.Sequential([ tf.keras.layers.Conv2D(kernel_size=3, filters=16, padding='same', activation='relu', input_shape=[IMG_SIZE,IMG_SIZE, 3]), tf.keras.layers.Conv2D(kernel_size=3, filters=30, padding='same', activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=2), tf.keras.layers.Conv2D(kernel_size=3, filters=60, padding='same', activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=2), tf.keras.layers.Conv2D(kernel_size=3, filters=90, padding='same', activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=2), tf.keras.layers.Conv2D(kernel_size=3, filters=110, padding='same', activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=2), tf.keras.layers.Conv2D(kernel_size=3, filters=130, padding='same', activation='relu'), tf.keras.layers.Conv2D(kernel_size=1, filters=40, padding='same', activation='relu'), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(1,'sigmoid') ]) model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])
Let’s train this model, just so it has weight values to save, as well as an optimizer state. You can save models you’ve never trained, too, but obviously, that’s less interesting.
history = model.fit(train_batches, epochs=5, validation_data=validation_batches)
You can pass a Dataset instance directly to the methods
Evaluate Mode After Training
We’ve trained our model on training samples. We evaluate our model using test data and given the results. It looks like this model should do well on predictions.
Save Trained Model As an HDF5 file
Keras provides a basic save format using the HDF5 standard. The saved model can be treated as a single binary blob.
Keras allows you to export a model and optimizer into a file so it can be used without access to the original Python code. It contains weights, variables, and model configuration. Since the optimizer state is recovered you can even resume training from exactly where you left off.
You can use
model.save(filepath) to save a Keras model into a single HDF5 file which will contain:
- the weights of the model.
- the training configuration (loss, optimizer)
- the state of the optimizer, allowing you to resume training exactly where you left off.
In your Python code probable the should be:
This allows you to save the entirety of the state of a model in a single file.
Load Trained Keras Model
Saved models can be re-instantiated via
loaded_model = tf.keras.models.load_model('dog_cat_model.h5') loaded_model.layers.input_shape #(None, 160, 160, 3)
You should run
model.summary() to see what the expected dimensions of the input are.
The model returned by
load_model() is a compiled model ready to be used (unless the saved model was never compiled in the first place).
Predict on Trained Keras Model
So first we need some new data as our test data that we’re going to use for predictions. New data that the model will be predicting on is typically called the test set.
batch_holder = np.zeros((20, IMG_SIZE, IMG_SIZE, 3)) img_dir='test_set/' for i,img in enumerate(os.listdir(img_dir)): img = image.load_img(os.path.join(img_dir,img), target_size=(IMG_SIZE,IMG_SIZE)) batch_holder[i, :] = img
In keras to predict all you do is call the predict function on your model. So I am calling
model.predict_classes and I’m passing in test samples.
result=loaded_model.predict_classes(batch_holder) fig = plt.figure(figsize=(20, 20)) for i,img in enumerate(batch_holder): fig.add_subplot(4,5, i+1) plt.title(get_label_name(result[i])) plt.imshow(img/256.) plt.show()
Predict Single Image
When predicting a single image, you have to reshape image even if you have only one image. Your input should be of shape:
[1, image_width, image_height, number_of_channels].
image_path="test_set/cat2.png" img = image.load_img(image_path, target_size=(IMG_SIZE, IMG_SIZE)) plt.imshow(img) img = np.expand_dims(img, axis=0) result=loaded_model.predict_classes(img) plt.title(get_label_name(result)) plt.show()
So that is how you can use Keras’s to make predictions on data that it wasn’t trained on.