构建自己的图片分类模型

使用retrain构建自己的图片分类模型

阅读这篇文章前,可以先看一下inception初探

1.inception模型的变换

关于图像分类我们可以有三种方式去构造训练模型
①完全自己搭建模型
②通过改变已有模型比如inception的结构,但是没有更改的部分的参数是固定的。
③改变已有的模型结构,之前的参数也进行重新训练,不过给予较低的学习率,不做大幅度调整
这里我们采用第二种方式,首先看一下结构图:
《构建自己的图片分类模型》
这里我们将原先的softmax输出抛弃,自定义分类的种类,训练参数,得到自己的分类模型。
具体的步骤如下:(使用的训练数据集可以去牛津大学的Visual Geometry Group下载)
1.1.新建好以下目录
bottleneck(空文件夹)
data(存放要训练的图片数据)
images(用来测试的图片数据)
1.2下载以下代码:
retrain.py(在inception模型基础上进行自己图片分类的代码)
1.3.编写脚本文件

#window下的批处理文件
#logdir 为retrain.py文件的根路径
#modeldir 为inception模型的参数模型文件(tgz格式)
#imagedir 为自定义训练的图片路径(图片的路径下各种类型的图片建立自己的文件夹,同时不能有大写字母)
python logdir/retrain.py ^
--bottleneck_dir bottleneck ^
--how_many_training_steps 200 ^
--model_dir modeldir ^
--output_graph output_graph.pb ^
--output_labels output_labels.txt ^
--image_dir datadir
#linux 下的脚本文件 run.sh
#!/bin/sh
python logdir/retrain.py \
--bottleneck_dir bottleneck \
--how_many_training_steps 200 \
--model_dir modeldir \
--output_graph output_graph.pb \
--output_labels output_labels.txt \
--image_dir datadir

1.4运行脚本文件
运行后会生成output_graph.pb和output_labels.txt两个文件 。文件夹下的内容如下图:
《构建自己的图片分类模型》
第一个pb文件为参数文件,第二个txt是标识图片类别的文件,同时bottleneck文件夹中也会有数据,每张图片都会有一个文件保存数据:
《构建自己的图片分类模型》
文件中的内容为该图片到bottlenck后所有计算的数据,因为一种图片传入进来之后,按照inception模型中的参数计算到bottleneck,保存的就是每一步图片与模型参数计算后的结果: 《构建自己的图片分类模型》
之后就可以用这两个文件(.pb和.txt文件)来进行图片分类。

2.使用图片测试分类

测试的步骤很简单
1.建立一个images文件夹在文件中存放需要测试的图片
2.编写测试代码,测试代码很简单,先读取txt中的内容,使用dictionary形成一个id到标签内容的映射。之后读取参数文件,和张量final_result,sess.run()一下final_result就可以了。具体代码如下:

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('retrain/output_labels.txt').readlines()
uid_to_human ={}
#读取参数中的数据
for uid,line in enumerate(lines):
    line=line.strip('\n')
    uid_to_human[uid]=line
def id_to_string(node_id):
    if node_id not in uid_to_human:
        return ''
    return uid_to_human[node_id]
#创建图来存放训练好的模型参数
with tf.gfile.FastGFile('retrain/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('retrain/images/'):
        for file in files:
            #载入图片
            image_data = tf.gfile.FastGFile(os.path.join(root,file),'rb').read()
            #jpeg格式的图片
            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()
            #排序
            top_k = predictions.argsort()[::-1]
            print(top_k)
            for node_id in top_k:
                human_string =id_to_string(node_id)
                #置信度
                score = predictions[node_id]
                print ('%s (score = %.5f)' % (human_string, score))
            print()

运行后可以得到分类的结果:
horse的测试: 《构建自己的图片分类模型》
airplane的测试: 《构建自己的图片分类模型》

    原文作者:人工智能
    原文地址: https://my.oschina.net/caibobit/blog/1591053#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