如何构建TensorFlow Lite benchmark_model?Android编译报错求助
1. How to Build TensorFlow Lite's benchmark_model for Android arm64-v8a
First, let's make sure you're set up correctly:
- Clone the TensorFlow source code (stick to a stable 2.x release—older versions have deprecated paths that cause unnecessary issues)
- Install a Bazel version compatible with your TensorFlow release (check TensorFlow's release notes for exact versions; mismatches lead to all sorts of headaches)
- Set up the Android NDK and SDK, with
ANDROID_NDK_HOMEandANDROID_SDK_HOMEenvironment variables pointing to their respective directories.
The command you used is almost right, but note that in newer TensorFlow versions, the path changed from tensorflow/contrib/lite to tensorflow/lite (the contrib module was phased out). Here's the updated build command:
bazel build -c opt --cxxopt='--std=c++11' \ //tensorflow/lite/tools:benchmark_model \ --crosstool_top=//external:android/crosstool \ --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \ --cpu=arm64-v8a \ --verbose_failures
2. Fixing the Protobuf Workspace Warning
That warning about the protobuf_archive's WORKSPACE file is a common gotcha with older TensorFlow versions. Here are your best solutions:
- Upgrade TensorFlow: The easiest and most permanent fix is to switch to TensorFlow 2.3 or later. The team resolved this protobuf workspace naming conflict in those releases, so the warning will disappear automatically.
- Suppress the warning temporarily: If you can't upgrade right now, add the
--nowarn=workspaceflag to your Bazel command. This tells Bazel to ignore workspace name-related warnings. Your full command becomes:bazel build -c opt --cxxopt='--std=c++11' \ //tensorflow/lite/tools:benchmark_model \ --crosstool_top=//external:android/crosstool \ --host_crosstool_top=@bazel_tools//tools/cpp:toolchain \ --cpu=arm64-v8a \ --verbose_failures \ --nowarn=workspace - Double-check your workspace setup: If you manually modified TensorFlow's WORKSPACE file, ensure there are no conflicting entries for protobuf. Custom changes can sometimes trigger this warning.
Once the build completes, you'll find the compiled benchmark_model binary in bazel-bin/tensorflow/lite/tools (or the contrib path if you're using an older version). Push it to your Android device with adb push and you're ready to benchmark your TensorFlow Lite models.
内容的提问来源于stack exchange,提问作者bin.li

