【问题标题】:Image Augmentation - TypeError: only size-1 arrays can be converted to Python scalars图像增强 - 类型错误:只有大小为 1 的数组可以转换为 Python 标量
【发布时间】:2022-07-10 10:20:58
【问题描述】:

我有一个包含 279 个图像的数据集,我希望执行批量大小为 4 的增强。以下是我编写的代码示例

import numpy as np
from skimage import io
import os
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator
 

# Construct an instance of the ImageDataGenerator class
# Pass the augmentation parameters through the constructor. 

datagen = ImageDataGenerator(
        rotation_range=45,     #Random rotation between 0 and 45
        width_shift_range=0.5,   #% shift
        height_shift_range=0.5,
        shear_range=0.4,
        zoom_range=0.5,
        horizontal_flip=True,
        fill_mode='reflect', cval=125)    #Also try nearest, constant, reflect, wrap



dataset = [] 
image_directory = '/home/Downloads/Data_aachen/Aachen_data/'
#SIZE = 512
dataset = []

my_images = os.listdir(image_directory)
for i, image_name in enumerate(my_images):
    if (image_name.split('.')[1] == 'jpg'):
        image = io.imread(image_directory + image_name)
        image = Image.fromarray(image, 'RGB')
        #image = image.resize((620,600))
        dataset.append(np.array(image))

x = np.array(dataset,dtype=object)
#x = np.int(dataset)




i = 0
for batch in datagen.flow(x, batch_size=4,  
                          save_to_dir='/home/Downloads/Data_aachen/Augmentations_4', 
                          save_prefix='aug', 
                          save_format='png'):
    i += 1
    if i > 279:
        break 

在这种情况下我遇到了类型错误,我不确定我的代码中出现这个错误的原因,请告诉我这个问题的原因。

    TypeError: only size-1 arrays can be converted to Python scalars


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "/tmp/ipykernel_1015850/35093792.py", line 2, in <module>
    for batch in datagen.flow(x, batch_size=4,

  File "/home/curevision/anaconda3/lib/python3.9/site-packages/keras/preprocessing/image.py", line 884, in flow
    return NumpyArrayIterator(

  File "/home/curevision/anaconda3/lib/python3.9/site-packages/keras/preprocessing/image.py", line 463, in __init__
    super(NumpyArrayIterator, self).__init__(

  File "/home/curevision/anaconda3/lib/python3.9/site-packages/keras_preprocessing/image/numpy_array_iterator.py", line 121, in __init__
    self.x = np.asarray(x, dtype=self.dtype)

  File "/home/curevision/anaconda3/lib/python3.9/site-packages/numpy/core/_asarray.py", line 102, in asarray
    return array(a, dtype, copy=False, order=order)

ValueError: setting an array element with a sequence.

【问题讨论】:

  • 您能否分享有关错误(类型错误、值错误)的更多信息?例如,引发这些错误的行(甚至可能是整个错误堆栈)。
  • 你知道这些错误在说哪些数组吗?
  • @Brandt,我已经更新了我的问题,请检查一下

标签: python scikit-learn scikit-image data-augmentation image-augmentation


【解决方案1】:

似乎问题在我遇到了似乎问题,你解决了吗?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-14
  • 2023-03-17
  • 2016-03-02
  • 2021-10-17
  • 2018-07-22
  • 2021-07-08
  • 2021-06-24
相关资源
最近更新 更多