简体   繁体   中英

Freeze TensorFlow by using dense layer as last layer

os : ubuntu 16.04.1 TensorFlow version : 1.3.0

Trying to freeze a TensorFlow model(so it could be loaded by opencv dnn), but it always return error message

AssertionError: regression_output is not in graph

Minimum codes:

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

Commands :

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

--output_node_names=regression_output but there's no regression_output in the saved model.

The output's name is regression_output/BiasAdd . So this command

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

can work in python2.7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM