【发布时间】:2019-09-14 00:43:57
【问题描述】:
我有以下代码用 opencv 读取图像并显示它:
import cv2, matplotlib.pyplot as plt
img = cv2.imread('imgs_soccer/soccer_10.jpg',cv2.IMREAD_COLOR)
img = cv2.resize(img, (128, 128))
plt.imshow(img)
plt.show()
我想使用 keras 生成一些随机图像,所以我定义了这个生成器:
image_gen = ImageDataGenerator(rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.01,
zoom_range=[0.9, 1.25],
horizontal_flip=True,
vertical_flip=False,
fill_mode='reflect',
data_format='channels_last',
brightness_range=[0.5, 1.5])
但是,当我以这种方式使用它时:
image_gen.flow(img)
我收到此错误:
'Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (128, 128, 3))
在我看来很明显:RGB,图像,当然是 3 维的! 我在这里想念什么? 文档说它需要一个 4 维数组,但没有指定 我应该在第 4 维中放入什么!
这个 4-dim 数组应该如何制作?我现在有(宽度、高度、通道),这个第 4 维度位于 开始或结束?
我对 numpy 也不是很熟悉:如何更改现有的 img 数组以添加第 4 维?
【问题讨论】:
标签: python numpy tensorflow keras deep-learning