In [7]:
import numpy as np
np.random.seed(1337) # for reproducibility
import keras
from keras.datasets import mnist
from keras.utils import np_utils
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'gray'
%matplotlib inline
Inference in Keras is rather simple. One just calls the predict
method of the loaded model.
In [2]:
model = keras.models.load_model('example_keras_mnist_model.h5')
model.summary()
Loading the dataset and looking at the first five samples of the test data.
In [5]:
dataset = mnist.load_data()
train_data = dataset[0][0] / 255
train_data = train_data[..., np.newaxis].astype('float32')
train_labels = np_utils.to_categorical(dataset[0][1]).astype('float32')
test_data = dataset[1][0] / 255
test_data = test_data[..., np.newaxis].astype('float32')
test_labels = np_utils.to_categorical(dataset[1][1]).astype('float32')
test_data.shape
Out[5]:
In [10]:
for i in range(5):
plt.imshow(test_data[i, ..., 0])
plt.show()
Doing the inference
In [11]:
softmax_predictions = model.predict(test_data[:5])
softmax_predictions
Out[11]:
In [12]:
predictions = np.argmax(softmax_predictions, axis=-1)
predictions
Out[12]:
In [15]:
predictions == np.argmax(test_labels[:5], axis=1)
Out[15]: