不同输入的集成模型(预期看到2个数组)

我已经训练了两个模型。

第一个模型是UNet:

print(model_unet.summary())__________________________________________________________________________________________________Layer (type)                    Output Shape         Param #     Connected to                     ==================================================================================================input_4 (InputLayer)            (None, 128, 128, 1)  0                                            __________________________________________________________________________________________________conv2d_26 (Conv2D)              (None, 128, 128, 32) 320         input_4[0][0]                    __________________________________________________________________________________________________conv2d_27 (Conv2D)              (None, 128, 128, 32) 9248        conv2d_26[0][0]  ..........conv2d_44 (Conv2D)              (None, 128, 128, 1)  33          zero_padding2d_4[0][0]           ==================================================================================================Total params: 7,846,081Trainable params: 7,846,081Non-trainable params: 0

第二个是ResNet:

print(model_resnet.summary())__________________________________________________________________________________________________Layer (type)                    Output Shape         Param #     Connected to                     ==================================================================================================input_3 (InputLayer)            (None, 128, 128, 3)  0                                            __________________________________________________________________________________________________conv1_pad (ZeroPadding2D)       (None, 134, 134, 3)  0           input_3[0][0]                    ........conv2d_25 (Conv2D)              (None, 128, 128, 3)  99          zero_padding2d_3[0][0]           ==================================================================================================Total params: 24,186,915Trainable params: 24,133,795Non-trainable params: 53,120

UNet有1个通道(灰度),而ResNet有3个通道。

然后,我试图创建一个集成模型:

def ensemble(models, models_input):    outputs = [model(models_input[idx]) for idx, model in enumerate(models)]    x = Average()(outputs)    model_inputs = [model for model in models_input]    model = Model(model_inputs, x)    return modelmodels = [model_unet, model_resnet]models_input = [Input((128,128,1)), Input((128,128, 3))]ensemble_model = ensemble(models, models_input)

当我尝试在验证数据上进行预测时:

pred_val = ensemble_model.predict(X_val)

我收到了以下错误:

Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[0.46755977],         [0.52268691],         [0.52766109],         ....X_val.shape is : (800, 128, 128, 1)

我认为问题出在通道上,但我不知道如何解决这个问题。


回答:

如果你的训练数据是灰度图像,并且考虑到你的ResNet模型需要输入RGB图像,那么你应该问自己如何从灰度转换到RGB?一种答案是将灰度图像重复三次以获得RGB图像。然后你可以轻松定义一个模型,带有一个输入层,该层接受你的灰度图像,并相应地将它们输入到你定义的模型中:

from keras import backend as Kinput_image = Input(shape=(128,128,1))unet_out = model_unet(input_image)rgb_image = Lambda(lambda x: K.repeat_elements(x, 3, -1))(input_image)resnet_out = model_resnet(rgb_image)output = Average()([unet_out, resnet_out])ensemble_model = Model(input_image, output)

然后你可以轻松地用一个输入数组调用predict:

pred_val = ensemble_model.predict(X_val)

这个解决方案的另一种选择是你问题中使用的解决方案。然而,你首先需要将你的图像从灰度转换为RGB,然后将两个数组都传递给predict方法:

X_val_rgb = np.repeat(X_val, 3, -1)pred_val = ensemble_model.predict([X_val, X_val_rgb])

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注