-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict2.py
More file actions
executable file
·53 lines (45 loc) · 1.18 KB
/
predict2.py
File metadata and controls
executable file
·53 lines (45 loc) · 1.18 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
#!/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 17:02:27
# Revision: none
# Compiler: gcc
#
# Author: zt ()
# Organization:
import tensorflow as tf
import random
import numpy as np
# import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.models import load_model
cfg = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
cfg.gpu_options.per_process_gpu_memory_fraction = 0.9
cfg.allow_soft_placement = True
sess = tf.compat.v1.InteractiveSession(config=cfg)
# 数据集
# 划分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('mnistmodel2.h5')
# 预测
x = x.reshape(1, 28, 28, 1)
predict = mymodel.predict(x)
# 取最大值的位置
predict = np.argmax(predict)
print('index', index)
print('original:', y)
print('predicted:', predict)