Home > Software engineering >  UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002154C6AE400>
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002154C6AE400>

Time:11-27

I'm trying to test the accuracy of a pre-trained CNN model(densenet121) by using its save weights but got stuck in this error. The code for testing is as follows:

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

The tree for the dataset is like this and the format of images is .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

The error I got while running this test code is:

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>


   

CodePudding user response:

This error is due to a corrupt file or file with an incorrect extension that the generator is not able to read.
Use the function below to check if the input data:
Code:

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])
  • Related