使用freeze_graph將SavedModel生成的模型合并成一個pb文件

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

在訓(xùn)練mnist模型的時候,增加一個參數(shù)昨寞,可以在訓(xùn)練完畢后導(dǎo)出SavedModel

python mnist.py --export_dir /tmp/mnist_saved_model

訓(xùn)練完成后夜涕,會以以下文件結(jié)構(gòu)在/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.

這個腳本就是用來將變量轉(zhuǎn)換為常量合并到到計算圖中的

具體怎么使用呢恨闪?

腳本中給出的例子是這樣的:

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去創(chuàng)建freeze_graph

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

執(zhí)行放坏,然后掛了

  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 字面意思應(yīng)該是傳入的文件少了點什么東西

再看看freeze_graph腳本的源碼

run_main()函數(shù)里看到了一個參數(shù)

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

原來是用錯參數(shù)了,噗...

修改一下命令

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

再執(zhí)行淤年,屁都沒冒一個钧敞,說明執(zhí)行成功了,沒報錯麸粮,哈哈

看下指定的輸出文件有沒有生成

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

嗯...具體這個文件能不能直接用還有待研究

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末溉苛,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子弄诲,更是在濱河造成了極大的恐慌愚战,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件齐遵,死亡現(xiàn)場離奇詭異寂玲,居然都是意外死亡,警方通過查閱死者的電腦和手機洛搀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門敢茁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人留美,你說我怎么就攤上這事∩烊校” “怎么了谎砾?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長捧颅。 經(jīng)常有香客問我景图,道長,這世上最難降的妖魔是什么碉哑? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任挚币,我火速辦了婚禮,結(jié)果婚禮上扣典,老公的妹妹穿的比我還像新娘妆毕。我一直安慰自己,他們只是感情好贮尖,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布笛粘。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪薪前。 梳的紋絲不亂的頭發(fā)上润努,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天,我揣著相機與錄音示括,去河邊找鬼铺浇。 笑死,一個胖子當(dāng)著我的面吹牛垛膝,可吹牛的內(nèi)容都是我干的鳍侣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼繁涂,長吁一口氣:“原來是場噩夢啊……” “哼拱她!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起扔罪,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤秉沼,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后矿酵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唬复,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年全肮,在試婚紗的時候發(fā)現(xiàn)自己被綠了敞咧。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡辜腺,死狀恐怖休建,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情评疗,我是刑警寧澤测砂,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站百匆,受9級特大地震影響砌些,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜加匈,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一存璃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧雕拼,春花似錦纵东、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽男图。三九已至,卻和暖如春甜橱,著一層夾襖步出監(jiān)牢的瞬間逊笆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工岂傲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留难裆,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓镊掖,卻偏偏與公主長得像乃戈,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子亩进,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容