1、下载tensorflow源码
tensorflow源码git链接
2、下载jdk1.8(这个是为安卓平台移植准备的,不考虑安卓平台可以pass)
wget --no-cookies --no-check-certificate --header “Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie” "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz”
或者jdk官方下载
安装:
tar -xvf jdk-8u221-linux-x64.tar.gz myjdkpath
添加到环境变量:
vim /home/xxx/.bashrc
添加内容如下:
export JAVA_HOME=/home/zeng/sda/NOISE/C_DTLN/jdk-1.8/jdk1.8.0_141
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
然后source /home/xxx/.bashrc生效环境变量配置
sudo update-alternatives --install /usr/bin/java java myjdkpath/jdk1.8.0_221/bin/java 300
sudo update-alternatives --install /usr/bin/java java myjdkpath/jdk1.8.0_221/bin/javac 300
sudo update-alternatives --config java 选择你新下载配置的jdk
3、下载bazel
bazel官方下载链接
选择对应的版本进行下载,具体的版本根据编译需求去选择,我下载的最新的进行编译不通过,提示使用5.1.1版本(bazel-5.1.1-linux-x86_64)
ERROR: The project you're trying to build requires Bazel 5.1.1 (specified in /home/zeng/sda/NOISE/C_DTLN/tensortflow/tensorflow/tensorflow/.bazelversion), but it wasn't found in /usr/local/lib/bazel/bin.
4、开始编译
cd ~/sda/NOISE/C_DTLN/tensortflow/tensorflow/tensorflow
./configure
WARNING: current bazel installation is not a release version.
Please specify the location of python. [Default is /usr/bin/python3]: /usr/bin/python3.7
Found possible Python library paths:
/usr/lib/python3/dist-packages
/usr/local/lib/python3.7/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]
/usr/local/lib/python3.7/dist-packages
Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]:
Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
Not configuring the WORKSPACE for Android builds.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
--config=mkl # Build with MKL support.
--config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
--config=monolithic # Config for mostly static monolithic build.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v1 # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
--config=nogcp # Disable GCP support.
--config=nonccl # Disable NVIDIA NCCL support.
Configuration finished
编译libtensorflowlite:
bazel build --config=opt //tensorflow/lite:libtensorflowlite.so
编译libtensorflow C
bazel build --config=opt --config=cuda //tensorflow:libtensorflow.so
编译libtensorflow C++
bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so
我编译的libtensorflowlite库如下:
zeng@zeng:~/sda/NOISE/C_DTLN/tensortflow/tensorflow/tensorflow$ ls bazel-bin/tensorflow/lite/
c core delegates experimental kernels libtensorflowlite.so libtensorflowlite.so-2.params ***libtensorflowlite***.so.runfiles libtensorflowlite.so.runfiles_manifest _objs profiling schema
zeng@zeng:~/sda/NOISE/C_DTLN/tensortflow/tensorflow/tensorflow$
安卓版本相关记录:
1、下载命令行安装android-sdk前准备
命令行 sdkmanager
(1)zeng@zeng:~/sda/NOISE/C_DTLN/android-sdk-linux$ wget https://dl.Google.com/androID/repository/commandlinetools-linux-7302050_latest.zip
--2022-07-11 13:50:30-- https://dl.google.com/androID/repository/commandlinetools-linux-7302050_latest.zip
正在解析主机 dl.google.com (dl.google.com)... 142.250.68.46
正在连接 dl.google.com (dl.google.com)|142.250.68.46|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 103957858 (99M) [application/zip]
正在保存至: “commandlinetools-linux-7302050_latest.zip”
commandlinetools-linux-7302050_latest.zip 100%[===============================================================================================================>] 99.14M 1.06MB/s 用时 96s
2022-07-11 13:52:07 (1.04 MB/s) - 已保存 “commandlinetools-linux-7302050_latest.zip” [103957858/103957858])
(2)zeng@zeng:~/sda/NOISE/C_DTLN/android-sdk-linux$ unzip commandlinetools-linux-7302050_latest.zip
(3)zeng@zeng:~/sda/NOISE/C_DTLN/android-sdk-linux/cmdline-tools$ sudo ./bin/sdkmanager
[sudo] zeng 的密码:
Error: Could not determine SDK root.
Error: Either specify it explicitly with --sdk_root= or move this package into its expected location: <sdk>/cmdline-tools/latest/
(4)zeng@zeng:~/sda/NOISE/C_DTLN/android-sdk-linux/cmdline-tools$ mkdir latest
(5)zeng@zeng:~/sda/NOISE/C_DTLN/android-sdk-linux/cmdline-tools$ mv * latest/
mv: 无法将目录'latest' 移动至自身的子目录'latest/latest' 下
zeng@zeng:~/sda/NOISE/C_DTLN/android-sdk-linux/cmdline-tools$ ls
latest
(6)zeng@zeng:~/sda/NOISE/C_DTLN/android-sdk-linux/cmdline-tools/cmdline-tools$ ./latest/bin/sdkmanager "build-tools;29.0.2" "platforms;android-30"
License android-sdk-license: ] 10% Computing updates...
---------------------------------------
Terms and Conditions
This is the Android Software Development Kit License Agreement
***********
***********
this, you agree that Google shall still be allowed to apply for injunctive remedies (or an equivalent type of urgent legal relief) in any jurisdiction.
January 16, 2019
---------------------------------------
Accept? (y/N): y
[=======================================] 100% Unzipping... android-11/framework
Android-sdk安装到了/home/zeng/sda/NOISE/C_DTLN/android-sdk-linux/cmdline-tools目录下,也就是上面执行./latest/bin/sdkmanager动作的上一层路径
2、开始配置tensorflow于android编译
```c
zeng@zeng:~/sda/NOISE/C_DTLN/tensortflow/tensorflow/tensorflow$ ./configure
You have bazel 5.1.1 installed.
Please specify the location of python. [Default is /usr/bin/python3]: /usr/bin/python3.7
Found possible Python library paths:
/usr/lib/python3/dist-packages
/usr/local/lib/python3.7/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]
/usr/local/lib/python3.7/dist-packages
Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.
Please specify the home path of the Android NDK to use. [Default is /home/zeng/Android/Sdk/ndk-bundle]: /home/zeng/share/ndk/android-ndk-r21e
Please specify the (min) Android NDK API level to use. [Available levels: ['16', '17', '18', '19', '21', '22', '23', '24', '26', '27', '28', '29', '30']] [Default is 21]: 30
Please specify the home path of the Android SDK to use. [Default is /home/zeng/Android/Sdk]: /home/zeng/sda/NOISE/C_DTLN/android-sdk-linux/cmdline-tools
Please specify the Android SDK API level to use. [Available levels: ['30']] [Default is 30]:
Please specify an Android build tools version to use. [Available versions: ['29.0.2']] [Default is 29.0.2]:
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
--config=mkl # Build with MKL support.
--config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
--config=monolithic # Config for mostly static monolithic build.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v1 # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
--config=nogcp # Disable GCP support.
--config=nonccl # Disable NVIDIA NCCL support.
Configuration finished
开始编译tensorflowlite动态库
zeng@zeng:~/sda/NOISE/C_DTLN/tensortflow/tensorflow/tensorflow$ bazel build --cxxopt='-std=c++11' -c opt --config=android_arm tensorflow/lite:libtensorflowlite.so
zeng@zeng:~/sda/NOISE/C_DTLN/tensortflow/tensorflow/tensorflow$ ls -lah bazel-bin/tensorflow/lite/
总用量 14M
drwxrwxr-x 11 zeng zeng 4.0K 7月 11 15:41 .
drwxrwxr-x 3 zeng zeng 4.0K 7月 11 15:40 ..
drwxrwxr-x 3 zeng zeng 4.0K 7月 11 15:40 c
drwxrwxr-x 3 zeng zeng 4.0K 7月 11 15:40 core
drwxrwxr-x 4 zeng zeng 4.0K 7月 11 15:40 delegates
drwxrwxr-x 4 zeng zeng 4.0K 7月 11 15:40 experimental
drwxrwxr-x 4 zeng zeng 4.0K 7月 11 15:41 kernels
-r-xr-xr-x 1 zeng zeng 71K 7月 11 15:40 liballocation.pic.a
-r-xr-xr-x 1 zeng zeng 307K 7月 11 15:40 libarena_planner.pic.a
-r-xr-xr-x 1 zeng zeng 1.2M 7月 11 15:40 libcc_api_experimental.pic.lo
-r-xr-xr-x 1 zeng zeng 4.3M 7月 11 15:40 libcc_api_stable.pic.lo
-r-xr-xr-x 1 zeng zeng 23K 7月 11 15:40 libexternal_cpu_backend_context.pic.a
-r-xr-xr-x 1 zeng zeng 238K 7月 11 15:40 libgraph_info.pic.a
-r-xr-xr-x 1 zeng zeng 19K 7月 11 15:40 libminimal_logging.pic.a
-r-xr-xr-x 1 zeng zeng 1.6M 7月 11 15:40 libmodel_builder.pic.a
-r-xr-xr-x 1 zeng zeng 658K 7月 11 15:40 libmutable_op_resolver.pic.a
-r-xr-xr-x 1 zeng zeng 1.2M 7月 11 15:40 liboptional_debug_tools.pic.a
-r-xr-xr-x 1 zeng zeng 168K 7月 11 15:40 libsimple_memory_arena.pic.a
-r-xr-xr-x 1 zeng zeng 9.2K 7月 11 15:41 libstderr_reporter.pic.a
-r-xr-xr-x 1 zeng zeng 173K 7月 11 15:40 libstring_util.pic.a
-r-xr-xr-x 1 zeng zeng 3.1M 7月 11 15:41 libtensorflowlite.so
drwxrwxr-x 3 zeng zeng 4.0K 7月 11 15:40 libtensorflowlite.so.runfiles
-r-xr-xr-x 1 zeng zeng 212 7月 11 15:40 libtensorflowlite.so.runfiles_manifest
-r-xr-xr-x 1 zeng zeng 21K 7月 11 15:40 libtensorflow_profiler_logger_shim.pic.lo
-r-xr-xr-x 1 zeng zeng 18K 7月 11 15:40 libtflite_with_xnnpack_optional.pic.a
-r-xr-xr-x 1 zeng zeng 188K 7月 11 15:40 libutil.pic.a
drwxrwxr-x 18 zeng zeng 4.0K 7月 11 15:41 _objs
drwxrwxr-x 3 zeng zeng 4.0K 7月 11 15:40 profiling
drwxrwxr-x 3 zeng zeng 4.0K 7月 11 15:40 schema