python - 是否可以创建同一个 CNN 的多个实例,这些实例接收多个图像并连接成一个密集层?(喀拉拉邦)
问题描述
与这个问题类似,我希望有几个图像输入层通过一个更大的 CNN(例如 XCeption 减去密集层),然后将所有图像中的一个 CNN 的输出连接成一个密集层。
Keras 是否可以实现这一点,或者甚至可以使用这种架构从头开始训练网络?
我本质上是想训练一个模型,该模型在每个样本中接收更大但固定数量的图像(即 3 个以上具有相似视觉特征的图像输入),但不是通过一次训练多个 CNN 来增加参数数量。这个想法是只训练一个可用于所有输出的 CNN。让所有图像进入相同的密集层很重要,因此模型可以学习跨多个图像的关联,这些图像总是根据它们的来源进行排序。
解决方案
您可以通过以下方式使用 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()```
推荐阅读
- django - 错误的模板渲染
- android - 我的 Android Studio 布局正在初始化而没有结束
- dart - Dart 如何在不阻塞的情况下运行异步代码?
- c++ - Winsock2 中的 inet_ntoa() 已弃用?
- reactjs - 如何使用反应路由器导航到其他页面
- python - Python 找工作游戏
- python - 如何正确格式化日期时间?
- ios - 如何在继续快速之前强制执行firebase功能
- machine-learning - 在元学习期间,什么时候除以 MAML 的 meta_batch_size?
- c# - 使用 FluentValidation 访问验证多个字段的数据库