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 Project\inception-2015-12-05\imagenet_2012_challenge_label_map_proto.pbtxt"
uid_lookup_path="F:\Tensorflow Project\inception-2015-12-05\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):
  #加载分类字符串
  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')
    #获取分类编号
    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 Project\inception-2015-12-05\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('F:\Tensorflow Project\images0815'):
    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()

运行效果

Tensorflow学习(练习)—使用inception做图像识别的更多相关文章

  1. TensorFlow学习笔记(四)图像识别与卷积神经网络

    一.卷积神经网络简介 卷积神经网络(Convolutional Neural Network,CNN)是一种前馈神经网络,它的人工神经元可以响应一部分覆盖范围内的周围单元,对于大型图像处理有出色表现. ...

  2. TensorFlow学习路径【转】

    作者:黄璞链接:https://www.zhihu.com/question/41667903/answer/109611087来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  3. TensorFlow学习线路

    如何高效的学习 TensorFlow 代码? 或者如何掌握TensorFlow,应用到任何领域? 作者:黄璞链接:https://www.zhihu.com/question/41667903/ans ...

  4. tensorflow学习笔记——自编码器及多层感知器

    1,自编码器简介 传统机器学习任务很大程度上依赖于好的特征工程,比如对数值型,日期时间型,种类型等特征的提取.特征工程往往是非常耗时耗力的,在图像,语音和视频中提取到有效的特征就更难了,工程师必须在这 ...

  5. TensorFlow学习笔记——LeNet-5(训练自己的数据集)

    在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...

  6. tensorflow学习笔记——VGGNet

    2014年,牛津大学计算机视觉组(Visual Geometry Group)和 Google DeepMind 公司的研究员一起研发了新的深度卷积神经网络:VGGNet ,并取得了ILSVRC201 ...

  7. 用tensorflow学习贝叶斯个性化排序(BPR)

    在贝叶斯个性化排序(BPR)算法小结中,我们对贝叶斯个性化排序(Bayesian Personalized Ranking, 以下简称BPR)的原理做了讨论,本文我们将从实践的角度来使用BPR做一个简 ...

  8. TensorFlow学习笔记之--[compute_gradients和apply_gradients原理浅析]

    I optimizer.minimize(loss, var_list) 我们都知道,TensorFlow为我们提供了丰富的优化函数,例如GradientDescentOptimizer.这个方法会自 ...

  9. 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识

    深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...

随机推荐

  1. libwebsockets 运行问题

    /****************************************************************************** * libwebsockets 运行问题 ...

  2. Http基础(记忆笔记)

    地址解析:http://localhost.com:8080/index.htm 协议名:Http 主机名:localhost.com 端口:8080 对象路径:/index.htm 通过域名解析lo ...

  3. BZOJ - 3622:已经没有什么好害怕的了 (广义容斥)

    [BZOJ3622]已经没有什么好害怕的了 Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output ...

  4. Java Platform SE binary已停止运行 Can't load AMD 64-bit.dll on a IA 32-bit platform错误

    这个我是在junit测试web项目时候遇到的问题,然后今天遇到一位网友也遇到了,报问题的是A卡了,所以今天做个记载,方便遇到问题的人,因为,之前我个人遇到这个问题,在网上找了很久都没有找到. 上面这个 ...

  5. 剑指offer-第五章优化时间和空间效率(数组中的逆序对的总数)

    题目:在数组中如果两个数字的前面的数比后面的数大,则称为一对逆序对.输入一个数组求出数组中逆序对的总数. 以空间换时间:思路:借助一个辅助数组,将原来的数组复制到该数组中.然后将该数组分成子数组,然后 ...

  6. PING分组网间探测 ICMP协议

      1.Ping的基础知识 Ping是潜水艇人员的专用术语,表示回应的声纳脉冲,在网络中Ping 是一个十分好用的TCP/IP工具.它主要的功能是用来检测网络的连通情况和分析网络速度.是ICMP的一个 ...

  7. 在Mac中安装python,配置python环境

    参考链接:http://blog.justbilt.com/2014/07/02/setup_python_on_mac/ 其实跟windows差不多,就是在python官网下载特定版本的python ...

  8. poj1011---DFS

    题目的大意是给了你有限个棍子以及每个棍子的长度,而且所有的棍子都是由有限个长度相同的棍子截断得到的,让你求被截棍子的最小长度 搜索剪枝神题,做的我够呛 提供一个比较好的解题报告  http://www ...

  9. (转)Makefile经典教程(掌握这些足够)

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客: http://blog.csdn.net/haoel/article/details/2886 makefile很重 ...

  10. erlang配置三方库

    暴力的: 直接下载解压以后放到erlang的lib目录,比如/usr/local/Cellar/erlang/17.3/lib/erlang/lib 和谐的: 在用户名下建立.erlang文件 在里面 ...