1. 程式人生 > >[深度學習]inception_v3識別任何圖片(程式碼)

[深度學習]inception_v3識別任何圖片(程式碼)

運用已經在imagenet上訓練的inception_v3網路,識別各種圖片:

1. 在網上下載Inception_v3的訓練模型,解壓後會得到如下檔案(需要的可以私信我): 在這裡插入圖片描述 其中第一個第二個是imagenet中數字標號和英文label的檔案: 在這裡插入圖片描述 在這裡插入圖片描述 第三個是該模型結構的帶權重的Graph. 2.運用下面的程式碼可以生成一個tfevents檔案,然後用tensorboard檢視他的網路結構.

import tensorflow as tf
import os
inception_pretrain_model_dir = './inception_v3'
log_dir = 'inception_v3_log'
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
   
inception_graph_def_file = os.path.join(inception_pretrain_model_dir, 'classify_image_graph_def.ckpt')

with tf.Session() as sess:
    with tf.gfile.FastGFile(inception_graph_def_file, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')
    writer = tf.summary.FileWriter(log_dir, sess.graph)
    writer.close()
    


在這裡插入圖片描述 在這裡插入圖片描述3.然後載入模型到圖中,把label寫入新的字典,方便後期數字和英文轉換.然後進行檢測.

import tensorflow as tf 
import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
label_lookup_path = './imagenet_2012_challenge_label_map_proto.pbtxt'
id_lookup_path ='./imagenet_synset_to_human_label_map.txt'

class Nodelookup(object):
    def __init__(self,label_lookup_path, id_lookup_path):
        self.label_lookup_path = label_lookup_path
        self.id_lookup_path =id_lookup_path
        self.node_lookup = self.load(self.label_lookup_path, self.id_lookup_path)
        
    def load(self, label_lookup_path, id_lookup_path):
        #分類字串對應的類別名稱  (n00004475	organism, being)
        human_label = tf.gfile.GFile(id_lookup_path).readlines()
        id_to_human = {}
        for line in human_label:
            line = line.strip('\n')
            parsed_item = line.split('\t')
            uid = parsed_item[0]
            human_string = parsed_item[1]
            id_to_human[uid] = human_string
         
        
        #分類字串與對應的編號
        '''entry {
                  target_class: 449
                  target_class_string: "n01440764"
                }'''
        
        label_to_id = tf.gfile.GFile(label_lookup_path).readlines()
        id_to_label = {}
        for line in label_to_id:
            if line.startswith('  target_class:'):
                target_class = int(line.split(': ')[1])
                
            if line.startswith('  target_class_string:'):
                target_class_string  = line.split(': ')[1]
                #不要左右的引號所以是從1:-2
                id_to_label[target_class] = target_class_string[1:-2]
         
        #建立一個新的字典,第二個字典的val作為第一個字典的key,得到的數值(也就是英文類別名稱)作為新字典的val.
        #再把第二個字典的key作為新字典的key,建立新的對應(44----dog)
        id_to_name = {}
        for key, val in id_to_label.items():
            number = id_to_human[val]
            id_to_name[key] = number
        return 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]

#建立一個圖來存放inception訓練好的模型
with tf.gfile.FastGFile('./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('image/'):
        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})
            predictions = np.squeeze(predictions)
            img_path = os.path.join(root, file)
            
            print(img_path)
            img = Image.open(img_path)
            plt.imshow(img)
            plt.axis('off')
            plt.show()
            
            node_lookup =Nodelookup(label_lookup_path, id_lookup_path)
            top_k = predictions.argsort()[-1:]
            for node_id in top_k:
                result = node_lookup.id_to_string(node_id)
                score = predictions[node_id]
                print('識別為: %s | 概率為: %.4f'% (result, score))
            print('\n')

在這裡插入圖片描述 在這裡插入圖片描述 在這裡插入圖片描述