TensorFlow学习笔记:使用Inception v3进行图像分类

Inception

0. Google Inception模型简介

Inception为Google开源的CNN模型,至今已经公开四个版本,每一个版本都是基于大型图像数据库ImageNet中的数据训练而成。因此我们可以直接利用Google的Inception模型来实现图像分类。本篇文章主要以Inception_v3模型为基础。Inception v3模型大约有2500万个参数,分类一张图像就用了50亿的乘加指令。在一台没有GPU的现代PC上,分类一张图像转眼就能完成。

1. Google Inception模型发展

以下为Inception四个版本所对应的论文,末尾为ILSVRC中的Top-5错误率:

2. 下载Inception_v3模型

解压下载好的压缩文件,如下:
文件列表
  • classify_image_graph_def.pb文件为Inception_v3本体
  • imagenet_2012_challenge_label_map_proto.pbtxt文件内容如下所示:
    imagenet_2012_challenge_label_map_proto.pbtxt

    包含target_class与target_class_string,前者为分类代码,从1~1000,共1k类,记为Node_ID;后者为一编号字符串“n********”,可以理解为“地址”或者“桥梁”,记为UID。

  • imagenet_synset_to_human_label_map.txt文件内容如下:
    imagenet_synset_to_human_label_map.txt

    包含UID与类别的映射,这种类别文字标签记为human_string。

3. 准备工作

随便从网上下载一张图片,命名为husky.jpg:


husky.jpg

下面的代码就将使用Inception_v3模型对这张哈士奇图片进行分类。

4. 代码

先创建一个类NodeLookup来将softmax概率值映射到标签上;然后创建一个函数create_graph()来读取并新建模型;最后读取哈士奇图片进行分类识别:

# -*- coding: utf-8 -*-

import tensorflow as tf
import numpy as np
#import re
import os

model_dir='C:/Users/Dexter/Documents/ML_files/171003_Inception_v3/Inception_model'
image = 'C:/Users/Dexter/Documents/ML_files/171003_Inception_v3/Images/husky.jpg'


#将类别ID转换为人类易读的标签
class NodeLookup(object):
    def __init__(self, label_lookup_path=None, uid_lookup_path=None):
        if not label_lookup_path:
            # 加载“label_lookup_path”文件
            # 此文件将数据集中所含类别(1-1000)与一个叫做target_class_string的地址对应起来
            # 其地址编码为“n********”星号代表数字
            label_lookup_path = os.path.join(
                    model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')
        if not uid_lookup_path:
            # 加载“uid_lookup_path”文件
            # 此文件将数据集中所含类别具体名称与编码方式为“n********”的地址/UID一一对应起来
            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
        # 加载编号字符串n********,即UID与分类名称之间的映射关系(字典):uid_to_human
        
        # 读取uid_lookup_path中所有的lines
        # readlines(): Returns all lines from the file in a list.
        # Leaves the '\n' at the end.
        proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
        
        # 创建空字典uid_to_human用以存储映射关系
        uid_to_human = {}
# =============================================================================
#         # 使用正则化方法处理文件:
#         p = re.compile(r'[n\d]*[ \S,]*')
#         for line in proto_as_ascii_lines:         
#              = p.findall(line)
#             uid = parsed_items[0]
#             human_string = parsed_items[2]
#             uid_to_human[uid] = human_string
# =============================================================================
        # 使用简单方法处理文件:
        # 一行行读取数据
        for line in proto_as_ascii_lines:
            # 去掉换行符
            line = line.strip('\n')
            # 按照‘\t’分割,即tab,将line分为两个部分
            parse_items = line.split('\t')
            # 获取分类编码,即UID
            uid = parse_items[0]
            # 获取分类名称
            human_string = parse_items[1]
            # 新建编号字符串n********,即UID与分类名称之间的映射关系(字典):uid_to_human
            uid_to_human[uid] = human_string
            

        # Loads mapping from string UID to integer node ID.
        # 加载编号字符串n********,即UID与分类代号,即node ID之间的映射关系(字典)
        
        # 加载分类字符串n********,即UID对应分类编号1-1000的文件
        proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
        # 创建空字典node_id_to_uid用以存储分类代码node ID与UID之间的关系
        node_id_to_uid = {}
        for line in proto_as_ascii:
            # 注意空格
            if line.startswith('  target_class:'):
                # 获取分类编号
                target_class = int(line.split(': ')[1])
            if line.startswith('  target_class_string:'):
                # 获取UID(带双引号,eg:"n01484850")
                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与分类名称之间的映射关系
        node_id_to_name = {}
        for key, val in node_id_to_uid.items():
            # 假如uid不存在于uid_to_human中,则报错
            if val not in uid_to_human:
                tf.logging.fatal('Failed to locate: %s', val)
            # 获取分类名称
            name = uid_to_human[val]
            # 构建分类编号1-1000对应分类名称的映射关系:key为node_id;val为name
            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]

