最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進(jìn)行圖像分類

 更新時(shí)間:2022年06月29日 17:10:01   作者:denny402  
這篇文章主要為大家介紹了python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進(jìn)行圖像分類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

谷歌在大型圖像數(shù)據(jù)庫ImageNet上訓(xùn)練好了一個(gè)Inception-v3模型,這個(gè)模型我們可以直接用來進(jìn)來圖像分類。

下載鏈接: https://pan.baidu.com/s/1XGfwYer5pIEDkpM3nM6o2A

提取碼: hu66

下載完解壓后,得到幾個(gè)文件:

其中

classify_image_graph_def.pb 文件就是訓(xùn)練好的Inception-v3模型。

imagenet_synset_to_human_label_map.txt是類別文件。

隨機(jī)找一張圖片

對這張圖片進(jìn)行識別,看它屬于什么類?

代碼如下:先創(chuàng)建一個(gè)類NodeLookup來將softmax概率值映射到標(biāo)簽上。

然后創(chuàng)建一個(gè)函數(shù)create_graph()來讀取模型。

讀取圖片進(jìn)行分類識別

# -*- 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轉(zhuǎn)換為人類易讀的標(biāo)簽
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]
#讀取訓(xùn)練好的Inception-v3模型來創(chuàng)建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()
#創(chuàng)建graph
create_graph()
sess=tf.Session()
#Inception-v3模型的最后一層softmax的輸出
softmax_tensor= sess.graph.get_tensor_by_name('softmax:0')
#輸入圖像數(shù)據(jù),得到softmax概率值(一個(gè)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個(gè)概率最大的值(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)

以上就是python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進(jìn)行圖像分類的詳細(xì)內(nèi)容,更多關(guān)于tensorflow訓(xùn)練模型圖像分類的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python中encode()方法的使用簡介

    Python中encode()方法的使用簡介

    這篇文章主要介紹了Python中encode()方法的使用簡介,是Python入門中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • sklearn的predict_proba使用說明

    sklearn的predict_proba使用說明

    這篇文章主要介紹了sklearn的predict_proba使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 深入淺析python變量加逗號,的含義

    深入淺析python變量加逗號,的含義

    這篇文章主要介紹了python變量加逗號,的含義,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Django環(huán)境下使用Ajax的操作代碼

    Django環(huán)境下使用Ajax的操作代碼

    AJAX 的主要目標(biāo)是在不刷新整個(gè)頁面的情況下,通過后臺與服務(wù)器進(jìn)行數(shù)據(jù)交換和更新頁面內(nèi)容,通過 AJAX,您可以向服務(wù)器發(fā)送請求并接收響應(yīng),然后使用 JavaScript 動(dòng)態(tài)地更新頁面的部分內(nèi)容,這篇文章主要介紹了Django環(huán)境下使用Ajax,需要的朋友可以參考下
    2024-03-03
  • 在?Python?中讀取?gzip?文件的過程解析

    在?Python?中讀取?gzip?文件的過程解析

    這篇文章主要介紹了在?Python?中讀取?gzip?文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • python二分查找搜索算法的多種實(shí)現(xiàn)方法

    python二分查找搜索算法的多種實(shí)現(xiàn)方法

    二分查找,也稱折半查找,是一種效率較高的查找方法,本文主要介紹了python二分查找搜索算法的多種實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • Django項(xiàng)目搭建之實(shí)現(xiàn)簡單的API訪問

    Django項(xiàng)目搭建之實(shí)現(xiàn)簡單的API訪問

    這篇文章主要給大家介紹了關(guān)于Django項(xiàng)目搭建之實(shí)現(xiàn)簡單的API訪問的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-02-02
  • python使用re模塊爬取豆瓣Top250電影

    python使用re模塊爬取豆瓣Top250電影

    這篇文章主要介紹了python使用re模塊爬取豆瓣Top250電影的示例,幫助大家更好的理解和學(xué)習(xí)python 爬蟲,感興趣的朋友可以了解下
    2020-10-10
  • python 從文件夾抽取圖片另存的方法

    python 從文件夾抽取圖片另存的方法

    今天小編就為大家分享一篇python 從文件夾抽取圖片另存的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python的collections模塊中namedtuple結(jié)構(gòu)使用示例

    Python的collections模塊中namedtuple結(jié)構(gòu)使用示例

    namedtuple顧名思義,就是名字+元組的數(shù)據(jù)結(jié)構(gòu),下面就來看一下Python的collections模塊中namedtuple結(jié)構(gòu)使用示例
    2016-07-07

最新評論

桐乡市| 佛学| 桃园市| 阿尔山市| 南昌市| 丰县| 古丈县| 驻马店市| 会昌县| 驻马店市| 溧阳市| 中阳县| 运城市| 苍溪县| 屯昌县| 新密市| 蓬溪县| 长治县| 金堂县| 通州区| 元阳县| 隆子县| 马关县| 英吉沙县| 泾川县| 来凤县| 达拉特旗| 濮阳市| 阆中市| 苍梧县| 夏河县| 舟山市| 涞水县| 扎赉特旗| 上栗县| 宝山区| 白山市| 麻江县| 马尔康县| 乐陵市| 卢湾区|