【问题标题】:Getting an error with a multi-input Keras model related to the input dimension与输入维度相关的多输入 Keras 模型出现错误
【发布时间】:2020-09-28 15:54:17
【问题描述】:

我有一个多输入 Keras 模型。这里的输入:

[<tf.Tensor 'input_1:0' shape=(None, 256, 256, 3) dtype=float32>,
 <tf.Tensor 'input_2:0' shape=(None, 256, 256, 3) dtype=float32>,
 <tf.Tensor 'input_3:0' shape=(None, 256, 256, 3) dtype=float32>,
 <tf.Tensor 'input_4:0' shape=(None, 256, 256, 3) dtype=float32>]

这里是模型的输入形状:

[(None, 256, 256, 3),
 (None, 256, 256, 3),
 (None, 256, 256, 3),
 (None, 256, 256, 3)]

训练数据形状如下:

(4, 422, 256, 256, 3)
4 = number of inputs (consist of appended arrays together).
422 = number of training images in each input.
256, 256, 3 = shape of the images

当我调用fit 函数时:

model.fit(train_x, train_y, validation_split=0.20, epochs=5, batch_size=3)

出现以下错误:

ValueError: 层 conv1_pad_0 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=5。收到的完整形状:[3, 422, 256, 256, 3]

我已经尝试了this post 中给出的解决方案,但我的基数不匹配。

ValueError:数据基数不明确:

我尝试过像下面这样传递火车数据,它奏效了:

model.fit([train_x[0], train_x[1], train_x[2], train_x[3]], train_y, validation_split=0.20, epochs=5, batch_size=3)

现在如果我想将我的模型扩展到 20 个输入,上面的代码行将会出现问题。

更新:

该模型基于预训练的ResNet50,所有输入都是没有顶层的resnet50,并从以下三层开始:

input_1_0 (InputLayer)        [(None, 256, 256, 3) 0  
conv1_pad_0 (ZeroPadding2D)   (None, 262, 262, 3)  0           input_1_0[0][0]
conv1_conv_0 (Conv2D)         (None, 128, 128, 64) 9472        conv1_pad_0[0][0]   

用于训练/测试模型的数据处理如下:

for row in np.array(tmp_data):
        row = images_preprocessing(row) # Depends on the model used
        train_x, test_x, train_y, test_y = split_data(row, target) # Here the train_test_split is used
        
        train_X.append(train_x)
        test_X.append(test_x)
        train_Y.append(train_y)
        test_Y.append(test_y)

【问题讨论】:

  • 你的网络的前两层是什么?
  • @ChrisSchmitz 我已经根据您的要求更新了帖子。
  • 能否也显示定义它们的代码?此外,您的训练数据的哪个维度是总 nr。示例/批量大小?
  • @ChrisSchmitz 我更新了帖子。示例数量是什么意思?
  • 训练数据形状如下:(4, 422, 256, 256, 3)。 4 和 422 在您的输入中代表什么?预训练的 ResNet50 的输入应该是 4d。

标签: python numpy tensorflow keras conv-neural-network


【解决方案1】:

试试

train_x_list = [tf.squeeze(tx) for tx in tf.split(train_x, num_or_size_splits=train_x.shape[0], axis=0)]

它将生成一个张量列表,其中训练数据沿维度 0 拆分。然后使用您的第二个解决方案,将该列表提供给 fit()

【讨论】:

  • 感谢您的回答,它成功了。如果可能的话,您能否解释一下为什么这有效而不是简单的数组列表?另外,我在一个循环中训练多个模型,第一个执行得很好,但是第二个,它产生了这个错误:tensorflow.python.framework.errors_impl.InternalError: Failed copying input tensor from /job:localhost/replica:0/task:0/device:CPU:0 to /job:localhost/replica:0/task:0/device:GPU:0 in order to run Split: Dst tensor is not initialized. [Op:Split] name: split
  • 不客气。我刚刚展示了一种拆分输入数据的方法。 np 数组列表也可能没问题。
  • 您的其他错误可能是内存问题:stackoverflow.com/questions/37313818/…
  • 我正在为另一个问题苦苦挣扎,你能看看我之前的帖子吗:stackoverflow.com/questions/64118599/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多