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

Tensorflow 2: how to connect two layers from saved models?

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

我知道您可以在实例化 Layer 时设置输入,方法是将输入作为参数 (x = Input(shape)) 传递。但是如何在现有图层上设置 Input,在我的例子中是 x?我查看了 Layer class here 的文档,但我看不到这个提及?

编辑:

添加两个模型的摘要...

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

我需要 model1conv2d_18 的输出作为 model2block1_conv1 的输入。

假设你有两个模型,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


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

找到了至少对我来说更有意义的另一种方法:

# 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))