Tensorflow:加载保存的模型:UnicodeDecodeError

时间:2019-03-14 11:12:26

标签: python tensorflow

我有一个由其他人创建的Tensorflow模型,我想从中创建一个冻结的图。

我拥有的文件:

+--- name.txt
+--- saved_model.pb
+--- variables
|   +--- variables.data-00000-of-00001
|   +--- variables.index

因此,在this page的帮助下,我执行了以下代码:

import tensorflow as tf
import os

pwd = os.getcwd()
toolsFilepath = os.path.dirname(tf.__file__)+"/python/tools"
modelFolder = [PATH_TO_MODEL]
networkArcFile = "saved_model.pb"
networkWeightsFile = "variables/variables.data-00000-of-00001"
outputFolder = pwd+"/output/"+networkArcFile+".modelzoo"

args = "--input_graph="+modelFolder+"/"+networkArcFile+"\
        --input_checkpoint="+modelFolder+"/"+networkWeightsFile+"\
        --output_graph="+outputFolder

os.system(toolsFilepath+"/freeze_graph.py "+args)

但是,当我这样做时,会出现错误:

Traceback (most recent call last):
  File "[MY_PATH]\lib\site-packages\tensorflow\python\tools\freeze_graph.py", line 491, in <module>
    run_main()
  File "[MY_PATH]\lib\site-packages\tensorflow\python\tools\freeze_graph.py", line 488, in run_main
    app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
  File "[MY_PATH]\lib\site-packages\tensorflow\python\platform\app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "[MY_PATH]\lib\site-packages\absl\app.py", line 300, in run
    _run_main(main, args)
  File "[MY_PATH]\lib\site-packages\absl\app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "[MY_PATH]\lib\site-packages\tensorflow\python\tools\freeze_graph.py", line 487, in <lambda>
    my_main = lambda unused_args: main(unused_args, flags)
  File "[MY_PATH]\lib\site-packages\tensorflow\python\tools\freeze_graph.py", line 381, in main
    flags.saved_model_tags, checkpoint_version)
  File "[MY_PATH]\lib\site-packages\tensorflow\python\tools\freeze_graph.py", line 340, in freeze_graph
    input_graph_def = _parse_input_graph_proto(input_graph, input_binary)
  File "[MY_PATH]\lib\site-packages\tensorflow\python\tools\freeze_graph.py", line 253, in _parse_input_graph_proto
    text_format.Merge(f.read(), input_graph_def)
  File "[MY_PATH]\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 132, in read
    pywrap_tensorflow.ReadFromStream(self._read_buf, length, status))
  File "[MY_PATH]\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 100, in _prepare_value
    return compat.as_str_any(val)
  File "[MY_PATH]\lib\site-packages\tensorflow\python\util\compat.py", line 107, in as_str_any
    return as_str(value)
  File "[MY_PATH]\lib\site-packages\tensorflow\python\util\compat.py", line 80, in as_text
    return bytes_or_text.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 3: invalid start byte

此错误的可能原因是什么,我该如何解决?

注意:

  • 我正在使用tensorflow 1.13.1和Python 3.6

1 个答案:

答案 0 :(得分:1)

针对类似的 UnicodeDecodeError:建议here调用--input_binary=true时尝试使用freeze_graph.py,因为'utf8'编解码器无法解码字节... 问题。您的saved_model.pb很可能是二进制文件。