【先说一下自己想说的】:昨晚上找了很久才搞定,代码和给的文件根本不匹配,转载也不验证一下就转。弄得我花了一整天!(我就为了加个单击图片显示可能的标签这么个功能我……我容易吗……555)
原帖:http://www.cnblogs.com/denny402/p/6942580.html(感谢此源贴的下方评论指引我找到了配套的库)
然后我鄙视一下这些转载不发源链接的↓(╬▔皿▔)凸(还有就是不验证就敢转发):
https://blog.csdn.net/u011600477/article/details/78607883
https://blog.csdn.net/m0_37167788/article/details/79084288
与原帖配套的模型和其他文件在:(不知道是不是源博主搞错了,博主给的云盘里的东西完全是不着边,这帮转贴的也不自己验证以下,像是传下去的谎言——真是荒谬又可笑)
“看到这个链接了,里面有博主提到的模型和pbtxt文件 https://github.com/taey16/tf/tree/master/imagenet”
以下是原帖,上边该补充的都说了=================分割线=============(红色粗体是自己加的)
谷歌在大型图像数据库ImageNet上训练好了一个Inception-v3模型,这个模型我们可以直接用来进来图像分类。 下载地址:https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip
(原帖上处错误,地址是:模型和pbtxt文件 https://github.com/taey16/tf/tree/master/imagenet”)
下载完解压后,得到几个文件:
其中的classify_image_graph_def.pb 文件就是训练好的Inception-v3模型。
imagenet_synset_to_human_label_map.txt是类别文件。
然后把他们放到与代码对应的路径:
model_dir='D:/tf/model/'(模型存放路径,或者改代码)
image='d:/cat.jpg'(要识别的图片路径)
可能出现的错误以及解决办法在文末给出~~~~~~~~~
代码如下:先创建一个类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)
以下是亲自验证,上图====================分割线====================================
上面这张图,识别成seashore,还是挺准的。
注意,我是windows环境运行的,目录要用r'路径'或者双反斜杠"\"转意) 或者斜杠"/"!不然总会出如下错误
tensorflow.python.framework.errors_impl.NotFoundError: NewRandomAccessFile failed to Create/Open: D: f.jpg : ϵ ͳ\udcd5Ҳ\udcbb\udcb5\udcbdָ\udcb6\udca8\udcb5\udcc4\udcceļ\udcfe\udca1\udca3
; No such file or directory
还有,我目前不知道为什么一把图片弄个路径就出错,目前我是直接放文件夹里才能用的。不然就是下面那个错误
#读取图片
image_data = tf.gfile.FastGFile('1.jpg', 'rb').read() #直接的'1.jpg'
tensorflow.python.framework.errors_impl.InvalidArgumentError: NewRandomAccessFile failed to Create/Open: D:\tf\1.jpg : \udcceļ\udcfe\udcc3\udcfb\udca1\udca2Ŀ¼\udcc3\udcfb\udcbb\udcf2\udcbe\udced\udcb1\udcea\udcd3\udcb2\udcbb\udcd5\udcfdȷ\udca1\udca3
; Unknown error
最后放两个不错的补充链接:
https://blog.csdn.net/juezhanangle/article/details/78725913
https://blog.csdn.net/muyiyushan/article/details/64124953