jggybf 2019-10-18 01:20 采纳率: 0%
浏览 418

tensorflow inceptionv3

#8.4使用inception-v3做各种图像识别
import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt

class NodeLookup(object):
    def __init__(self):

        label_lookup_path = 'inception_model/imagenet_2012_challenge_label_map_proto.pbtxt'
        uid_lookup_path = 'inception_model/imagenet_sysnet_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')
            #按照'\t'分割
            parsed_items = line.split('\t')
            #获取分类编号
            human_string = parsed_items[1]
            #保存编号字符串n********于分类名称映射的关系
            uid_to_human[uid] = human_string
    #加载分类字符串n*********对应分类编号1-1000的文件
    proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
    node_id_to_uid = {}
    for line in proto_as_ascii:
        if line.starstwith(' target_class:'):#前面要有空格
            #获取分类编号1-1000
            target_class = int(line.split(': ')[1])#:后面要有空格
        if line.startswith(' target_class_string:'):
            #获取编号字符串n********
            target_class_string = line.split(': ')[1]
            #保存分类编号1-1000于编号字符串n********映射关系
            node_id_to_uid[target_class] = target_class_string[1:-2]#第一个字符取到倒数第二个
    #建立分类编好1-1000对应分类名称的映射关系
    node_id_to_name = {}
    for key,val in node_id_to_uid.items():
        #获取分类名称
        name = uid_to_human[val]
        #建立分类编号1-1000到分类名称的映射关系
        node_id_to_name[key] = name
return node_id_to_name

#传入分类编号1-1000,返回分类名称
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.GFile(os.path.join(root,file),'rb').read()
            predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})#图片格式为jpg
            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()

NameError Traceback (most recent call last)
in
7 import matplotlib.pyplot as plt
8
----> 9 class NodeLookup(object):
10 def init(self):
11

in NodeLookup()
29 uid_to_human[uid] = human_string
30 #加载分类字符串n*********对应分类编号1-1000的文件
---> 31 proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
32 node_id_to_uid = {}
33 for line in proto_as_ascii:

NameError: name 'label_lookup_path' is not defined

  • 写回答

1条回答 默认 最新

  • AIShark 2019-10-23 16:14
    关注

    proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines() 这一行缩进除了问题吧

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题