错误 - `NumpyArrayIterator` 中的输入数据应为 4

问题描述

这是用于对特定文件夹中的图像进行数据增强的代码。当我尝试运行该程序时,我遇到了以下错误消息:

ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape',(1,53,80))

输入图像具有不同的形状 (x,y,3) 并且是 RBG。因此,为了避免返回的错误,我决定将循环中的每个图像转换为形状 (1,x,3)。在执行代码直到 reshape() 并打印 image_to_augment 的形状后,我得到以下结果:

<class 'imageio.core.util.Array'>
(1,60,80,3)

然而,第一条错误信息仍然出现。我对图像的形状应该是什么感到困惑,因为我已经将它转换为 4 级对象。此外,错误消息显示我的输入图像的形状为 (1,80),但显然是 (1,3)。

代码:

path = 'images_dataset/Airplane'
    
for image_path in os.listdir(path):
    input_path = os.path.join(path,image_path)
    image_to_augment = imageio.imread(input_path)
    
    image_to_augment = image_to_augment/255
    
    # augment the image
    datagen = ImageDataGenerator( 
    rotation_range = 40,shear_range = 0.2,zoom_range = 0.2,horizontal_flip = True,brightness_range = (0.5,1.5) )
    
    image_to_augment = image_to_augment.reshape(1,*image_to_augment.shape)
    
    i = 0
    for batch in datagen.flow(image_to_augment,batch_size = 1,save_to_dir ='images_dataset/Airplane',save_prefix ='image',save_format ='jpeg'): 
        i += 1
        if i > 5: 
            break

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)