博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Inception v3进行图像分类
阅读量:3897 次
发布时间:2019-05-23

本文共 1981 字,大约阅读时间需要 6 分钟。

Google Inception模型简介

Inception为Google开源的CNN模型,至今已经公开四个版本,每一个版本都是基于大型图像数据库ImageNet中的数据训练而成。因此我们可以直接利用Google的Inception模型来实现图像分类。本篇文章主要以Inception_v3模型为基础。Inception v3模型大约有2500万个参数,分类一张图像就用了50亿的乘加指令。在一台没有GPU的现代PC上,分类一张图像转眼就能完成。

已经训练好的Inception_v3模型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import tensorflow as tfimport numpy as npuid_to_human = {}for line in tf.gfile.GFile('imagenet_synset_to_human_label_map.txt').readlines():	items = line.strip().split('\t')	uid_to_human[items[0]] = items[1]node_id_to_uid = {}for line in tf.gfile.GFile('imagenet_2012_challenge_label_map_proto.pbtxt').readlines():	if line.startswith('  target_class:'):		target_class = int(line.split(': ')[1])	if line.startswith('  target_class_string:'):		target_class_string = line.split(': ')[1].strip('\n').strip('\"')		node_id_to_uid[target_class] = target_class_stringnode_id_to_name = {}for key, value in node_id_to_uid.items():	node_id_to_name[key] = uid_to_human[value]def create_graph():	with tf.gfile.FastGFile('classify_image_graph_def.pb', 'rb') as f:		graph_def = tf.GraphDef()		graph_def.ParseFromString(f.read())		_ = tf.import_graph_def(graph_def, name='')def classify_image(image, top_k=1):	image_data = tf.gfile.FastGFile(image, 'rb').read()	create_graph()	with tf.Session() as sess:		# 'softmax:0': A tensor containing the normalized prediction across 1000 labels		# 'pool_3:0': A tensor containing the next-to-last layer containing 2048 float description of the image		# 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG encoding of the image		softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')		predictions = sess.run(softmax_tensor, feed_dict={'DecodeJpeg/contents:0': image_data})		predictions = np.squeeze(predictions)		top_k = predictions.argsort()[-top_k:]		for node_id in top_k:			human_string = node_id_to_name[node_id]			score = predictions[node_id]			print('%s (score = %.5f)' % (human_string, score))classify_image('test2.png')

test2.png如下:

在这里插入图片描述
运行结果如下:
Pekinese, Pekingese, Peke (score = 0.90348)

转载地址:http://dmyen.baihongyu.com/

你可能感兴趣的文章
sass的安装
查看>>
Vue-cli中路由配置
查看>>
豆瓣高分JAVA书籍,你都读过吗?
查看>>
java图书管理系统
查看>>
C#图书管理系统
查看>>
C#酒店管理系统
查看>>
你对ArrayList了解多少?
查看>>
《从Paxos到ZooKeeper分布式一致性原理与实践》学习知识导图
查看>>
Java基础面试题(一) (2020持续更新)
查看>>
JAVA人事管理系统
查看>>
Dubbo面试题(关注小R持续更新)
查看>>
JAVA仿微博系统(JAVA毕业设计含源码和运行教程)
查看>>
24BITBMP位图的文件结构及创建
查看>>
如何在自定义控件中获得width和height?
查看>>
Android UI开发专题之界面设计【基础API】
查看>>
ejarmaker: jar 、java类的加密工具
查看>>
配置NFS实现Linux服务器之间的文件共享
查看>>
PostgreSQL连接池pgbouncer的使用
查看>>
Kryo序列化进阶学习: 加密数据
查看>>
swift 3.0 数组赋值
查看>>