Python 如何将现有的keras模型拆分为两个独立的模型?

Python 如何将现有的keras模型拆分为两个独立的模型?,python,keras,Python,Keras,我有一个keras模型(已经训练过),我想将其分为两部分(一部分从原始输入计算内部表示,另一部分从预计算的内部表示计算输出) 获取第一部分很简单(输入到内部表示) 但第二部分是有问题的 我发现了两个相关的答案,但在我的案例中它们是有问题的 这些答案中描述的方法是: 在这个解决方案中,您重新定义了网络的第二部分-这似乎是可行的,但需要大量的代码复制(网络相当复杂) 在该解决方案中,模型由两个模型组成-这看起来是一个不错的解决方案,但与现有的经过培训的网络无关我发现的最佳解决方案: 定义一个“嵌

我有一个keras模型(已经训练过),我想将其分为两部分(一部分从原始输入计算内部表示,另一部分从预计算的内部表示计算输出)

获取第一部分很简单(输入到内部表示) 但第二部分是有问题的

我发现了两个相关的答案,但在我的案例中它们是有问题的

这些答案中描述的方法是:

  • 在这个解决方案中,您重新定义了网络的第二部分-这似乎是可行的,但需要大量的代码复制(网络相当复杂)


  • 在该解决方案中,模型由两个模型组成-这看起来是一个不错的解决方案,但与现有的经过培训的网络无关

    我发现的最佳解决方案:

  • 定义一个“嵌套”模型(子模型的组合)-答案中建议

  • 确保图层名称与旧模型中的图层名称相对应-这是重要的部分,因为它使图层映射更简单

  • 将权重从旧模型复制到新模型-如本例所示:

    for sub_model in filter(lambda l: isinstance(l, keras.models.Model), new_model.model.layers):
      for layer in filter(lambda l: l.weights, sub_model.layers):
        layer.set_weights(original_model.model.get_layer(layer.name).get_weights())
    
  • 这是我的解决方案(仅适用于顺序模型)。我在MobileNet 2中使用了这个函数,它对我来说非常有效,只需调用函数并给出预训练的模型和要拆分的索引,它将返回两个拆分的模型:

        def split_keras_model(model, index):
          '''
          Input: 
            model: A pre-trained Keras Sequential model
            index: The index of the layer where we want to split the model
          Output:
            model1: From layer 0 to index
            model2: From index+1 layer to the output of the original model 
          The index layer will be the last layer of the model_1 and the same shape of that layer will be the input layer of the model_2
          '''
          # Creating the first part...
          # Get the input layer shape
          layer_input_1 = Input(model.layers[0].input_shape[1:])
          # Initialize the model with the input layer
          x = layer_input_1
          # Foreach layer: connect it to the new model
          for layer in model.layers[1:index]:
                x = layer(x)
          # Create the model instance
          model1 = Model(inputs=layer_input_1, outputs=x)
    
    
          # Creating the second part...
          # Get the input shape of desired layer
          input_shape_2 = model.layers[index].get_input_shape_at(0)[1:] 
          print("Input shape of model 2: "+str(input_shape_2))
          # A new input tensor to be able to feed the desired layer
          layer_input_2 = Input(shape=input_shape_2) 
    
          # Create the new nodes for each layer in the path
          x = layer_input_2
          # Foreach layer connect it to the new model
          for layer in model.layers[index:]:
              x = layer(x)
    
          # create the model
          model2 = Model(inputs=layer_input_2, outputs=x)
    
          return (model1, model2)
    

    这是最好的解决办法。谢谢