-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-model.py
More file actions
61 lines (47 loc) · 1.81 KB
/
test-model.py
File metadata and controls
61 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Author: Ryan McCormick
File : test-model.py
Purpose: To test a trained convolutional neural network and
provide visuals with matplotlib to see the success.
"""
import random
import numpy as np
from matplotlib import pylab as plt
from keras.datasets import mnist
from keras.preprocessing import image
from keras.utils import to_categorical
from keras.models import model_from_yaml
# Define number of classes
num_classes = 10
# Input (image) dimensions
rows, cols = 28, 28
# mnist.load_data() returns 2 tuples split into training/testing
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Display image to test
image_num = random.randint(0, 10000)
plt.imshow(x_test[image_num], cmap=plt.get_cmap('gray'))
plt.show()
# If color channels are last parameter
x_test = x_test.reshape(x_test.shape[0], rows, cols, 1).astype('float32')
y_test = to_categorical(y_test, num_classes)
# Normalize pixel values between 0 and 1 per channel
x_test /= 255
# Get random test image
test_image = x_test[image_num,:,:,:]
test_image = np.expand_dims(test_image, axis=0)
# Get corresponding test label for random image
test_label = y_test[image_num]
test_label = to_categorical(test_label, num_classes)
# ----------------------------------------------------
with open('model.yaml', 'r') as yaml_file:
loaded_yaml_model = yaml_file.read()
# Create model from yaml config
loaded_model = model_from_yaml(loaded_yaml_model)
# Load weights from trained network
loaded_model.load_weights('model_weights.h5')
# Had to add this line with newest Keras version
loaded_model.compile(loss='categorical_crossentropy',
optimizer='sgd')
print("Loaded model and weights from disk")
prediction = loaded_model.predict(test_image, batch_size=1, verbose=1)
print('Prediction: {} with {:.2f}% confidence.'.format(np.argmax(prediction), np.max(prediction)*100))