简体   繁体   English

ResNet(2D 图像)与全连接网络(1D 输入)的串联

[英]Concatenation of ResNet (2D images) with fully-connected network (1D input)

I'm using a pre-built ResNet in Keras (TensorFlow 2) in the following way:我通过以下方式在 Keras (TensorFlow 2) 中使用预构建的 ResNet:

from tensorflow.keras.applications.resnet50 import ResNet50
base_model = ResNet50(weights=None, include_top=False, input_shape=(39,39,3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
output_tensor = Dense(self.num_classes, activation='softmax')(x)
cnn_model = Model(inputs=base_model.input, outputs=output_tensor)
opt = Adam(lr=0.001)
cnn_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])

The inputs to the model ( base_model.input ) are 39 x 39 x 3 images. model ( base_model.input ) 的输入是39 x 39 x 3图像。 In addition, I now also want to provide a 20 dimensional vector with additional information to the model (ie 20 x 1 ).此外,我现在还想向 model (即20 x 1 )提供一个带有附加信息的 20 维向量。 I can do this in two different ways:我可以通过两种不同的方式做到这一点:

  1. Appending the 20 dimensional vector after the GlobalAveragePooling2D step.GlobalAveragePooling2D步骤之后附加 20 维向量。
  2. Creating an additional Fully-connected network for the 20-dimensional vector and concatenating the output of this fully-connected network to the above ResNet after the GlobalAveragePooling2D step.为 20 维向量创建一个额外的全连接网络,并在GlobalAveragePooling2D步骤之后将这个全连接网络的 output 连接到上述 ResNet。 Here ideally both networks are trained at the same time but I don't know if that is possible.理想情况下,两个网络都是同时训练的,但我不知道这是否可能。

Can I adjust my models for both options or does that not work?我可以为这两个选项调整我的模型还是不起作用?

Yes both options make sense and are possible with Keras.是的,这两个选项都有意义,并且可以使用 Keras。 For #2 you could define another model which takes the 20D vector as the input and passes it through a fully connected layer, then concatenate that output with the output of your pooling layer.对于#2,您可以定义另一个 model ,它将 20D 向量作为输入并将其传递给全连接层,然后将 output 与池化层的 output 连接起来。 And for both options you'd have to adjust your final model inputs to include both the base_model input and your 20D vector.对于这两个选项,您必须调整最终的 model 输入以包括 base_model 输入和 20D 向量。

This should do it, Comment out the Dense Layer to concatenate them after the global average pooling.应该这样做,注释掉密集层以在全局平均池化之后将它们连接起来。

from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.layers import GlobalAveragePooling2D, Dropout, Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
import tensorflow as tf


base_model = ResNet50(weights=None, include_top=False, input_shape=(39, 39, 3))
x1 = base_model.output
x1 = GlobalAveragePooling2D()(x1)
x1 = Dropout(0.5)(x1)

input_2 = tf.keras.layers.Input(shape=(20, 1))
x2 = tf.keras.layers.Flatten()(input_2)
# comment this if needed.
x2 = tf.keras.layers.Dense(16, activation='relu')(x2)

x = tf.keras.layers.Concatenate()([x1, x2])

output_tensor = Dense(self.num_classes, activation='softmax')(x)
cnn_model = Model(inputs=[base_model.input, input_2], outputs=output_tensor)
opt = Adam(lr=0.001)
cnn_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])
print(cnn_model.summary())

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

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