tensorflow編譯
-
Mac平臺首先安裝brew,如果你不知道brew請自行google
-
使用brew安裝bazel工具
brew install bazel
複製程式碼
確認bazel安裝成功,可以使用命令 - bazel version
- 使用bazel構建tensorflow的so庫
//下載tensorflow原始碼
git clone https://github.com/tensorflow/tensorflow.git
//進入tensorflow原始碼目錄,在workspace檔案中新增如下配置
#注意對應配置好你的sdk版本以及位置,還有ndk的版本和位置
android_sdk_repository(
name = "androidsdk",
#api_level需要設定成你本地sdk最高的版本,推薦23及以上,在android:build.gradle 以及AndroidManifest.xml也對應修改targetSdkVersion
api_level = 25,
build_tools_version = "27.0.3",
path = "/Users/yanfei/Library/Android/sdk",
)
android_ndk_repository(
name = "androidndk",
path = "/Users/yanfei/Library/Android/sdk/ndk-bundle",
api_level = 15, #api_level必須為14或更高
)
//進入目錄:tensorflow/contrib/lite/java/demo/app/src/main
bazel build --cxxopt='--std=c++11' //tensorflow/contrib/lite/java:tensorflowlite
--crosstool_top=//external:android/crosstool
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain
--cpu=armeabi-v7a
cpu版本最好使用armeabi-v7a,如果選用armeabi的話編譯會出錯,別的cpu型別沒有嘗試
複製程式碼
-
編譯完成後在tensorflow/bazel-out/armeabi-v7a-opt/bin/tensorflow/contrib/lite/java可以看到相應的.so
-
剩下就是在android專案中引入.so和相應的jar了, 部分build.gradle檔案
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
ndk {
moduleName "tensorflowlite_jni"
abiFilters "armeabi"
}
aaptOptions {
noCompress "tflite"
noCompress "lite"
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/res']
aidl.srcDirs = ['src/main/java']
// renderscript.srcDirs = ['src']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
jniLibs.srcDirs = ['src/main/libs']
jni.srcDirs = []
}
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
dependencies {
implementation 'org.tensorflow:tensorflow-lite:+'
}
複製程式碼