当前位置: 首页 > article >正文

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问题解决


http://www.kler.cn/a/146240.html

相关文章:

  • IoTDB 数据类型相关问题
  • Redis系列之底层数据结构字典Dict
  • 2024CVPR《HomoFormer》
  • Spring boot框架下的RocketMQ消息中间件
  • 从玩具到工业控制--51单片机的跨界传奇【3】
  • C#表达式和运算符
  • 免费部署开源大模型
  • 人脑工作机制 基本工作原理 神经元 神经网络 学习和记忆 和身体的互动 模仿游戏
  • 2023.11.25电商项目平台建设2 -四大业务之核销主题建模
  • 计算机毕业设计 基于SpringBoot的智能停车场计费系统的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 3.OpenResty系列之Nginx反向代理
  • 推荐你一个基于Koin, Ktor Paging等组件的KMM Compose Multiplatform项目
  • 内衣洗衣机怎么选?内衣洗衣机便宜好用的牌子推荐
  • SOLIDWORKS髙级孔命令及相关问题
  • 某生物科技巨头:引入安全工具,推动基因科技领域智能化发展
  • C 文件 rewind() 函数
  • JVM字节码文件的相关概述解读
  • leetcode周赛373场
  • Linux C语言 30-套接字操作
  • TCP/IP、Http、Socket之间的区别
  • LeetCode 4 寻找两个正序数组的中位数
  • 知识图谱06——将pdf中的表格(文字形式)保存至csv中
  • Flume采集Kafka并把数据sink到OSS
  • Controller 层代码就该这么写,简洁又优雅!
  • Xcode:does not contain bitcode
  • 华为eNSP使用教程(Enterprise Network Simulation Platform,企业网络仿真平台)