Search code examples
pythonscikit-learnscikit-imagedata-augmentationimage-augmentation

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


I have a dataset of 279 images and i wish to perform augmentations with batch size of 4. Following is the code sample that i have written

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 

I am facing a type error in this situation, i am not sure the cause for this error within my code, kindly please suggest me the reason for this problem.

    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.

Solution

  • TypeError: only size-1 arrays can be converted to Python scalars #Most often from trying to convert a NumPy array values to another format

    #solution use astype:

    x = np.array([1.0,2.0,3.0])

    x.astype(int)