The parameter ‘pretrained‘ is deprecated since 0.13 and may be removed in the future ...的参考解决方法
The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future ...的参考解决方法
·
写在前面
自己的测试环境:
Ubuntu 20.04,
anaconda
一、问题描述
程序中调用下列语句时
vgg16 = torchvision.models.vgg16(pretrained=True)
出现如下警告:
/lib/python3.8/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
二、解决方法
针对提到的警告信息,这是由于torchvision库在新版本中对加载预训练模型的方式进行了更新。警告提示pretrained参数已被弃用,并建议使用weights参数。
对于torchvision.models.vgg16(pretrained=True)这样的调用,您应该将其更新为使用weights参数,并且需要从torchvision.models中导入VGG16_Weights。
因此调用的代码应该改为:
# 使用新的'weights'参数
vgg16 = torchvision.models.vgg16(weights=models.VGG16_Weights.DEFAULT)
在这里,models.VGG16_Weights.DEFAULT指定了要加载的预训练权重。torchvision提供了几种不同的权重选项,例如默认的DEFAULT,还有其他选项,如IMAGENET1K_V1等,具体取决于您的需求。
这里的IMAGENET1K_V1指的是预训练模型是在ImageNet-1K数据集上训练的版本1,而DEFAULT将加载torchvision认为最合适的权重版本。
参考链接
[1] kimi
更多推荐



所有评论(0)