Trying to incorporate ML onnx model to Android App

ZookKep

I have a trained onnx model that I want to incorporate into an android app. I'm actually working on a uni project, combining ML & Android development.

After a long research, since I don't want to use a python private REST API, I came to the conclusion that there are two ways I could continue from here: I can either try converting my onnx model into a TF model and then generate a TFLite model through TFLite Converter API, or give it a try with onnxruntime.

I tried the first way with TFLite, using the answer from this post, and hence this code:

import onnx

from onnx_tf.backend import prepare

onnx_model = onnx.load("input_path")  # load onnx model
tf_rep = prepare(onnx_model)  # prepare tf representation
tf_rep.export_graph("output_path")  # export the model

but I’m stuck in that first conversion from .onnx to .pb, since I think onnx-tf doesn’t support dynamic dimensions (that my model has). I’m constantly getting an "Input size (depth of inputs) must be accessible via shape inference," or RuntimeError: Node name is not unique in your model. Please recreate your model with unique node name. and similar errors.

I also gave it a try with onnxruntime, but I can't seem to manage to "Create a minimal build for Android with NNAPI support". I'm getting this error while building:

[1/67] Building CXX object CMakeFiles/libprotobuf.dir/C_/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc.obj
FAILED: CMakeFiles/libprotobuf.dir/C_/Users/chris/onnxruntime/cmake/external/protobuf/src/google/protobuf/io/io_win32.cc.obj
C:\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.cc
C:/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.obj
ninja: 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.

Am I in a completely wrong path? It's my first time trying to combine ML with Android so I have no experience in this. Any advice would be very welcome.

Littlish

For your issue with onnxruntime, try changing the CMakeCache.txt :

CMAKE_CXX_FLAGS:STRING=-U__STRICT_ANSI__

It should fix the error you mentioned. I think you're in the right path with onnxruntime.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related