为了对图像进行分类,我们使用了具有一些卷积层和一些全连接层的神经网络。
元数据中包含一些数值信息,这些信息有助于图像分类。有没有一种简单的方法将数值元数据与卷积输出共同输入到第一个全连接层中?是否可以使用TensorFlow或者更好的Keras实现这个功能?
元数据中包含一些数值信息,这些信息有助于图像分类。有没有一种简单的方法将数值元数据与卷积输出共同输入到第一个全连接层中?是否可以使用TensorFlow或者更好的Keras实现这个功能?
您可以在另一个分支中处理数字数据,然后将结果与CNN分支合并,然后将合并的张量传递给几个最终的密集层。以下是解决方案的一般概述:
# process image data using conv layers
inp_img = Input(shape=...)
# ...
# process numerical data
inp_num = Input(shape=...)
x = Dense(...)(inp_num)
out_num = Dense(...)(x)
# merge the result with a merge layer such as concatenation
merged = concatenate([out_conv, out_num])
# the rest of the network ...
out = Dense(num_classes, activation='softmax')(...)
# create the model
model = Model([inp_img, inp_num], out)
当然,要构建这样的模型,您需要使用Keras Functional API。因此,我强烈建议阅读官方指南以达到此目的。
有没有一种简单的方法将数字元数据与卷积的输出一起输入到第一个全连接层中?
是的,这是可能的。您需要两个输入,一个用于数字元数据,另一个用于图像。
inp1 = Input(28,28,1) # image
inp2 = Input(30,) # numerical metadata (assume size of numerical feature is 30)
conv2d = Convolution2D(100,strides=1,padding='same')(inp1)
embedding = Embedding(1000)(inp2)
# ... rest of the network
prev_layer = Concatenation(axis=-1)[feature_image, feature_metadata]
prediction = Dense(100)(prev_layer)
model = Model(inputs=[inp1, inp2], outputs=prediction)