2

I use the following code to read all images in a folder and use them for image augmentation. load_images() function reads all images as numpy array but when I use this function as input for image augmentation in the second part of the code, I get the error (setting an array element with a sequence). Any help is appreciated.

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from matplotlib.pyplot import imread, imshow, subplots, show
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import os

image_path = '/path/to/images/'
def load_images(image_path):
    imagees = []
    for filename in os.listdir(image_path):
        img = mpimg.imread(os.path.join(image_path, filename))
        if img is not None:
            imagees.append(img)
    return imagees

datagen = ImageDataGenerator(rotation_range=90, width_shift_range=0.3, 
height_shift_range=0.3,shear_range=45.0, brightness_range=(0.1, 0.9), 
zoom_range=[0.5, 1.5],channel_shift_range = 150.0, horizontal_flip=True, vertical_flip=True)
images = load_images(image_path)
images = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
save_here = '/path/to/images/'
datagen.fit(images)
for x, val in zip(datagen.flow(images,
        save_to_dir=save_here,
         save_prefix='aug',
        save_format='png'),range(36)):
  pass
Sheri
  • 1,383
  • 3
  • 10
  • 26
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Nov 29 '19 at 18:39
  • first you use `load_images` to read all images and assign them to `images` but one line later you assign `image` to the same varaible `images` and `image` probably is single image. If you want to reshape all images then you should do it inside `load_images` (with `img.reshape()`) or create `for`-loop which will do it with every image (from list `images`) separatelly. – furas Nov 29 '19 at 18:44
  • you could use `print()` and/or `print( type() )` - to see what you have in variable `images` - `print(images)` `print( type(images) )`. After `images =load_images()` you should have `list` with images/arrays. After `images = image.reshape()` you should have single image/array. – furas Nov 29 '19 at 18:52

3 Answers3

0

I can't run it so I will guess.

In line

images = load_images(image_path)

you assign to images list with arrays/images. And here is all OK.

But in next line

images = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))

you assign single image/array to images so you remove previous list. I don't see variable image in your code so I don't know what it is so I only guess.

If you want to reshape all images on list images then you could do it when you read images

imagees.append(img.reshape((1, img.shape[0], img.shape[1], img.shape[2])))

Or you would have to use for-loop or list comprehension to reshape every image separatelly

new_images = []

for image in images:
    new_images.append(image.reshape((1, image.shape[0], image.shape[1], image.shape[2])))

images = new_images

or with list comprehension

images = [img.reshape((1, img.shape[0], img.shape[1], img.shape[2])) for img in images]
furas
  • 134,197
  • 12
  • 106
  • 148
0

You may have a typo there, you call your list imagees and continue with images, are you aware of that?

code-lukas
  • 1,586
  • 9
  • 19
-1

Thank you very much for your help. I made some changes to my code based on your comments but sometimes, get the following error. The main problem is the code augments only one image from the list of images in the folder not all. [Code with error[1]