我想使用Keras内置的图像预处理功能来增强序列中的图像。我的数据集形状为(13200, 4, 168, 168, 1)
,包含13200个序列,每个序列由4张168×168像素的灰度图像组成。
当我尝试在我的数据集上运行datagen.flow()
时,我得到了以下错误:
ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (13200, 4, 168, 168, 1))
我猜测ImageDataGenerator
无法正确处理每个样本中包含4张图像的序列。有什么方法可以解决这个问题吗?
回答:
尝试定义一个新的生成器:
def sequence_image_generator(x, y, batch_size, generator, seq_len=4): new_y = numpy.repeat(y, seq_len, axis = 0) helper_flow = generator.flow(x.reshape((x.shape[0] * seq_len, x.shape[2], x.shape[3], x.shape[4])), new_y, batch_size=seq_len * batch_size) for x_temp, y_temp in helper_flow: yield x_temp.reshape((x_temp.shape[0] / seq_len, seq_len, x.shape[2], x.shape[3], x.shape[4])), y_temp[::seq_len,:]