Windows Keras load_model报错及解决

236 篇文章 43 订阅
14 篇文章 1 订阅

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

参考

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛一枚~

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值