Keras - CNN回归+最终完全连接层的附加输入

时间:2017-12-06 18:49:56

标签: keras keras-layer

让我们说我试图从个人资料图片中猜测年龄。执行卷积/合并后,我想在进行最终预测之前添加另一条信息,例如HEART RATE。

所以而不是 个人资料图片 - >卷积/汇集 - >完全连接的层 - > AGE

我想: 个人资料图片 - >卷积/汇集 - >完全连接的层,其中添加了关于心率的新输入 - > AGE

为此,我创建了一个函数如下:

def Add_to_FinalLayers(X, Additional):
    X = concatenate([X, Additional])
    return X

def AgeModel(input_shape):
    X_input = Input(input_shape)
    X = ZeroPadding2D((3,3))(X_input)
    X = Conv2D(32, (7,7) strides=(1,1), name ='conv0')(X)
    X = BatchNormalization(axis =3, name = 'bn0')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((2,2), name='max_pool')(X)
    X = Flatten()(X)
    X = Add_to_FinalLayers(X, HeartRateData_train)
    X = Dense(1, activation='linear', name='fc')(X)
    model = Model(inputs=[X_input, HeartRate_train], outputs=X,   name='AgeModel')
    return model

ageModel = AgeModel(X_train.shape[1:])
ageModel.compile(optimizer="RMSprop", loss="mse", metrics=["mse"])
ageModel.fit(x=[X_train,HeartRate_train], y=Y_train, epochs=30, batch_size=32)
preds = happyModel.predict(X_test)

我的数据大小是

number of training examples = 600
number of test examples = 150
X_train shape: (600, 64, 64, 3)
Y_train shape: (600, 1)
X_test shape: (150, 64, 64, 3)
Y_test shape: (150, 1)
HeartRate_train shape: (600, 1)
HeartRate_test shape: (150, 1)

我收到的错误消息是:

ValueError: Error when checking input: expected input_22 to have shape (None, 10) but got array with shape (600, 1)
ValueError: The model expects 2 arrays, but only received one array.  Found: array with shape (150, 64, 64, 3)
ValueError: 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 1 arrays) but in stead got the following list of 2 arrays:...

非常感谢任何建议。谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要将Heartbeat定义为模型的输入,方法是将其指定为:

input2 = Input(shape=[1], name='heart_rate')

然后,您可以在展平图层之后将其连接为:

 X = Concatenate(name='concat_layer')[X, input_2]

您的模型现在接受两个输入,一个图像和听到节拍。确保在测试时通过两者以获得预测。