无法关闭PyTorch中beta transforms警告的问题求助
解决torchvision Beta版警告无法关闭的问题
问题场景
环境:Win x64、Spyder、Python 3.9、PyTorch 2.0.1+cu117,使用torchvision.transforms.v2时反复出现以下Beta版警告:
C:\Users\sengnr3\.conda\envs\pytorch\Lib\site-packages\torchvision\transforms\v2\__init__.py:54: UserWarning: The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. While we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes. You can silence this warning by calling torchvision.disable_beta_transforms_warning(). warnings.warn(_BETA_TRANSFORMS_WARNING)
按提示调用torchvision.disable_beta_transforms_warning()后警告仍大量出现。
有效解决方法
方法1:提前调用禁用函数
警告会在导入torchvision.transforms.v2模块时触发,必须在导入任何v2相关模块前执行禁用代码:
# 先执行禁用操作 import torchvision torchvision.disable_beta_transforms_warning() # 再导入v2相关模块 from torchvision.transforms import v2
方法2:用Python warnings模块强制过滤
如果方法1仍无效,直接通过Python内置的warnings模块精准过滤该警告:
import warnings # 匹配警告核心内容,忽略该类提示 warnings.filterwarnings("ignore", message="The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta.") # 之后再导入相关模块 import torchvision from torchvision.transforms import v2
方法3:升级torchvision到稳定版
如果不需要v2的新特性,可升级torchvision到脱离Beta阶段的版本(需保证与PyTorch版本兼容):
# conda环境升级命令 conda install torchvision==<兼容的稳定版本号> -c pytorch # pip环境升级命令 pip install torchvision==<兼容的稳定版本号>
内容的提问来源于stack exchange,提问作者DrBwts
相关产品推荐
相关产品推荐

