2

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 ?

Ioannis Nasios
  • 8,292
  • 4
  • 33
  • 55
Kai
  • 45
  • 1
  • 7

1 Answers1

2

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)
Yu-Yang
  • 14,539
  • 2
  • 55
  • 62
  • if you dont find `keras.applications.imagenet_utils._preprocess_symbolic_input`, change `keras.applications` to `keras_applications`. – pitfall May 28 '20 at 21:58