我创建了四个3D-CNN模型,每个模型都被训练成一组不同(但相关的)图像,这样每组都有相同对象的不同视角的图像。(即:n个对象有来自4个不同透视图的图像,每个模型都与一个透视图相关联)。
def get_model(width=128, height=128, depth=4):
inputs = Input((width, height, depth, 3))
x = Conv3D(filters=64, kernel_size=8,padding='same', activation="relu")(inputs)
x = MaxPool3D(pool_size=2,data_format= "channels_first", padding='same')(x)
x = BatchNormalization()(x)
x = Conv3D(filters=256, kernel_size=3,padding='same', activation="relu")(x)
x = MaxPool3D(pool_size=2,data_format= "channels_first", padding='same')(x)
x = BatchNormalization()(x)
x = GlobalAveragePooling3D()(x)
x = Dense(units=512, activation="relu")(x)
x = Dropout(0.3)(x)
outputs = Dense(units=2, activation="sigmoid")(x)
# Define the model.
model = keras.Model(inputs, outputs)
return model我现在有四个预先训练过的模型,我想通过删除最后的密集层(sigmoid)来组合它们,然后连接所有四个模型的密集层,然后是一个激活函数(即: sigmoid)。我想保留四个输入层,这样每个层都会从一个角度拍摄一个对象的图像。我已经看到了将model_1的输出层连接到model_2的输入层的例子,但是,我不知道如何处理四个独立的输入层并连接到模型的末尾。
发布于 2021-01-18 12:51:05
让我们假设您的预培训模型文件名为"A.h5“和"B.h5”。您可以简单地在TensorFlow中加载它们,访问与层属性交互的层,并将它们与Functional合并。一个例子可以是:
import tensorflow as tf
pretrainedmodel_files = ["A.h5", "B.h5"]
A,B = [tf.keras.models.load_model(filename) for filename in pretrainedmodel_files]
# Skipping the last dense layer and the dropout means accessing the layer at the index -3
concat = tf.keras.layers.Concatenate()([A.layers[-3].output, B.layers[-3].output])
out = tf.keras.layers.Dense(2,activation="sigmoid")(concat)
model = tf.keras.Model(inputs=[A.input, B.input], outputs=out)我用以下代码创建了两个简单的模型:
tf.keras.Sequential(
[
tf.keras.layers.Dense(10, activation="relu", input_shape=(5,)),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(2, activation="sigmoid")
]
)然后将它们与我的示例代码合并。
A和B具有以下体系结构(使用netron可视化):

以及合并后的网络:

https://stackoverflow.com/questions/65774167
复制相似问题