Windows Keras load_model报错及解决

Posted 程序媛一枚~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows Keras load_model报错及解决相关的知识,希望对你有一定的参考价值。

1. 报错:ValueError: Unknown metric function:psnr

superResModel = load_model(config.SUPER_RES_MODEL)

解决

设置custom_objects

superResModel = load_model(config.SUPER_RES_MODEL,
                           custom_objects="psnr": psnr)

依然报错:ValueError: Unknown metric function:psnr

[INFO] loading model...
2022-05-22 16:25:17.428440: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "C:\\Users\\cici\\.IntelliJIdea2018.3\\config\\plugins\\python\\helpers\\pydev\\pydevd.py", line 1741, in <module>
    main()
  File "C:\\Users\\cici\\.IntelliJIdea2018.3\\config\\plugins\\python\\helpers\\pydev\\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\\Users\\cici\\.IntelliJIdea2018.3\\config\\plugins\\python\\helpers\\pydev\\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\\Users\\cici\\.IntelliJIdea2018.3\\config\\plugins\\python\\helpers\\pydev\\_pydev_imps\\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\\n", file, 'exec'), glob, loc)
  File "D:/deepLearning/py-demo/p220522/generate_super_res.py", line 99, in <module>
    custom_objects="psnr": psnr)
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\saving\\save.py", line 150, in load_model
    return saved_model_load.load(filepath, compile)
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\saving\\saved_model\\load.py", line 93, in load
    model._training_config))  # pylint: disable=protected-access
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\training\\tracking\\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training.py", line 356, in compile
    self._cache_output_metric_attributes(metrics, weighted_metrics)
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training.py", line 1901, in _cache_output_metric_attributes
    metrics, self.output_names, output_shapes, self.loss_functions)
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training_utils.py", line 815, in collect_per_output_metric_info
    metric, output_shape=output_shapes[i], loss_fn=loss_fns[i])
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\engine\\training_utils.py", line 1020, in get_metric_function
    return metrics_module.get(metric)
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\metrics.py", line 2859, in get
    return deserialize(str(identifier))
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\metrics.py", line 2851, in deserialize
    printable_module_name='metric function')
  File "E:\\python\\lib\\site-packages\\tensorflow_core\\python\\keras\\utils\\generic_utils.py", line 210, in deserialize_keras_object
    raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
ValueError: Unknown metric function:psnr

.h5模型可以解决,saved_model.pb不行

解决:

由于保存的模型为saved_model.pb

# superResModel = load_model(config.SUPER_RES_MODEL)
# .h5模型设置 custom_objects就可以解决,.pb不行,得先设置compile=False,然后手动compile
# superResModel = load_model(config.SUPER_RES_MODEL,
#                            custom_objects="psnr": psnr)
superResModel = load_model(config.SUPER_RES_MODEL, compile=False)
superResModel.compile(optimizer="adam", loss="mse", metrics=[psnr])

参考

以上是关于Windows Keras load_model报错及解决的主要内容,如果未能解决你的问题,请参考以下文章

Tensorflow tf.keras.models.load_model() 打开h5文件失败

无法使用 keras.load_model 保存/加载模型 - IndexError: list index out of range

keras.models load_model中的TypeError('关键字参数不理解:','组')

带有自定义对象的 Keras load_model 无法正常工作

keras中模型如何传递到函数里供函数体使用

Keras Model AttributeError:’str‘ object has no attribute ’call‘