TensorFlow 安装及使用
安装
(1)安装包安装:pip install tensorflow==1.14 -i https://pypi.douban.com/simple
virtualenv -p /usr/bin/python2.7 venv-python2.7-tf1.14.0
source ./venv-python2.7-tf1.14.0/bin/activate
pip list
python
pip install numpy==1.16.5 opt-einsum==2.3.2 future -i https://pypi.douban.com/simple
pip install tensorflow==1.14.0 -i https://pypi.douban.com/simple
(2)源码编译安装:https://tensorflow.google.cn/install/source
Install bazel-0.25.2
# wget https://github.com/bazelbuild/bazel/releases/download/0.25.2/bazel-0.25.2-linux-x86_64
# chmod u+x bazel-0.25.2-linux-x86_64
# ln -s /path/bazel-0.25.2-linux-x86_64 /usr/bin/bazel
# bazel version
Build label: 0.25.2
Install tensorflow-1.14.0
# git clone https://github.com/tensorflow/tensorflow.git
# cd tensorflow
# git checkout v1.14.0
# ./configure # /usr/bin/python3, others are default
# bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
# ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
# mv /tmp/tensorflow_pkg/tensorflow-1.14.0-cp36-cp36m-linux_x86_64.whl ./
# python3 -m pip install ./tensorflow-1.14.0-cp36-cp36m-linux_x86_64.whl
install tensorflow-1.14.0 with MKL&Patch
# git clone https://github.com/tensorflow/tensorflow.git
# cd tensorflow/
# git checkout v1.14.0
# patch -p0 < /path/tf-mkl.patch
# ./configure # /usr/bin/python3, others are default
# bazel build --config=mkl --config=opt //tensorflow/tools/pip_package:build_pip_package
# ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
# mv /tmp/tensorflow_pkg/tensorflow-1.14.0-cp36-cp36m-linux_x86_64.whl ./
# python3 -m pip install ./tensorflow-1.14.0-cp36-cp36m-linux_x86_64.whl
# python3 -m pip list
tensorboard (1.14.0)
tensorflow (1.14.0)
tensorflow-estimator (1.14.0)
使用
模型优化
(1)查看 saved_model 模型的输入和输出
# bazel build tensorflow/python/tools:saved_model_cli
# saved_model_cli show --dir detection/ --all
或者
# python3 /usr/local/lib/python3.6/site-packages/tensorflow/python/tools/saved_model_cli.py show --dir detection/ --all
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:
signature_def['serving_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['image'] tensor_info:
dtype: DT_UINT8
shape: (1, -1, -1, 3)
name: image:0
inputs['true_image_shape'] tensor_info:
dtype: DT_INT32
shape: (1, 3)
name: true_image_shape:0
The given SavedModel SignatureDef contains the following output(s):
outputs['detection_boxes'] tensor_info:
dtype: DT_FLOAT
shape: (1, -1, 4)
name: ChangeCoordToOriginalImage/stack:0
outputs['detection_classes'] tensor_info:
dtype: DT_INT32
shape: (1, -1)
name: add:0
outputs['detection_keypoints'] tensor_info:
dtype: DT_FLOAT
shape: (1, -1, 4, 2)
name: TextKeypointPostProcess/Reshape_2:0
outputs['detection_scores'] tensor_info:
dtype: DT_FLOAT
shape: (1, -1)
name: strided_slice_3:0
outputs['num_detections'] tensor_info:
dtype: DT_INT32
shape: (1)
name: BatchMultiClassNonMaxSuppression/stack_8:0
Method name is: tensorflow/serving/predict
(2)将 tf 的 saved_model 保存成 frozen_model
# bazel build tensorflow/python/tools:freeze_graph
# freeze_graph --input_saved_model_dir detection/ --output_graph detection_frozen_model.pb --output_node_names ChangeCoordToOriginalImage/stack,add,TextKeypointPostProcess/Reshape_2,strided_slice_3,BatchMultiClassNonMaxSuppression/stack_8
或者
# python3 /usr/local/lib/python3.6/site-packages/tensorflow/python/tools/freeze_graph.py --input_saved_model_dir detection/ --output_graph detection_frozen_model.pb --output_node_names ChangeCoordToOriginalImage/stack,add,TextKeypointPostProcess/Reshape_2,strided_slice_3,BatchMultiClassNonMaxSuppression/stack_8
(3)将 frozen_model 通过优化得到 optimized_model
# bazel build tensorflow/python/tools:optimize_for_inference // ouput: bazel-bin/tensorflow/python/tools/optimize_for_inference
# optimize_for_inference --input detection_frozen_model.pb --output detection_optimized_model.pb --input_names image,true_image_shape --output_names ChangeCoordToOriginalImage/stack,add,TextKeypointPostProcess/Reshape_2,strided_slice_3,BatchMultiClassNonMaxSuppression/stack_8 --frozen_graph true --placeholder_type_enum 4,3,1,3,1,1,3
或者
# python3 /usr/local/lib/python3.6/site-packages/tensorflow/python/tools/optimize_for_inference.py --input detection_frozen_model.pb --output detection_optimized_model.pb --input_names image,true_image_shape --output_names ChangeCoordToOriginalImage/stack,add,TextKeypointPostProcess/Reshape_2,strided_slice_3,BatchMultiClassNonMaxSuppression/stack_8 --frozen_graph true --placeholder_type_enum 4,3,1,3,1,1,3
其中 placeholder_type_enum 详情如下:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/types.proto
(4)将 pb 模型输出成 TensorFlow 的可视化 graph
# bazel build tensorflow/python/tools:import_pb_to_tensorboard
# import_pb_to_tensorboard --model_dir ./recognition_frozen_model.pb --log_dir ./recognition_log
或者
# python3 /usr/local/lib/python3.6/site-packages/tensorflow/python/tools/import_pb_to_tensorboard.py --model_dir ./recognition_frozen_model.pb --log_dir ./recognition_frozen_model.graph
# nohup tensorboard --logdir=./recognition_frozen_model.graph --port=6006 2>&1 &
可视化工具 TensorBoard 用法: https://blog.csdn.net/gg_18826075157/article/details/78440766
(5)量化、固化、优化 pb 模型
官方手册:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/graph_transforms
intel 量化手册:https://github.com/IntelAI/tools/tree/master/tensorflow_quantization
# bazel build tensorflow/tools/graph_transforms:transform_graph
# bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph="./detection_frozen_model.pb" --out_graph="./detection_transformed_model.pb" --inputs="image,true_image_shape" --outputs="ChangeCoordToOriginalImage/stack,add,TextKeypointPostProcess/Reshape_2,strided_slice_3,BatchMultiClassNonMaxSuppression/stack_8" --transforms='
add_default_attributes
strip_unused_nodes()
remove_nodes(op=Identity, op=CheckNumerics)
fold_constants(ignore_errors=true)
fold_batch_norms
fold_old_batch_norms
quantize_weights'
PS:
模型优化 refer:https://blog.csdn.net/qq_14845119/article/details/78846372
模型量化:https://www.jianshu.com/p/d2637646cda1
tf的log和vlog输出配置
There are two flags, similarly named, but with somewhat different semantics:
TF_CPP_MIN_LOG_LEVEL - which has 3 or 4 basic levels - low numbers = more messages.
0 outputs Information, Warning, Error, and Fatals (default)
1 outputs Warning, and above
2 outputs Errors and above.
etc... I didn't check edge cases
TF_CPP_MIN_VLOG_LEVEL - which causes very very many extra Information errors - really for debugging only - low numbers = less messages.
3 Outputs lots and lots of stuff
2 Outputs less
1 Outputs even less
0 Outputs nothing extra (default)
TensorFlow 安装及使用的更多相关文章
- tensorflow安装日志(PIP)
最近刚刚接触深度学习,安装一下tf 环境:华硕Z170主板.i7 6700k.GTX1070.Ubuntu16.04.Python2.7 在这之前先装好了cuda8.0.cudnn5.0.caffe整 ...
- TensorFlow安装-windows系统
官方各版本的安装说明:https://www.tensorflow.org/install/ 本文介绍如何在windows环境下安装tensorflow, 跑起来简单的demo. 1.安装python ...
- TensorFlow 安装详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! 『不要把手段当成目标 — <一个瑜伽行者的自传>』 本文提纲 1. 机器学习 2 ...
- tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案
tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案 一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 二, 安装环境: ...
- TensorFlow 安装报错的解决办法(安装1.5版本)
1.安装Anaconda 百度下载windows版本,一路点下一步,安装好了Anaconda,自带python3.6.6. 2.安装TensorFlow (1)打开Anaconda Prompt,输入 ...
- ubuntu TensorFlow安装
Tensorflow安装确实比caffe简单... cuda,cudnn安装就不说了 1 安裝pip(有就跳过) sudo apt-get install python-pip python-dev ...
- TensorFlow安装之后导入报错:libcudnn.so.6:cannot open sharedobject file: No such file or directory
转载自:http://blog.csdn.net/silent56_th/article/details/77587792 系统环境:Ubuntu16.04 + GTX1060 目的:配置一下pyth ...
- TensorFlow 安装以及python虚拟环境
python虚拟环境 由于TensorFlow只支持某些版本的python解释器,如Python3.6.如果其他版本用户要使用TensorFlow就必须安装受支持的python版本.为了方便在不同项目 ...
- TensorFlow安装时错误CondaValueError: prefix already exists: G:\softs\Anaconda\envs\tensorflow
TensorFlow安装时,TensorFlow环境已经调好了,就是下面的第(3)步, 可我自己偏偏选了个Python3.7,因为检测到自己的Python最新版本为3.7,就手贱安了TensorFlo ...
- TensorFlow安装,升级,基本操作
一. 安装 ubuntu 16 python 2.7 pip install tensorflow 测试安装完成效果: 查看tensorFlow版本python import tensorflow a ...
随机推荐
- I - Rake It In
题目链接:https://nanti.jisuanke.com/t/A1538 题意:给一个4*4的方阵,k个回合,a和b轮流选一个2*2的矩阵和,a要使和最大,b要使和最小,选完后2*2矩阵要逆时针 ...
- 大数据笔记(二)——Apache Hadoop的体系结构
一.分布式存储 NameNode(名称节点) 1.维护HDFS文件系统,是HDFS的主节点. 2.接收客户端的请求:上传.下载文件.创建目录等. 3.记录客户端操作的日志(edits文件),保存了HD ...
- RHEL6 kernel bug在hadoop上的测试
最近给hadoop集群升级了RHEL6,发现性能比之前的差了不少.发现淘宝内核组发现并解决了这个问题 原文链接:http://blog.donghao.org/2013/03/20/hadoop%E9 ...
- HDU 5172 GTY's gay friends (线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5172 题意: 给你一个n个数的数组,m次询问,询问在[L, R] 这个区间里面有没有 [1, R-L+ ...
- 通过HookNtCreateSection 动态监控驱动sys、动态链接库dll、可执行文件exe加载
[cpp] view plaincopyprint? /* windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过 */ #include < ...
- malloc(50) 内存泄露 内存溢出 memory leak会最终会导致out of memory
https://en.wikipedia.org/wiki/Memory_leak In computer science, a memory leak is a type of resource l ...
- django小知识
def __str__: return self.name 在显示的时候,将原来显示的额object对象,显示成这个类的名字
- python2.7+appium第一个脚本(使用夜神模拟器)
搭建好环境后,可以开始准备脚本的编写工作 目录 1.安装夜神模拟器 2.使用uiautomatorviewer定位 3.运行第一个脚本 1.安装夜神模拟器 第一步:官网下载夜神模拟器,完成安装 双击下 ...
- 新手解惑:nginx&php-fpm&fastcgi 是什么关系
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index ...
- TCP为什么要三次握手和四次挥手
http://www.jellythink.com/archives/705 简析TCP的三次握手与四次分手 https://zhuanlan.zhihu.com/p/24001696 计算机网络面试 ...