使用Pytorch 2.0训练踩坑

概述

最近博主在跑一个实验,但它在Pytorch 1.8版本下感觉有点慢,刚好又看到Pytorch 2.0版本加速很多,所以准备用Pytorch 2.0版本运行代码。在这个过程中,出现了一些小问题,还有一些warning。为了防止这些错误和warning干扰到实验的结果,我在网上查找了相关办法,并在此记录。

问题

FutureWarning: The module torch.distributed.launch is deprecated and will be removed in future. Use torchrun.Note that --use-env is set by default in torchrun

同时它还会提示你如下信息:

train.py: error: unrecognized arguments: --local-rank=0

我之前是在Pytorch 1.8版本下运行的,对应的运行命令是:

python -m  torch.distributed.launch --master_port 9843  --nproc_per_node=2  train.py

在Pytorch 2.0版本下应该使用如下命令:

python -m  torch.distributed.launch --master_port 9843  --nproc_per_node=2 --use_env train.py

增加一个--use_env即可。
声明--use_env后,pytorch会将当前进程在本机上的rank添加到环境变量LOCAL_RANK中,而不再添加到args.local_rank

UserWarning: The parameter ‘pretrained’ is deprecated since 0.13 and may be removed in the future, please use ‘weights’ instead.

这是因为torchvision里面的pretrained参数已经在Pytorch 2.0里面过时了,现在使用的是weights参数。
除了上面的警告,还会出现:

UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. 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.

我在Pytorch 1.8中使用的语句是:

resnet = models.resnet50(pretrained=True)

解决方法是:

resnet = models.resnet50(weights=torchvision.models.ResNet50_Weights.IMAGENET1K_V1)

使用最新的方法就可以了。

AttributeError: module ‘numpy’ has no attribute ‘int’.

因为在np.int在numpy1.20已经被废弃掉了。具体可以查看: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
我的原语句是:

cls_gt = np.zeros((3, 384, 384), dtype=np.int)

改成:

cls_gt = np.zeros((3, 384, 384), dtype=np.int_)

或者使用np.int32或者np.int64

np.bool was a deprecated alias for the builtin bool. To avoid this error in existing code, use bool by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.bool_ here.

因为在np.bool在numpy1.20已经被废弃掉了。
我的原语句是:

annotation = annotation.astype(np.bool)

改成:

annotation = annotation.astype(np.bool_)

UserWarning: ComplexHalf support is experimental and many operators don’t support it yet

除了警报,还会报错:

RuntimeError: cuFFT only supports dimensions whose sizes are powers of two when computing in half precision

出现这个的原因是我使用了混合精度。
解决办法是将Automatic mixed precision关掉

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
安装PyTorch 2.0可以通过以下步骤进行: 1. 首先,你可以参考官方文档提供的详细信息来安装PyTorch 2.0。 2. 请确保你的操作系统是支持PyTorch 2.0的版本。如果你使用的是Windows系统,可以检查官方文档中列举的支持的Windows发行版。 3. 注意,根据引用所提到的,目前Windows系统还不支持使用compile功能。如果你期待使用compile功能,可以等待Windows系统早日支持。 4. 根据官方给出的安装命令,你可以使用pip install命令来安装PyTorch 2.0,在命令行中输入以下命令: pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 这个命令将会从指定的URL下载并安装PyTorch 2.0以及相应的扩展库。 总结起来,你可以按照官方文档提供的详细信息和给出的安装命令来安装PyTorch 2.0,并注意Windows系统是否支持你期望的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [认识PyTorch2.0及安装过程](https://blog.csdn.net/qq_17716819/article/details/129656230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [pytorch2.0安装教程](https://blog.csdn.net/A2321161581/article/details/130012511)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzl2040

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值