本文使用Tensorflow1.11,Python3.6编写。Inception-v3原理不是本文重点,感兴趣的可以参考相关论文。
1、使用Tensorboard可视化Inception-v3
import tensorflow as tf
import os
import tarfile
import requests
#inception模型下载地址(最好只用下载软件手动下载,否则速度太慢)
inception_pretrain_model_url = "http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz"
#模型存放地址,
inception_pretrain_model_dir = "inception_model" #此文件夹如果不存在会自动创建
if not os.path.exists(inception_pretrain_model_dir):
os.makedirs(inception_pretrain_model_dir)
#获取文件名,以及文件路径
filename = inception_pretrain_model_url.split('/')[-1]
filepath = os.path.join(inception_pretrain_model_dir, filename)
#下载模型
if not os.path.exists(filepath):
print("download: ", filename)
r = requests.get(inception_pretrain_model_url, stream=True)
with open(filepath, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print("finish: ", filename)
#解压文件
tarfile.open(filepath, 'r:gz').extractall(inception_pretrain_model_dir)
#模型结构存放文件
log_dir = 'inception_log'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
#classify_image_graph_def.pb为google训练好的模型
inception_graph_def_file = os.path.join(inception_pretrain_model_dir, 'classify_image_graph_def.pb')
#'classify_image_graph_def.pb'为inception-v3中训练好的一个模型
with tf.Session() as sess:
#创建一个图来存放google训练好的模型
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()
在tensorboard中打开log_dir即可看到网络结构:
其中重点是mix结构,展开可以看到:
2、使用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):
#imagenet_2012_challenge_label_map_proto.pbtxt文件说明:
#LabelMap from ImageNet 2012 full data set UID to int32 target class
#包含target_class,共计1000个分类(int32),每个分类对应一个target_class_string(UID:imagenet_synset_to_human_label_map.txtimagenet_synset_to_human_label_map.txt)
label_lookup_path = 'inception_model/imagenet_2012_challenge_label_map_proto.pbtxt'
#imagenet_synset_to_human_label_map.txt文件说明:
#target_class_string(UID)与对应的描述,如:n00007846对应person, individual, someone, somebody, mortal, soul
uid_lookup_path = 'inception_model/imagenet_synset_to_human_label_map.txt'
self.node_lookup = self.load(label_lookup_path, uid_lookup_path)
#f返回分类编号1-1000对应分类名称的映射关系的字典
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')
parsed_items = line.split('\t')
uid = parsed_items[0] # uid如n15092227
human_string = parsed_items[1] #对应的描述
uid_to_human[uid] = human_string#将uid与描述之间的映射存储为字典形式
#将在分类字符串n*****对应分类编号1-1000的文化
proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
node_id_to_uid = {}
for line in proto_as_ascii:
#获取分类编号1-1000
if line.startswith(' target_class:'):
target_class = int(line.split(': ')[1])
#获取编号字符串n*****
if line.startswith(' target_class_string:'):
target_class_string = line.split(': ')[1]
#保存分类编号1-1000与编号字符串n********的映射关系
node_id_to_uid[target_class] = target_class_string[1: -2] #0和-1是引号,不需要
#建立分类编号1-1000对应分类名称的映射关系
node_id_to_name = {}
for key, val in node_id_to_uid.items(): #key:分类编号 val:编号字符串
name = uid_to_human[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]
# 创建一个图来存放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的分类器,用softmax分类器进行预测,其中得到的predictions是二维的
softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
# 遍历目录
for root, dirs, files in os.walk('images/'): #root:/images dirs:子目录,此处为空,files是图片
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维
#打印图片路径和名称
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))
print()
结果如下: