使用freeze_graph将SavedModel生成的模型合并成一个pb文件

技巧:使用freeze_graph将SavedModel生成的模型合并成一个pb文件

在训练mnist模型的时候,增加一个参数,可以在训练完毕后导出SavedModel

python mnist.py --export_dir /tmp/mnist_saved_model

训练完成后,会以以下文件结构在/tmp/mnist_saved_model生成模型文件

── 1530159051
   ├── saved_model.pb
   └── variables
       ├── variables.data-00000-of-00001
       └── variables.index

saved_model.pb 保存的是计算图信息
variables 保存的是计算中的变量信息

我的目是想要在Android上运行测试这个模型,但是从官方给的Demo中可以看到,所加载的模型都是单个文件,暂时也没看到加载SavedModel的接口

为了方便Android平台使用,用freeze_graph脚本将SavedModel合并

查看这个脚本的源码,在最开头可以看到

Converts checkpoint variables into Const ops in a standalone GraphDef file.
This script is designed to take a GraphDef proto, a SaverDef proto, and a set of
variable values stored in a checkpoint file, and output a GraphDef with all of
the variable ops converted into const ops containing the values of the
variables.
It's useful to do this when we need to load a single file in C++, especially in
environments like mobile or embedded where we may not have access to the
RestoreTensor ops and file loading calls that they rely on.

这个脚本就是用来将变量转换为常量合并到到计算图中的

具体怎么使用呢?

脚本中给出的例子是这样的:

bazel build tensorflow/python/tools:freeze_graph && \
bazel-bin/tensorflow/python/tools/freeze_graph \
--input_graph=some_graph_def.pb \
--input_checkpoint=model.ckpt-8361242 \
--output_graph=/tmp/frozen_graph.pb --output_node_names=softmax

因为我电脑是完整安装了tensorflow的,所以可以直接使用freeze_graph而不用通过bazel去创建freeze_graph

freeze_graph --input_graph=saved_model.pb --input_checkpoint=variables/variables --output_graph=merge_graph.pb --output_node_names=softmax

执行,然后挂了

  from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "/home/xxx/anaconda2/bin/freeze_graph", line 11, in <module>
    sys.exit(run_main())
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/tensorflow/python/tools/freeze_graph.py", line 379, in run_main
    app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/tensorflow/python/tools/freeze_graph.py", line 378, in <lambda>
    my_main = lambda unused_args: main(unused_args, flags)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/tensorflow/python/tools/freeze_graph.py", line 272, in main
    flags.saved_model_tags, checkpoint_version)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/tensorflow/python/tools/freeze_graph.py", line 231, in freeze_graph
    input_graph_def = _parse_input_graph_proto(input_graph, input_binary)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/tensorflow/python/tools/freeze_graph.py", line 174, in _parse_input_graph_proto
    text_format.Merge(f.read(), input_graph_def)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/google/protobuf/text_format.py", line 536, in Merge
    descriptor_pool=descriptor_pool)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/google/protobuf/text_format.py", line 590, in MergeLines
    return parser.MergeLines(lines, message)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/google/protobuf/text_format.py", line 623, in MergeLines
    self._ParseOrMerge(lines, message)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/google/protobuf/text_format.py", line 638, in _ParseOrMerge
    self._MergeField(tokenizer, message)
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/google/protobuf/text_format.py", line 706, in _MergeField
    name = tokenizer.ConsumeIdentifierOrNumber()
  File "/home/xxx/anaconda2/lib/python2.7/site-packages/google/protobuf/text_format.py", line 1166, in ConsumeIdentifierOrNumber
    raise self.ParseError('Expected identifier or number, got %s.' % result)
google.protobuf.text_format.ParseError: 1:1 : Expected identifier or number, got.

什么鬼错,Expected identifier or number 字面意思应该是传入的文件少了点什么东西

再看看freeze_graph脚本的源码

run_main()函数里看到了一个参数

  parser.add_argument(
      "--input_saved_model_dir",
      type=str,
      default="",
      help="Path to the dir with TensorFlow \'SavedModel\' file and variables.")

原来是用错参数了,噗...

修改一下命令

freeze_graph \
    --input_saved_model_dir=1530159051 \
    --output_node_names=Softmax,ArgMax \
    --output_graph=merge1_graph.pb

再执行,屁都没冒一个,说明执行成功了,没报错,哈哈

看下指定的输出文件有没有生成

ww3:~/source/TFmodels_out/mnist_saved_model$ ls -l
总用量 12804
drwxr-xr-x 3 ww3 ww3     4096  6月 28 12:10 1530159051
-rw-rw-r-- 1 ww3 ww3 13105521  6月 28 17:58 merge1_graph.pb

嗯...具体这个文件能不能直接用还有待研究

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容