问题描述
我正在尝试合并两个训练有素的神经网络.我有两个训练有素的Keras模型文件A和B.
I am trying to merge two trained neural networks.I have two trained Keras model files A and B.
模型A用于图像超分辨率,模型B用于图像着色.
Model A is for image super-resolution and model B is for image colorization.
我正在尝试合并两个训练有素的网络,以便可以更快地推断出SR +色彩. (我不愿意使用单个网络来完成SR和着色任务.我需要使用两个不同的网络来执行SR和着色任务.)
I am trying to merge two trained networks so that I can inference SR+colorization faster. (I am not willing to use a single network to accomplish both SR and colorization tasks. I need to use two different networks for SR and colorization tasks.)
关于如何合并两个Keras神经网络的任何技巧?
Any tips on how to merge two Keras neural networks?
推荐答案
只要网络A的输出形状与模型B的输入形状兼容,就可以.
As long a the shape of the output of the network A is compatible with the shape of the input of the model B, it is possible.
由于tf.keras.models.Model
继承自tf.keras.layers.Layer
,因此可以像创建keras模型时使用Layer
一样使用Model
.
As a tf.keras.models.Model
inherits from tf.keras.layers.Layer
, you can use a Model
as you would use a Layer
when creating your keras model.
一个简单的例子:
首先让我们创建2个简单网络A和B,其约束是B的输入与A的输出具有相同的形状.
Lets first create 2 simple networks, A and B, with the constraints that the input of B has the same shape as the output of A.
import tensorflow as tf
A = tf.keras.models.Sequential(
[
tf.keras.Input((10,)),
tf.keras.layers.Dense(5, activation="tanh")
],
name="A"
)
B = tf.keras.models.Sequential(
[
tf.keras.Input((5,)),
tf.keras.layers.Dense(10, activation="tanh")
],
name="B"
)
然后,我们可以将这两个模型合并为一个模型,在这种情况下,可以使用功能性API(完全可以使用顺序API来实现):
Then we can merge those two models as one, in that case using the functional API (this is completely possible using the Sequential API as well):
merged_input = tf.keras.Input((10,))
x = A(merged_input)
merged_output = B(x)
merged_model = tf.keras.Model(inputs=merged_input, outputs=merged_output, name="merged_AB")
产生以下网络:
>>> merged_model.summary()
Model: "merged_AB"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 10)] 0
_________________________________________________________________
A (Sequential) (None, 5) 55
_________________________________________________________________
B (Sequential) (None, 10) 60
=================================================================
Total params: 115
Trainable params: 115
Non-trainable params: 0
_________________________________________________________________
这篇关于合并两个训练有素的网络以进行顺序推理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!