无法导入torchvision.io.read_image

在运行官网pytorch案例的时候,发现无法从torchvision.io中导入read_image。

ImportError: cannot import name 'read_image' from 'torchvision.io'

查看torchvision的版本发现是0.6的落后版本。

In [1]: import torchvision as tv

In [2]: tv.__version__
Out[2]: '0.6.0'

升级torchvision版本即可,使用pip进行升级。

pip install --upgrade torchvision

但是发现存在权限访问异常的问题。

ERROR: Could not install packages due to an OSError: [WinError 5] 拒绝访问。: 'c:\\users\\zhang\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\~orch\\lib\\asmjit.dll'
Consider using the `--user` option or check the permissions.

在升级的时候加上–user选项即可。

pip install --user --upgrade torchvison

检查版本号,发现升级成功。

In [1]: import torchvision as tv

In [2]: tv.__version__
Out[2]: '0.10.0+cpu'

In [3]: tv.io.read_image
Out[3]: <function torchvision.io.image.read_image(path: str, mode: torchvision.io.image.ImageReadMode = <ImageReadMode.UNCHANGED: 0>) -> torch.Tensor>

不过这样还是不行,因为升级到了cpu版本,cuda不能调用了。索性卸载了torch,重新安装了一遍。

pip3 install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

这下就没问题了。

In [1]: import torch

In [2]: torch.__version__
Out[2]: '1.9.0+cu111'

In [3]: torch.cuda.is_available()
Out[3]: True

In [4]: import torchvision as tv

In [5]: tv.__version__
Out[5]: '0.10.0+cu111'
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[5], line 1 ----> 1 import easyocr File ~\AppData\Roaming\Python\Python313\site-packages\easyocr\__init__.py:1 ----> 1 from .easyocr import Reader 3 __version__ = '1.7.2' File ~\AppData\Roaming\Python\Python313\site-packages\easyocr\easyocr.py:3 1 # -*- coding: utf-8 -*- ----> 3 from .recognition import get_recognizer, get_text 4 from .utils import group_text_box, get_image_list, calculate_md5, get_paragraph,\ 5 download_and_unzip, printProgressBar, diff, reformat_input,\ 6 make_rotated_img_list, set_result_with_confidence,\ 7 reformat_input_batched, merge_to_free 8 from .config import * File ~\AppData\Roaming\Python\Python313\site-packages\easyocr\recognition.py:6 4 import torch.utils.data 5 import torch.nn.functional as F ----> 6 import torchvision.transforms as transforms 7 import numpy as np 8 from collections import OrderedDict File ~\AppData\Roaming\Python\Python313\site-packages\torchvision\__init__.py:10 7 # Don't re-order these, we need to load the _C extension (done when importing 8 # .extensions) before entering _meta_registrations. 9 from .extension import _HAS_OPS # usort:skip ---> 10 from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils # usort:skip 12 try: 13 from .version import __version__ # noqa: F401 File ~\AppData\Roaming\Python\Python313\site-packages\torchvision\datasets\__init__.py:1 ----> 1 from ._optical_flow import FlyingChairs, FlyingThings3D, HD1K, KittiFlow, Sintel 2 from ._stereo_matching import ( 3 CarlaStereo, 4 CREStereo, (...) 12 SintelStereo, 13 ) 14 from .caltech import Caltech101, Caltech256 File ~\AppData\Roaming\Python\Python313\site-packages\torchvision\datasets\_optical_flow.py:13 10 from PIL import Image 12 from ..io.image import decode_png, read_file ---> 13 from .utils import _read_pfm, verify_str_arg 14 from .vision import VisionDataset 16 T1 = Tuple[Image.Image, Image.Image, Optional[np.ndarray], Optional[np.ndarray]] File ~\AppData\Roaming\Python\Python313\site-packages\torchvision\datasets\utils.py:20 18 import numpy as np 19 import torch ---> 20 from torch.utils.model_zoo import tqdm 22 from .._internally_replaced_utils import _download_file_from_remote_location, _is_remote_location_available 24 USER_AGENT = "pytorch/vision" ModuleNotFoundError: No module named 'torch.utils.model_zoo'
最新发布
03-12
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ProfSnail

谢谢老哥嗷

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

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

打赏作者

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

抵扣说明:

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

余额充值