Search code examples
keraskeras-layerkeras-2

load_model error with "function=preprocess_input" in lambda layer (Keras)


keras 2.1.5 / TF backend

I tried to use lambda layer to do image pre-process (via function)

the model:

from keras.applications.resnet50 import preprocess_input

base_model = keras.applications.resnet50.ResNet50(include_top=False, input_shape=(224, 224, 3))

model = Sequential()
model.add(Lambda(preprocess_input, name='Input_Image', input_shape=(224, 224, 3))
model.add(base_model)
model.add(GlobalAveragePooling2D())
model.add(Dense(len(classes), activation="softmax"))

I called load_model with "custom_objects"

from keras.models import load_model
model = load_model(h5_weights, custom_objects={'preprocess_input': preprocess_input})

But then get the error

File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 663, in call
return self.function(inputs, **arguments)
File "/usr/local/lib/python2.7/dist-packages/keras/applications/imagenet_utils.py", line 177, in preprocess_input
return _preprocess_symbolic_input(x, data_format=data_format,
NameError: global name '_preprocess_symbolic_input' is not defined

The undefined function:

_preprocess_symbolic_input

is in the

File "/usr/local/lib/python2.7/dist-packages/keras/applications/imagenet_utils.py"

Any suggestion ?


Solution

  • Put _preprocess_symbolic_input also into custom_objects.

    custom_objects = {
        'preprocess_input': preprocess_input,
        '_preprocess_symbolic_input': keras.applications.imagenet_utils._preprocess_symbolic_input
    }
    model = load_model(h5_weights, custom_objects=custom_objects)