教程2:tensorflow lite 编译和安装二 使用bazel编译

教程1:tensorflow lite 交叉编译和安装一 
教程2:tensorflow lite 编译和安装二 使用bazel编译  
教程3:tensorflow lite c++ 接口解读 
教程4:TensorFlow2.0 label_image 的编译和使用  

这里的坑就是 bazel编译时就是按照指定的版本进行编译,比如这里用的是

0.24.1,最新的版本1.0 还不能识别。


(base) lid@lid-VirtualBox:~/tensorflow_lite$ cd tensorflow/

(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ ls
ACKNOWLEDGMENTS  arm_compiler.BUILD  BUILD               CODEOWNERS  configure.cmd  CONTRIBUTING.md  ISSUE_TEMPLATE.md  models.BUILD  RELEASE.md   tensorflow   tools
ADOPTERS.md      AUTHORS             CODE_OF_CONDUCT.md  configure   configure.py   ISSUES.md        LICENSE            README.md     SECURITY.md  third_party  WORKSPACE
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ ./configure 
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
You have bazel 0.24.1 installed.
Please specify the location of python. [Default is /home/lid/miniconda3/bin/python]: bazel shutdown


Invalid python path: bazel shutdown cannot be found.
Please specify the location of python. [Default is /home/lid/miniconda3/bin/python]: ^[[A^[[B^CTraceback (most recent call last):
  File "./configure.py", line 100, in get_input
    answer = raw_input(question)
NameError: name 'raw_input' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./configure.py", line 1599, in <module>
    main()
  File "./configure.py", line 1405, in main
    setup_python(environ_cp)
  File "./configure.py", line 208, in setup_python
    default_python_bin_path)
  File "./configure.py", line 593, in get_from_env_or_user_or_default
    var = get_input(ask_for_var)
  File "./configure.py", line 102, in get_input
    answer = input(question)  # pylint: disable=bad-builtin
KeyboardInterrupt
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ ./configure 
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
You have bazel 0.24.1 installed.
Please specify the location of python. [Default is /home/lid/miniconda3/bin/python]: 


Found possible Python library paths:
  /home/lid/miniconda3/lib/python3.7/site-packages
Please input the desired Python library path to use.  Default is [/home/lid/miniconda3/lib/python3.7/site-packages]

Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.
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.
Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/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=monolithic   # Config for mostly static monolithic build.
 --config=gdr          # Build with GDR support.
 --config=verbs        # Build with libverbs support.
 --config=ngraph       # Build with Intel nGraph support.
 --config=numa         # Build with NUMA support.
 --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
 --config=v2           # Build TensorFlow 2.x instead of 1.x.
Preconfigured Bazel build configs to DISABLE default on features:
 --config=noaws        # Disable AWS S3 filesystem support.
 --config=nogcp        # Disable GCP support.
 --config=nohdfs       # Disable HDFS support.
 --config=noignite     # Disable Apache Ignite support.
 --config=nokafka      # Disable Apache Kafka support.
 --config=nonccl       # Disable NVIDIA NCCL support.
Configuration finished
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$



上述过程中没有输入n 的直接enter默认。安装CPU 版,

(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
Starting local Bazel server and connecting to it...
INFO: Options provided by the client:
  Inherited 'common' options: --isatty=1 --terminal_columns=205
INFO: Reading rc options for 'build' from /home/lid/tensorflow_lite/tensorflow/.bazelrc:
  'build' options: --apple_platform_type=macos --define framework_shared_object=true --define open_source_build=true --define=use_fast_cpp_protos=true --define=allow_oversize_protos=true --spawn_strategy=standalone --strategy=Genrule=standalone -c opt --announce_rc --define=grpc_no_ares=true --define=PREFIX=/usr --define=LIBDIR=$(PREFIX)/lib --define=INCLUDEDIR=$(PREFIX)/include --config=v2
INFO: Reading rc options for 'build' from /home/lid/tensorflow_lite/tensorflow/.tf_configure.bazelrc:
  'build' options: --action_env PYTHON_BIN_PATH=/home/lid/miniconda3/bin/python --action_env PYTHON_LIB_PATH=/home/lid/miniconda3/lib/python3.7/site-packages --python_path=/home/lid/miniconda3/bin/python --action_env TF_CONFIGURE_IOS=0
INFO: Found applicable config definition build:v2 in file /home/lid/tensorflow_lite/tensorflow/.bazelrc: --define=tf_api_version=2
INFO: Found applicable config definition build:opt in file /home/lid/tensorflow_lite/tensorflow/.tf_configure.bazelrc: --copt=-march=native --copt=-Wno-sign-compare --host_copt=-march=native --define with_default_optimizations=true
DEBUG: Rule 'io_bazel_rules_docker' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1556410077 -0400"
ERROR: /home/lid/tensorflow_lite/tensorflow/third_party/python_runtime/BUILD:5:1: no such package '@local_config_python//': Traceback (most recent call last):
 File "/home/lid/tensorflow_lite/tensorflow/third_party/py/python_configure.bzl", line 344
 _create_local_python_repository(repository_ctx)
 File "/home/lid/tensorflow_lite/tensorflow/third_party/py/python_configure.bzl", line 296, in _create_local_python_repository
 _get_numpy_include(repository_ctx, python_bin)
 File "/home/lid/tensorflow_lite/tensorflow/third_party/py/python_configure.bzl", line 276, in _get_numpy_include
 _execute(repository_ctx, [python_bin, "-c",..."], <2 more arguments>)
 File "/home/lid/tensorflow_lite/tensorflow/third_party/py/python_configure.bzl", line 56, in _execute
 _fail("\n".join([error_msg.strip() if ... ""]))
 File "/home/lid/tensorflow_lite/tensorflow/third_party/py/python_configure.bzl", line 27, in _fail
 fail(("%sPython Configuration Error:%...)))
Python Configuration Error: Problem getting numpy include path.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
Is numpy installed?
 and referenced by '//third_party/python_runtime:headers'
ERROR: Analysis of target '//tensorflow/tools/pip_package:build_pip_package' failed; build aborted: Analysis failed
INFO: Elapsed time: 1103.475s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (178 packages loaded, 3487 targets configured)
(base) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 


没有安装numpy  报错,安装numpy


    python3 -m pip install numpy

依赖的包 six numpy wheel mock enum34 keras


安装完成后继续编译  bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package




ERROR: /home/lid/tensorflow_lite/tensorflow/tensorflow/BUILD:852:1: Executing genrule //tensorflow:tf_python_api_gen_v2 failed (Exit 1)
Traceback (most recent call last):
  File "/home/lid/.cache/bazel/_bazel_lid/2037b6f96cfe3f12acbf314e5e238949/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_2_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/tools/api/generator/create_python_api.py", line 27, in <module>
    from tensorflow.python.tools.api.generator import doc_srcs
  File "/home/lid/.cache/bazel/_bazel_lid/2037b6f96cfe3f12acbf314e5e238949/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_2_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/__init__.py", line 83, in <module>
    from tensorflow.python import keras
  File "/home/lid/.cache/bazel/_bazel_lid/2037b6f96cfe3f12acbf314e5e238949/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_2_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/keras/__init__.py", line 32, in <module>
    from tensorflow.python.keras import datasets
  File "/home/lid/.cache/bazel/_bazel_lid/2037b6f96cfe3f12acbf314e5e238949/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_2_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/keras/datasets/__init__.py", line 25, in <module>
    from tensorflow.python.keras.datasets import imdb
  File "/home/lid/.cache/bazel/_bazel_lid/2037b6f96cfe3f12acbf314e5e238949/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_2_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/keras/datasets/imdb.py", line 25, in <module>
    from tensorflow.python.keras.preprocessing.sequence import _remove_long_seq
  File "/home/lid/.cache/bazel/_bazel_lid/2037b6f96cfe3f12acbf314e5e238949/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_2_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/keras/preprocessing/__init__.py", line 21, in <module>
    import keras_preprocessing
ModuleNotFoundError: No module named 'keras_preprocessing'
Target //tensorflow/tools/pip_package:build_pip_package failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 28509.189s, Critical Path: 135.85s
INFO: 8459 processes: 8459 local.
FAILED: Build did NOT complete successfully


没有安装keras  安装Keras,

进入tf 环境,source activate tf 

pip insatall keras

如果出现read失败,多安装几次。

安装成功后出现

Successfully built pyyaml
Installing collected packages: scipy, pyyaml, keras
Successfully installed keras-2.3.1 pyyaml-5.1.2 scipy-1.3.1


继续编译 bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

依旧失败。微信图片_20191029172321.png

Ubuntu编译需要8个小时,痛心啊。

执行安装 conda install keras-preprocessing 


(tf) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ 
(tf) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ conda install keras-preprocessing 
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
  current version: 4.7.10
  latest version: 4.7.12
Please update conda by running
    $ conda update -n base -c defaults conda
## Package Plan ##
  environment location: /home/lid/miniconda3/envs/tf
  added / updated specs:
    - keras-preprocessing
The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    blas-1.0                   |              mkl           6 KB
    ca-certificates-2019.10.16 |                0         131 KB
    intel-openmp-2019.4        |              243         729 KB
    keras-preprocessing-1.1.0  |             py_1          36 KB
    libgfortran-ng-7.3.0       |       hdf63c60_0        1006 KB
    mkl-2019.4                 |              243       131.2 MB
    mkl-service-2.3.0          |   py36he904b0f_0         219 KB
    mkl_fft-1.0.14             |   py36ha843d7b_0         155 KB
    mkl_random-1.1.0           |   py36hd6b4f25_0         324 KB
    numpy-1.17.2               |   py36haad9e8e_0           4 KB
    numpy-base-1.17.2          |   py36hde5b4d6_0         4.2 MB
    openssl-1.1.1d             |       h7b6447c_3         3.7 MB
    scipy-1.3.1                |   py36h7c811a0_0        14.0 MB
    six-1.12.0                 |           py36_0          23 KB
    ------------------------------------------------------------
                                           Total:       155.7 MB

The following NEW packages will be INSTALLED:

  blas               pkgs/main/linux-64::blas-1.0-mkl
  intel-openmp       pkgs/main/linux-64::intel-openmp-2019.4-243
  keras-preprocessi~ pkgs/main/noarch::keras-preprocessing-1.1.0-py_1
  libgfortran-ng     pkgs/main/linux-64::libgfortran-ng-7.3.0-hdf63c60_0
  mkl                pkgs/main/linux-64::mkl-2019.4-243
  mkl-service        pkgs/main/linux-64::mkl-service-2.3.0-py36he904b0f_0
  mkl_fft            pkgs/main/linux-64::mkl_fft-1.0.14-py36ha843d7b_0
  mkl_random         pkgs/main/linux-64::mkl_random-1.1.0-py36hd6b4f25_0
  numpy              pkgs/main/linux-64::numpy-1.17.2-py36haad9e8e_0
  numpy-base         pkgs/main/linux-64::numpy-base-1.17.2-py36hde5b4d6_0
  scipy              pkgs/main/linux-64::scipy-1.3.1-py36h7c811a0_0
  six                pkgs/main/linux-64::six-1.12.0-py36_0

The following packages will be UPDATED:

  ca-certificates                               2019.8.28-0 --> 2019.10.16-0
  openssl                                 1.1.1d-h7b6447c_2 --> 1.1.1d-h7b6447c_3


Proceed ([y]/n)? y


Downloading and Extracting Packages
openssl-1.1.1d       | 3.7 MB    |##################################################################################### | 100% 
numpy-base-1.17.2    | 4.2 MB    | ############################################################################ | 100% 
six-1.12.0           | 23 KB     | ##################################################################### | 100% 
scipy-1.3.1          | 14.0 MB   | ##################################################################### | 100% 
mkl_fft-1.0.14       | 155 KB    | #################################################################### | 100% 
numpy-1.17.2         | 4 KB      | ################################################################### | 100% 
ca-certificates-2019 | 131 KB    | ################################################################# | 100% 
blas-1.0             | 6 KB      | ##################################################################### | 100% 
intel-openmp-2019.4  | 729 KB    | ################################################################## | 100% 
mkl-service-2.3.0    | 219 KB    | #################################################################### | 100% 
mkl-2019.4           | 131.2 MB  | ###################################4                                                                         |  22% 
keras-preprocessing- | 36 KB     | ################################################################### | 100% 
mkl_random-1.1.0     | 324 KB    | ############################################################### | 100% 
libgfortran-ng-7.3.0 | 1006 KB   | ################################################################# | 100% 

CondaError: Downloaded bytes did not match Content-Length
  url: https://repo.anaconda.com/pkgs/main/linux-64/mkl-2019.4-243.conda
  target_path: /home/lid/miniconda3/pkgs/mkl-2019.4-243.conda
  Content-Length: 137562990
  downloaded bytes: 30079702

下载失败,继续下载。


(tf) lid@lid-VirtualBox:~/tensorflow_lite/tensorflow$ conda install keras-preprocessing 
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.7.10
  latest version: 4.7.12
Please update conda by running
    $ conda update -n base -c defaults conda
WARNING conda.core.package_cache_data:_make_single_record(350): Encountered corrupt package tarball at /home/lid/miniconda3/pkgs/mkl-2019.4-243.conda. Conda has removed it, but you need to re-run conda to download it again.
## Package Plan ##
  environment location: /home/lid/miniconda3/envs/tf
  added / updated specs:
    - keras-preprocessing
The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    mkl-2019.4                 |              243       131.2 MB
    ------------------------------------------------------------
                                           Total:       131.2 MB
The following NEW packages will be INSTALLED:
  blas               pkgs/main/linux-64::blas-1.0-mkl
  intel-openmp       pkgs/main/linux-64::intel-openmp-2019.4-243
  keras-preprocessi~ pkgs/main/noarch::keras-preprocessing-1.1.0-py_1
  libgfortran-ng     pkgs/main/linux-64::libgfortran-ng-7.3.0-hdf63c60_0
  mkl                pkgs/main/linux-64::mkl-2019.4-243
  mkl-service        pkgs/main/linux-64::mkl-service-2.3.0-py36he904b0f_0
  mkl_fft            pkgs/main/linux-64::mkl_fft-1.0.14-py36ha843d7b_0
  mkl_random         pkgs/main/linux-64::mkl_random-1.1.0-py36hd6b4f25_0
  numpy              pkgs/main/linux-64::numpy-1.17.2-py36haad9e8e_0
  numpy-base         pkgs/main/linux-64::numpy-base-1.17.2-py36hde5b4d6_0
  scipy              pkgs/main/linux-64::scipy-1.3.1-py36h7c811a0_0
  six                pkgs/main/linux-64::six-1.12.0-py36_0

The following packages will be UPDATED:

  ca-certificates                               2019.8.28-0 --> 2019.10.16-0
  openssl                                 1.1.1d-h7b6447c_2 --> 1.1.1d-h7b6447c_3

Proceed ([y]/n)? y
Downloading and Extracting Packages
mkl-2019.4           | 131.2 MB  | #################################################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done



继续编译。换了台服务器


INFO: Elapsed time: 2827.593s, Critical Path: 1332.69s
INFO: 8703 processes: 8703 local.
INFO: Build completed successfully, 9304 total actions
(tf2) i@i-Super-Server:~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow$ 


 终于大功告成,编译成功。

生成whl安装包

bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/tensorflow_pkg


(tf2) iqi@iqi-Super-Server:~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow$ bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/tensorflow_pkg
2019年 10月 29日 星期二 19:03:45 CST : === Preparing sources in dir: /tmp/tmp.AhRdeYfsPh
~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow ~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow
~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow
/tmp/tmp.AhRdeYfsPh/tensorflow/include ~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow
~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow
2019年 10月 29日 星期二 19:04:09 CST : === Building wheel
warning: no files found matching 'README'
warning: no files found matching '*.pyd' under directory '*'
warning: no files found matching '*.pd' under directory '*'
warning: no files found matching '*.dylib' under directory '*'
warning: no files found matching '*.dll' under directory '*'
warning: no files found matching '*.lib' under directory '*'
warning: no files found matching '*.csv' under directory '*'
warning: no files found matching '*.h' under directory 'tensorflow_core/include/tensorflow'
warning: no files found matching '*' under directory 'tensorflow_core/include/third_party'
2019年 10月 29日 星期二 19:04:36 CST : === Output wheel file is in: /home/iqi/tensorflow_pkg
(tf2) iqi@iqi-Super-Server:~/work/apq8053-le.um.1.3.r2-08400-8x53/poky/build/tensorflow-lite/tensorflow$ ls /home/iqi/tensorflow_pkg/tensorflow-2.0.0-cp37-cp37m-linux_x86_64.whl 
/home/iqi/tensorflow_pkg/tensorflow-2.0.0-cp37-cp37m-linux_x86_64.whl


sitemap