在使用pytorch官方给出的torchvision中的预训练模型参数时为保证收敛性要求使用原始的数据预处理方式

本文主要内容如题:

在使用pytorch官方给出的torchvision中的预训练模型参数时为保证收敛性要求使用原始的数据预处理方式

具体的pytorch官方讨论:

https://github.com/pytorch/vision/pull/782

===================================================

原文最初的讨论是数据预训练是否在模型训练的时候显示的给出:

回答是:

由于数据预处理步骤与训练数据集相关而不是与模型相关,所以官方将模型的预训练的步骤写在了torchvision中的dataset部分。

但是这个讨论最为有信息量的回答是:

从这个回答中我们知道两个点,

第一点:

那就是torchvision中的数据预处理虽然写在了torchvision中的dataset里面,但是实际上 googlenet and inceptionV3 这两个模型的数据预处理是与其他模型不同的。其他模型的数据预处理方式中有正则化操作:

 但是,googlenet and inceptionV3 ,并不进行数据显示的正则化操作。

第二点:

 The normalisation is not independent of the model if you are using transfer learning. Not using imagenet normalisation (by default) while using imagenet pretrained weights will lead to slower convergence of the model as the models are expecting the images to come from the distribution having mean and std as that of imagenet on which it was thoroughly trained.

数据预处理虽然和模型无关只和数据集有关,但是如果使用的是预训练模型参数那么数据的预处理方式也很重要,因为在迁移学习的时候迁移后的模型还是更适用于采用了原始预处理方式的数据,不然的话会减慢模型的收敛。迁移后的模型还是期望获得原始数据(预训练的数据集)分布的数据(相同的均值和方差)。

==================================================

Yes, I have a source. Please have a look at the inceptionV3 (here) and googlenet (here) implementation.

googlenet 网络模型的数据处理方式:

https://github.com/pytorch/vision/blob/9d9f48a3b483771ad0ed50ed871c1677edef6d2a/torchvision/models/googlenet.py#L81-L85

inception  网络模型的数据处理方式:

https://github.com/pytorch/vision/blob/9d9f48a3b483771ad0ed50ed871c1677edef6d2a/torchvision/models/inception.py#L76-L80

可以看到,这两个网络其实是不进行数据预处理的,因为为了保证数据集中数据预处理的统一性所以在dataset中所有模型均进行了数据正则化操作,但是实际这两个模型不需要数据正则化所以在模型定义中这两个模型又进行了逆正则化操作,也就是将dataset中的数据正则化进行消除。

====================================================

通过该文知道了torchvision 中googlenet 和 inception 网络数据预处理的不同于其他模型,同时也知道了在模型迁移时我们是有必要知道预训练参数的训练数据的预处理操作的,如果预训练时候对数据进行了正则化那么迁移后也是需要对新数据进行同样的正则化,不然就会损失掉模型的收敛性。

=========================================

原文地址:https://www.cnblogs.com/devilmaycry812839668/p/15595168.html