在Android上运行自己的TensorFlow模型会产生本机推理错误:“在Run()之前没有使用图形创建会话!”

时间:2016-11-11 20:32:26

标签: android python c++ tensorflow

我能够在Android上运行Inception-v3模型,我现在想在Android上运行我自己训练的TensorFlow模型。我正在遵循TensorFlow's image recognition tutorial和Android TensorFlow演示的方法,并根据需要进行调整。从Android logcat,运行应用程序时运行到此:

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!

环境信息:OS X Yosemite(10.10.5),LGE Nexus 5(Android 6.0.1),Android SDK 23,Android OpenCV SDK 23,Bazel 0.4.0。

这是我到目前为止采取的步骤:

  1. 使用tf.train.Saver()然后tf.train.write_graph()
  2. 分别保存自己模型的检查点(.ckpt)和图表定义(.pb)文件
  3. 使用freeze_graph.py(使用bazel)冻结图形,提供227.5 MB文件
  4. 使用optimize_for_inference.py优化图表(另外尝试过strip_unused.py)
  5. 将冻结,优化或剥离的图表复制到android / assets
  6. 使用jni_utils.cc中的coded_stream.SetTotalBytesLimit()将字节数限制加倍,以处理我的大型模型
  7. 使用bazel构建了tensorflow安卓应用
  8. 使用adb和bazel安装在Android设备上
  9. 作为一个完整性检查,我已经使用bazel按照这里的教程label_image测试了我的模型,我的模型正确地输出了一个预测。我也尝试过在冻结之前保存我的图形def和检查点文件的顺序,但没有变化。

    任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

此问题已得到解决(GitHub问题链接here)。

采取的步骤:与最新的tf构建完全同步(最后一次提交:798ae42)并使用bazel -v 0.4.3-homebrew构建。 Android SDK v23和NDK v23。剥离所有预处理(即从构建中删除OpenCV),并使用自己的模型和标签文件替换开箱即用的启动演示。 Logcat现在显示:

01-06 10:27:39.048 26344-26344/? I/TensorFlowImageClassifier: Reading labels from: mylabels.txt 
01-06 10:27:39.049 26344-26344/? I/TensorFlowImageClassifier: Read 7, 7 specified 
01-06 10:27:39.049 26344-26344/? I/native: tensorflow_inference_jni.cc:97 Native TF methods loaded. 
01-06 10:27:39.049 26344-26344/? I/TensorFlowInferenceInterface: Native methods already loaded. 
01-06 10:27:39.049 26344-26344/? I/native: tensorflow_inference_jni.cc:85 Creating new session variables for 25a68072eeb0d05 
01-06 10:27:39.049 26344-26344/? I/native: tensorflow_inference_jni.cc:113 Loading Tensorflow. 
01-06 10:27:39.053 26344-26344/? I/native: tensorflow_inference_jni.cc:120 Session created. 
01-06 10:27:39.053 26344-26344/? I/native: tensorflow_inference_jni.cc:126 Acquired AssetManager. 
01-06 10:27:39.053 26344-26344/? I/native: tensorflow_inference_jni.cc:128 Reading file to proto: file:///android_asset/cnn_frozen_graph.pb 
01-06 10:27:39.680 26344-26344/? I/native: tensorflow_inference_jni.cc:132 GraphDef loaded from file:///android_asset/cnn_frozen_graph.pb with 40 nodes. 
01-06 10:27:39.680 26344-26344/? I/native: stat_summarizer.cc:38 StatSummarizer found 40 nodes 
01-06 10:27:39.680 26344-26344/? I/native: tensorflow_inference_jni.cc:139 Creating TensorFlow graph from GraphDef. 
01-06 10:27:39.713 26344-26344/org.tensorflow.demo I/native: tensorflow_inference_jni.cc:151 Initialization done in 664.038ms 
01-06 10:27:39.714 26344-26344/org.tensorflow.demo I/tensorflow: ClassifierActivity: Sensor orientation: 90, Screen orientation: 0 
01-06 10:27:39.714 26344-26344/org.tensorflow.demo I/tensorflow: ClassifierActivity: Initializing at size 640x480