Tensorflow学习(7)用别人训练好的模型进行图像分类
来源:互联网 发布:php 随机昵称生成器 编辑:程序博客网 时间:2024/06/08 15:38
谷歌在大型图像数据库ImageNet上训练好了一个Inception-v3模型,这个模型我们可以直接用来进来图像分类。
下载地址:https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip
下载完解压后,得到几个文件:
其中的classify_image_graph_def.pb 文件就是训练好的Inception-v3模型。
imagenet_synset_to_human_label_map.txt是类别文件。
随机找一张图片:如
对这张图片进行识别,看它属于什么类?
代码如下:先创建一个类NodeLookup来将softmax概率值映射到标签上。
然后创建一个函数create_graph()来读取模型。
最后读取图片进行分类识别:
# -*- coding: utf-8 -*-import tensorflow as tfimport numpy as npimport reimport osmodel_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模型来创建graphdef 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()#创建graphcreate_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)
阅读全文
0 0
- Tensorflow学习(7)用别人训练好的模型进行图像分类
- 【caffe】用训练好的imagenet模型分类图像
- 用训练好的caffe模型进行分类完整版
- 如何重新训练Tensorflow图像分类模型
- tensorflow 学习:用CNN进行图像分类
- Tensorflow 的安装和用InceptionV3训练新的图像分类模型
- 使用caffe训练好的模型进行分类
- Python调用已训练好的caffe模型进行分类
- 利用训练好的参数模型对图片进行分类
- 用训练好的模型(caffemodel)来分类
- Tensorflow之用自己的训练好的cpkt模型,进行测试识别
- Caffe_Windows学习笔记(五)用训练好的caffemodel来进行分类
- Tensorflow保存模型,恢复模型,使用训练好的模型进行预测和提取中间输出(特征)
- Tensorflow保存模型,恢复模型,使用训练好的模型进行预测和提取中间输出(特征)【转】
- Windows下caffe用fine-tuning训练好的caffemodel来进行图像分类
- 基于caffe特征可视化 以及 用训练好的模型进行分类
- 基于caffe特征可视化 以及 用训练好的模型进行分类 2
- tensorflow将训练好的模型
- Blog19@linux介绍selinux
- Service Workers/Web Workers/WebSockets的区别
- 【Scikit-Learn 中文文档】广义线性模型
- STM32学习笔记(2):存储器映射和存储器重映射(Memory Map and Memory Remap)
- python面向对象编程(5)
- Tensorflow学习(7)用别人训练好的模型进行图像分类
- yarn集群上完整spark作业生命周期管理
- Centos6.5 (64位) 搭建FTP服务器
- 一级简单的判断check+布局
- uva10375(唯一分解定理)
- Flatten 2D Vector
- <A Critical Review of Recurrent Neural Networks for Sequence Learning>阅读笔记
- [USACO1.5]回文质数 Prime Palindromes
- SPOJ Lexicographical Substring Search 后缀自动机