AttributeError: ‘_OpNamespace‘ ‘image‘ object has no attribute ‘read_file‘解决

问题再现

from torchvision.io.image import read_image
img_path = 'border-collie.jpg'
img = read_image(img_path)

报错如下:

WARNING:root:no value was provided for `target_layer`, thus set to 'layer4'.
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/anaconda3/envs/DVS/lib/python3.8/site-packages/torch/_ops.py:757, in _OpNamespace.__getattr__(self, op_name)
    756 try:
--> 757     op, overload_names = torch._C._jit_get_operation(qualified_op_name)
    758 except RuntimeError as e:
    759     # Turn this into AttributeError so getattr(obj, key, default)
    760     # works (this is called by TorchScript with __origin__)

RuntimeError: No such operator image::read_file

The above exception was the direct cause of the following exception:

AttributeError                            Traceback (most recent call last)
/home/algroup/chenao/RankMatch/CAM.ipynb 单元格 16 in ()
      1 # Get your input
----> 2 img = read_image(img_path)
      3 # Preprocess it for your chosen model
      4 input_tensor = normalize(resize(img, (224, 224)) / 255., [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

File ~/anaconda3/envs/DVS/lib/python3.8/site-packages/torchvision/io/image.py:222, in read_image(path, mode)
    206 def read_image(path: str, mode: ImageReadMode = ImageReadMode.UNCHANGED) -> torch.Tensor:
    207     """
    208     Reads a JPEG or PNG image into a 3 dimensional RGB Tensor.
    209     Optionally converts the image to the desired format.
   (...)
    220         output (Tensor[image_channels, image_height, image_width])
    221     """
--> 222     data = read_file(path)
    223     return decode_image(data, mode)

File ~/anaconda3/envs/DVS/lib/python3.8/site-packages/torchvision/io/image.py:42, in read_file(path)
     31 def read_file(path: str) -> torch.Tensor:
     32     """
     33     Reads and outputs the bytes contents of a file as a uint8 Tensor
     34     with one dimension.
   (...)
     40         data (Tensor)
     41     """
---> 42     data = torch.ops.image.read_file(path)
     43     return data

File ~/anaconda3/envs/DVS/lib/python3.8/site-packages/torch/_ops.py:761, in _OpNamespace.__getattr__(self, op_name)
    757     op, overload_names = torch._C._jit_get_operation(qualified_op_name)
    758 except RuntimeError as e:
    759     # Turn this into AttributeError so getattr(obj, key, default)
    760     # works (this is called by TorchScript with __origin__)
--> 761     raise AttributeError(
    762         f"'_OpNamespace' '{self.name}' object has no attribute '{op_name}'"
    763     ) from e
    765 # let the script frontend know that op is identical to the builtin op
    766 # with qualified_op_name
    767 torch.jit._builtins._register_builtin(op, qualified_op_name)

AttributeError: '_OpNamespace' 'image' object has no attribute 'read_file'
# packages in environment at /home/algroup/anaconda3/envs/DVS:
#
# Name                    Version                   Build  Channel
torch                     2.1.1                    pypi_0    pypi
torchcam                  0.4.0                    pypi_0    pypi
torchextractor            0.3.0                    pypi_0    pypi
torchvision               0.11.3                   pypi_0    pypi

Note: you may need to restart the kernel to use updated packages.

修改版本

说是torchvision版本不匹配什么的,我懒得改,因为还和pytorch有关系,要改要一起改很麻烦;

改用Image.open()读取文件

from PIL import Image
import torchvision.transforms as transforms
 
totensor = transforms.ToTensor()
img = totensor(Image.open(path))

对于菜鸡而言,不敢瞎改,怕改错了整个程序都崩溃了。但是读清楚之后,发现出问题的只是一个读取图片的小函数,之前理论上如果能运行的话,是吧.jpg文件读取成tensor,那我用别的函数做同样的时候就好,这是就自然想到Image.open()把图片转换成什么类型了,如果不是tensor,还要加一道转换步骤,就这样逻辑就严密了!

参考

[1] No such operator image::read_file问题解决

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用内容,你遇到的问题是AttributeError: 'str' object has no attribute 'seek'。这个错误是由于你的代码中将一个字符串对象作为参数传递给了seek()函数,而不是一个File对象。对于这个问题,你可以检查你的代码,找到哪里将文件变成了字符串对象。可能的原因可能是在load_img()函数中的filepath参数,它可能是一个字符串而不是一个有效的文件路径。 根据引用内容,你提到你已经尝试了一些解决方法,比如使用seek()函数来处理文本文件。seek()函数是用于移动文件读取指针到指定位置的函数,但是在你的情况下,它被错误地应用在了一个字符串对象上。因此,你需要检查代码,确保传递给seek()函数的参数是一个有效的File对象。 此外,根据引用内容,你还提到了你的代码中使用了DataLoader来加载数据集。你可以继续检查你的代码中是否有其他地方也出现了类似的错误,比如在数据加载过程中可能出现了对字符串对象的错误操作。 综上所述,你需要仔细检查代码,找到哪里将文件变成了字符串对象,并确保正确使用File对象进行文件操作,以解决AttributeError: 'str' object has no attribute 'seek'的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [解决运行 AttributeError: ‘str‘ object has no attributeread‘ and ‘str‘ object has no ...](https://blog.csdn.net/weixin_43638241/article/details/129364741)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值