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