【7-4使用inception-v3做各种图像的识别】
参考程序:
1 import tensorflow as tf 2 import os 3 import numpy as np 4 import re 5 from PIL import Image 6 import matplotlib.pyplot as plt 7 8 class NodeLookup(object): 9 def __init__(self): 10 label_lookup_path = 'inception_model/imagenet_2012_challenge_label_map_proto.pbtxt' 11 uid_lookup_path = 'inception_model/imagenet_synset_to_human_label_map.txt' 12 self.node_lookup = self.load(label_lookup_path, uid_lookup_path) 13 14 def load(self, label_lookup_path, uid_lookup_path): 15 # 加载分类字符串n********对应分类名称的文件 16 proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines() 17 uid_to_human = {} 18 #一行一行读取数据 19 for line in proto_as_ascii_lines : 20 #去掉换行符 21 line=line.strip('\n') 22 #按照'\t'分割 23 parsed_items = line.split('\t') 24 #获取分类编号 25 uid = parsed_items[0] 26 #获取分类名称 27 human_string = parsed_items[1] 28 #保存编号字符串n********与分类名称映射关系 29 uid_to_human[uid] = human_string 30 31 # 加载分类字符串n********对应分类编号1-1000的文件 32 proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines() 33 node_id_to_uid = {} 34 for line in proto_as_ascii: 35 if line.startswith(' target_class:'): 36 #获取分类编号1-1000 37 target_class = int(line.split(': ')[1]) 38 if line.startswith(' target_class_string:'): 39 #获取编号字符串n******** 40 target_class_string = line.split(': ')[1] 41 #保存分类编号1-1000与编号字符串n********映射关系 42 node_id_to_uid[target_class] = target_class_string[1:-2] #要将两侧的双引号去掉 43 44 #建立分类编号1-1000对应分类名称的映射关系 45 node_id_to_name = {} 46 for key, val in node_id_to_uid.items(): 47 #获取分类名称 48 name = uid_to_human[val] 49 #建立分类编号1-1000到分类名称的映射关系 50 node_id_to_name[key] = name 51 return node_id_to_name 52 53 #传入分类编号1-1000返回分类名称 54 def id_to_string(self, node_id): 55 if node_id not in self.node_lookup: 56 return '' 57 return self.node_lookup[node_id] 58 59 60 #创建一个图来存放google训练好的模型 61 with tf.gfile.FastGFile('inception_model/classify_image_graph_def.pb', 'rb') as f: 62 graph_def = tf.GraphDef() 63 graph_def.ParseFromString(f.read()) 64 tf.import_graph_def(graph_def, name='') 65 66 67 with tf.Session() as sess: 68 softmax_tensor = sess.graph.get_tensor_by_name('softmax:0') 69 #遍历目录 70 for root,dirs,files in os.walk('images/'): 71 for file in files: 72 #载入图片 73 image_data = tf.gfile.FastGFile(os.path.join(root,file), 'rb').read() 74 predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})#图片格式是jpg格式 75 predictions = np.squeeze(predictions)#把结果转为1维数据 76 77 #打印图片路径及名称 78 image_path = os.path.join(root,file) 79 print(image_path) 80 #显示图片 81 img=Image.open(image_path) 82 plt.imshow(img) 83 plt.axis('off') 84 plt.show() 85 86 #排序 87 top_k = predictions.argsort()[-5:][::-1] 88 node_lookup = NodeLookup() 89 for node_id in top_k: 90 #获取分类名称 91 human_string = node_lookup.id_to_string(node_id) 92 #获取该分类的置信度 93 score = predictions[node_id] 94 print('%s (score = %.5f)' % (human_string, score)) 95 print()
在inception_model中有这2个文件:
分别长这样:
分类共有1000种结果以及所对应得字符串:
上述字符串及所对应的描述:
程序首先读入这2个文件:
proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines() uid_to_human = {} #一行一行读取数据for line in proto_as_ascii_lines :#去掉换行符line=line.strip('\n')#按照'\t'分割parsed_items = line.split('\t')#获取分类编号uid = parsed_items[0]#获取分类名称human_string = parsed_items[1]#保存编号字符串n********与分类名称映射关系uid_to_human[uid] = human_string
从 uid_lookup_path 中读取的结果存放在 proto_as_ascii_lines中,创建了一个空字典 uid_to_human 用来存储键值对,一行一行的读取数据,并将换行符去掉,以Tab键作为分割,
parsed_items[0]代表分类字符串n********(Tab键之前的内容),parsed_items[1]对应分类的名称(Tab键之后的内容),作为键值对存入字典。
# 加载分类字符串n********对应分类编号1-1000的文件proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()node_id_to_uid = {}for line in proto_as_ascii:if line.startswith(' target_class:'):#获取分类编号1-1000target_class = int(line.split(': ')[1])if line.startswith(' target_class_string:'):#获取编号字符串n********target_class_string = line.split(': ')[1]#保存分类编号1-1000与编号字符串n********映射关系node_id_to_uid[target_class] = target_class_string[1:-2]
程序同理,以: 分割,获取分类编号以及编号字符串n********,作为键值对存入字典,target_class_string[1:-2]是去掉字符串中的2个双引号:
转载于:https://www.cnblogs.com/direwolf22/p/11055235.html
【7-4使用inception-v3做各种图像的识别】相关推荐
- matlab能做深度图像的识别吗,Matlab图像识别/检索系列(6)-10行代码完成深度学习网络之基于CNN的图像分类...
在Matlab2017中,完成一个使用CNN网络进行分类的示例非常简单.为了便于创建图像集,Matlab2015引入了ImageDatastore对象,实现函数为imageDatastore,该函数可 ...
- Tensorflow— 使用inception-v3做各种图像的识别
代码: import tensorflow as tf import os import numpy as np import re from PIL import Image import matp ...
- 【TensorFlow系列】【五】利用inception v3 pb模型文件做预测
2019独角兽企业重金招聘Python工程师标准>>> 本文介绍如何利用imagenet比赛上训练好的inception v3冻结的pb模型进行inference. 1.下载ince ...
- 1、VGG16 2、VGG19 3、ResNet50 4、Inception V3 5、Xception介绍——迁移学习
ResNet, AlexNet, VGG, Inception: 理解各种各样的CNN架构 本文翻译自ResNet, AlexNet, VGG, Inception: Understanding va ...
- 【Tensorflow】深度学习实战05——Tensorflow实现Inception V3
[fishing-pan:https://blog.csdn.net/u013921430转载请注明出处] 前言 前些日子在忙其他的事情,一直没有更新自己学习神经网络的博客,就在端午这天更吧!也祝大家 ...
- 迁移学习-使用预训练的Inception v3进行宠物分类
个人博客:http://www.chenjianqu.com/ 原文链接:http://www.chenjianqu.com/show-53.html 迁移学习 迁移学习(Transfer Learn ...
- Inception v3 论文笔记
Inception v3:Rethinking the Inception Architecture for Computer Vision 摘要: \quad \;卷积网络是大多数计算机视觉任务的 ...
- 深度学习卷积神经网络——经典网络GoogLeNet(Inception V3)网络的搭建与实现
一.Inception网络(google公司)--GoogLeNet网络的综述 获得高质量模型最保险的做法就是增加模型的深度(层数)或者是其宽度(层核或者神经元数), 但是这里一般设计思路的情况下会出 ...
- 脸型分类-Face shape classification using Inception v3
函数解析 github 代码:https://github.com/adonistio/inception-face-shape-classifier CLASSIFY_FACE.py 用于运行训练好 ...
最新文章
- 安装完成后在命令行运行bash时报错0x80070057
- 【转】日服巫术online过驱动保护分析(纯工具)(工具+自写驱动)
- 百度富文本编辑器的应用技巧---在一个页面中使用多个样式不同功能不同的编辑器...
- ROS笔记(8) 服务通信
- 怎么监听Android软键盘的打开和关闭
- python面板数据分析代码_用python预处理面板数据(续)
- 安装Lua For Windows
- mysql创建数据库sql语句_创建数据库的SQL语句:mysql数据库
- JSP Webshell 免杀
- 当代移动通信发展四个阶段
- 【实习周报】2019年5月 前端开发实习工作周报汇总
- 创新专题一:省份层面(创新效率、创新能力、投入产出、高质量发展等)
- 前置:API:DSP:核心交换机:边界网关协议:边界:(防御)防火墙:负载均衡:摆渡机:名词解释
- Android App Dark Theme(暗黑模式)适配指南
- JAVA上加密算法的实现用例(转)
- 【技术分享】链路聚合
- http请求从浏览器到tomcat过程
- Git必学的merge和rebase区别
- 【机器学习】特征编码OneHot与Dummy的区别与联系
- 又一年神仙打架,清华大学特等奖学金答辩会上15位大神现场竞争!
热门文章
- .net 读取xml文件
- .net 中应用 ValidationAttribute
- 谈论 NOD32: 教育网超级 病毒更新服务器: http://222.197.166.33/main.htm
- 首次运行 tensorflow 项目之 vgg 网络
- Django 静态文件处理
- 应力偏张量的物理意义_物理学中张量的由来
- return view前端怎么获取_Web 前端路由原理解析和功能实现
- 查看计算机80端口,电脑win10 80端口被占用的检测和解决方法
- wxpython窗口跳转_WxPython-用按钮打开一个新窗口
- ChaiNext:过去24小时,市场情绪“燥”