Error resulting from ImageDataGenerator during data augmentation

366 Views Asked by At

Can someone please help me in fixing the error? The code works fine before the for loop. Before the for loop, an array of the image was printed. Is there something wrong with the for loop? The output should be a file stored with augmented images of the input image. The input image is a jpg image.

The code I wrote:

import keras 
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator

data_gen = tf.keras.preprocessing.image.ImageDataGenerator(
    rotation_range=45,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='contrast',
    cval=125
)

x = io.imread('mona.jpg')
x = x.reshape((1, ) + x.shape)  #Array with shape (1, 256, 256, 3)

i = 0
for batch in data_gen.flow(x, batch_size=16,                            save_to_dir='/Users/ghad/Desktop', 
                          save_prefix='aug', 
                          save_format='jpg'):
    i += 1
    if i > 20: 

The generated error:

RuntimeError                              Traceback (most recent call last)
Input In [14], in <cell line: 31>()
     28 x = x.reshape((1, ) + x.shape)  #Array with shape (1, 256, 256, 3)
     30 i = 0
---> 31 for batch in data_gen.flow(x, batch_size=16,  
     32                           save_to_dir='/Users/ghadahalhabib/Desktop', 
     33                           save_prefix='aug', 
     34                           save_format='jpg'):
     35     i += 1
     36     if i > 20:

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:148, in Iterator.__next__(self, *args, **kwargs)
    147 def __next__(self, *args, **kwargs):
--> 148   return self.next(*args, **kwargs)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:160, in Iterator.next(self)
    157   index_array = next(self.index_generator)
    158 # The transformation of images is not under thread lock
    159 # so it can be done in parallel
--> 160 return self._get_batches_of_transformed_samples(index_array)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:709, in NumpyArrayIterator._get_batches_of_transformed_samples(self, index_array)
    707 x = self.x[j]
    708 params = self.image_data_generator.get_random_transform(x.shape)
--> 709 x = self.image_data_generator.apply_transform(
    710     x.astype(self.dtype), params)
    711 x = self.image_data_generator.standardize(x)
    712 batch_x[i] = x

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:1800, in ImageDataGenerator.apply_transform(self, x, transform_parameters)
   1797 img_col_axis = self.col_axis - 1
   1798 img_channel_axis = self.channel_axis - 1
-> 1800 x = apply_affine_transform(
   1801     x,
   1802     transform_parameters.get('theta', 0),
   1803     transform_parameters.get('tx', 0),
   1804     transform_parameters.get('ty', 0),
   1805     transform_parameters.get('shear', 0),
   1806     transform_parameters.get('zx', 1),
   1807     transform_parameters.get('zy', 1),
   1808     row_axis=img_row_axis,
   1809     col_axis=img_col_axis,
   1810     channel_axis=img_channel_axis,
   1811     fill_mode=self.fill_mode,
   1812     cval=self.cval,
   1813     order=self.interpolation_order)
   1815 if transform_parameters.get('channel_shift_intensity') is not None:
   1816   x = apply_channel_shift(x,
   1817                           transform_parameters['channel_shift_intensity'],
   1818                           img_channel_axis)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:2324, in apply_affine_transform(x, theta, tx, ty, shear, zx, zy, row_axis, col_axis, channel_axis, fill_mode, cval, order)
   2321 final_affine_matrix = transform_matrix[:2, :2]
   2322 final_offset = transform_matrix[:2, 2]
-> 2324 channel_images = [ndimage.interpolation.affine_transform(  # pylint: disable=g-complex-comprehension
   2325     x_channel,
   2326     final_affine_matrix,
   2327     final_offset,
   2328     order=order,
   2329     mode=fill_mode,
   2330     cval=cval) for x_channel in x]
   2331 x = np.stack(channel_images, axis=0)
   2332 x = np.rollaxis(x, 0, channel_axis + 1)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/keras/preprocessing/image.py:2324, in <listcomp>(.0)
   2321 final_affine_matrix = transform_matrix[:2, :2]
   2322 final_offset = transform_matrix[:2, 2]
-> 2324 channel_images = [ndimage.interpolation.affine_transform(  # pylint: disable=g-complex-comprehension
   2325     x_channel,
   2326     final_affine_matrix,
   2327     final_offset,
   2328     order=order,
   2329     mode=fill_mode,
   2330     cval=cval) for x_channel in x]
   2331 x = np.stack(channel_images, axis=0)
   2332 x = np.rollaxis(x, 0, channel_axis + 1)

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/scipy/ndimage/interpolation.py:574, in affine_transform(input, matrix, offset, output_shape, output, order, mode, cval, prefilter)
    572     npad = 0
    573     filtered = input
--> 574 mode = _ni_support._extend_mode_to_code(mode)
    575 matrix = numpy.asarray(matrix, dtype=numpy.float64)
    576 if matrix.ndim not in [1, 2] or matrix.shape[0] < 1:

File ~/opt/anaconda3/envs/tensorflow/lib/python3.9/site-packages/scipy/ndimage/_ni_support.py:54, in _extend_mode_to_code(mode)
     52     return 6
     53 else:
---> 54     raise RuntimeError('boundary mode not supported')

RuntimeError: boundary mode not supported
1

There are 1 best solutions below

1
On

for the code

for batch in data_gen.flow(x, batch_size=16,                            save_to_dir='/Users/ghad/Desktop', save_prefix='aug',  save_format='jpg'):

you are inputting only a single image but asking to produce 16 augmented images. That won't work. Normal the length of x is LARGER than the batch size. Set the batch size to 1. That way you will produce 1 augment image each time you feed a new image into the generator