温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Tensorflow 2: how to connect two layers from saved models?

python - Tensorflow 2:如何从保存的模型连接两层?

发布于 2020-04-03 23:29:06

我有两个保存的模型。我想加载并将模型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
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)

我知道您可以在实例化a时Layer通过将输入作为参数(x = Input(shape)来设置输入但是Input,在我的情况下x,如何在现有层上设置?我在这里查看了Layer该类的文档,但是看不到提到的内容吗?

编辑:

添加两个模型的汇总...

这是顶部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]           
__________________________________________________________________________________________________

我需要的输出conv2d_18model1被馈送作为输入到block1_conv1model2

查看更多

提问者
CSharp
被浏览
33
Shubham Shaswat 2020-01-31 22:44

假设您有两个模型,model1和model2,则可以将一个模型的输出传递给另一个模型,

您可以通过以下方式进行操作:

在这里,model2.layers[1:]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


注意:具有更好解决方案的任何人都可以编辑此答案。