在Android上运行自己的TensorFlow模型会产生原生推理错误:"Session was not created with a graph before Run()!"

Running own TensorFlow model on Android gives native inference error: "Session was not created with a graph before Run()!"

本文关键字:with created not Session was graph Run before 自己的 TensorFlow 运行      更新时间:2023-10-16

我能够在Android上运行Inception-v3模型,现在我想在Android上运行我自己训练的TensorFlow模型。我遵循TensorFlow的图像识别教程和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()保存自己模型的检查点(.ckpt)和图形定义(.pb)文件
  2. 使用freeze_graph.py(使用bazel)冻结图形,给出227.5 MB的文件
  3. 使用optimize_for_inference.py(另外尝试了strip_unused.py)优化图形
  4. 复制冻结、优化或剥离的图形到android/assets
  5. 在jni_utils中使用coded_stream.SetTotalBytesLimit()将总字节限制加倍。cc来处理我的大模型尺寸
  6. 使用bazel构建tensorflow android应用程序
  7. 安装在android设备上使用adb和bazel

作为完整性检查,我已经按照这里的label_image教程在用bazel构建的c++中测试了我的模型,并且我的模型正确地输出了一个预测。我也试过在冻结前保存我的图形定义和检查点文件的顺序,但没有改变。

这个问题已经解决(GitHub问题链接在这里)。

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

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