• 深度学习: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)
    
    • 1
    • 2

    会提示警告:

    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, "
    
    • 1
    • 2

    看出给出的原因是在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 `__.
    
        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
                `_
                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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 首先看到第一行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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

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

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

    也可以

    pretrained_model = models.resnet18(models.ResNet18_Weights.DEFAULT)
    
    • 1

    这两个是一样的。

    3.总结

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

  • 相关阅读:
    Java数组
    Linux中查看并删除端口
    工具类:展开收起文字
    5000+ 字解读 | 产品经理:如何做好元器件选型?
    .net6 WebApi 如何将变量注入到控制器 以及配置文件的读取
    5分钟看明白rust mod use
    【Promise12数据集】Promise12数据集介绍和预处理
    MSTP+VRRP配置
    【0235】修改私有内存(private memory)中的MyBEEntry时,st_changecount值前后变化
    线性预测和自回归建模
  • 原文地址:https://blog.csdn.net/qudunan6468/article/details/133808253