python深度学习tensorflow训练好的模型进行图像分类
作者:denny402 发布时间:2023-02-20 20:40:37
标签:python,深度学习,tensorflow,训练模型,图像分类
谷歌在大型图像数据库ImageNet上训练好了一个Inception-v3模型,这个模型我们可以直接用来进来图像分类。
下载链接: https://pan.baidu.com/s/1XGfwYer5pIEDkpM3nM6o2A
提取码: hu66
下载完解压后,得到几个文件:
其中
classify_image_graph_def.pb 文件就是训练好的Inception-v3模型。
imagenet_synset_to_human_label_map.txt是类别文件。
随机找一张图片
对这张图片进行识别,看它属于什么类?
代码如下:先创建一个类NodeLookup来将softmax概率值映射到标签上。
然后创建一个函数create_graph()来读取模型。
读取图片进行分类识别
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
import re
import os
model_dir='D:/tf/model/'
image='d:/cat.jpg'
#将类别ID转换为人类易读的标签
class NodeLookup(object):
def __init__(self,
label_lookup_path=None,
uid_lookup_path=None):
if not label_lookup_path:
label_lookup_path = os.path.join(
model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')
if not uid_lookup_path:
uid_lookup_path = os.path.join(
model_dir, 'imagenet_synset_to_human_label_map.txt')
self.node_lookup = self.load(label_lookup_path, uid_lookup_path)
def load(self, label_lookup_path, uid_lookup_path):
if not tf.gfile.Exists(uid_lookup_path):
tf.logging.fatal('File does not exist %s', uid_lookup_path)
if not tf.gfile.Exists(label_lookup_path):
tf.logging.fatal('File does not exist %s', label_lookup_path)
# Loads mapping from string UID to human-readable string
proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
uid_to_human = {}
p = re.compile(r'[n\d]*[ \S,]*')
for line in proto_as_ascii_lines:
parsed_items = p.findall(line)
uid = parsed_items[0]
human_string = parsed_items[2]
uid_to_human[uid] = human_string
# Loads mapping from string UID to integer node ID.
node_id_to_uid = {}
proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
for line in proto_as_ascii:
if line.startswith(' target_class:'):
target_class = int(line.split(': ')[1])
if line.startswith(' target_class_string:'):
target_class_string = line.split(': ')[1]
node_id_to_uid[target_class] = target_class_string[1:-2]
# Loads the final mapping of integer node ID to human-readable string
node_id_to_name = {}
for key, val in node_id_to_uid.items():
if val not in uid_to_human:
tf.logging.fatal('Failed to locate: %s', val)
name = uid_to_human[val]
node_id_to_name[key] = name
return node_id_to_name
def id_to_string(self, node_id):
if node_id not in self.node_lookup:
return ''
return self.node_lookup[node_id]
#读取训练好的Inception-v3模型来创建graph
def create_graph():
with tf.gfile.FastGFile(os.path.join(
model_dir, 'classify_image_graph_def.pb'), 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
#读取图片
image_data = tf.gfile.FastGFile(image, 'rb').read()
#创建graph
create_graph()
sess=tf.Session()
#Inception-v3模型的最后一层softmax的输出
softmax_tensor= sess.graph.get_tensor_by_name('softmax:0')
#输入图像数据,得到softmax概率值(一个shape=(1,1008)的向量)
predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})
#(1,1008)->(1008,)
predictions = np.squeeze(predictions)
# ID --> English string label.
node_lookup = NodeLookup()
#取出前5个概率最大的值(top-5)
top_5 = predictions.argsort()[-5:][::-1]
for node_id in top_5:
human_string = node_lookup.id_to_string(node_id)
score = predictions[node_id]
print('%s (score = %.5f)' % (human_string, score))
sess.close()
最后输出
tiger cat (score = 0.40316)
Egyptian cat (score = 0.21686)
tabby, tabby cat (score = 0.21348)
lynx, catamount (score = 0.01403)
Persian cat (score = 0.00394)
来源:https://www.cnblogs.com/denny402/p/6942580.html


猜你喜欢
- 在上一讲中已经连接了数据库。就数据库而言,连接之后就要对其操作。但是,目前那个名字叫做qiwsirtest的数据仅仅是空架子,没有什么可操作
- 一、同一台电脑需要安装2个MYSQL,以mysql-5.7.39和mysql-8.0.30为例; 1.下载:https://dow
- Javascript 数组的工作方式与大多数编程语言的数组类似。<!DOCTYPE html><html lang=&qu
- 在SQL Server日常的函数、存储过程和SQL语句中,经常会用到不同数据类型的转换。在SQL Server有两种数据转换类型:一种是显性
- 本文主要研究的是python将字典内容存入mysql,分享了实现代码,具体介绍如下。1.背景项目需要,用python实现了将字典内容存入本地
- 我们都知道代码都是顺序执行的,也就是先执行第1条语句,然后是第2条、第3条……一直到最后一条语句
- 记得导入包,其他按键可类比def keyPressEvent(self, event): if event.key() == Q
- rss的优点 1.您可以有选择地浏览您感兴趣的以及与您的工作相关的新闻。 2.您可以把需要的信息从不需要的信息(兜售信息,垃圾邮件等)中分离
- 在我们武汉的一个项目中,用户提供的数据库服务器有16G左右的内存,但我们只能使用8G多的内存,为了提高内存的得用率,特意参考了一些资料,得出
- 引言通过一张照片居然发现女友在宿舍里没去上课!强大的照片位置信息获取,快来一起学习吧!一、exifread函数库要怎样获得拍摄图片的GPS呢
- 如何编写一个只在Web服务关闭时执行的程序?如:<SCRIPT LANGUAGE="VBScript"&
- 0. 学习目标栈和队列是在程序设计中常见的数据类型,从数据结构的角度来讲,栈和队列也是线性表,是操作受限的线性表,它们的基本操作是线性表操作
- 前言在实际的生产环境中,如果对MySQL数据库的读和写都在一台数据库服务中操作,无论在安全性、高可用性,还是高并发性等各个方面都是完全不能满
- 本文实例讲述了redis数据库及与python交互用法。分享给大家供大家参考,具体如下:redis数据操作1.string类型:主要存储字符
- 前言大家好,我叫善念。这是我的第二篇博客,也是第一篇技术博客,希望大家多多支持,让我更加有动力去更新一些python爬虫类的案例教程。开始确
- 本文实例为大家分享了python绘制直方图的具体代码,供大家参考,具体内容如下运行结果如下代码如下from matplotlib impor
- 这篇文章主要介绍了微信小程序wxml列表渲染原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 一、含有一个装饰器#encoding: utf-8############含有一个装饰器#########def outer(func):
- 此文刊登在《程序员》三月期,有删改提到安全问题,首先想到应付这些问题的应该是系统管理员以及后台开发工程师们,而前端开发工程师似乎离这些问题很
- 昨天晚上才发现已经出了jQuery的1.3版本,于是下载下来,把原来一个兄弟翻译的1.2.6的文档移植到了1.3中,点击这里可