python深度學(xué)習(xí)tensorflow訓(xùn)練好的模型進(jìn)行圖像分類
正文
谷歌在大型圖像數(shù)據(jù)庫(kù)ImageNet上訓(xùn)練好了一個(gè)Inception-v3模型,這個(gè)模型我們可以直接用來(lái)進(jìn)來(lái)圖像分類。
下載鏈接: 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ī)找一張圖片

對(duì)這張圖片進(jìn)行識(shí)別,看它屬于什么類?
代碼如下:先創(chuàng)建一個(gè)類NodeLookup來(lái)將softmax概率值映射到標(biāo)簽上。
然后創(chuàng)建一個(gè)函數(shù)create_graph()來(lái)讀取模型。
讀取圖片進(jìn)行分類識(shí)別
# -*- 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模型來(lái)創(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)練模型圖像分類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python深度學(xué)習(xí)tensorflow實(shí)例數(shù)據(jù)下載與讀取
- 使用Python、TensorFlow和Keras來(lái)進(jìn)行垃圾分類的操作方法
- Python基于TensorFlow接口實(shí)現(xiàn)深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)回歸
- Python基于Tensorflow2.X實(shí)現(xiàn)汽車油耗預(yù)測(cè)
- Python tensorflow與pytorch的浮點(diǎn)運(yùn)算數(shù)如何計(jì)算
- python深度學(xué)習(xí)tensorflow1.0參數(shù)和特征提取
- python如何下載指定版本TensorFlow
相關(guān)文章
python 接口測(cè)試response返回?cái)?shù)據(jù)對(duì)比的方法
本篇文章主要介紹了python 接口測(cè)試response返回?cái)?shù)據(jù)對(duì)比的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
在Python運(yùn)行時(shí)動(dòng)態(tài)查看進(jìn)程內(nèi)部信息的方法
今天小編就為大家分享一篇在Python運(yùn)行時(shí)動(dòng)態(tài)查看進(jìn)程內(nèi)部信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python機(jī)器學(xué)習(xí)應(yīng)用之基于決策樹(shù)算法的分類預(yù)測(cè)篇
所謂決策樹(shù),就是一個(gè)類似于流程圖的樹(shù)形結(jié)構(gòu),樹(shù)內(nèi)部的每一個(gè)節(jié)點(diǎn)代表的是對(duì)一個(gè)特征的測(cè)試,樹(shù)的分支代表該特征的每一個(gè)測(cè)試結(jié)果,而樹(shù)的每一個(gè)葉子節(jié)點(diǎn)代表一個(gè)類別。樹(shù)的最高層是就是根節(jié)點(diǎn)2022-01-01
Python利用卡方Chi特征檢驗(yàn)實(shí)現(xiàn)提取關(guān)鍵文本特征
卡方檢驗(yàn)最基本的思想就是通過(guò)觀察實(shí)際值與理論值的偏差來(lái)確定理論的正確與否。本文將利用卡方Chi特征檢驗(yàn)實(shí)現(xiàn)提取關(guān)鍵文本特征功能,感興趣的可以了解一下2022-12-12
Python基于hashlib模塊的文件MD5一致性加密驗(yàn)證示例
這篇文章主要介紹了Python基于hashlib模塊的文件MD5一致性加密驗(yàn)證,涉及Python使用hashlib模塊進(jìn)行字符串與文件的MD5加密驗(yàn)證操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02
Python中處理字符串之endswith()方法的使用簡(jiǎn)介
這篇文章主要介紹了Python中處理字符串之endswith()方法的使用,是Python入門(mén)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
python dict remove數(shù)組刪除(del,pop)
我們?cè)谟脭?shù)組列表做刪除的時(shí)候,可能選擇2個(gè)方法,一個(gè)是del,一個(gè)是pop方法2013-03-03
python解決pandas處理缺失值為空字符串的問(wèn)題
下面小編就為大家分享一篇python解決pandas處理缺失值為空字符串的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python常用數(shù)據(jù)結(jié)構(gòu)字典梳理
這篇文章主要介紹了python常用數(shù)據(jù)結(jié)構(gòu)字典梳理,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
python解析Chrome瀏覽器歷史瀏覽記錄和收藏夾數(shù)據(jù)
大家好,本篇文章主要講的是python解析Chrome瀏覽器歷史瀏覽記錄和收藏夾數(shù)據(jù),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02

