简体   繁体   English

使用图像和数字输入的神经网络

[英]Neural network using both images and numerical inputs

To classify images, we are using a neural network with a few convolutional layers followed by a few fully-connected layers. 为了对图像进行分类,我们使用了一个神经网络,其中包含一些卷积层,然后是一些完全连接的层。

The metadata has some numerical information that could help classifying the images. 元数据具有一些有助于分类图像的数字信息。 Is there an easy way to input the numerical metadata into the first fully-connected layer, together with the output of the convolutions? 是否有一种简单的方法将数字元数据以及卷积的输出一起输入到第一个完全连接的层中? Is it possible to implement this using TensorFlow, or even better Keras? 是否可以使用TensorFlow甚至更好的Keras来实现?

You may process the numerical data in another branch and then merge the result with the CNN branch and then pass the merged tensor to a few final dense layers. 您可以在另一个分支中处理数值数据,然后将结果与CNN分支合并,然后将合并的张量传递到几个最终的密集层。 Here is a general sketch of the solution: 这是解决方案的一般示意图:

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

Surely, to build such a model you need to use Keras Functional API. 当然,要构建这样的模型,您需要使用Keras Functional API。 Therefore, I highly recommend to read the official guide for this purpose. 因此,我强烈建议您为此目的阅读官方指南

Is there an easy way to input the numerical metadata into the first fully-connected layer, together with the output of the convolutions? 是否有一种简单的方法将数字元数据以及卷积的输出一起输入到第一个完全连接的层中?

Yes, it is possible. 对的,这是可能的。 You need two inputs for numerical metadata and images. 对于数字元数据和图像,需要两个输入。

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)

See complete example in keras here . 此处查看keras中的完整示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM