title: Running own TensorFlow model on Android gives native inference error: "Session was not created with a graph before Run()!"
body: <p>I was able to run the Inception-v3 model on Android just fine, and I now want to run my own trained TensorFlow model on Android. I'm following the approach from <a href="https://www.tensorflow.org/versions/r0.11/tutorials/image_recognition/index.html" rel="nofollow noreferrer">TensorFlow's image recognition tutorial</a> and the Android TensorFlow demo, and adapting as necessary. From Android logcat, running into this when running the app:</p>
  
  <pre><code>E/native: tensorflow_inference_jni.cc:202 Error during inference: Invalid argument: Session was not created with a graph before Run()!
  ...
  E/native: tensorflow_inference_jni.cc:159 Output [output/Softmax:0] not found, aborting!
  </code></pre>
  
  <p>Environment info: OS X Yosemite (10.10.5), LGE Nexus 5 (Android 6.0.1), Android SDK 23, Android OpenCV SDK 23, Bazel 0.4.0.</p>
  
  <p>These are the steps I've taken so far:</p>
  
  <ol>
  <li>Saved own model's checkpoint (.ckpt) and graph definition (.pb) files separately using <code>tf.train.Saver()</code> then <code>tf.train.write_graph()</code>  </li>
  <li>Froze graph using freeze_graph.py (using bazel), gives 227.5 MB file</li>
  <li>Optimized the graph using optimize_for_inference.py (additionally tried strip_unused.py)</li>
  <li>Copied frozen, optimized, or stripped graph to android/assets</li>
  <li>Doubled the total byte limit using <code>coded_stream.SetTotalBytesLimit()</code> in jni_utils.cc to handle my large model size</li>
  <li>Built the tensorflow android app using bazel</li>
  <li>Installed on android device using adb and bazel</li>
  </ol>
  
  <p>As a sanity check, I have tested my model in C++ built with bazel following the tutorial here <a href="https://www.tensorflow.org/versions/r0.8/how_tos/image_retraining/index.html" rel="nofollow noreferrer">label_image</a>, and my model correctly outputs a prediction. I have also tried playing with the order by which I save my graph def and checkpoint files before freezing, but no change.</p>
  
  <p>Any help would be great.</p>