# 读取并创建一个图graph来存放Google训练好的Inception_v3模型(函数)
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()

#创建graph
create_graph()

# 创建会话,因为是从已有的Inception_v3模型中恢复,所以无需初始化
with tf.Session() as sess:
    # Inception_v3模型的最后一层softmax的输出
    # 形如'conv1'是节点名称,而'conv1:0'是张量名称,表示节点的第一个输出张量
    softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
    # 输入图像(jpg格式)数据,得到softmax概率值(一个shape=(1,1008)的向量)
    predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})
    # 将结果转为1维数据
    predictions = np.squeeze(predictions)
    # 新建类:ID --> English string label.
    node_lookup = NodeLookup()
    # 排序,取出前5个概率最大的值(top-5)
    # argsort()返回的是数组值从小到大排列所对应的索引值
    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))

最后输出:

runfile('C:/Users/Dexter/Documents/ML_files/171003_Inception_v3/test.py', wdir='C:/Users/Dexter/Documents/ML_files/171003_Inception_v3')
Siberian husky (score = 0.51033)
Eskimo dog, husky (score = 0.41048)
malamute, malemute, Alaskan malamute (score = 0.00653)
kelpie (score = 0.00136)
dogsled, dog sled, dog sleigh (score = 0.00133)

稍微修改一下代码,使输入为多张图片,输出为图片路径+图片+预测结果:

# -*- coding: utf-8 -*-
"""
Created on Fri Oct  6 19:32:04 2017
test2:将test中输入一张图片变为输入一个文件夹的图片,并使输出可见
@author: Dexter
"""

import tensorflow as tf
import numpy as np
#import re
import os
from PIL import Image
import matplotlib.pyplot as plt

model_dir='C:/Users/Dexter/Documents/ML_files/171003_Inception_v3/Inception_model'
image = 'C:/Users/Dexter/Documents/ML_files/171003_Inception_v3/Images/'


