【问题标题】:Neural network using both images and numerical inputs使用图像和数值输入的神经网络
【发布时间】:2019-05-27 14:22:47
【问题描述】:

为了对图像进行分类,我们使用了一个神经网络,其中包含几个卷积层和几个全连接层。

元数据包含一些有助于对图像进行分类的数字信息。有没有一种简单的方法可以将数字元数据连同卷积的输出一起输入到第一个全连接层?是否可以使用 TensorFlow 甚至更好的 Keras 来实现这一点?

【问题讨论】:

  • 您可以使用以下想法:通过 CNN 后,您的图像将转换为准备好输入 ANN 的平面数字列表。此时,您可以将所需的任何元数据附加到此平面列表(只要元数据也是数字列表)并将这个更长的列表输入 ANN。

标签: python tensorflow keras neural-network conv-neural-network


【解决方案1】:

您可以在另一个分支中处理数值数据,然后将结果与 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 功能 API。因此,我强烈建议为此阅读official guide

【讨论】:

    【解决方案2】:

    有没有一种简单的方法可以将数字元数据输入到第一个 全连接层,连同卷积的输出?

    是的,这是可能的。您需要两个用于数字元数据和图像的输入。

    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)
    

    在 keras here 中查看完整示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 2015-06-24
      • 1970-01-01
      • 2019-05-15
      • 2022-01-18
      • 2020-09-13
      • 2013-06-30
      相关资源
      最近更新 更多