-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnistModel.py
More file actions
62 lines (51 loc) · 1.77 KB
/
mnistModel.py
File metadata and controls
62 lines (51 loc) · 1.77 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
62
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
models = [keras.models.load_model("mnistCNNmodel2.h5")]
# model1=keras.models.load_model("mnistCNNmodel2.h5")
model1=models[0]
def predict_image(x, model): #takes in 3D Numpy array that represents an image
x = x.astype('float32')
x/=255.0 #converts to 0-1 float scale
x = np.expand_dims(x, axis=0)
image_predict = model.predict(x, verbose=0)
# plt.imshow(np.squeeze(x))
# plt.xticks([])
# plt.yticks([])
# plt.show()
print("Predicted Label: ", np.argmax(image_predict))
return image_predict
def plot_value_array(predictions_array, true_label):
plt.grid(False)
plt.xticks(range(10))
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array[0], color="#777777")
plt.ylim([-1, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
plt.show()
def eval(path):
# img=image.load_img(path,target_size=(28,28),color_mode="grayscale")
# img_arr=image.img_to_array(img)
# img=tf.keras.utils.load_img(path,target_size=(28,28),color_mode="grayscale")
img=tf.keras.utils.load_img(path,target_size=(28, 28),color_mode="grayscale")
img.show()
img_arr=tf.keras.utils.img_to_array(img)
arr=predict_image(img_arr, model1)
plot_value_array(arr,3)
def predictImages(x, models):
list = []
for m in models:
list.append(predict_image(x, m))
return list
def evalAvg(path):
img=tf.keras.utils.load_img(path,target_size=(28, 28),color_mode="grayscale")
img.show()
img_arr=tf.keras.utils.img_to_array(img)
arrs=predictImages(img_arr, models)
arr=np.mean(np.array(arrs), axis=0)
plot_value_array(arr, 3)