上一篇博客介紹了怎么獲取inception v3模型數據,現在我們用下載好的模型進行簡單的圖片分類實驗。
import tensorflow as tf import os import numpy as np import re from PIL import Image import matplotlib.pyplot as plt class NodeLookup(object): def __init__(self): label_lookup_path='inception_model/imagenet_2012_challenge_label_map_proto.pbtxt' uid_lookup_path='inception_model/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): #加載分類字符串n************對應分類名稱的文件 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] #保存編號字符串n********與分類名稱的映射關系 uid_to_human[uid]=human_string #加載分類字符串n**********對應分類編號1-1000的文件 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:'): #獲取分類編號1-1000 target_class=int(line.split(':')[1]) if line.startswith(' target_class_string:'): #獲取編號字符串n********* target_class_string=line.split(':')[1] #保存分類編號1-1000與編號字符串n*******映射關系 node_id_to_uid[target_class]=target_class_string[2:-2] #建立分類編號1-1000對應分類名稱的映射關系 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 #傳入分類編號1-1000返回分類名稱 def id_to_string(self, node_id): if node_id not in self.node_lookup: return '' return self.node_lookup[node_id] #創建一個圖來放google訓練好的模型 with tf.gfile.FastGFile('inception_model/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('images/'): for file in files: #載入圖片 image_data=tf.gfile.FastGFile(os.path.join(root,file), 'rb').read() predictions=sess.run(softmax_tensor, {'DecodeJpeg/contents:0' : image_data}) #圖片格式是jpg格式 predictions=np.squeeze(predictions) #把結果轉換為1維數據 #打印圖片路徑及名稱 print() 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))
目錄:
- tensorflow簡介、目錄
- tensorflow中的圖(02-1)
- tensorflow變量的使用(02-2)
- tensorflow中的Fetch、Feed(02-3)
- tensorflow版helloworld---擬合線性函數的k和b(02-4)
- tensorflow非線性回歸(03-1)
- MNIST手寫數字分類simple版(03-2)
- 二次代價函數、交叉熵(cross-entropy)、對數似然代價函數(log-likelihood cost)(04-1)
- 多層網絡通過防止過擬合,增加模型的准確率(04-2)
- 修改優化器進一步提升准確率(04-3)
- 手寫數字識別-卷積神經網絡cnn(06-2)
- 循環神經網絡rnn與長短時記憶神經網絡簡述(07-2)
- 循環神經網絡lstm代碼實現(07-3)
- tensorflow模型保存和使用08
- 下載inception v3 google訓練好的模型並解壓08-3
- 使用inception v3做各種圖像分類識別08-4
- word2vec模型訓練簡單案例
- word2vec+textcnn文本分類簡述及代碼