Keras ImageDataGenerator:随机变换 [英] Keras ImageDataGenerator: random transform

查看:94
本文介绍了Keras ImageDataGenerator:随机变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对通过随机图像变换来扩充数据集感兴趣.我正在使用Keras ImageDataGenerator ,在尝试应用时遇到以下错误 random_transform 转换为单个图像:

I'm interested in augmenting my dataset with random image transformations. I'm using Keras ImageDataGenerator, and I'm getting the following error when trying to apply random_transform to a single image:

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

我在这里.但是,我不确定如何调试运行时错误.下面是我的代码:

I found the source code for the ImageDataGenerator here. However, I'm not sure how to debug the runtime error. Below is the code I have:

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 时,错误发生在最后一行.

The error occurs at the last line when calling random_transform.

推荐答案

问题是 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 之前调用它.

So you'll need to call it before np.expand_dims.

这篇关于Keras ImageDataGenerator:随机变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