目前我有一个工作的CNN,从给定的图像输出-1到1的值。现在我还想将给定值输入网络,因为它们可以真正为输出添加准确性。当然,这项工作无法通过CNN完成,但应该可以将它们添加到完全连接的层中,不应该吗? 如果有人告诉我如何与Keras接近,那将会很棒。
我现在的CNN:
model = Sequential()
model.add(Lambda(lambda x: x/127.5-1.0, input_shape=(66,200,3)))
model.add(Conv2D(24, 5, 5, activation='elu', subsample=(2, 2)))
model.add(Conv2D(36, 5, 5, activation='elu', subsample=(2, 2)))
model.add(Conv2D(48, 5, 5, activation='elu', subsample=(2, 2)))
model.add(Conv2D(64, 3, 3, activation='elu'))
model.add(Conv2D(64, 3, 3, activation='elu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(100, activation='elu'))
model.add(Dense(50, activation='elu'))
model.add(Dense(10, activation='elu'))
model.add(Dense(1))
model.summary()
我认为它应该如何运作: 〜修改图形来自 https://blog.insightdatascience.com/classifying-e-commerce-products-based-on-images-and-text-14b3f98f899e
答案 0 :(得分:2)
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)
答案 1 :(得分:1)
如果您尝试在图片上重现模型,则无法使用Keras中的Sequential
。
你需要使用Model
,它会非常相似但有一些mods: