【问题标题】:How to add a Lambda layer as an input layer to an existing model in Keras?如何将 Lambda 层作为输入层添加到 Keras 中的现有模型中?
【发布时间】:2019-03-21 06:38:00
【问题描述】:

我的任务是向 Keras 模型添加图像预处理层,因此在加载 Keras 模型后,我想为该模型添加一个新的输入层。

我发现我可以使用Lambda 层来预处理图像数据。图层代码为:

def vgg16preprocessing(x):
    mean_tensor = K.backend.variable([125.307, 122.95, 113.865], name="mean")
    std_tensor = K.backend.constant([62.9932, 62.0887, 66.7048], name="std_tensor")
    result = (x - mean_tensor) / (std_tensor)
    return K.backend.reshape(result, (-1, 32, 32, 3))
preproc_layer = K.layers.Lambda(vgg16preprocessing, output_shape=(32, 32, 3), input_shape=(32, 32, 3))

但我不知道如何在我的模型前面添加这一层。我找到了this answer,但是我无法在keras.layers.Input() 中添加图层。

有没有办法将Lambda层设置为新的输入层?

【问题讨论】:

    标签: python machine-learning keras keras-layer vgg-net


    【解决方案1】:

    您可以使用 VGG16 模型并将其应用于Lambda 层的输出:

    vgg = VGG16(...)
    
    input_img = Input(shape=...)
    preproc_img = Lambda(vgg16preprocessing)(input_img)
    output = vgg(preproc_img)
    
    model = Model(input_img, output)
    

    【讨论】:

      猜你喜欢
      • 2021-06-18
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多