通过使用密集层作为最后一层来冻结TensorFlow

时间:2017-09-18 16:21:48

标签: python opencv tensorflow

os:ubuntu 16.04.1 TensorFlow版本:1.3.0

尝试冻结TensorFlow模型(因此可以通过opencv dnn加载),但它总是返回错误消息

AssertionError:regression_output不在图表中

最低代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf

features = {'x' : tf.placeholder(tf.float32, [None, 128, 128, 2], name = 'input')}        
model = tf.reshape(features['x'], [-1, 128*128*2])
model = tf.layers.dense(inputs = model, units = 8, name = 'regression_output')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    saver = tf.train.Saver()
    saver.save(sess, 'reshape.ckpt')
    tf.train.write_graph(sess.graph.as_graph_def(), "", 'graph.pb')

命令:

python3 ~/.keras2/lib/python3.5/site-packages/tensorflow/python/tools/freeze_graph.py --input_graph=graph.pb --input_checkpoint=reshape.ckpt --output_graph=frozen_graph.pb --output_node_names=regression_output

1 个答案:

答案 0 :(得分:2)

--output_node_names=regression_output但保存的模型中没有regression_output

输出的名称是regression_output/BiasAdd。所以这个命令

python /usr/local/lib/python2.7/dist-packages/tensorflow/python/tools/freeze_graph.py --input_graph=graph.pb --input_checkpoint=reshape.ckpt --output_graph=frozen_graph.pb --output_node_names=regression_output/BiasAdd

可以在python2.7中运行。