【问题标题】:Numpy: Increasing size of 4th dimensionNumpy:增加第四维的大小
【发布时间】:2018-10-12 03:33:11
【问题描述】:

我有一个 4D numpy 数组:RGB 图像的 (#images, height, width, channels)。确切的尺寸是 (46, 224, 224, 3)。

我也有相同图像的灰度版本。确切的尺寸是 (46, 224, 224, 1)。

我正在玩 Keras,试图制作一个自动编码器来从灰度值中学习图像值,但模型抱怨尺寸,所以我想将 2 个额外的通道添加到灰度数组中,但是我似乎无法弄清楚如何。

temp_gray_images = np.zeros(original_images.shape, dtype=np.float32)
temp_gray_images[:,:,:,0] = gray_images
ValueError: could not broadcast input array from shape (46,224,224,1) into shape (46,224,224)

然后我了解了 numpy.hstack,并打开了一个新终端并尝试使用一些虚拟数据和我可以处理的一些维度,并且能够获得预期的结果。但是,这似乎不适用于 4 维。

temp_gray_images = np.hstack([gray_images, np.zeros([original_images.shape[0], original_images.shape[1], original_images.shape[2], 2])])
ValueError: all the input array dimensions except for the concatenation axis must match exactly

这没有意义,因为我保持前 3 个维度的大小相同。

但最终验证数据与训练数据不匹配,因为 RGB 与灰度中有 2 个额外通道。

x_train shape: (46, 224, 224, 3)
xtest_shape shape: (46, 224, 224, 1)
autoencoder.fit(x_train, x_train, epochs=50, batch_size=256, shuffle=True, validation_data=(x_test, x_test))

谢谢。

【问题讨论】:

  • temp_gray_images[:,:,:,0,None] = gray_images 适合您吗?还是temp_gray_images[:,:,:,:1] = gray_images?还是temp_gray_images[:,:,:,0] = gray_images[:,:,:,0]

标签: python numpy keras autoencoder


【解决方案1】:

这就是你要找的吗?

import numpy as np
x_train=np.random.randn(46, 224, 224, 1)
y=np.repeat(x_train,3,axis=3)
print(y.shape) #(46, 224, 224, 3)

repeat 将灰度图像复制到所需数量的通道中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多