前言
在前面(一)的代码中,我在训练一个自己的数据集时候(缺陷检测),epoch为1500,但最终的loss仍然很高,为9左右。
因此检测效果也很差,如图中所示,缺陷没检测出来。
这个时候该怎么办?增加epoch?这个方法不仅费时,而且很有可能效果仍然不好。
有一种简单的方法就是微调参数。
参数微调
1.预训练
先对整个数据集进行一次pre-training(预训练),得到每一层的weights和bias。
2.重新训练,微调参数
Freeze前面若干层的权值,而对最后几层的参数进行重新训练,从而得到较低的loss和较高的准确率。
其实大家可以发现,这个就是和迁移学习中的内容几乎一致。
把别人的权值(如vgg19等)直接拿来训练自己的模型,冻结前面的参数,把最后几层的参数重新训练,从而适应自己的数据集。
代码分析
这段代码是前面(一)中的代码,我们主要分析tiny_train.py的主要代码,也就是训练的代码。
先看main函数,前面几个路径不用看,只需要看到后面几句话:
一个是input_shape ,表示样本都被转化为416*416
还有个是create_model函数,表示创建模型
train当然是训练模型了
def _main():
annotation_path = 'train.txt'
log_dir = 'logs/'
classes_path = 'model_data/voc_classes.txt'
anchors_path = 'model_data/tiny_yolo_anchors.txt'
class_names = get_classes(classes_path)
anchors = get_anchors(anchors_path)
input_shape = (416,416) # multiple of 32, hw
#input_shape = (224,224)
model = create_model(input_shape, anchors, len(class_names) )
train(model, annotation_path, input_shape, anchors, len(class_names), log_dir=log_dir)
这段是train函数,设置batch_size为16,因为我的显存是6g的,所以比较小,epoch为1500,保存参数写在这里。
def train(model, annotation_path, input_shape, anchors, num_classes, log_dir='logs/'):
model.compile(optimizer=