尝试将ML ONNX模型整合到Android应用中

我有一个训练好的ONNX模型,我想将其整合到一个Android应用中。实际上,我正在进行一个大学项目,结合机器学习和Android开发。

经过长时间的研究,由于我不想使用Python私有REST API,我得出结论有两种方法可以继续:我可以尝试将我的ONNX模型转换为TF模型,然后通过TFLite转换器API生成一个TFLite模型,或者尝试使用onnxruntime

我尝试了第一种方法,使用TFLite,并参考了这个帖子的答案,因此使用了以下代码:

import onnxfrom onnx_tf.backend import prepareonnx_model = onnx.load("input_path")  # 加载ONNX模型tf_rep = prepare(onnx_model)  # 准备TF表示tf_rep.export_graph("output_path")  # 导出模型

但我在从.onnx到.pb的首次转换中遇到了困难,因为我认为onnx-tf不支持动态维度(我的模型有这种特性)。我不断收到"Input size (depth of inputs) must be accessible via shape inference,"RuntimeError: Node name is not unique in your model. Please recreate your model with unique node name.以及类似的错误。

我也尝试了使用onnxruntime,但我似乎无法成功“为Android创建一个支持NNAPI的最小构建”。在构建时我遇到了以下错误:

[1/67] Building CXX object CMakeFiles/libprotobuf.dir/C_/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc.objFAILED: CMakeFiles/libprotobuf.dir/C_/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc.objC:\PROGRA~2\CODEBL~1\MinGW\bin\C__~1.EXE -DGOOGLE_PROTOBUF_CMAKE_BUILD -DHAVE_PTHREAD -I. -IC:/Users/chris/onnxruntime/cmake/external/protobuf/src -std=c++11 -MD -MT CMakeFiles/libprotobuf.dir/C_/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc.obj -MF CMakeFiles\libprotobuf.dir\C_\Users\chris\onnxruntime\cmake\external\protobuf\src\google\protobuf\io\io_win32.cc.obj.d -o CMakeFiles/libprotobuf.dir/C_/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc.obj -c C:/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.ccC:/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc: In function 'int google::protobuf::io::win32::stat(const char*, google::protobuf::io::win32::_stat*)':C:/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc:315:40: error: cannot convert 'google::protobuf::io::win32::_stat*' to '_stat*' for argument '2' to 'int _wstat(const wchar_t*, _stat*)'   return ::_wstat(wpath.c_str(), buffer);                                        ^In file included from C:/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc:52:0:C:/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.h:75:51: note: class type 'google::protobuf::io::win32::_stat' is incomplete PROTOBUF_EXPORT int stat(const char* path, struct _stat* buffer);                                                   ^C:/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc: In function 'FILE* google::protobuf::io::win32::fopen(const char*, const char*)':C:/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc:337:10: error: '::_wfopen' has not been declared   return ::_wfopen(wpath.c_str(), wmode.c_str());          ^[6/67] Building CXX object CMakeFiles/libprotobuf.dir/C_/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/message_lite.cc.objninja: build stopped: subcommand failed.Traceback (most recent call last):  File "C:\Users\chris\onnxruntime\\tools\ci_build\build.py", line 2023, in <module>    sys.exit(main())  File "C:\Users\chris\onnxruntime\\tools\ci_build\build.py", line 1918, in main    cmake_path, source_dir, build_dir, args)  File "C:\Users\chris\onnxruntime\\tools\ci_build\build.py", line 1673, in build_protoc_for_host    run_subprocess(cmd_args)  File "C:\Users\chris\onnxruntime\\tools\ci_build\build.py", line 544, in run_subprocess    return run(*args, cwd=cwd, capture_stdout=capture_stdout, shell=shell, env=my_env)  File "C:\Users\chris\onnxruntime\tools\python\util\run.py", line 44, in run    env=env, shell=shell)  File "C:\Users\chris\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 512, in run    output=stdout, stderr=stderr)subprocess.CalledProcessError: Command '['C:\\Program Files\\CMake\\bin\\cmake.EXE', '--build', 'C:\\Users\\chris\\onnxruntime\\\\build\\Windows\\host_protoc', '--config', 'Release', '--target', 'protoc']' returned non-zero exit status 1.

我是不是走错了路?这是我第一次尝试将机器学习与Android结合,所以我在这方面没有经验。任何建议都将非常受欢迎。


回答:

关于你在onnxruntime中遇到的问题,尝试更改CMakeCache.txt文件:

CMAKE_CXX_FLAGS:STRING=-U__STRICT_ANSI__

这应该能解决你提到的错误。我认为你使用onnxruntime的方向是正确的。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注