与输入维有关的多输入Keras模型出现错误 [英] Getting an error with a multi-input Keras model related to the input dimension
问题描述
我有一个多输入Keras模型。这里是输入:
I have a multi input Keras model. Here the inputs:
[<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>]
这里是模型的输入形状:
And here the input shape of the model :
[(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]
ValueError: Input 0 of layer conv1_pad_0 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [3, 422, 256, 256, 3]
我已经尝试过这篇文章,但我的基数不匹配。
I have tried the solution given in this post, but I got a mismatch in cardinality.
ValueError:数据基数模棱两可:
ValueError: Data cardinality is ambiguous:
我曾尝试像下面这样传递火车数据,并且有效:
I have tried passing the train data like bellow and it worked:
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个输入,则上面的代码行会出现问题。
Now If I want to scale my model to 20 inputs the above line of code will be problematique.
更新:
该模型基于预先训练的 ResNet50 ,所有输入均为一个没有顶层的resnet50,并从以下三个层开始:
The model are based on the pretrained ResNet50, all inputs are a resnet50 without the top layers and begin with the following three layers :
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]
用于训练/测试模型的数据按以下方式处理:
The data for training/testing the model is processed as follows:
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)
推荐答案
尝试
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()
。
it will produce a list of tensors with training data split along dimension 0. Then use your second solution, feeding the list to fit()
.
这篇关于与输入维有关的多输入Keras模型出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!