首页 > 解决方案 > 是否可以创建同一个 CNN 的多个实例,这些实例接收多个图像并连接成一个密集层?(喀拉拉邦)

问题描述

这个问题类似,我希望有几个图像输入层通过一个更大的 CNN(例如 XCeption 减去密集层),然后将所有图像中的一个 CNN 的输出连接成一个密集层。

在此处输入图像描述

Keras 是否可以实现这一点,或者甚至可以使用这种架构从头开始训练网络?

我本质上是想训练一个模型,该模型在每个样本中接收更大但固定数量的图像(即 3 个以上具有相似视觉特征的图像输入),但不是通过一次训练多个 CNN 来增加参数数量。这个想法是只训练一个可用于所有输出的 CNN。让所有图像进入相同的密集层很重要,因此模型可以学习跨多个图像的关联,这些图像总是根据它们的来源进行排序。

标签: pythontensorflowkerasdeep-learningconv-neural-network

解决方案


您可以通过以下方式使用 Keras 功能 API 轻松实现此目的。

from tensorflow.python.keras import layers, models, applications

# Multiple inputs
in1 = layers.Input(shape=(128,128,3))
in2 = layers.Input(shape=(128,128,3))
in3 = layers.Input(shape=(128,128,3))

# CNN output
cnn = applications.xception.Xception(include_top=False)


out1 = cnn(in1)
out2 = cnn(in2)
out3 = cnn(in3)

# Flattening the output for the dense layer
fout1 = layers.Flatten()(out1)
fout2 = layers.Flatten()(out2)
fout3 = layers.Flatten()(out3)

# Getting the dense output
dense = layers.Dense(100, activation='softmax')

dout1 = dense(fout1)
dout2 = dense(fout2)
dout3 = dense(fout3)

# Concatenating the final output
out = layers.Concatenate(axis=-1)([dout1, dout2, dout3])

# Creating the model
model = models.Model(inputs=[in1,in2,in3], outputs=out)
model.summary()```

推荐阅读