微信公众号:龙跃十二
我是小玉,一个平平无奇的小天才!
悄悄说,github上出了源码详解还有思维导图,答辩PPT,赶快去star!
我的github:https://github.com/ixiaoyu-tech/xiaoyu-project
在前边,我们将训练好的模型存储在指定路径下,这个时候我们的项目也就要接近尾声了,接下来我们需要的就是导入一副图像,做最后的图像测试。
文件名: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
42def evaluate_one_image(): image_array=get_one_image_file("./data/test/68.jpg") #测试集路径,自己更改一下 with tf.Graph().as_default(): BATCH_SIZE = 1 # 获取一张图片 N_CLASSES = 2 #二分类 image = tf.cast(image_array, tf.float32) image = tf.image.per_image_standardization(image) image = tf.reshape(image, [1, 208, 208, 3]) #inference输入数据需要是4维数据,需要对image进行resize logit = inference(image, BATCH_SIZE, N_CLASSES) logit = tf.nn.softmax(logit) #inference的softmax层没有激活函数,这里增加激活函数 #因为只有一副图,数据量小,所以用placeholder x = tf.placeholder(tf.float32, shape=[208, 208, 3]) # # 训练模型路径 logs_train_dir = './model/' 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: print('This is a cat with possibility %.6f' %prediction[:, 0]) else: print('This is a dog with possibility %.6f' %prediction[:, 1]) def main(): run_training() evaluate_one_image() if __name__ == '__main__': main()
运行程序以后我们就可以看到,在加载进来一副图像时,用我们训练好的模型对图像进行识别,就可以得到这副图像是猫还是狗,也可以得到一个预测的概率。
在这个问题中,我们经常为了方便直观的看到数据的变化趋势,可以使用tensorboard进行可视化处理,这里我就不在赘述。
猫狗大战这个经典的分类问题就跟大家分享完了,如果你觉着对你很有帮助,点个赞再走好吗?
我是小玉,一个平平无奇的小天才,等你点赞哦!
最后
以上就是美好帅哥最近收集整理的关于猫狗大战——基于TensorFlow的猫狗识别(5)的全部内容,更多相关猫狗大战——基于TensorFlow内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复