我想用python实现一个多尺度CNN。我的目标是为三个不同的尺度使用三个不同的cnn,并将最终层的最终输出连接起来,并将它们馈送到FC层以获得输出预测。
但是我不明白我怎么才能实现这个。我知道如何实现单尺度CNN。
有谁能帮我一下吗?我不明白为什么要有3个CNN,因为大多数情况下你会得到和一个CNN相同的结果。也许你可以训练得快一点。也许你也可以做池和一些重发操作(我猜这可能证明类似于你想要的)。
然而,对于每个CNN,你需要一个成本函数来优化你使用的"启发式"(例如:提高识别)。此外,你可以在NN风格转移中做一些事情,其中你比较几个"目标"(内容和风格矩阵)之间的结果;或者简单地训练3个cnn,然后切断最后一层(或冻结它们),用已经训练过的权重再次训练,但现在是你的目标FN层…
这是一个多输入CNN的例子。您只需要引用提供每个网络输出的变量。然后使用concatate并将它们传递到一个密集的网络或任何你喜欢的任务。
def multires_CNN(filters, kernel_size, multires_data):
'''uses Functional API for Keras 2.x support.
multires data is output from load_standardized_multires()
'''
input_fullres = Input(multires_data[0].shape[1:], name = 'input_fullres')
fullres_branch = Conv2D(filters, (kernel_size, kernel_size),
activation = LeakyReLU())(input_fullres)
fullres_branch = MaxPooling2D(pool_size = (2,2))(fullres_branch)
fullres_branch = BatchNormalization()(fullres_branch)
fullres_branch = Flatten()(fullres_branch)
input_medres = Input(multires_data[1].shape[1:], name = 'input_medres')
medres_branch = Conv2D(filters, (kernel_size, kernel_size),
activation=LeakyReLU())(input_medres)
medres_branch = MaxPooling2D(pool_size = (2,2))(medres_branch)
medres_branch = BatchNormalization()(medres_branch)
medres_branch = Flatten()(medres_branch)
input_lowres = Input(multires_data[2].shape[1:], name = 'input_lowres')
lowres_branch = Conv2D(filters, (kernel_size, kernel_size),
activation = LeakyReLU())(input_lowres)
lowres_branch = MaxPooling2D(pool_size = (2,2))(lowres_branch)
lowres_branch = BatchNormalization()(lowres_branch)
lowres_branch = Flatten()(lowres_branch)
merged_branches = concatenate([fullres_branch, medres_branch, lowres_branch])
merged_branches = Dense(128, activation=LeakyReLU())(merged_branches)
merged_branches = Dropout(0.5)(merged_branches)
merged_branches = Dense(2,activation='linear')(merged_branches)
model = Model(inputs=[input_fullres, input_medres ,input_lowres],
outputs=[merged_branches])
model.compile(loss='mean_absolute_error', optimizer='adam')
return model