在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()!"
我能够在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.
这些是我目前为止所采取的步骤:
- 分别使用
tf.train.Saver()
和tf.train.write_graph()
保存自己模型的检查点(.ckpt)和图形定义(.pb)文件 - 使用freeze_graph.py(使用bazel)冻结图形,给出227.5 MB的文件
- 使用optimize_for_inference.py(另外尝试了strip_unused.py)优化图形
- 复制冻结、优化或剥离的图形到android/assets
- 在jni_utils中使用
coded_stream.SetTotalBytesLimit()
将总字节限制加倍。cc来处理我的大模型尺寸 使用bazel构建tensorflow android应用程序 - 安装在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
相关文章:
- Problems with std::cin.fail()
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Issues with Win32 ReadProcessMemory API
- Qt with WinAPI MouseProc
- [[maybe_unused]] with structured_binding?
- Issue with WriteProcessMemory
- OpenCV RTP-Stream with FFMPEG
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- std::adjacent_difference with std::chrono time_point
- DLL Made with CMake 使程序崩溃
- QtCreator with C 库中的链接器问题
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- specialized std::default_delete with QQmlComponent
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- Inference pytorch C++ with alexnet and cv::imread image
- Pybind11: init<> with lambda
- 在Android上运行自己的TensorFlow模型会产生原生推理错误:"Session was not created with a graph before Run()!"