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

参考

猜你喜欢

转载自blog.csdn.net/qq_40985985/article/details/124912379
今日推荐