1 Star 0 Fork 1

chenhui0518/flower_world

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test.py 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
confuse 提交于 2018-06-03 13:10 . init
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)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chenhui0518/flower_world.git
git@gitee.com:chenhui0518/flower_world.git
chenhui0518
flower_world
flower_world
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385