Diego给出了答案,我将使用
Functional API
。假设您有3个想要的特性
添加到每个图像。每个特征都是一个实数,所以如果我们将它们串联起来,我们就有一个包含三个分量的特征向量。
from keras.layers import ( Conv2D, Flatten, Lambda, Dense, concatenate,
Dropout, Input )
from keras.models import Model
image = np.random.rand(10, 66, 200, 3)
feature = np.random.rand(10, 3) # feature vector
y = np.random.normal(0, 1, (10, 1))
image_input = Input(shape=(66, 200, 3))
aux_input = Input(shape=(3,))
lamb = Lambda(lambda x: x/127.5-1.0, input_shape=(66,200,3))(image_input)
cov1 = Conv2D(24, 5, 5, activation='elu', subsample=(2, 2))(lamb)
cov2 = Conv2D(36, 5, 5, activation='elu', subsample=(2, 2))(cov1)
cov3 = Conv2D(48, 5, 5, activation='elu', subsample=(2, 2))(cov2)
cov4 = Conv2D(64, 3, 3, activation='elu')(cov3)
cov5 = Conv2D(64, 3, 3, activation='elu')(cov4)
dropout = Dropout(0.5)(cov5)
flatten = Flatten()(dropout)
# Here we add in the feature vectors
merge = concatenate([flatten, aux_input])
d1 = Dense(100, activation='elu')(merge)
d2 = Dense(50, activation='elu')(d1)
d3 = Dense(10, activation='elu')(d2)
out = Dense(1)(d3)
model = Model(inputs=[image_input, aux_input], outputs=[out])
model.compile(loss='mse', optimizer='rmsprop')
model.fit([image, feature], y)