当前位置: 首页 > news >正文

深度学习: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): Thepretrained weights to use. See:class:`~torchvision.models.ResNet18_Weights` below formore details, and possible values. By default, no pre-trainedweights are used.progress (bool, optional): If True, displays a progress bar of thedownload 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.总结

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

http://www.lryc.cn/news/190899.html

相关文章:

  • leetcode-279. 完全平方数
  • MySQL常用指令
  • Pulsar 之架构,客户端以及多区域容灾
  • 【SQL】MySQL中的索引,索引优化
  • uniapp 跳转到指定位置
  • 基于java的图书馆预约座位系统的设计与实现(部署+源码+LW)
  • golang 拉取 bitbucket.org 私有库
  • Sub-1G射频收发器soc芯片 UM2080F32 低功耗 32 位 IoTP
  • 国际减灾日 | 智慧减灾——百分点科技的数据科学视角
  • ChatGLM流式输出的报错修复
  • HDLbits: ece241 2013 q12 // Exams/m2014 q4k
  • vue3模板-vscode设置(语法糖)
  • RFID超高频读写器的特点和应用
  • 诡异事件:开发的安卓摄像头应用突然不能保存图片,回滚代码都查不出来
  • validator库的使用详解
  • ADS版图中连接提示线设置
  • 【MySQL】内置函数——数学函数+其他函数
  • Ubuntu 23.10 Beta 镜像开放下载
  • mybatispagehelp嵌套分页处理
  • 增速波动!W「下」AR「上」!HUD前装供应商比拼硬核能力
  • XXE漏洞复现实操
  • github创建个人网页登录后404无法显示的问题
  • MySQL——源码安装教程(初版)
  • 1.1.C++项目:仿muduo库实现并发服务器之any类的设计
  • linux项目启动脚本start.sh和stop.sh停止脚本
  • Avalonia常用小控件Svg
  • 设计模式-行为型模式
  • 【EventLoop】问题一次搞定
  • Unity中Shader光照模型Phong
  • 消息队列缓存,以蓝牙消息服务为例