3

I'd like to pass the parameters of the trained model (weights and bias for convolution and fully connected layers) to other frameworks or languages including iOS and Torch by parsing the saved file.

I tried tf.train.write_graph(session.graph_def, '', 'graph.pb'), but it seems it only includes the graph architecture without weights and bias. If so, to create checkpoint file (saver.save(session, "model.ckpt")) is the best way? Is it easy to parse ckpt file type in Swift or other languages?

Please let me know if you have any suggestions.

kangaroo
  • 407
  • 4
  • 19

1 Answers1

1

Instead of parsing a .ckpt file, you can just try evaluating the tensor (in your case the weights of a convolutional layer) and getting the values as a numpy array. Here is a quick toy example (tested on r0.10 - there might some small API changes in newer versions):

import tensorflow as tf
import numpy as np

x = tf.placeholder(np.float32, [2,1])
w = tf.Variable(tf.truncated_normal([2,2], stddev=0.1))
b = tf.Variable(tf.constant(1.0, shape=[2,1]))
z = tf.matmul(w, x) + b

with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    w_val, z_val = sess.run([w, z], feed_dict={x: np.arange(2).reshape(2,1)})
    print(w_val)
    print(z_val)

Output:

[[-0.02913031  0.13549708]
 [ 0.13807134  0.03763327]]
[[ 1.13549709]
 [ 1.0376333 ]]

If you have trouble getting a reference to your tensor (say it is in nested into a higher-level "layer" operation), try finding by name. More info here: Tensorflow: How to get a tensor by name?

If you want to see the how the weights change during training, you can also try to save all the values you are interested into tf.Summary objects and parse them later: Parsing `summary_str` byte string evaluated on tensorflow summary object

Community
  • 1
  • 1
Gustavo Bezerra
  • 9,984
  • 4
  • 40
  • 48
  • Thank you and please let me clarify. After getting the above output, how TensorFlow users usually store these values to pass other platforms by using some files (I guess .dat or .pb)? – kangaroo Dec 17 '16 at 23:16
  • If your other platform is Python based, the quickest approach would be make a pickle files (or numpy array) files. In case you want something more cross-platform perhaps a binary format such as HDF5 would be more appropriate (I personally use HDF5 to distribute some datasets). – Gustavo Bezerra Dec 18 '16 at 00:18
  • See https://stackoverflow.com/questions/34343259/is-there-an-example-on-how-to-generate-protobuf-files-holding-trained-tensorflow – Cristi Nov 03 '17 at 11:12