tensorflow(16)训练自己的模型

迁移学习:

物体识别模型可能含有数百万个参数,将耗费几周的时间去完全训练。因此我们采用迁移学习的方法,在已经训练好的模型(基于ImageNet)上调整部分参数(Inception_V3),实现自己数据集的分类。

3种方法:

  • 方法1:无到有(从头开始训练)自己准备好的数据,拿一个别的模型(inception模型),利用最最初始的参数重新开始训练一个属于自己数据集的模型【更改整个网络的参数
  • 方法2:改变模型的部分参数,只是修改最后一层,微调准备一个模型(inception3的模型),在不改变部分参数卷积层和池化层,改变最后一层(因为是5个类),就是主要是更改绿色的箭头的部分,红色的不改变。利用基于ImageNet图像训练的Inception_v3模型所导出的pb文件,更改最后的softmax layer为自己需要的分类器,然后对这一更改的softmax layer进行训练除开最后一层,其他层的参数全部固化,无法更新。因此,在实际的Retrain中,往往先将数据集(包含训练集、验证集与测试集)中的所有图片导入到Inception_v3模型中,获取最后一层的输入,或者说是倒数第二层的输出,定义为Bottlenecks。然后直接使用Bottlenecks对最后更改的softmax layer进行训练,将大幅度提升训练速度

方法2优点:(训练速度快[只计算最后一层])(因为参数少,需要训练周期少)(需要的图片数据量比较少

  • 方法3:对一个网络调优:基于方法2,卷积和池化的参数不是固定,是小的学习率,进行参数微调主要是在最后一层进行更改,原来的是1000个分类,现在的是5个分类。利用基于ImageNet图像训练的Inception_v3模型所导出的Ckpt文件,在训练过程中,整个网络的参数都可以随之修改,不仅仅局限于被替换掉的softmax layer

方法2的实践:

参考:https://blog.csdn.net/weixin_38663832/article/details/80555341

视频讲解https://www.bilibili.com/video/av20542427/?p=29   (class29、09-2)

训练步骤:

1.)数据集下载  http://www.robots.ox.ac.uk/~vgg/data/ 

2.)retrain.py下载  https://github.com/tensorflow/hub

      本来是这个地址https://github.com/tensorflow/tensorflow/,但是却没有retrain.py文件

3.)inception-v3 模型下载  http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz

4.)批处理 retrain.bat,双击训练

python C:/Users/ZSl/Documents/hub-master/examples/image_retraining/retrain.py ^
--bottleneck_dir bottleneck ^
--how_many_training_steps 200 ^
--model_dir C:/Users/ZSl/Documents/inception_model/ ^
--output_graph output_graph.pb ^
--output_labels output_labels.txt ^
--image_dir C:/Users/ZSl/Documents/retrain/data/train/
pause

5.)利用训练好的模型进行测试(图像分类)

# coding: utf-8
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]
 
 
#创建一个图来存放google训练好的模型
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()
            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()[::-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()

方法1的实践:

视频讲解:https://www.bilibili.com/video/av20542427/?p=30  (class30、09-3)

步骤(随着代码的解释):

1.)(图片预处理)生成tfrecord:把自己的图片装换为tfrecord文件,后期再训练的时候实际上读取的是这个文件,而不是图片。

2.)批处理文件训练

猜你喜欢

转载自blog.csdn.net/zhaoshuling1109/article/details/81538694