load_model lambda 层中的 "function=preprocess_input" 错误 (Keras)

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

keras 2.1.5 / TF 后端

我尝试使用 lambda 层进行图像预处理(通过函数)

模特:

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

我用 "custom_objects"

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

然后得到错误

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

未定义函数:

_preprocess_symbolic_input

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

有什么建议吗?

_preprocess_symbolic_input也放入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)