zoukankan      html  css  js  c++  java
  • Tensorflow学习(练习)—使用inception做图像识别

    import os
    import tensorflow as tf
    import numpy as np
    import re

    from PIL import Image
    import matplotlib.pyplot as plt

    print("hello")

    class NodeLookup(object):
    def __init__(self):
    label_lookup_path = "F:Tensorflow Projectinception-2015-12-05imagenet_2012_challenge_label_map_proto.pbtxt"
    uid_lookup_path="F:Tensorflow Projectinception-2015-12-05imagenet_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):
      #加载分类字符串
      proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
      uid_to_human = {}
      #读取数据
      for line in proto_as_ascii_lines:
        #去掉换行符
        line = line.strip(' ')
        #根据'/t'分割
        parsed_items = line.split(' ')
        #获取分类编号
        uid = parsed_items[0]
        #获取分类名称
        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])
        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]

    #创建一个图用来存储训练好的模型
    with tf.gfile.FastGFile('F:Tensorflow Projectinception-2015-12-05classify_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('F:Tensorflow Projectimages0815'):
        for file in files:
          #Tensorflow载入图片
          image_data = tf.gfile.FastGFile(os.path.join(root,file),'rb').read()
          #执行函数,传入jpg格式图片计算并得到结果
          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()

    运行效果

  • 相关阅读:
    silverlight datagrid 右键菜单处理
    Silverlight学习之【最简单数据绑定示例】
    silverlight 自定义资源整理(待后续补充)
    Attribute 扩展应用
    关联,依赖,泛化(又称继承分为扩展或包含),实现,聚合(共享),复合(组合)
    silverlight 右键菜单处理
    silverlight 获得焦点问题 你获得了没?反正我失败了
    ObjectiveC入门(转)
    silverlight Blend 相关 收集
    c# 覆盖\重写\重载
  • 原文地址:https://www.cnblogs.com/herd/p/9482892.html
Copyright © 2011-2022 走看看