与输入维度相关的多输入 Keras 模型出现错误

我有一个多输入 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]


我已经尝试过这篇文章中给出的解决方案,但我发现基数不匹配。


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)


慕尼黑的夜晚无繁华
浏览 82回答 1
1回答

繁华开满天机

尝试train_x_list&nbsp;=&nbsp;[tf.squeeze(tx)&nbsp;for&nbsp;tx&nbsp;in&nbsp;tf.split(train_x,&nbsp;num_or_size_splits=train_x.shape[0],&nbsp;axis=0)]它将生成一个张量列表,其中训练数据沿维度 0 分割。然后使用第二个解决方案,将列表提供给fit()。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python