【问题标题】:Keras ImageDataGenerator: random transformKeras ImageDataGenerator:随机变换
【发布时间】:2017-11-23 18:25:08
【问题描述】:

我有兴趣通过随机图像转换来扩充我的数据集。我正在使用 Keras ImageDataGenerator,在尝试将 random_transform 应用于单个图像时出现以下错误:

--> x = apply_transform(x, transform matrix, img_channel_axis, fill_mode, cval)
>>> RuntimeError: affine matrix has wrong number of rows.

我找到了 ImageDataGenerator here 的源代码。但是,我不确定如何调试运行时错误。以下是我的代码:

from keras.preprocessing.image import img_to_array, load_img
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.inception_v3 import preprocess_input    

image_path = './figures/zebra.jpg'

#data augmentation
train_datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')

print "\nloading image..."
image = load_img(image_path, target_size=(299, 299))
image = img_to_array(image)
image = np.expand_dims(image, axis=0) # 1 x input_shape
image = preprocess_input(image)

train_datagen.fit(image)
image = train_datagen.random_transform(image)

调用random_transform时最后一行出现错误。

【问题讨论】:

    标签: computer-vision deep-learning keras


    【解决方案1】:

    问题是random_transform 需要一个 3D 数组。

    查看文档字符串:

    def random_transform(self, x, seed=None):
        """Randomly augment a single image tensor.
        # Arguments
            x: 3D tensor, single image.
            seed: random seed.
        # Returns
            A randomly transformed version of the input (same shape).
        """
    

    所以你需要在np.expand_dims之前调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2020-10-21
      • 2019-08-04
      • 2017-09-05
      • 2020-07-17
      相关资源
      最近更新 更多