在Windows下编译Bazel和使用Bazel编译tensorflow(使用TensorFlow Lite将ssd_mobilenet移植至安卓客户端)

背景

有时需要最新版的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工具,编译转换工具

安装bazel
安装bazel3.1.0

在这里插入图片描述
这个.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从文件读取图片的四种方式

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值