预训练模型,首先回答三个问题
- What?
预训练模型(pre-trained model)是前人为了解决类似问题所创造出来的模型。简单来说就是不用在去从零开始解决一个问题,
这里要说明的是一个预训练模型不一定完全契合于你的应用,但是不得不说它可以为你节省大量功夫。- Why?
减少训练时间- How?
训练神经网络时,我们希望网络能够在多次正向反向迭代的过程中,找到合适的权重。通过使用之前在大数据集上经过训练的预训练模型,我们可以直接使用相应的结构和权重,将它们应用到我们正在面对的问题上。这被称作“迁移学习”,即将预训练的模型“迁移”到我们正在应对的特定问题中。
在选择预训练模型的时候你需要非常仔细,如果你的问题与预训练模型训练情景下又很大的出入,那么模型所得到的预测结果将会非常不准确。
在迁移学习中,这些预训练的网络对于ImageNet数据集外的图片也表现出类很好的泛化性能,既然预训练模型已经训练得很好,我们就不会在段时间内去修改过多的权重,在迁移学习中用到它的时候,往往只是进行微调(fine tune)。
在修改模型的过程中,我们通过会采用比一般训练模型更低的学习速率。下面给出自己使用的Keras.applications.models权重资源地址:
- VGG16:
WEIGHTS_PATH = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5’
WEIGHTS_PATH_NO_TOP = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5’- VGG19:
TF_WEIGHTS_PATH = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5’
TF_WEIGHTS_PATH_NO_TOP = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5’- RESNET50:
WEIGHTS_PATH = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5’
WEIGHTS_PATH_NO_TOP = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5’- INCEPTIONS_V3:
WEIGHTS_PATH = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5’
WEIGHTS_PATH_NO_TOP = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5’- XCEPTION:
TF_WEIGHTS_PATH = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5’
TF_WEIGHTS_PATH_NO_TOP = ‘https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels_notop.h5’详细信息可以进入https://github.com/fchollet/deep-learning-models/releases查看
1. git上release的文件的下载
对于git上release的文件,可以打开下载git文件网站,复制下载的链接进行下载,界面很简单,方便使用。
2. 加载权重过慢或失败的解决
2.1 加载文件
Linux: 将下载好的*.h5文件下载到用户目录下的~/.keras/models
Windows: 在电脑的地址栏中输入%userprofile%,将下载好的*.h5文件放入.keras/models文件夹下模型的预训练权重在载入模型时自动载入。(eg:C:\Users\你的用户名.keras\models)
2.2 相对文件路径进行加载
为了防止C盘占用空间过多,也可以采用相对路径进行存储和加载
path='./[权重文件存放路径]/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
vgg16 = VGG16(weights=path, include_top=True)
vgg16.summary()
说明:上面的tf代表使用tensorflow作为后端,如果使用theano作为后端的则需要下载_th_版本的。
keras中文文档对此的说明:
http://keras-cn.readthedocs.io/en/latest/backend/