3

I have a task to add a image preprocessing layer to a Keras model, so after I loaded a Keras model, I want to add a new input layer for this model.

I found I can use Lambda layer to preprocess the image data. The layer code is:

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

But I don't know how to add this layer in the front of my model. I found this answer, but I can't add the layer in the keras.layers.Input().

Is there any ways to set the Lambda layer as a new input layer?

1 Answer 1

2

You can use the VGG16 model and apply it on the output of Lambda layer:

vgg = VGG16(...)

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

model = Model(input_img, output)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.