-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
executable file
·49 lines (41 loc) · 1016 Bytes
/
predict.py
File metadata and controls
executable file
·49 lines (41 loc) · 1016 Bytes
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# FileName: predict.py
#
# Description:
#
# Version: 1.0
# Created: 2019-10-08 16:50:42
# Last Modified: 2019-10-09 11:37:25
# Revision: none
# Compiler: gcc
#
# Author: zt ()
# Organization:
import random
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.models import load_model
# 数据集
# 划分MNIST训练集、测试集
(_, _), (X_test, y_test) = mnist.load_data()
# 随机数
index = random.randint(0, X_test.shape[0])
x = X_test[index]
y = y_test[index]
# 显示该数字
plt.imshow(x, cmap='gray_r')
plt.title("original {}".format(y))
plt.show()
# 加载
mymodel = load_model('mnistmodel.h5')
# 预测
x.shape = (1, 784) # 变成[[]]
# x = x.flatten()[None] # 也可以用这个
predict = mymodel.predict(x)
# print(predict)
predict = np.argmax(predict) # 取最大值的位置
print('index', index)
print('original:', y)
print('predicted:', predict)