Python CNN程序:你传递给模型的Numpy数组列表不是模型预期的大小



我有以下代码,我试图将一组 23 张大小为 (96、96、96( 的 3D 图像与其相应的测试值(显示为 input_tmtA(组合(例如(50。换句话说,我希望一张图像的测试值为 50,而另一张图像的测试值为 80。我可以在没有第二个输入的情况下通过 CNN 运行我的图像,但是当我尝试连接第二个输入时,模型似乎找不到第二个输入的数组。

我尝试更改输入值和更改模型输入。这可能看起来不多,但我只是对可能的问题感到完全困惑,并且想不出任何其他方法或可能的错误可以尝试。我不确定如何将我的第二个数组与第一个数组一起添加到 CNN 中。我收到的错误是检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 2 个数组,但得到了以下 1个数组的列表,其中单个数组来自图像。错误指向"model.fit"行。谢谢

tmtA = np.array([50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80])
batch_size = 3
input_image = Input((x_train.shape[1]*x_train.shape[2]*x_train.shape[3], 1))
inputs = Input((x_train.shape[1], x_train.shape[2], x_train.shape[3], 1))
conv1 = Conv3D(32, [3, 3, 3], padding='same', activation='relu')(inputs)
conv1 = Conv3D(32, [3, 3, 3], padding='same', activation='relu')(conv1)
pool1 = MaxPooling3D(pool_size=(2, 2, 2), padding='same')(conv1)
drop1 = Dropout(0.5)(pool1)
conv2 = Conv3D(64, [3, 3, 3], padding='same', activation='relu')(drop1)
conv2 = Conv3D(64, [3, 3, 3], padding='same', activation='relu')(conv2)
pool2 = MaxPooling3D(pool_size=(2, 2, 2), padding='same')(conv2)
drop2 = Dropout(0.5)(pool2)
conv3 = Conv3D(128, [3, 3, 3], padding='same', activation='relu')(drop2)
conv3 = Conv3D(128, [3, 3, 3], padding='same', activation='relu')(conv3)
pool3 = MaxPooling3D(pool_size=(2, 2, 2), padding='same')(conv3)
drop3 = Dropout(0.5)(pool3)
conv4 = Conv3D(64, [3, 3, 3], padding='same', activation='relu')(drop3)
conv4 = Conv3D(64, [3, 3, 3], padding='same', activation='relu')(conv4)
pool4 = MaxPooling3D(pool_size=(2, 2, 2), padding='same')(conv4)
drop4 = Dropout(0.5)(pool4)
conv5 = Conv3D(32, [3, 3, 3], padding='same', activation='relu')(drop4)
conv5 = Conv3D(32, [3, 3, 3], padding='same', activation='relu')(conv5)
pool5 = MaxPooling3D(pool_size=(2, 2, 2), padding='same')(conv5)
drop5 = Dropout(0.5)(pool5)
flat1 = Flatten()(drop5)
dense1 = Dense(128, activation='relu')(flat1)
dense2 = Dense(64, activation='relu')(dense1)
dense3 = Dense(32, activation='relu')(dense2)
drop6 = Dropout(0.5)(dense3)
dense4 = Dense(num_classes, activation='softmax')(drop6)
input_tmtA = Input((len(tmtA), 1))
dense_tmtA1 = Dense(1, activation='softmax')(input_tmtA)
combine1 = concatenate([input_image, input_tmtA], axis=1)
model = Model(inputs=[input_image, input_tmtA], outputs=[combine1])
opt = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.02, amsgrad=False)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=batch_size, epochs=15, shuffle=True)
score = model.evaluate(x_test, y_test, batch_size=batch_size)
print(score)

这似乎与您的问题类似:https://github.com/keras-team/keras/issues/9475#issuecomment-377129713。

查看x_trainy_train的数组形状。Model(inputs=[input_image, input_tmtA], ...建议你应该传递两个数组,但x_train看起来它只是一个。你试过像model.fit([x_train1, x_train2], ...这样的东西吗?

我不太确定您要对输出做什么。dense_tmtA1dense4看起来从来没有被使用过。它们是您的输出图层吗?我觉得

Model(inputs=[input_image, input_tmtA], outputs=[combine1])

应改为

Model(inputs=[input_image, input_tmtA], outputs=[output_layer1, output_layer2]) 
// or [dense_tmtA1, dense4] or whatever your output layers are

有关进一步参考,请参阅 https://keras.io/guides/functional_api/#multi-input-and-multi-output-models。

我使用以下格式解决了它:

model_image = Model(inputs=inputs, outputs=dense4)

# tmtA model
input_tmtA = Input((1, 1))
flat_tmtA1 = Flatten()(input_tmtA)
dense_tmtA1 = Dense(num_subjects, activation='relu')(flat_tmtA1)
dense_tmtA2 = Dense(num_classes, activation='softmax')(dense_tmtA1)
model_tmtA = Model(inputs=input_tmtA, outputs=dense_tmtA1)
combine1 = concatenate([model_image.output, model_tmtA.output])
dense_combine1 = Dense(num_subjects, activation='relu')(combine1)
dense_combine2 = Dense(num_classes, activation='softmax')(dense_combine1)

# final model
model = Model(inputs=[model_image.input, model_tmtA.input], outputs=[dense_combine2])
opt = optimizers.Adam(lr=1e-6)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.fit([x_train, x_train_tmtA], y_train, batch_size=batch_size, epochs=15, shuffle=True)
score = model.evaluate([x_test, x_test_tmtA], y_test, batch_size=batch_size)
print(score)

最新更新