Loading Tensorflow graph from binary file in Android application
<p>I'm trying to integrate TensorFlow into an Android application. Since I'm new to TensorFlow, I'm proceeding starting with very simple operations.</p>

<p>As a first step I created just the following model:</p>

<pre><code>import tensorflow as tf

with tf.Graph().as_default() as g:
​
    x = tf.placeholder("float", [1, 10], name="input")
    a = tf.zeros([10, 5], dtype=tf.float32, name="a")
    y = tf.matmul(x, a, name="output")
    init = tf.initialize_all_variables()

    sess = tf.Session()
    sess.run(init)
​
    graph_def = g.as_graph_def()

    tf.train.write_graph(graph_def, 'models/', 'graph.pb', as_text=False) 
</code></pre>

<p>This works fine. I'm able to properly get output from my C++ code (and then from Android as well).</p>

<p>I tried then to generate <code>a</code> by using <code>tf.random_normal</code>. It seems this is not feasible by just replacing <code>tf.zeros</code> with <code>tf.random_normal</code>. That's because <code>tf.zeros</code> returs a costant, while <code>tf.random_normal</code> not. In particular it seems I must handle it as a variable.</p>

<p>Idea I followed is the same proposed in other examples I've found on GitHub... so, evaluating <code>a</code> before writing the graph, as reported in code below:</p>

<pre><code>import tensorflow as tf

a = tf.Variable(tf.random_normal([10, 5], dtype=tf.float32), name="a")
init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

a_eval = a.eval(sess)
# print here properly produces matrix in output
print a_eval
sess.close()

with tf.Graph().as_default() as g:

    x = tf.placeholder(tf.float32, [1, 10], name="input")
    a_2 = tf.constant(a_eval, name="a_2") 
    y = tf.matmul(x, a_2, name="output")
    init = tf.initialize_all_variables()

    sess = tf.Session()
    sess.run(init)

    graph_def = g.as_graph_def()

    tf.train.write_graph(graph_def, 'models/', 'graph.pb', as_text=False)
</code></pre>

<p>Unfortunately it seems it doesn't work due to an error occurring in reading binary file:</p>

<blockquote>
  <p>Out of range: Read less bytes than requested</p>
</blockquote>

<p>This is C++ code that I'm currently using for loading graph from file:</p>

<pre><code>tensorflow::GraphDef graph_def;
Status load_graph_status = ReadBinaryProto(Env::Default(), filepath, &amp;graph_def);
if (!load_graph_status.ok()) {
    LOG(ERROR) &lt;&lt; "could not create tensorflow graph: " &lt;&lt; load_graph_status;
    return NULL;
}
</code></pre>

<p>Hope someone could help me with this problem.</p>