遍历图像以进行数据扩充

Iterating through images for data augmentation

我正在尝试遍历图像以执行数据扩充。为此,我使用以下内容:

from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from skimage import io

gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
                        height_shift_range=0.1, shear_range=0.15,
                        zoom_range=0.1, channel_shift_range=10., horizontal_flip=True,
                        fill_mode='nearest')

image_path = r'X:\Users\my_path'
os.chdir(image_path)

for img in os.listdir(image_path):
    if img.endswith(".jpg"):
        image = io.imread(img, plugin='matplotlib')
        image = image.reshape((1, ) + image.shape)

i = 0
for batch in gen.flow(image, batch_size=16,
                        save_to_dir=image_path,
                        save_prefix='aug',
                        save_format='jpg'):
    i += 1
    if i > 5:
        break

但出于某种原因,我收到一条错误消息:UnidentifiedImageError: cannot identify image file '36.jpg'

但是 36.jpg 在那里存在,具有完全相同的标题和扩展名。

导致问题的原因是什么?

提前致谢。

确切的两张图片有问题。为了避免更多问题,我将每一个都转换为 RGB 并将其放入循环中:

gen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1,
                        height_shift_range=0.15, shear_range=0.1,
                        zoom_range=0.1, channel_shift_range=10., horizontal_flip=True,
                        brightness_range=[0.3,0.9], fill_mode='nearest')

image_path = r'X:\Users\my_path'
os.chdir(image_path)

    j = 1
    for img in os.listdir(image_path):
        im = Image.open(img)
        rgb_im = im.convert('RGB')
        rgb_im.save(img)
        j += 1
    
    
    # Obtaining image
    for img in os.listdir(image_path):
        #if img.endswith(".jpg"):
        image = io.imread(img, plugin='matplotlib')
        image = image.reshape((-1, ) + image.shape)
    
        i = 0
        for batch in gen.flow(image, batch_size=16,
                              save_to_dir=image_path,
                              save_prefix='aug',
                              save_format='jpg'):
            i += 1
            if i > 6:
                break