赞
踩
使用之前在其他场景下训练好的CNN模型,用于当前图像的识别分类。已训练好的模型结构如下图,我只需要dense层之前的,所以仅需要这个模型的前几层。
- Model: "model"
- _________________________________________________________________
- Layer (type) Output Shape Param #
- =================================================================
- img (InputLayer) [(None, 1280, 1024, 1)] 0
- _________________________________________________________________
- conv2d (Conv2D) (None, 639, 511, 2) 20
- _________________________________________________________________
- batch_normalization (BatchNo (None, 639, 511, 2) 8
- _________________________________________________________________
- activation (Activation) (None, 639, 511, 2) 0
- _________________________________________________________________
- max_pooling2d (MaxPooling2D) (None, 319, 255, 2) 0
- _________________________________________________________________
- conv2d_1 (Conv2D) (None, 159, 127, 4) 76
- _________________________________________________________________
- batch_normalization_1 (Batch (None, 159, 127, 4) 16
- _________________________________________________________________
- activation_1 (Activation) (None, 159, 127, 4) 0
- _________________________________________________________________
- max_pooling2d_1 (MaxPooling2 (None, 79, 63, 4) 0
- _________________________________________________________________
- conv2d_2 (Conv2D) (None, 77, 61, 4) 148
- _________________________________________________________________
- batch_normalization_2 (Batch (None, 77, 61, 4) 16
- _________________________________________________________________
- activation_2 (Activation) (None, 77, 61, 4) 0
- _________________________________________________________________
- max_pooling2d_2 (MaxPooling2 (None, 38, 30, 4) 0
- _________________________________________________________________
- global_average_pooling2d (Gl (None, 4) 0
- _________________________________________________________________
- dense (Dense) (None, 10) 50
- _________________________________________________________________
- dropout (Dropout) (None, 10) 0
- _________________________________________________________________
- dense_1 (Dense) (None, 10) 110
- _________________________________________________________________
- dropout_1 (Dropout) (None, 10) 0
- _________________________________________________________________
- dense_2 (Dense) (None, 1) 11
- =================================================================
- Total params: 455
- Trainable params: 435
- Non-trainable params: 20
- _________________________________________________________________
使用keras构建一般模型代码如下:
- inp1 = Input(shape=(1280,1024,1),name="img")#输入层
- out = Dense(20)(inp1)#一个全连接层
- out = Dropout(0.5)(out)#dropout层
- out = Dense(1)(out)#输出层
-
- model = Model(inputs = inp1,outputs = out)#构建模型
将PreTrain好的模型的一些层放在自己的模型中代码如下:(迁移学习)
- inp1 = Input(shape=(1280,1024,1),name="img")#输入层
-
- #加入预训练的模型的卷积层,
- conv = load_model('pre_train.h5')#此模型结构如图1
- conv.trainable = False#设置让预训练好的模型的参数不更新
- for i,layer in enumerate(conv.layers[1:-5]):#将conv模型的第一层到倒数第5层,也就是dense的前一层加入模型。
- if(i==0):
- out = layer(inp1)
- else:
- out = layer(out)
- #后面接自己的网络层。
- out = Dense(20)(out)#一个全连接层,将inp1改为out
- out = Dropout(0.5)(out)#dropout层
- out = Dense(1)(out)#输出层
-
- model = Model(inputs = inp1,outputs = out)#构建模型
总结
这是最近用到的一个方法,学习记录一下,还有一种利用官方或者学术大佬公开模型参数进行迁移学习的,放在下一篇文章中记录。
PS:需要知道模型结构每层是什么网络层,方便后面按照层数放入自己的模型。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。