Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

freezing models #23

Open
apcode opened this issue Apr 21, 2016 · 5 comments
Open

freezing models #23

apcode opened this issue Apr 21, 2016 · 5 comments

Comments

@apcode
Copy link

apcode commented Apr 21, 2016

The tensorflow retrain.py example uses a frozen inception model that is entirely defined within the graphdef. By frozen, I mean they convert all the variables to constants in the graph so they get stored to the graphdef proto. This way everything is in one file.

Would it be possible to add this as an option to this lib?

@ethereon
Copy link
Owner

While the script currently does not directly support this, it is fairly straightforward to create a "frozen" version from the emitted code. See this related issue for details.

@apcode
Copy link
Author

apcode commented Apr 22, 2016

The linked related issue is not what I meant. It uses a saver to save the weights. But as I understand it the graph_def will be separate.
As I understand it you normally need two files for a network that can be trained:

  1. the graph_def file stored in a protobuf. This contains the nodes of the graph, however, variables are not stored with their weight values. (or a script to generate the graph).
  2. the weights file, or checkpoint file, saved with a tf.train.Saver.

I think what you suggested is using the generated py from caffe-tensorflow to build the graph, and store the weights as a checkpoint file. This is not what I and others mean by a 'frozen' model. i.e. it can still be trained as all the weights are still variables.

What I asked for is different though, I would like to have the graph and weights stored in a single graph_def proto file. To do that you need to convert the weights from variables to constants and then write the graph_def proto to a file (this doesn't use a Saver, but tf.train.write_graph.
This is what Google did with their inceptionv3 network. If I load that network it can't be trained as there are no variables in it - all weights are constant.

Anyway, I'll write a script to do it. I understand that this lib doesn't support that directly. I just thought it would be useful to create 'frozen' (non variable) versions of the caffe models.

@ethereon
Copy link
Owner

Apologies - looks like I misinterpreted your question. You're right - it would be a useful addition. I'll re-open this issue as a reminder.

@ethereon ethereon reopened this Apr 22, 2016
@vodp
Copy link

vodp commented Jun 11, 2016

@apcode I use this snippet code to convert to a graphdef file. Hope it helps.

from googlenet import GoogleNet # the output python script of caffe2tensorflow
import tensorflow as tf
from freeze_graph import freeze_graph # tensorflow comes up with a tool allowing freeze graph

x = tf.placeholder(tf.float32, shape=[1, 224, 224, 3])
y = tf.placeholder(tf.float32, shape=[1, 1000])

net = GoogleNet({'data': x})
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
 net.load('googlenet.npy', sess)
saver = tf.train.Saver()
saver.save(sess, 'chkpt', global_step=0, latest_filename='chkpt_state')
tf.train.write_graph(sess.graph.as_graph_def(), './', googlenet.pb', False)

input_saver_def_path = ''
input_binary=True
input_checkpoint_path = 'chkpt-0'
input_graph_path = 'googlenet.pb'
output_graph_path = 'googlenet.pb'
output_node_names = 'prob'
restore_op_name = "save/restore_all"
filename_tensor_name = "save/Const:0"
clear_devices = True

freeze_graph(input_graph_path, input_saver_def_path,
                              input_binary, input_checkpoint_path,
                              output_node_names, restore_op_name,
                              filename_tensor_name, output_graph_path,
                              clear_devices, "")

@shrutisharmavsco
Copy link

shrutisharmavsco commented Oct 6, 2016

for the second half of the solution above, freeze_graph did not work for me but convert_variables_to_constant worked for me: http://stackoverflow.com/a/38853802/5597458

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants