背景
有时需要最新版的tensorflow安装文件国内镜像网站又还没提供,或者需要一个针对你自己的机器环境的版本,这时知道怎么自己从源码编译出tensorflow的whl安装文件就必须的了,要自己编译首先就得了解下Bazel这工具,谷歌废弃使用CMake改用并力推自己的Bazel看来这是大势所趋,花了几天时间琢磨了下Bazel的编译和使用Bazel编译tensorflow,一般人工作都没同时配有几台机器,Windows日常工作又得时时开着,至于自己花钱攒机器的就不要说了,为了使用Linux环境弄个VM在硬件不牛时性能可不好,所以还是琢磨Windows下怎么搞定这事吧,不过一般的编译在Linux下没毛病在Windows下支持都不好,所以动手前对于可能遇到困难是有心里准备的。
1、下载安装MSYS2
安装 MSYS2
所有安装步骤都默认一路next下去,安装完成以后,需要配置:
将目录C:\msys64和C:\msys64\usr\bin 加入系统环境变量path
2、安装vs2015(已安装,省略此步)
3、安装bazel工具,编译转换工具
这个.bazelversion可以查看版本,我的是3.1.0
验证成功安装:
这是我的配置
3、Anaconda的虚拟环境配置
4、bazel编译
下载tensorflow工程代码
git clone https://github.com/tensorflow/tensorflow.git
#进入到tensorflow-master目录下, 执行
cd tensorflow/
python ./configure.py
#开始安装, 大部分都是选择n, 除非安装cuda和cudnn, 默认的正确可以直接回车.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python
Found possible Python library paths:
/usr/lib/python3.6/dist-packages
/usr/local/lib/python3.6/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/pytho n3.6/dist-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]: y
CUDA support will be enabled for TensorFlow.
Do you wish to build TensorFlow with TensorRT support? [y/N]: n
No TensorRT support will be enabled for TensorFlow.
Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10]:10
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7
Please specify the locally installed NCCL version you want to use. [Leave empty to use http://github.com/nvidia/nccl]:
Please specify the comma-separated list of base paths to look for CUDA libraries and headers. [Leave empty to use the default]:
Please specify a list of comma-separated CUDA compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia. com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size, and that TensorFlow only supports compute capabilities >= 3.5 [Default is: 6.1,6.1]:
Do you want to use clang as CUDA compiler? [y/N]: n
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Do you wish to build TensorFlow with MPI support? [y/N]:n
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]: n
开始编译
bazel build --config=opt tensorflow/python/tools:freeze_graph
bazel build --config=opt tensorflow/contrib/lite/toco:toco
5、利用bazel生成tflite文件:
官网:
bazel run --config=opt tensorflow/contrib/lite/toco:toco -- \
--input_file=$OUTPUT_DIR/tflite_graph.pb \
--output_file=$OUTPUT_DIR/detect.tflite \
--input_shapes=1,300,300,3 \
--input_arrays=normalized_input_image_tensor \
--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' \
--inference_type=QUANTIZED_UINT8 \
--mean_values=128 \
--std_values=128 \
--change_concat_input_ranges=false \
--allow_custom_ops
本人使用的方法
bazel run --config=opt tensorflow/contrib/lite/toco:toco -- \
--input_file=$OUTPUT_DIR/tflite_graph.pb \
--output_file=$OUTPUT_DIR/detect.tflite \
--input_shapes=1,300,300,3 \
--input_arrays=normalized_input_image_tensor \
--output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' \
--inference_type=FLOAT \
--allow_custom_ops
执行后生成的detect.tflite便可移植至安卓客户端
参考的博客
迁移学习实战 | 快速训练残差网络 ResNet-101,完成图像分类与预测,精度高达 98%!
android studio安装教程
基于Tensorflow2 Lite在Android手机上实现图像分类
将 TensorFlow 移植到 Android手机,实现物体识别、行人检测和图像风格迁移详细教程
Tensorflow在android studio 上运行
win10 + bazel-0.20.0 + tensorflow-1.13.1 编译tensorflow GPU版本的C++库
运行TensorFlow object_detection 在Android上的例子
将TensorFlow训练好的模型迁移到Android APP上(TensorFlowLite
ubuntu–bazel编译tensorflow源码
量化版——使用TF Lite将Mobilenet SSD目标检测移植至安卓客户端
tensorflow object_detection ssd_mobilenet 以及移植到android系统上教程
使用TensorFlow Lite将ssd_mobilenet移植至安卓客户端
GitHub
在win10上安装bazel
初探TensorFLow从文件读取图片的四种方式