当前位置:   article > 正文

Keras使用Pretrain自己训练好的模型,迁移学习1(tensorflow相关)_如何对自己训练好的模型进行迁移学习

如何对自己训练好的模型进行迁移学习

目的

使用之前在其他场景下训练好的CNN模型,用于当前图像的识别分类。已训练好的模型结构如下图,我只需要dense层之前的,所以仅需要这个模型的前几层。

  1. Model: "model"
  2. _________________________________________________________________
  3. Layer (type) Output Shape Param #
  4. =================================================================
  5. img (InputLayer) [(None, 1280, 1024, 1)] 0
  6. _________________________________________________________________
  7. conv2d (Conv2D) (None, 639, 511, 2) 20
  8. _________________________________________________________________
  9. batch_normalization (BatchNo (None, 639, 511, 2) 8
  10. _________________________________________________________________
  11. activation (Activation) (None, 639, 511, 2) 0
  12. _________________________________________________________________
  13. max_pooling2d (MaxPooling2D) (None, 319, 255, 2) 0
  14. _________________________________________________________________
  15. conv2d_1 (Conv2D) (None, 159, 127, 4) 76
  16. _________________________________________________________________
  17. batch_normalization_1 (Batch (None, 159, 127, 4) 16
  18. _________________________________________________________________
  19. activation_1 (Activation) (None, 159, 127, 4) 0
  20. _________________________________________________________________
  21. max_pooling2d_1 (MaxPooling2 (None, 79, 63, 4) 0
  22. _________________________________________________________________
  23. conv2d_2 (Conv2D) (None, 77, 61, 4) 148
  24. _________________________________________________________________
  25. batch_normalization_2 (Batch (None, 77, 61, 4) 16
  26. _________________________________________________________________
  27. activation_2 (Activation) (None, 77, 61, 4) 0
  28. _________________________________________________________________
  29. max_pooling2d_2 (MaxPooling2 (None, 38, 30, 4) 0
  30. _________________________________________________________________
  31. global_average_pooling2d (Gl (None, 4) 0
  32. _________________________________________________________________
  33. dense (Dense) (None, 10) 50
  34. _________________________________________________________________
  35. dropout (Dropout) (None, 10) 0
  36. _________________________________________________________________
  37. dense_1 (Dense) (None, 10) 110
  38. _________________________________________________________________
  39. dropout_1 (Dropout) (None, 10) 0
  40. _________________________________________________________________
  41. dense_2 (Dense) (None, 1) 11
  42. =================================================================
  43. Total params: 455
  44. Trainable params: 435
  45. Non-trainable params: 20
  46. _________________________________________________________________

构建方法

使用keras构建一般模型代码如下:

  1. inp1 = Input(shape=(1280,1024,1),name="img")#输入层
  2. out = Dense(20)(inp1)#一个全连接层
  3. out = Dropout(0.5)(out)#dropout层
  4. out = Dense(1)(out)#输出层
  5. model = Model(inputs = inp1,outputs = out)#构建模型

将PreTrain好的模型的一些层放在自己的模型中代码如下:(迁移学习)

  1. inp1 = Input(shape=(1280,1024,1),name="img")#输入层
  2. #加入预训练的模型的卷积层,
  3. conv = load_model('pre_train.h5')#此模型结构如图1
  4. conv.trainable = False#设置让预训练好的模型的参数不更新
  5. for i,layer in enumerate(conv.layers[1:-5]):#将conv模型的第一层到倒数第5层,也就是dense的前一层加入模型。
  6. if(i==0):
  7. out = layer(inp1)
  8. else:
  9. out = layer(out)
  10. #后面接自己的网络层。
  11. out = Dense(20)(out)#一个全连接层,将inp1改为out
  12. out = Dropout(0.5)(out)#dropout层
  13. out = Dense(1)(out)#输出层
  14. model = Model(inputs = inp1,outputs = out)#构建模型

总结

这是最近用到的一个方法,学习记录一下,还有一种利用官方或者学术大佬公开模型参数进行迁移学习的,放在下一篇文章中记录。

PS:需要知道模型结构每层是什么网络层,方便后面按照层数放入自己的模型。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/732183
推荐阅读
相关标签
  

闽ICP备14008679号