2

我的任务是向 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))

但我不知道如何在我的模型前面添加这一层。我找到了这个答案,但我无法在keras.layers.Input().

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

4

1 回答 1

2

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

vgg = VGG16(...)

input_img = Input(shape=...)
preproc_img = Lambda(vgg16preprocessing)(input_img)
output = vgg(preproc_img)

model = Model(input_img, output)
于 2018-10-16T17:05:33.403 回答