首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >运行ImageDataGenerator.next()代码时出错

运行ImageDataGenerator.next()代码时出错
EN

Stack Overflow用户
提问于 2022-06-15 06:35:49
回答 1查看 332关注 0票数 1

我目前正在运行python3.8.6,当从https://machinelearningmastery.com/how-to-configure-image-data-augmentation-when-training-deep-learning-neural-networks/运行下面的代码时,我会看到一个错误,说明未定义not。

代码语言:javascript
运行
复制
# 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
'''

我似乎找不到任何解决方案,如何解决这个错误,但任何帮助将是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2022-06-15 06:49:15

当没有安装scipy时,我也得到了相同的错误。在安装scipy之后,它成功了。请使用:

pip install scipy

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72626849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档