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
0
投稿
猜你喜欢
- Django测试框架非常简单,首选方法是使用python标准库中的unittest模块。Writing testsDjango的单元测试使用
- 什么是设计模式设计模式是针对软件开发中出现的常见问题的可重用解决方案。它们并不特定于任何编程语言或框架,而是描述了可应用于各种情况的一般原则
- 在工作中,我们经常需要从命令行当中解析出指定的参数,而 Python 也提供了相应的标准库来做这件事情,比如 sys, optparse,
- 有很多种方法来实现图片的预加载,通常大部分使用Javascript让事情滚动。不要再受Javascript预载的束缚了吧,用CSS你就可以毫
- 本文实例讲述了Python通过for循环理解迭代器和生成器。分享给大家供大家参考,具体如下:迭代器可迭代对象通过 for…in… 循环依次拿
- 通常我们做统计图的时候需要借助组件来完成例如mschart,aspchart等但是这个类不需要任何组件,而且使用方便,本站测试可用:clsG
- 直接调用系统的颜色显示在网页上本来是件很好玩滴事,但是,也有个缺点,就是可用的色太少 比如Bindows在它的启动画面一点点应用。=。= 上
- 不知道从什么时候开始,在网络上到处可以看到div+css,到底什么是div+css呢?难道就是传说中的标准重构吗?标准从最简单的根源来说不是
- 前言 简述bs4:使用pip install beautifulsoup4将bs4包安装到当前的python解释器环境,使用fro
- 这里说的“相对路径”是相对于“主调文件”所在的文件夹。#include file #include file后面跟的是文件的“相对路径”,不
- 今天介绍一下 go语言的并发机制以及它所使用的CSP并发模型CSP并发模型CSP模型是上个世纪七十年代提出的,用于描述两个独立的并发实体通过
- Python中对信号处理的模块主要是使用signal模块,但signal主要是针对Unix系统,所以在Windows平台上Python不能很
- aspImage是ServerObjects站点上非常好的一个组件,它可以使我们利用Asp实现很多对于图形的处理功能,他的功能强大,如果你需
- 开发环境:python 3.6django 1.11场景一经常有从数据库中获取一批数据,然后在前端以列表的形式展现,比如:获取到所有的用户,
- python pycharm中使用opencv时,没有代码自动补全提示解决方案有2种今天工作时突然发现,在写OPENCV相关代码时,没有自动
- 如何使整个页面内容居中,如何使高度适应内容自动伸缩。这是学习CSS布局最常见的问题。下面就给出一个实际的例子,并详细解释。(本文的经验和是蓝
- 本文实例为大家分享了JSP学生信息管理系统源码,供大家参考,具体内容如下新建学生信息数据库1.添加记录模块<%@ page conte
- 大家都知道,Matplotlib 是众多 Python 可视化包的鼻祖,也是Python最常用的标准可视化库,其功能非常强大,同时也非常复杂
- 1.关系模型:用二维表格结构表示实体集,外键表示实体间联系的数据模型称为关系模型。关系模型是由若干个关系模式组成的集合。2.关系模式:关系模
- asp使用fso对象遍历目录及目录下的文件代码:<%@ Language=VBScript %><%&