UnidentifiedImageError: 无法识别图像文件 <_io.BytesIO object at 0x000002154C6AE400>
Posted
技术标签:
【中文标题】UnidentifiedImageError: 无法识别图像文件 <_io.BytesIO object at 0x000002154C6AE400>【英文标题】:UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002154C6AE400> 【发布时间】:2022-01-03 23:59:00 【问题描述】:我正在尝试测试预训练 CNN 的准确性 模型(densenet121)通过使用它的保存权重但被困在这个 错误。测试代码如下:
image_size=512
batch_size=4
model_name='weights/densenet121_CAB_messidor.h5' #pretrained weight of the model
test_dir='./data/Messidor_Classes/' #dataset directory
model=load_model(model_name) #loading the model
test_num=0
result=np.zeros((4,4),dtype=int)
recall=np.zeros((1,4),dtype=float)
for i in range(4): #one loop for each class
datadirs=test_dir+str(i)+'/' #image directory
filenames=os.listdir(datadirs)
num=len(filenames)
test_num=test_num+num
valid = ImageDataGenerator() #data augmentation
valid_data=valid.flow_from_directory(directory=test_dir,target_size=(image_size,image_size),
batch_size=batch_size,class_mode=None,classes=str(i))
predict=model.predict(valid_data,batch_size=batch_size,verbose=1,workers=1) #the line where the error is encountered
predict=np.argmax(predict,axis=-1)
for j in range(4):
result[i,j]=np.sum(predict==j)
right=result[0,0]+result[1,1]+result[2,2]+result[3,3]
print('Acc:',right/test_num) #accuracy
w_kappa=weight_kappa(result,test_num)
print('w_kappa:',w_kappa) #kappa score
数据集的树是这样的,图像的格式是.tif:
Messidor--|0->img_1.tif #images for class 0
| ...
| ...
| ->img_2.tif
|1->img_1.tif #images for class 1
| ...
| ...
| ->img_2.tif
|2->img_1.tif #images for class 2
| ...
| ...
| ->img_2.tif
|3->img_1.tif #images for class 3
| ...
| ...
| ->img_2.tif
我在运行这个测试代码时遇到的错误是:
Found 428 images belonging to 1 classes.
Traceback (most recent call last):
File "C:\Users\aokra\source\repos\CABnet\test.py", line 53, in <module>
predict=model.predict(valid_data,batch_size=batch_size,verbose=1,workers=1)
File "C:\Users\aokra\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\aokra\anaconda3\lib\site-packages\keras_preprocessing\image\iterator.py", line 227, in _get_batches_of_transformed_samples
img = load_img(filepaths[j],
File "C:\Users\aokra\anaconda3\lib\site-packages\keras_preprocessing\image\utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "C:\Users\aokra\anaconda3\lib\site-packages\PIL\Image.py", line 2967, in open
raise UnidentifiedImageError(
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001C299030EF0>
【问题讨论】:
【参考方案1】:此错误是由于生成器无法读取的损坏文件或扩展名不正确的文件造成的。 使用以下函数检查输入数据是否:代码:
import os
from PIL import Image
folder_path = r'data\Messidor_Classes'
extensions = []
for fldr in os.listdir(folder_path):
sub_folder_path = os.path.join(folder_path, fldr)
for filee in os.listdir(sub_folder_path):
file_path = os.path.join(sub_folder_path, filee)
print('** Path: **'.format(file_path), end="\r", flush=True)
im = Image.open(file_path)
rgb_im = im.convert('RGB')
if filee.split('.')[1] not in extensions:
extensions.append(filee.split('.')[1])
【讨论】:
非常感谢您的快速回复和帮助,如果再帮我一次,那就太好了。我已经运行了你给出的代码。当我运行它时,得到如下输出: "UnidentifiedImageError: cannot identify image file 'Messidor_Classes\\0\\20051020_43808_0100_PP.tif'" 。这是否意味着图像文件已损坏? 第 10 行“im = Image.open(file_path)”指示错误。 是的。如果出现错误,TensorFlow 将无法读取打印路径中存在的图像。 问题已解决。谢谢。以上是关于UnidentifiedImageError: 无法识别图像文件 <_io.BytesIO object at 0x000002154C6AE400>的主要内容,如果未能解决你的问题,请参考以下文章
PIL.UnidentifiedImageError:无法识别图像文件 <_io.BytesIO 对象
UnidentifiedImageError: 无法识别图像文件 <_io.BytesIO object at 0x000002154C6AE400>
模型训练出现UnidentifiedImageError: cannot identify image file <io.BytesIO object at 0x7faaa82bdb90>
ValueError 层序贯_13的输入0与层不兼容:预期ndim = 3,发现ndim = 4收到完整形状:(无,无,无,无)