python - 具有不同输入的集成模型(预计会看到 2 个数组)

标签 python machine-learning keras deep-learning ensemble-learning

我已经训练了 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,081
Trainable params: 7,846,081
Non-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,915
Trainable params: 24,133,795
Non-trainable params: 53,120

UNet 有 1 个 channel (灰色),ResNet 有 3 个 channel 。

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

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 model

models = [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)

我认为问题出在 channel 上,但我不知道如何克服这个问题。

最佳答案

如果你的训练数据是灰度图像,并且考虑到你的 ResNet 模型将 RGB 图像作为输入,那么你应该问自己,你想如何从灰度到 RGB?一种答案是重复灰度图像 3 次以获得 RBG 图像。然后,您可以轻松地定义一个具有一个输入层的模型,该输入层获取灰度图像并将其相应地输入到您定义的模型中:

from keras import backend as K

input_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)

然后您可以使用一个输入数组轻松调用预测:

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])

关于python - 具有不同输入的集成模型(预计会看到 2 个数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52634226/

相关文章:

python - clang 错误: unknown argument: '-mno-fused-madd' (python package installation failure)

python - 在 mechanize 中,是否有重写 URL 以 POST 形式?

python - 带条件的嵌套列表的列表理解

python - 从日期时间对象中减去 datetime.datetime.now()

python - 类型错误 : fit() takes exactly 3 arguments (2 given) with sklearn and sklearn_pandas

rest - TensorFlow Serving REST API 的正确负载

machine-learning - 无法在浏览器中打开 Tensorboard

machine-learning - 较小的参数值如何有助于防止过度拟合?

python-3.x - 如何在不指定输入(或输入形状)的情况下在 keras 2 功能 API 中链接/组合层

python - 在 keras model.predict 中获取所有相同的值