-
Notifications
You must be signed in to change notification settings - Fork 178
/
test.py
76 lines (60 loc) · 2.47 KB
/
test.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from PIL import Image
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import model
from input_data import get_files
# 获取一张图片
def get_one_image(train):
# 输入参数:train,训练图片的路径
# 返回参数:image,从训练图片中随机抽取一张图片
n = len(train)
ind = np.random.randint(0, n)
img_dir = train[ind] # 随机选择测试的图片
img = Image.open(img_dir)
plt.imshow(img)
plt.show()
image = np.array(img)
return image
# 测试图片
def evaluate_one_image(image_array):
with tf.Graph().as_default():
BATCH_SIZE = 1
N_CLASSES = 4
image = tf.cast(image_array, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1, 64, 64, 3])
logit = model.inference(image, BATCH_SIZE, N_CLASSES)
logit = tf.nn.softmax(logit)
x = tf.placeholder(tf.float32, shape=[64, 64, 3])
# you need to change the directories to yours.
logs_train_dir = 'D:/ML/flower/save/'
saver = tf.train.Saver()
with tf.Session() as sess:
print("Reading checkpoints...")
ckpt = tf.train.get_checkpoint_state(logs_train_dir)
if ckpt and ckpt.model_checkpoint_path:
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
saver.restore(sess, ckpt.model_checkpoint_path)
print('Loading success, global_step is %s' % global_step)
else:
print('No checkpoint file found')
prediction = sess.run(logit, feed_dict={x: image_array})
max_index = np.argmax(prediction)
if max_index == 0:
result = ('这是玫瑰花的可能性为: %.6f' % prediction[:, 0])
elif max_index == 1:
result = ('这是郁金香的可能性为: %.6f' % prediction[:, 1])
elif max_index == 2:
result = ('这是蒲公英的可能性为: %.6f' % prediction[:, 2])
else:
result = ('这是这是向日葵的可能性为: %.6f' % prediction[:, 3])
return result
# ------------------------------------------------------------------------
if __name__ == '__main__':
img = Image.open('D:/ML/flower/flower_photos/roses/12240303_80d87f77a3_n.jpg')
plt.imshow(img)
plt.show()
imag = img.resize([64, 64])
image = np.array(imag)
evaluate_one_image(image)