torch与torchvision版本对应关系 & ImportError: cannot import name ‘QuantStub‘ from ‘torch.ao.quantization‘

博客内容讲述了在使用 PyTorch 库时遇到 torchvision 和 torch 版本不兼容的错误。作者提供了检查和解决该问题的步骤,包括查看 torch 版本,参考版本对应表格,以及如何从特定网址下载匹配的 torchvision 版本。错误信息显示为无法导入 'QuantStub',提示可能是由于 torchvision 的旧版本不支持 torch 的某些特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

torch与torchvision版本对应关系看下边的表格

兄弟们看看是不是报这个:

  File "xxxxxxxx/xxxxx/tools/train.py", line 10, in <module>
    from mmcv.runner import init_dist
  File "xxxxxxxx/.conda/envs/ga/lib/python3.7/site-packages/mmcv/runner/__init__.py", line 2, in <module>
    from .checkpoint import (_load_checkpoint, load_checkpoint, load_state_dict,
  File "xxxxxxxx/.conda/envs/ga/lib/python3.7/site-packages/mmcv/runner/checkpoint.py", line 11, in <module>
    import torchvision
  File "xxxxxxxx/.conda/envs/ga/lib/python3.7/site-packages/torchvision/__init__.py", line 7, in <module>
    from torchvision import models
  File "xxxxxxxx/.conda/envs/ga/lib/python3.7/site-packages/torchvision/models/__init__.py", line 18, in <module>
    from . import quantization
  File "xxxxxxxx/.conda/envs/ga/lib/python3.7/site-packages/torchvision/models/quantization/__init__.py", line 3, in <module>
    from .mobilenet import *
  File "xxxxxxxx/.conda/envs/ga/lib/python3.7/site-packages/torchvision/models/quantization/mobilenet.py", line 1, in <module>
    from .mobilenetv2 import *  # noqa: F401, F403
  File "xxxxxxxx/.conda/envs/ga/lib/python3.7/site-packages/torchvision/models/quantization/mobilenetv2.py", line 6, in <module>
    from torch.ao.quantization import QuantStub, DeQuantStub
ImportError: cannot import name 'QuantStub' from 'torch.ao.quantization'

参考自:
https://forums.fast.ai/t/cannot-import-name-quantstub-from-torch-ao-quantization/94078/2

意思是可能 torchtorchvision 的版本不匹配

看下自己的版本

>>> import torch
>>> torch.__version__
'1.10.0+cu113'

这样说明是在
https://download.pytorch.org/whl/torch_stable.html
下载的,如果没有 cu113 大概率pip或者conda下载的

  • 如果没有 cu113 就直接参考下边的表格,pip install torchvision==对应的版本 即可
torchtorchvisiontorchaudio
pytorch==1.12.0torchvision==0.13.0torchaudio==0.12.0
pytorch==1.11.0torchvision==0.12.0torchaudio==0.11.0
pytorch==1.10.1torchvision==0.11.2torchaudio==0.10.1
pytorch==1.10.0torchvision==0.11.0torchaudio==0.10.0
pytorch==1.9.1torchvision==0.10.1torchaudio==0.9.1
pytorch==1.9.0torchvision==0.10.0torchaudio==0.9.0
pytorch==1.8.1torchvision==0.9.1torchaudio==0.8.1
pytorch==1.8.0torchvision==0.9.0torchaudio==0.8.0
pytorch==1.7.1torchvision==0.8.2torchaudio==0.7.2
pytorch==1.7.0torchvision==0.8.0torchaudio==0.7.0
pytorch==1.6.0torchvision==0.7.0
pytorch==1.5.1torchvision==0.6.1
pytorch==1.5.0torchvision==0.6.0
pytorch==1.4.0torchvision==0.5.0
pytorch==1.2.0torchvision==0.4.0
pytorch==1.1.0torchvision==0.3.0

表格参考自:
https://pytorch.org/get-started/previous-versions/

比如我是 cuda11.2,那就找大于它但是最接近的版本,以及指定python版本,同时根据上述表格,找到torch1.10对应的torchvision版本0.11

在这里插入图片描述
下载这个 wheel 文件,pip 安装即可

如果不这样搞,直接 pip install torchvision==0.11 会报这个错:

Collecting torchvision==0.11
  Using cached torchvision-0.11.0-cp37-cp37m-manylinux1_x86_64.whl (23.3 MB)
Requirement already satisfied: numpy in /xxxxx/.conda/envs/ga/lib/python3.7/site-packages (from torchvision==0.11) (1.21.5)
ERROR: Could not find a version that satisfies the requirement torch==1.10.0+cu102 (from torchvision) (from versions: 1.0.0, 1.0.1, 1.0.1.post2, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.4.0, 1.5.0, 1.5.1, 1.6.0, 1.7.0, 1.7.1, 1.8.0, 1.8.1, 1.9.0, 1.9.1, 1.10.0, 1.10.1, 1.10.2, 1.11.0, 1.12.0, 1.12.1)
ERROR: No matching distribution found for torch==1.10.0+cu102
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值