[PyTorch Tutorial] 04-Detailed explanation of the update of the pre-training model loading in torchvision 0.13 and the solution to the error report (latest in 2022)




Load pre-trained model (with major update)

I believe that comrades who have installed or updated PyTorch and torchvision recently (July 2022) may have encountered one of the following errors when running the code:

  • UserWarning: The parameter ‘pretrained’ is deprecated since 0.13 and will be removed in 0.15, please use ‘weights’ instead.

  • UserWarning: Arguments other than a weight enum or None for ‘weights’ are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing weights=ResNet50_Weights.IMAGENET1K_V1. You can also use weights=ResNet50_Weights.DEFAULT to get the most up-to-date weights.

  • Expected type ‘Optional[ResNet50_Weights]’, got ‘str’ instead

This is because torchvision 0.13 has made a major update to the pre-trained model loading method. Today, all the above 3 bugs can be eliminated at once.


Starting from torchvision 0.13, torchvision provides a new multi-weight support API (Multi-weight support API), which supports loading different versions of weight parameter files into the model.


1. Comparison between old and new versions

Starting from torchvision 0.13, the parameters of the load pre-trained model function are pretrained = Truechanged from weights=预训练模型参数版本. And the old version will be deprecated in the future torchvision 0.15 version.

for example:

from torchvision import models

# 旧版本的写法,将在未来的torchvision 0.15版本中被Deprecated
model_old = models.resnet50(pretrained=True) # deprecated
model_old = models.resnet50(True) # deprecated

# torchvision 0.13及以后的新版本写法
model_new = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
# 没有预训练模型加载
model = models.resnet50(weights=None)
model = models.resnet50()

Among them, the 8th line of code IMAGENET1K_V1represents the weight parameter file of the first version of ResNet-50 pre-trained on the ImageNet dataset. is a version identifier.


2. The benefits of the new way of writing

In the old version of writing pretrained = True, we don't have much choice for the pre-training weight parameters, and we must use the default pre-training weight file version as soon as it is executed. But the problem is that the development of deep learning is changing with each passing day, and models with stronger performance will soon be born.

Using the new version of writing weights=预训练模型参数版本means that we have the right to choose the pre-training weight parameter file. We can use more accurate, faster, stronger and updated pre-training weight parameter files to our heart's content, helping our research to a higher level.


for example:

from torchvision import models

# 加载精度为76.130%的旧权重参数文件V1
model_v1 = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
# 等价写法
model_v1 = models.resnet50(weights="IMAGENET1K_V1")

# 加载精度为80.858%的新权重参数文件V2
model_v2 = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V2)
# 等价写法
model_v1 = models.resnet50(weights="IMAGENET1K_V2")

If you don't know which version of the weight file is the latest, it doesn't matter, just choose the default DEFAULT. The official will keep the DEFAULT weight file version up to date with the upgrade of torchvision. As shown in the following code:

from torchvision import models

# 如果你不知道哪个版本是最新, 直接选择默认DEFAULT即可
model_new = models.resnet50(weights=models.ResNet50_Weights.DEFAULT)

Guess you like

Origin blog.csdn.net/Sihang_Xie/article/details/125646287