根据AlexNet模型做出微调https://kratzert.github.io/2017/02/24/finetuning-alexnet-with-tensorflow.html(原文出处)
正文为黑色,着重部分为红色,注释部分为彩色(按照颜色的排列顺序,重要程度递减)
一些简单的概述
我们所做的这个微调的方法很简单,就是先创建一个模型(当然了,这个模型并不是原始的AlexNet模型),我们将想要skip的layer的名字放在 skip_layer 当中(之所以要这么做,是因为在这个链接中的Alexnet是加载了训练好的参数,并没有自己训练的过程,如果你要修改他的网络,那么就需要重新训练你所更改的这部分),然后再tenso中设置我们的Loss和 optimizer ops , 最后一步,创建一个sess,训练我们的网络。
ps_1 : 链接中的代码还使用了tensorboard,如果你并不是很熟悉这个可视化工具,我将会在后面的部分粗略的说一下
ps_2 : 这部分微调Alexnet使用的是猫狗大战的数据集,可以自行下载,或者使用这个麻烦的方法(https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data)
数据集的处理
作者将数据集分成了三部分,分别是训练集(training_set),验证集(validation_set )和测试集(test_set),三者之间的比例是70:15:15;我们还要分别为这三个集合创建一个txt文件,用来存储image和class label 的路径。
有了这个txt文件,我们可以再来创建一个class作为图像数据生成器( image data generator ), 这个image data generator 的作用就像one of Keras for example,
这个使用image data generator可能并不是最好的方法,但是后面我们需要关注图像是如何进行装载和预处理的,这种方法会对我们要关注的部分提供很大的便利。
配置部分(The configuration part)
首先是import 部分的定义, 没有什么好说的;
import numpy as np
import tensorflow as tf
from datetime import datetime
from alexnet import AlexNet
from datagenerator import ImageDataGenerator
# # Path to the textfiles for the trainings and validation set
# 设置训练集和验证集的txt文件的路径
train_file = '/path/to/train.txt'
val_file = '/path/to/val.txt'
# Learning params
# 这里的参数是在训练过程中会用的的,譬如梯度下降中的learning rate等等。。
learning_rate = 0.001 # 梯度下降的步长
num_epochs = 10 # 一共10批()
batch_size = 128 # 每批包含128个数据
# 如果不懂,参考一下这位朋友的文章 http://m.elecfans.com/article/800487.html
# Network params
# 网络层的一些参数,(也就是我们在创建一个新的AlexNet实例的时候,需要注意改变的一些参数,)
# 参数的详细内容请查看原文中的这部分(class AlexNet(object): )
dropout_rate = 0.5
num_classes = 2
train_layers = ['fc8', 'fc7']
# 有关可视化的一些操作,如果你想理解这部分,你需要懂得一些tensorboard的知识
# 这里的checkPoint可能是存放参数的文件(这个模型有很大一部分参数是从训练好的模型中得到)
# How often we want to write the tf.summary data to disk
display_step = 1
# Path for tf.summary.FileWriter and to store model checkpoints
filewriter_path = "/tmp/finetune_alexnet/dogs_vs_cats"
checkpoint_path = "/tmp/finetune_alexnet/"
# Create parent path if it doesn't exist
if not os.path.isdir(checkpoint_path): os.mkdir(checkpoint_path)
作者这里选择调整的两层网络是fc7和fc8,我们可以根据我们自己的数据集和自己的需求来调整这个AlexNet网络,并不一定要照搬他的方法;
作者将几个参数的取值留在了之前的标注AlexNet当中(譬如dropout probability, learning rate ...),我们可以找到他们的位置并且修改它们,充分理解参数的作用并且掌握修改他们的方法才可以使得你的模型得到你想要的好结果。
- - - - - - - - - -
TensorFlow stuff - - parameter set
现在我们来完成一些在tensorflow中的工作;;;;
首先我们要为输出和label来创建一些变量,哦,当然这也包括dropout rate
(in test mode we deactivate dropout, while TensorFlow takes care of activation scaling)
x = tf.placeholder(tf.float32, [batch_size, 227, 227, 3]) # input
y = tf.placeholder(tf.float32, [None, num_classes]) # groundtruth
keep_prob = tf.placeholder(tf.float32) # dropout中,每个神经元被丢掉的概率为keep_prob
OK了,到这里,我们就可以来定义一个新的AlexNet类了,
# Initialize model
model = AlexNet(x, keep_prob, num_classes, train_layers)
# 对比一下之前标准AlexNet的定义
# def __init__(self, x, keep_prob, num_classes, skip_layer,
# weights_path = 'DEFAULT'):
"""
Inputs:
- x: tf.placeholder, for the input images
- keep_prob: tf.placeholder, for the dropout rate
- num_classes: int, number of classes of the new dataset
- skip_layer: list of strings, names of the layers you want to reinitialize
- weights_path: path string, path to the pretrained weights,
(if bvlc_alexnet.npy is not in the same folder)
"""
我们还要在创建一个指向模型输出的变量,也就是指向fc8的输出(这个输出是unscaled score 的,)
#link variable to model output
score = model.fc8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TensorFlow stuff - - train process
下面是我们训练网络是所要用到的素有的步骤
首先,我们来定义一个list用来存储我们需要训练的变量(再次重申