简单迁移学习inception-v3各种图像的识别
来源:互联网 发布:wind金融终端mac版 编辑:程序博客网 时间:2024/04/27 19:10
接着上一篇文章,上一篇文章中,我们下载了inception-v3的模型,接下来,我们进行简单的迁移学习,将物体进行分类。
下面是代码:
# coding: UTF-8import tensorflow as tfimport osimport numpy as npimport refrom PIL import Imageimport matplotlib.pyplot as pltclass NodeLookup(object): def __init__(self): label_lookup_path = 'inception_model/imagenet_2012_challenge_label_map_proto.pbtxt' uid_lookup_path = 'inception_model/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): # 加载分类字符串n***********对应分类名称的文件 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') parsed_items = line.split('\t') uid = parsed_items[0] # n15092227 human_string = parsed_items[1] uid_to_human[uid] = human_string 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:'): 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] node_id_to_name = {} for key, val in node_id_to_uid.items(): 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]# 创建一个图来存放google调整好的模型with tf.gfile.FastGFile('inception_model/classify_image_graph_def.pb', 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='')with tf.Session() as sess: softmax_tensor = sess.graph.get_tensor_by_name('softmax:0') # 遍历目录 for root, dirs, files in os.walk('images/'): for file in files: image_data = tf.gfile.FastGFile(os.path.join(root, file), 'rb').read() predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data}) predictions = np.squeeze(predictions) image_path = os.path.join(root, file) print image_path img = Image.open(image_path) plt.imshow(img) plt.axis('off') plt.show() top_k = predictions.argsort()[-5:][::-1] node_lookup = NodeLookup() for node_id in top_k: human_string = node_lookup.id_to_string(node_id) score = predictions[node_id] print('%s (score=%.5f)' % (human_string, score)) print()
其中代码NodeLookup是类,作用是将inception-v3中1000个分类得分转换成类别的字符串,其中的文件如下所示:
imagenet_2012_challenge_label_map_proto.pbtxt
imagenet_synset_to_human_label_map.txt
从上面的图中可以很简单的看出来。
然后创建一个图用来存放调整好的google存放的权重的图。
with tf.gfile.FastGFile('inception_model/classify_image_graph_def.pb', 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='')然后创建会话,在会话中引进softmax的分类器,用softmax分类器进行预测predictions,其中,得到的predictions是二维的,
predictions = sess.run(softmax_tensor, {'DecodeJpeg/contents:0': image_data})
,将图片jped的格式传入。然后np.squeeze得到一维。
最后得到预测值。
阅读全文
0 0
- 简单迁移学习inception-v3各种图像的识别
- inception-v3迁移学习
- Tensorflow学习笔记--使用迁移学习做自己的图像分类器(Inception v3)
- TensorFlow入门-Inception(v3)图像识别
- Tensorflow + ImageNet Inception-v3 视频图像识别
- 【TensorFlow】迁移学习(使用Inception-v3)
- Tenosorflow官方Inception V3迁移学习
- tensorflow 卷积神经网络 Inception-v3模型 迁移学习
- 【深度学习】Inception的前世今生(三)--Inception V3
- TfRecord+inception-v3训练图像
- 深度学习之图像分类模型inception v2、inception v3解读
- 深度学习之图像分类模型inception v2、inception v3解读
- Keras 入门课6 -- 使用Inception V3模型进行迁移学习
- GoogLe图像分类模型 Inception-v2,Inception-v3 论文笔记
- Inception V3 的 tensorflow 实现
- inception-v3的BUG解决
- Inception V3
- Inception V3
- Hashtable源码探讨(基于JDK1.8)
- 11.28日训练赛总结
- Unity 几种优化建议
- Java建造者模式原型和链式变种
- 欢迎使用CSDN-markdown编辑器
- 简单迁移学习inception-v3各种图像的识别
- (10)spring中bean的作用域
- 【Linux】回收子进程—wait/waitpid
- 炸裂滚动条Nicescroll
- Learning Scrapy.pdf 英文原版 免费下载
- Octave教程一:基本操作
- java直接插入排序算法实例
- 【学习笔记】Windows上安装Python
- Graphical Models Reading Group