资讯专栏INFORMATION COLUMN

python深度神经网络tensorflow练习好一点的实体模型开展图像分类

89542767 / 417人阅读

  此篇文章主要是给大家介绍了python深度神经网络tensorflow练习好一点的实体模型开展图像分类实例详细说明,感兴趣的小伙伴可以参考借鉴一下,希望可以有一定的帮助,祝愿大家多多的发展,尽早涨薪。


  文章正文


  Google在各类图象数据库系统ImageNet上练习好啦1个Inception-v3实体模型,这一实体模型大家也可以用来进去图像分类。


  下载地址:https://pan.baidu.com/s/1XGfwYer5pIEDkpM3nM6o2A


  提取码:hu66


  免费下载完缓解压力后,获得好多个文档:


  在其中


  classify_image_graph_def.pb文件便是练习好一点的Inception-v3实体模型。


  imagenet_synset_to_human_label_map.txt是类型文档。


  随机找一张图片

01.png

  对这张图片进行识别,看它属于什么类?


  代码如下:先创建一个类NodeLookup来将softmax概率值映射到标签上。


  然后创建一个函数create_graph()来读取模型。


  读取图片进行分类识别


</>复制代码

  1.   #-*-coding:utf-8-*-
  2.   import tensorflow as tf
  3.   import numpy as np
  4.   import re
  5.   import os
  6.   model_dir='D:/tf/model/'
  7.   image='d:/cat.jpg'
  8.   #将类别ID转换为人类易读的标签
  9.   class NodeLookup(object):
  10.   def __init__(self,
  11.   label_lookup_path=None,
  12.   uid_lookup_path=None):
  13.   if not label_lookup_path:
  14.   label_lookup_path=os.path.join(
  15.   model_dir,'imagenet_2012_challenge_label_map_proto.pbtxt')
  16.   if not uid_lookup_path:
  17.   uid_lookup_path=os.path.join(
  18.   model_dir,'imagenet_synset_to_human_label_map.txt')
  19.   self.node_lookup=self.load(label_lookup_path,uid_lookup_path)
  20.   def load(self,label_lookup_path,uid_lookup_path):
  21.   if not tf.gfile.Exists(uid_lookup_path):
  22.   tf.logging.fatal('File does not exist%s',uid_lookup_path)
  23.   if not tf.gfile.Exists(label_lookup_path):
  24.   tf.logging.fatal('File does not exist%s',label_lookup_path)
  25.   #Loads mapping from string UID to human-readable string
  26.   proto_as_ascii_lines=tf.gfile.GFile(uid_lookup_path).readlines()
  27.   uid_to_human={}
  28.   p=re.compile(r'[nd]*[S,]*')
  29.   for line in proto_as_ascii_lines:
  30.   parsed_items=p.findall(line)
  31.   uid=parsed_items[0]
  32.   human_string=parsed_items[2]
  33.   uid_to_human[uid]=human_string
  34.   #Loads mapping from string UID to integer node ID.
  35.   node_id_to_uid={}
  36.   proto_as_ascii=tf.gfile.GFile(label_lookup_path).readlines()
  37.   for line in proto_as_ascii:
  38.   if line.startswith('target_class:'):
  39.   target_class=int(line.split(':')[1])
  40.   if line.startswith('target_class_string:'):
  41.   target_class_string=line.split(':')[1]
  42.   node_id_to_uid[target_class]=target_class_string[1:-2]
  43.   #Loads the final mapping of integer node ID to human-readable string
  44.   node_id_to_name={}
  45.   for key,val in node_id_to_uid.items():
  46.   if val not in uid_to_human:
  47.   tf.logging.fatal('Failed to locate:%s',val)
  48.   name=uid_to_human[val]
  49.   node_id_to_name[key]=name
  50.   return node_id_to_name
  51.   def id_to_string(self,node_id):
  52.   if node_id not in self.node_lookup:
  53.   return''
  54.   return self.node_lookup[node_id]
  55.   #读取训练好的Inception-v3模型来创建graph
  56.   def create_graph():
  57.   with tf.gfile.FastGFile(os.path.join(
  58.   model_dir,'classify_image_graph_def.pb'),'rb')as f:
  59.   graph_def=tf.GraphDef()
  60.   graph_def.ParseFromString(f.read())
  61.   tf.import_graph_def(graph_def,name='')
  62.   #读取图片
  63.   image_data=tf.gfile.FastGFile(image,'rb').read()
  64.   #创建graph
  65.   create_graph()
  66.   sess=tf.Session()
  67.   #Inception-v3模型的最后一层softmax的输出
  68.   softmax_tensor=sess.graph.get_tensor_by_name('softmax:0')
  69.   #输入图像数据,得到softmax概率值(一个shape=(1,1008)的向量)
  70.   predictions=sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})
  71.   #(1,1008)-&gt;(1008,)
  72.   predictions=np.squeeze(predictions)
  73.   #ID--&gt;English string label.
  74.   node_lookup=NodeLookup()
  75.   #取出前5个概率最大的值(top-5)
  76.   top_5=predictions.argsort()[-5:][::-1]
  77.   for node_id in top_5:
  78.   human_string=node_lookup.id_to_string(node_id)
  79.   score=predictions[node_id]
  80.   print('%s(score=%.5f)'%(human_string,score))
  81.   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)


  综上所述,上述就给大家介绍完毕了,希望可以给大家带来帮助。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128739.html

相关文章

  • caffe的python接口deploy形成caffemodel归类新的图片

      本文主要是给大家介绍了caffe的python插口生成deploy文件学习培训及其用练习好一点的实体模型(caffemodel)来归类新的图片实例详细说明,感兴趣的小伙伴可以参考借鉴一下,希望可以有一定的帮助,祝愿大家多多的发展,尽早涨薪  caffe的python插口生成deploy文件  假如要将练习好一点的实体模型用于检测新的图片,那必然必须得一个deploy.prototxt文件,这一...

    89542767 评论0 收藏0
  • 深度学习

    摘要:深度学习在过去的几年里取得了许多惊人的成果,均与息息相关。机器学习进阶笔记之一安装与入门是基于进行研发的第二代人工智能学习系统,被广泛用于语音识别或图像识别等多项机器深度学习领域。零基础入门深度学习长短时记忆网络。 多图|入门必看:万字长文带你轻松了解LSTM全貌 作者 | Edwin Chen编译 | AI100第一次接触长短期记忆神经网络(LSTM)时,我惊呆了。原来,LSTM是神...

    Vultr 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<