Python Tensorflow 2:如何从保存的模型连接两个层?
我有两个保存的模型。我想加载模型1的输出并将其连接到模型2的输入:Python Tensorflow 2:如何从保存的模型连接两个层?,python,tensorflow2.0,Python,Tensorflow2.0,我有两个保存的模型。我想加载模型1的输出并将其连接到模型2的输入: # Load model1 model1 = tf.keras.models.load_model('/path/to/model1.h5') # Load model2 model2 = tf.keras.models.load_model('/path/to/model2.h5') # get the input/output tensors model1Output = model1.output model2Inpu
# Load model1
model1 = tf.keras.models.load_model('/path/to/model1.h5')
# Load model2
model2 = tf.keras.models.load_model('/path/to/model2.h5')
# get the input/output tensors
model1Output = model1.output
model2Input = model2.input
# reshape to fit
x = Reshape((imageHeight, imageWidth, 3))(model1Output)
# how do I set 'x' as the input to model2?
# this is the combined model I want to train
model = models.Model(inputs=model1.input, outputs=model2.output)
我知道,在实例化层时,可以通过将输入作为参数传递(x=Input(shape)
)来设置输入。但是如何在现有层上设置输入
,在我的例子中是x
?我已经看过层
类的文档,但是我看不到提到的这个
编辑:
添加两个模型的摘要
以下是model1
的顶部:
__________________________________________________________________________________________________
conv2d_transpose_3 (Conv2DTrans (None, 304, 304, 16) 4624 activation_14[0][0]
__________________________________________________________________________________________________
dropout_7 (Dropout) (None, 304, 304, 32) 0 concatenate[3][0]
__________________________________________________________________________________________________
conv2d_17 (Conv2D) (None, 304, 304, 16) 4624 dropout_7[0][0]
__________________________________________________________________________________________________
batch_normalization_17 (BatchNo (None, 304, 304, 16) 64 conv2d_17[0][0]
__________________________________________________________________________________________________
activation_16 (Activation) (None, 304, 304, 16) 0 batch_normalization_17[0][0]
__________________________________________________________________________________________________
conv2d_18 (Conv2D) (None, 304, 304, 10) 170 activation_16[0][0]
==================================================================================================
下面是model2
的输入:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 299, 299, 3) 0
__________________________________________________________________________________________________
block1_conv1 (Conv2D) (None, 149, 149, 32) 864 input_1[0][0]
__________________________________________________________________________________________________
block1_conv1_bn (BatchNormaliza (None, 149, 149, 32) 128 block1_conv1[0][0]
__________________________________________________________________________________________________
block1_conv1_act (Activation) (None, 149, 149, 32) 0 block1_conv1_bn[0][0]
__________________________________________________________________________________________________
block1_conv2 (Conv2D) (None, 147, 147, 64) 18432 block1_conv1_act[0][0]
__________________________________________________________________________________________________
我需要将model1
中conv2d\u 18
的输出作为model2
中block1\u conv1
的输入 假设您有两个模型,model1和model2,您可以将一个模型的输出传递给另一个模型的输入
您可以这样做:
在这里,model2.layers[1://code>针对您的问题选择索引1
,以跳过第一层并通过模型的第二层传播输入
在模型之间,我们可能需要额外的卷积层来适应输入的形状
def mymodel():
# Load model1
model1 = tf.keras.models.load_model('/path/to/model1.h5')
# Load model2
model2 = tf.keras.models.load_model('/path/to/model2.h5')
x = model1.output
#x = tf.keras.models.layers.Conv2D(10,(3,3))(x)
for i,layer in enumerate(model2.layers[1:]):
x = layer(x)
model = keras.models.Model(inputs=model1.input,outputs= x)
return model
注意:任何拥有更好解决方案的人都可以编辑此答案 找到了另一种方法,至少对我来说更有意义:
# Load model1
model1 = tf.keras.models.load_model('/path/to/model1.h5')
# Load model2
model2 = tf.keras.models.load_model('/path/to/model2.h5')
# reduce the 10 dim channels to 1 dim
newModel2Input = tf.math.reduce_max(model1.output, axis=-1)
# convert to 3 dims to match input expected by model2
newModel2Input = Reshape((299, 299, 3))(newModel2Input)
# this is the combined model I want to train
model = models.Model(inputs=model1.input, outputs=model2(newModel2Input))
那么,你想让model1的最后一层block1\u conv2
成为model2的输入,你也可以发布model2的摘要吗?我已经相应地编辑了这篇文章。我还更正了摘要中的一个错误。您现在看到的是正确的。模型1的输出具有形状(无,304,304,10)
,模型2的输入具有形状(无,299,299,3)
,通道分别为10和3是的,我添加了一个重塑层来转换尺寸:x=重塑((图像高度,图像宽度,3))(模型1输出)
(请参见代码)。但是我不知道如何将model2
的输入设置为x
。我的坏对于我来说,枚举中的层(model2.layers[1:]):x=layer(x)
你必须迭代每一层,还要注意层名,它们对于模型和它都应该是唯一的;你最好根据模型编写自己的图层