深度学习:UserWarning: The parameter ‘pretrained‘ is deprecated since 0.13..解决办法

深度学习:UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead. 解决办法

1 报错警告:

pytorch版本:0.14.1

在利用pytorch中的预训练模型时,如resnet18

复制代码
import torchvision.models as models
pretrained_model = models.resnet18(pretrained=True)

会提示警告:

复制代码
UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  f"The parameter '{pretrained_param}' is deprecated since 0.13 and may be removed in the future, "

看出给出的原因是在0.13版本后,开始使用weights参数。

2 处理方法:

接下来为处理这个问题的方法,不同的预训练模型方法适用 以model.resnet18()为例

  • 首先点击models.resnet18()函数,进入函数内部,可以看到如下内容

    复制代码
    @handle_legacy_interface(weights=("pretrained", ResNet18_Weights.IMAGENET1K_V1))

    def resnet18(*, weights: Optional[ResNet18_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet:
    """ResNet-18 from Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>__.

    复制代码
      Args:
          weights (:class:`~torchvision.models.ResNet18_Weights`, optional): The
              pretrained weights to use. See
              :class:`~torchvision.models.ResNet18_Weights` below for
              more details, and possible values. By default, no pre-trained
              weights are used.
          progress (bool, optional): If True, displays a progress bar of the
              download to stderr. Default is True.
          **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
              base class. Please refer to the `source code
              <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
              for more details about this class.
    
      .. autoclass:: torchvision.models.ResNet18_Weights
          :members:
      """
      weights = ResNet18_Weights.verify(weights)
    
      return _resnet(BasicBlock, [2, 2, 2, 2], weights, progress, **kwargs)
  • 首先看到第一行weights=("pretrained", ResNet18_Weights.IMAGENET1K_V1),所以这个还是可以用的,相当于利用了ResNet18_Weights.IMAGENET1K_V1参数。然后看第二行的这个weights函数接受的ResNet18_Weights,再次进入内部,可以看到如下:

    class ResNet18_Weights(WeightsEnum):
    IMAGENET1K_V1 = Weights(
    url="https://download.pytorch.org/models/resnet18-f37072fd.pth",
    transforms=partial(ImageClassification, crop_size=224),
    meta={
    **_COMMON_META,
    "num_params": 11689512,
    "recipe": "https://github.com/pytorch/vision/tree/main/references/classification#resnet",
    "_metrics": {
    "ImageNet-1K": {
    "acc@1": 69.758,
    "acc@5": 89.078,
    }
    },
    "_docs": """These weights reproduce closely the results of the paper using a simple training recipe.""",
    },
    )
    DEFAULT = IMAGENET1K_V1

这个是选择的参数,其他的预训练模型可以有多个版本,如下面ResNet50_Weights, 可以根据自己需求选择需要的。

  • 上面的函数已经给出了调用方法Args: weights (:class:~torchvision.models.ResNet18_Weights, optional)
    所以直接

    pretrained_model = models.resnet18(models.ResNet18_Weights.IMAGENET1K_V1)

也可以

复制代码
pretrained_model = models.resnet18(models.ResNet18_Weights.DEFAULT)

这两个是一样的。

3.总结

在版本更新之后可能会有些变化,有些函数调用方式的变化可以直接通过函数内部查看然后修改,重点是修改思路解决相同类似的问题。

相关推荐
美狐美颜sdk2 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
DeepSeek-大模型系统教程2 小时前
推荐 7 个本周 yyds 的 GitHub 项目。
人工智能·ai·语言模型·大模型·github·ai大模型·大模型学习
有Li2 小时前
通过具有一致性嵌入的大语言模型实现端到端乳腺癌放射治疗计划制定|文献速递-最新论文分享
论文阅读·深度学习·分类·医学生
郭庆汝2 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
小雷FansUnion4 小时前
深入理解MCP架构:智能服务编排、上下文管理与动态路由实战
人工智能·架构·大模型·mcp
资讯分享周4 小时前
扣子空间PPT生产力升级:AI智能生成与多模态创作新时代
人工智能·powerpoint
思则变5 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
叶子爱分享6 小时前
计算机视觉与图像处理的关系
图像处理·人工智能·计算机视觉
鱼摆摆拜拜6 小时前
第 3 章:神经网络如何学习
人工智能·神经网络·学习
一只鹿鹿鹿6 小时前
信息化项目验收,软件工程评审和检查表单
大数据·人工智能·后端·智慧城市·软件工程