1 通过训练生成自己tensorflow .pb文件模型文件和.txt 标签文件
2 新建一个python代码,引入模型和标签文件,对某一图像进行处理得出图片类别
生成模型文件所需文件
1 训练集文件,文件内包含所需分类的文件夹,每个文件夹包含一类图像
2 inception-V3压缩包
3 tensorflow中的retrain.py文件
可以通过批处理文件来生成所需得到模型和标签文件
python retrain.py ^
--bottleneck_dir bottleneck ^
--how_many_training_steps 100 ^
--model_dir inception_model/ ^
--output_graph output_graph.pb ^
--output_labels output_labels.txt ^
--image_dir data
pause
新建一个python代码
首先新建一个字典,数字为key,标签为value
创建一个图来保存训练好的模型
创建对话处理图片
import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt
lines = tf.gfile.GFile('output_labels.txt').readlines()
uid_to_human = {}
for uid,line in enumerate(lines) :
line=line.strip('\n')
uid_to_human[uid] = line
with tf.gfile.FastGFile('output_graph.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('final_result: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})
predictions = np.squeeze(predictions)
top_k = predictions.argsort()[::-1]
node_id = top_k[0]
print(uid_to_human[node_id])