#将类别ID转换为人类易读的标签
class NodeLookup(object):
    def __init__(self, label_lookup_path=None, uid_lookup_path=None):
        if not label_lookup_path:
            # 加载“label_lookup_path”文件
            # 此文件将数据集中所含类别(1-1000)与一个叫做target_class_string的地址对应起来
            # 其地址编码为“n********”星号代表数字
            label_lookup_path = os.path.join(
                    model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')
        if not uid_lookup_path:
            # 加载“uid_lookup_path”文件
            # 此文件将数据集中所含类别具体名称与编码方式为“n********”的地址/UID一一对应起来
            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
        # 加载编号字符串n********,即UID与分类名称之间的映射关系(字典):uid_to_human
        
        # 读取uid_lookup_path中所有的lines
        # readlines(): Returns all lines from the file in a list.
        # Leaves the '\n' at the end.
        proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
        
        # 创建空字典uid_to_human用以存储映射关系
        uid_to_human = {}
# =============================================================================
#         # 使用正则化方法处理文件:
#         p = re.compile(r'[n\d]*[ \S,]*')
#         for line in proto_as_ascii_lines:         
#              = p.findall(line)
#             uid = parsed_items[0]
#             human_string = parsed_items[2]
#             uid_to_human[uid] = human_string
# =============================================================================
        # 使用简单方法处理文件:
        # 一行行读取数据
        for line in proto_as_ascii_lines:
            # 去掉换行符
            line = line.strip('\n')
            # 按照‘\t’分割,即tab,将line分为两个部分
            parse_items = line.split('\t')
            # 获取分类编码,即UID
            uid = parse_items[0]
            # 获取分类名称
            human_string = parse_items[1]
            # 新建编号字符串n********,即UID与分类名称之间的映射关系(字典):uid_to_human
            uid_to_human[uid] = human_string
            

        # Loads mapping from string UID to integer node ID.
        # 加载编号字符串n********,即UID与分类代号,即node ID之间的映射关系(字典)
        
        # 加载分类字符串n********,即UID对应分类编号1-1000的文件
        proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
        # 创建空字典node_id_to_uid用以存储分类代码node ID与UID之间的关系
        node_id_to_uid = {}
        for line in proto_as_ascii:
            # 注意空格
            if line.startswith('  target_class:'):
                # 获取分类编号
                target_class = int(line.split(': ')[1])
            if line.startswith('  target_class_string:'):
                # 获取UID(带双引号,eg:"n01484850")
                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与分类名称之间的映射关系
        node_id_to_name = {}
        for key, val in node_id_to_uid.items():
            # 假如uid不存在于uid_to_human中,则报错
            if val not in uid_to_human:
                tf.logging.fatal('Failed to locate: %s', val)
            # 获取分类名称
            name = uid_to_human[val]
            # 构建分类编号1-1000对应分类名称的映射关系:key为node_id;val为name
            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]

# 读取并创建一个图graph来存放Google训练好的Inception_v3模型(函数)
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='')

#创建graph
create_graph()

# 创建会话,因为是从已有的Inception_v3模型中恢复,所以无需初始化
with tf.Session() as sess:
    # Inception_v3模型的最后一层softmax的输出
    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()
            # 输入图像(jpg格式)数据,得到softmax概率值(一个shape=(1,1008)的向量)
            predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})
            # 将结果转为1维数据
            predictions = np.squeeze(predictions)
    
            # 打印图片路径及名称
            image_path = os.path.join(root, file)
            print(image_path)
            # 显示图片
            img = Image.open(image_path)
            plt.imshow(img)
            plt.axis('off')
            plt.show()
            
            # 新建类:ID --> English string label.
            node_lookup = NodeLookup()
            # 排序,取出前5个概率最大的值(top-5)
            # argsort()返回的是数组值从小到大排列所对应的索引值
            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))
            print()

最后输出:

runfile('C:/Users/Dexter/Documents/ML_files/171003_Inception_v3/test2.py', wdir='C:/Users/Dexter/Documents/ML_files/171003_Inception_v3')
images/dog.jpg
dingo, warrigal, warragal, Canis dingo (score = 0.46103)
Chihuahua (score = 0.05741)
Eskimo dog, husky (score = 0.04384)
dhole, Cuon alpinus (score = 0.04106)
Pembroke, Pembroke Welsh corgi (score = 0.02823)
images/husky.jpg

Siberian husky (score = 0.51033)
Eskimo dog, husky (score = 0.41048)
malamute, malemute, Alaskan malamute (score = 0.00653)
kelpie (score = 0.00136)
dogsled, dog sled, dog sleigh (score = 0.00133)

5. 相关函数补充说明

  • tf.get_default_graph()
    返回当前进程中的默认图(可以使用Graph.as_default()设置)

Returns the default graph for the current thread.
The returned graph will be the innermost graph on which a Graph.as_default() context has been entered, or a global default graph if none has been explicitly created.
NOTE: The default graph is a property of the current thread. If you create a new thread, and wish to use the default graph in that thread, you must explicitly add a with g.as_default(): in that thread's function.

Returns:
The default Graph being used in the current thread.


  • tf.Graph.as_default()
    将Graph设置为默认图

Returns a context manager that makes this Graph the default graph.


  • tf.Graph.get_tensor_by_name()

All tensors have string names which you can see as follows:

[tensor.name for tensor in tf.get_default_graph().as_graph_def().node]

Once you know the name you can fetch the Tensor using <name>:0 (0 refers to endpoint which is somewhat redundant)

import tensorflow as tf

c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d, name='example')

with tf.Session() as sess:
    test = sess.run(e)
    print (e.name)  
    #example:0
    #<name>:0 (0 refers to endpoint which is somewhat redundant)
    test = tf.get_default_graph().get_tensor_by_name("example:0")
    print (test)    
    #Tensor("example:0", shape=(2, 2), dtype=float32)
参考资料:

6. 一些改进

6.1 使用png或者其他图片格式,代替jpg作为输入

The shipped InceptionV3 graph used in classify_image.py
only supports JPEG images out-of-the-box. There are two ways you could use this graph with PNG images:

  1. Convert the PNG image to a height
    x width x 3 (channels) Numpy array, for example using PIL, then feed the 'DecodeJpeg:0' tensor:
import numpy as np
from PIL import Image
# ...

image = Image.open("example.png")
image_array = np.array(image)[:, :, 0:3]  # Select RGB channels only.

prediction = sess.run(softmax_tensor, {'DecodeJpeg:0': image_array})

Perhaps confusingly, 'DecodeJpeg:0' is the output of the DecodeJpeg op, so by feeding this tensor, you are able to feed raw image data.

  1. Add a tf.image.decode_png() op to the imported graph. Simply switching the name of the fed tensor from 'DecodeJpeg/contents:0'
    to 'DecodePng/contents:0' does not work because there is no 'DecodePng' op in the shipped graph. You can add such a node to the graph by using the input_map argument to tf.import_graph_def()
    :
png_data = tf.placeholder(tf.string, shape=[])
decoded_png = tf.image.decode_png(png_data, channels=3)
# ...

graph_def = ...
softmax_tensor = tf.import_graph_def(
    graph_def,
    input_map={'DecodeJpeg:0': decoded_png},
    return_elements=['softmax:0'])

sess.run(softmax_tensor, {png_data: ...})
  1. The following code should handle of both cases.
import numpy as np
from PIL import Image

image_file = 'test.jpeg'
with tf.Session() as sess:

    #     softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    if image_file.lower().endswith('.jpeg'):
        image_data = tf.gfile.FastGFile(image_file, 'rb').read()
        prediction = sess.run('final_result:0', {'DecodeJpeg/contents:0': image_data})
    elif image_file.lower().endswith('.png'):
        image = Image.open(image_file)
        image_array = np.array(image)[:, :, 0:3]
        prediction = sess.run('final_result:0', {'DecodeJpeg:0': image_array})

    prediction = prediction[0]    
    print(prediction)

or shorter version with direct strings:

image_file = 'test.png' # or 'test.jpeg'
image_data = tf.gfile.FastGFile(image_file, 'rb').read()
ph = tf.placeholder(tf.string, shape=[])

with tf.Session() as sess:        
    predictions = sess.run(output_layer_name, {ph: image_data} )
参考资料:

7. 参考资料

  1. TensorFlow 教程 #07 - Inception 模型
  2. 『TensorFlow』迁移学习_他山之石,可以攻玉
  3. 『TensorFlow』迁移学习_他山之石,可以攻玉_V2
  4. 使用inception-v3做各种图像的识别
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容