Matthew Fernandez
Matthew Fernandez

Reputation: 11

Error when running ImageDataGenerator.next() code

I am currently running python 3.8.6, and when running the following code from https://machinelearningmastery.com/how-to-configure-image-data-augmentation-when-training-deep-learning-neural-networks/ I get an error saying that scipy is not defined.

# example of random rotation image augmentation
from numpy import expand_dims
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
# load the image
img =  tf.keras.preprocessing.image.load_img('campaign_data/campaign2/0000/image_0000500.png')
# convert to numpy array
data =  tf.keras.preprocessing.image.img_to_array(img)
# expand dimension to one sample
samples = expand_dims(data, 0)
# create image data augmentation generator
datagen = ImageDataGenerator(rotation_range=90)
# prepare iterator
it = datagen.flow(samples, batch_size=1)
# generate samples and plot
for i in range(9):
    # define subplot
    pyplot.subplot(330 + 1 + i)
    # generate batch of images
    batch = it.next()
    # convert to unsigned integers for viewing
    image = batch[0].astype('uint8')
    # plot raw pixel data
    pyplot.imshow(image)
# show the figure
pyplot.show()

'''
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [131], in <cell line: 18>()
     20 pyplot.subplot(330 + 1 + i)
     21 # generate batch of images
---> 22 batch = it.next()
     23 # convert to unsigned integers for viewing
     24 image = batch[0].astype('uint8')

File ~/jupyter-venv/lib/python3.8/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 ~/jupyter-venv/lib/python3.8/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 ~/jupyter-venv/lib/python3.8/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 ~/jupyter-venv/lib/python3.8/site-packages/keras/preprocessing/image.py:2244, in apply_affine_transform(x, theta, tx, ty, shear, zx, zy, row_axis, col_axis, channel_axis, fill_mode, cval, order)
   2212 @keras_export('keras.preprocessing.image.apply_affine_transform')
   2213 def apply_affine_transform(x, theta=0, tx=0, ty=0, shear=0, zx=1, zy=1,
   2214                            row_axis=1, col_axis=2, channel_axis=0,
   2215                            fill_mode='nearest', cval=0., order=1):
   2216   """Applies an affine transformation specified by the parameters given.
   2217 
   2218   Args:
   (...)
   2242       ImportError: if SciPy is not available.
   2243   """
-> 2244   if scipy is None:
   2245     raise ImportError('Image transformations require SciPy. '
   2246                       'Install SciPy.')
   2248   # Input sanity checks:
   2249   # 1. x must 2D image with one or more channels (i.e., a 3D tensor)
   2250   # 2. channels must be either first or last dimension

NameError: name 'scipy' is not defined
'''

I cannot seem to find any solution on how to solve this bug, but any help will be greatly appreciated.

Upvotes: 1

Views: 1099

Answers (1)

Asadullah Naeem
Asadullah Naeem

Reputation: 344

I got the same error when scipy was not installed. After installing scipy it worked. Please use:

pip install scipy.

Upvotes: 1

Related Questions