【问题标题】:keras shape of variable sized training data可变大小训练数据的 keras 形状
【发布时间】:2018-03-22 06:14:21
【问题描述】:

我用 Keras (tensorflow) 实现了以下模型:

Layer (type)                 Output Shape              Param #
=================================================================
conv2d_1 (Conv2D)            (None, None, None, 32)    896
_________________________________________________________________
conv2d_2 (Conv2D)            (None, None, None, 32)    9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, None, None, 32)    0
_________________________________________________________________
dropout_1 (Dropout)          (None, None, None, 32)    0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, None, None, 64)    18496
_________________________________________________________________
conv2d_4 (Conv2D)            (None, None, None, 64)    36928
_________________________________________________________________
dropout_2 (Dropout)          (None, None, None, 64)    0
_________________________________________________________________
global_average_pooling2d_1 ( (None, 64)                0
_________________________________________________________________
dense_1 (Dense)              (None, 256)               16640
_________________________________________________________________
dropout_3 (Dropout)          (None, 256)               0
_________________________________________________________________
dense_2 (Dense)              (None, 14)                3598
=================================================================
Total params: 85,806
Trainable params: 85,806
Non-trainable params: 0

我的数据集是 Leed 运动集,其中包含可变宽度和高度的图像。根据 Keras documentation 和 github issue,我只需将 inputshape 设置为 (None, None, Num_Channels)。为了准备我的数据集,我将注释和图像加载为 numpy 数组,如下所示:

# Train Input, contains paths of images first
x_train = image_list[train_indexes]

print("Converting x_train images to numpy...")
x_train = np.array([misc.imread(path) for path in x_train])
print(x_train.shape)
print(x_train[0].shape)
print(x_train[1].shape)

打印调用的输出是:

(9600,) # x_train
(188, 282, 3) # x_train first image
(686, 1024, 3) # x_train second image

如果我现在将 x_train 提供给我的模型,它会抛出以下错误:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (9600, 1)

如何塑造我的训练集以让我的模型接受?

【问题讨论】:

    标签: python tensorflow machine-learning keras


    【解决方案1】:

    Keras 希望您为x_train 输入一个与您的输入形状相匹配的张量(一个 4 维张量)。您在x_train 中创建的是一个由不同形状的 3-D 数组对象组成的 1-D 数组,而不是 4-D 数组。这不是张量。张量不能是锯齿状的,它不能有不同形状的行。

    由于矢量化,Keras 要求您输入张量。为了获得向量化的好处,所有输入都必须是张量。 Keras 不能处理锯齿状张量,numpy 也不能。要运行不同形状的输入,您必须使用 Keras 的矢量化来填充每个图像(在所有 3 个通道中使用 0),以使它们的形状一致。或者,您可以编写一个 for 循环,在其中一次通过模型运行输入。这基本上会失去矢量化的所有好处,并且可能需要很长时间来训练你的模型。

    【讨论】:

    • 你能举个例子来说明你的填充选项吗?
    • 据我所知,np.pad 必须获得固定数量的填充物。如果我的可变大小有问题,我该如何做到这一点?
    • 您可以设置一个默认大小,然后编写一个函数来计算根据图像大小与默认大小相比您需要填充多少图像(基本减法),并且然后使用 np.pad 填充图像。
    • 还可以查看空间金字塔池化作为构建 CNN 的一种方法,该 CNN 可以采用任意大小的输入图像:github.com/yhenon/keras-spp
    猜你喜欢
    • 2018-02-19
    • 2017-10-25
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2017-07-29
    • 2017-05-29
    相关资源
    最近更新 更多