load_model错误," function = preprocess_input"在lambda层(Keras)

时间:2018-03-30 05:19:22

标签: keras keras-layer keras-2

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"

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

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