简体   繁体   English

如何使用 Keras 的深度学习模型解决不适合 imagenet 数据集的问题?

[英]How to use Deep Learning Models from Keras for a problem that does not fit imagenet dataset?

I followed a blog on how to implement a vgg16-model from scratch and want to do the same with the pretrained model from Keras.我关注了一篇关于如何从头开始实现 vgg16 模型的博客,并希望对来自 Keras 的预训练 model 做同样的事情。 I looked up some other blogs but can't find a fitting solution I think.我查阅了其他一些博客,但找不到我认为合适的解决方案。 My task is to classify integrated circuit images into defect or non defects.我的任务是将集成电路图像分类为缺陷或非缺陷。

I have seen on a paper that they used pretrained imagenet model of vgg16 for fabric defect detection, where they freezed the first seven layers and fine tuned the last nine for their own problem.我在一篇论文中看到他们使用 vgg16 的预训练 imagenet model 进行织物缺陷检测,他们冻结了前七层并针对自己的问题微调了后九层。 (Source: https://journals.sagepub.com/doi/full/10.1177/1558925019897396 ) (来源: https://journals.sagepub.com/doi/full/10.1177/1558925019897396

I have already seen examples on how to freeze all layers except the fully connected layers, but how can I try the example with freezing first x layers and fine tune the others for my problem?我已经看到了有关如何冻结除完全连接层之外的所有层的示例,但是如何尝试冻结前 x 层并针对我的问题微调其他层的示例?

The VGG16 is fairly easy to implement from scratch but for models like resnet or xception it is getting a little trickier. VGG16 从头开始实现相当容易,但对于 resnet 或 xception 等模型来说,它变得有点棘手。

It is not necessary to implement a model from scratch to freeze a few layers.没有必要从头开始实现 model 来冻结几层。 You can do this on pre-trained models as well.您也可以在预训练模型上执行此操作。 In keras, you'd use trainable = False .在 keras 中,您将使用 trainable trainable = False

For example, let's say you want to use the pre-trained Xception model from keras and want to freeze the first x layers:例如,假设您想使用来自 keras 的预训练 Xception model 并想冻结前 x 层:

#In your includes
from keras.applications import Xception

#Since you're using the model for a different task, you'd want to remove the top
base_model = Xception(weights='imagenet', include_top=False)

#Freeze layers 0 to x
for layer in base_model.layers[0:x]:
    layer.trainable = False

#To see all the layers in detail and to check trainable parameters
base_model.summary()

Ideally you'd want to add another layer on top of this model with the output as your classes.理想情况下,您希望在此 model 之上添加另一层,并将 output 作为您的类。 For more details, you can check this keras guide: https://keras.io/guides/transfer_learning/有关更多详细信息,您可以查看此 keras 指南: https://keras.io/guides/transfer_learning/

A lot of times the pre-trained weights can be very useful in other classification tasks but in case you want to train a model from scratch on your dataset, you can load the model without the imagenet weights.很多时候,预训练的权重在其他分类任务中非常有用,但如果您想在数据集上从头开始训练 model,您可以在没有 imagenet 权重的情况下加载 model。 Or better, load the weights but don't freeze any layers.或者更好的是,加载权重但不要冻结任何层。 This will retrain every layer taking imagenet weights as an initialization.这将重新训练每一层,将 imagenet 权重作为初始化。

I hope I've answered your question.我希望我已经回答了你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM