Keras:rescale=1./255 vs preprocessing_function=preprocess_输入要使用哪一个?

2024-10-06 00:07:47 发布

您现在位置:Python中文网/ 问答频道 /正文

背景

我发现了很多代码示例,在这些示例中,人们要么使用rescale=1./255对图像数据进行预处理,要么使用preprocessing_function将其设置为他们在ImageDataGenerator中使用的相应模型的preprocess_input。首先,我认为使用rescale=1./255只在处理预先训练的vgg16模型时有效,但是我不断看到一些例子,其中它也用于预先训练的resetnet50、inception等。在

当keras博客(https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html)使用这种方法时。。。在

ImageDataGenerator(rescale=1./255, ...

。。。Keras文档(https://keras.io/applications/)使用这种方法:

^{pr2}$

我认为使用我要训练的相应模型的预处理_输入总是优于使用rescale=1./255方法,因为它将100%地反映在训练预训练模型期间使用的预处理。在

问题

我需要一些关于何时使用rescale=1./255vs keras内置的preprocess_input的相应模型的preprocess_input来进行迁移学习的预处理。这是否只在使用预先训练的模型时才有区别,即负重与从头开始训练?在


Tags: 数据方法代码httpsio模型图像示例
2条回答

First I thought using rescale=1./255 only works when dealing with a pretrained vgg16 model, but I keep seeing examples where it is being used with pre-trained resetnet50, inception etc. as well.

这样做的原因是您需要规范化您的输入。通常,最小最大规格化的公式为

enter image description here

相当于

1./255

因为图像的像素值在0和1之间

规范化输入的原因与数值稳定性和收敛性有关(从技术上讲,你不需要它,但有了它,神经网络收敛的几率更高,而梯度下降/adam算法更可能稳定)

根据Does this only make a difference when using pretrained-models i.e. with loaded weights vs training from scratch?不,它不只是与预先训练的模型相联系,它是在机器学习中使用某些算法(神经网络就是其中之一)时的常用技术。在

如果您有兴趣真正了解这一切背后的原因以及规范化为何如此重要,我强烈建议您使用Andrew Ng course on machine learning

我也有类似的问题,在运行了下面的小实验之后,我认为您需要在使用预先训练的模型时始终使用preprocess_input,并在从头开始训练时使用rescale。在

显然,当您直接使用预先训练的模型进行推理时,您必须使用preprocess_input:例如,我试图在kaggle dogs vs cats数据集上使用resnet50,使用rescale=1./255它返回索引111(nematode, nematode worm, roundworm),作为所有图像最可能的类,而使用preprocess_input它主要返回与狗和猫如所料。在

然后我尝试使用resnet50include_top=False、来自imagenet的冻结权重、一个GlobalAveragePooling2D层和最后一个密集的sigmoid层。我和亚当一起训练了2000张kaggle狗对猫的图片(我用1000张图片作为验证)。使用rescale它在5个时期后都无法适应任何情况,它总是预测第一个类(虽然奇怪的是训练精度97%,但是当我运行evaluate_generator`` on the training images, the accuracy is **50%**). But withpreprocess_input, it achieves **98%** accuracy on the validation set. Also note that you do not really need the images to be of the same dimensions as the trained models, for example if I use 150 instead of 224, I still get a **97.5%** accuracy. Without any rescaling orpreprocess_input`,我得到了一个95%的验证准确率。在

我用vgg16做了同样的尝试,重新调整了它的大小,但是使用preprocess_input87%97%,没有任何东西,95%。在

然后我从零开始训练了一个10个时期的小型conv网络,没有任何东西或者使用resnet50preprocess_input,它根本不适合,但是通过重新缩放,我获得了70%验证准确率。在

相关问题 更多 >