# Graph Transform Tool
## Table of Contents
* [Introduction](#introduction)
* [Using the Graph Transform Tool](#using-the-graph-transform-tool)
* [Inspecting Graphs](#inspecting-graphs)
* [Common Use Cases](#common-use-cases)
* [Optimizing for Deployment](#optimizing-for-deployment)
* [Fixing Missing Kernel Errors on
Mobile](#fixing-missing-kernel-errors-on-mobile)
* [Shrinking File Size](#shrinking-file-size)
* [Eight-bit Calculations](#eight-bit-calculations)
* [Transform Reference](#transform-reference)
* [add_default_attributes](#add_default_attributes)
* [backport_concatv2](#backport_concatv2)
* [flatten_atrous_conv](#flatten_atrous_conv)
* [fold_batch_norms](#fold_batch_norms)
* [fold_constants](#fold_constants)
* [fold_old_batch_norms](#fold_old_batch_norms)
* [freeze_requantization_ranges](#freeze_requantization_ranges)
* [fuse_convolutions](#fuse_convolutions)
* [insert_logging](#insert_logging)
* [merge_duplicate_nodes](#merge_duplicate_nodes)
* [obfuscate_names](#obfuscate_names)
* [quantize_nodes](#quantize_nodes)
* [quantize_weights](#quantize_weights)
* [remove_attribute](#remove_attribute)
* [remove_device](#remove_device)
* [remove_nodes](#remove_nodes)
* [rename_attribute](#rename_attribute)
* [rename_op](#rename_op)
* [round_weights](#round_weights)
* [sparsify_gather](#sparsify_gather)
* [set_device](#set_device)
* [sort_by_execution_order](#sort_by_execution_order)
* [strip_unused_nodes](#strip_unused_nodes)
* [Writing Your Own Transforms](#writing-your-own-transforms)
* [Transform Functions](#transform-functions)
* [Pattern Syntax](#pattern-syntax)
* [ReplaceMatchingOpTypes](#replacematchingoptypes)
* [Parameters](#parameters)
* [Function Libraries](#function-libraries)
* [Registering](#registering)
## Introduction
When you have finished training a model and want to deploy it in production,
you'll often want to modify it to better run in its final environment. For
example if you're targeting a phone you might want to shrink the file size by
quantizing the weights, or optimize away batch normalization or other
training-only features. The Graph Transform framework offers a suite of tools
for modifying computational graphs, and a framework to make it easy to write
your own modifications.
This guide is structured into three main parts, first giving some tutorials on
how to perform common tasks, second a reference covering all of the different
transformations that are included, together with the options that apply to them,
and third a guide to creating your own transforms.
## Using the Graph Transform Tool
The Graph Transform tool is designed to work on models that are saved as
GraphDef files, usually in a binary protobuf format. This is the low-level
definition of a TensorFlow computational graph, including a list of nodes and
the input and output connections between them. If you're using a Python API to
train your model, this will usually be saved out in the same directory as your
checkpoints, and usually has a '.pb' suffix.
If you want to work with the values of your trained parameters, for example to
quantize weights, you'll need to run
[tensorflow/python/tools/freeze_graph.py](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py)
to convert the checkpoint values into embedded constants within the graph file
itself.
You call the Graph Transform tool itself like this:
```bash
bazel build tensorflow/tools/graph_transforms:transform_graph
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
--in_graph=tensorflow_inception_graph.pb \
--out_graph=optimized_inception_graph.pb \
--inputs='Mul:0' \
--outputs='softmax:0' \
--transforms='
strip_unused_nodes(type=float, shape="1,299,299,3")
remove_nodes(op=Identity, op=CheckNumerics)
fold_old_batch_norms
'
```
The arguments here are specifying where to read the graph from, where to write
the transformed version to, what the input and output layers are, and what
transforms to modify the graph with. The transforms are given as a list of
names, and can each have arguments themselves. These transforms define the
pipeline of modifications that are applied in order to produce the output.
Sometimes you need some transforms to happen before others, and the ordering
within the list lets you specify which happen first.
Note that the optimization
`remove_nodes(op=Identity, op=CheckNumerics)` will break the model with control
flow operations, such as `tf.cond`, `tf.map_fn`, and `tf.while`.
## Inspecting Graphs
Many of the transforms that the tool supports need to know what the input and
output layers of the model are. The best source for these is the model training
process, where for a classifier the inputs will be the nodes that receive the
data from the training set, and the output will be the predictions. If you're
unsure, the
[`summarize_graph`](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/summarize_graph_main.cc)
tool can inspect the model and provide guesses about likely input and output nodes,
as well as other information that's useful for debugging. Here's an example of
how to use it on the [Inception V3
graph](http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz):
```bash
bazel build tensorflow/tools/graph_transforms:summarize_graph
bazel-bin/tensorflow/tools/graph_transforms/summarize_graph --in_graph=tensorflow_inception_graph.pb
```
## Common Use Cases
This section has small guides for some of the most frequently-used
transformation pipelines, aimed at users who want to quickly accomplish one of
these tasks. A lot of them will use the Inception V3 model for their examples,
which can be downloaded from
[http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz](http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz).
### Optimizing for Deployment
If you've finished training your model and want to deploy it on a server or a
mobile device, you'll want it to run as fast as possible, and with as few
non-essential dependencies as you can. This recipe removes all of the nodes that
aren't called during inference, shrinks expressions that are always constant
into single nodes, and optimizes away some multiply operations used during batch
normalization by pre-multiplying the weights for convolutions.
```bash
bazel build tensorflow/tools/graph_transforms:transform_graph
bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
--in_graph=tensorflow_inception_graph.pb \
--out_graph=optimized_inception_graph.pb \
--inputs='Mul' \
--outputs='softmax' \
--transforms='
strip_unused_nodes(type=float, shape="1,299,299,3")
remove_nodes(op=Identity, op=CheckNumerics)
fold_constants(ignore_errors=true)
fold_batch_norms
fold_old_batch_norms'
```
The batch norm folding is included twice because there are two different flavors
of batch normalization used in TensorFlow. The older version was implemented
with a single BatchNormWithGlobalNormalization op, but it was deprecated in
favor of a more recent approach using individual ops to implement the same
computation. The two transforms are in there so that both styles are recognized
and optimized.
### Fixing Missing Kernel Errors on Mobile
The mobile version of TensorFlow is focused on inference, and so by default the
list of supported ops (defined in
[tensorflow/core/kernels/BUILD:android_extended_ops](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/BUILD)
for Bazel and
[tensorflow/contrib/makefile/tf_op_files.txt](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/makefile/tf_op_files.txt)
for make builds) doesn't include a lot that are training related. This can cause
`No OpKernel was registered to support Op`
没有合适的资源?快使用搜索试试~ 我知道了~
tensorflow-master.zip 2018.10.29最新版
共2000个文件
cc:3572个
pbtxt:3011个
py:2983个
需积分: 13 72 下载量 95 浏览量
2018-10-29
12:57:04
上传
评论 1
收藏 35.63MB ZIP 举报
温馨提示
tensorflow-master.zip 2018.10.29最新版
资源推荐
资源详情
资源评论
收起资源包目录
tensorflow-master.zip 2018.10.29最新版 (2000个子文件)
hexagon_controller.c 18KB
graph_functions_wrapper.c 11KB
soc_interface.c 9KB
filterbank_util.c 9KB
filterbank.c 4KB
_main.c 4KB
pcan_gain_control_util.c 3KB
c_api_internal.c 3KB
frontend_util.c 3KB
log_scale.c 3KB
frontend_io.c 3KB
frontend.c 3KB
filterbank_io.c 3KB
window.c 3KB
frontend_main.c 2KB
fft_util.c 2KB
window_util.c 2KB
noise_reduction.c 2KB
pcan_gain_control.c 2KB
frontend_memmap_main.c 2KB
fft.c 2KB
noise_reduction_util.c 2KB
window_io.c 2KB
frontend_memmap_generator.c 2KB
log_lut.c 2KB
fft_io.c 2KB
noise_reduction_io.c 2KB
log_scale_util.c 1KB
log_scale_io.c 1014B
libtensorflow_test.c 937B
compatibility_test.c 816B
inception_v3_dummy_int_data.c 748B
inception_v3_dummy_float_data.c 728B
inception_v1_graph_init.c 703B
inception_v3_graph_init.c 700B
wrappers.go 1.07MB
genop.go 16KB
tensor.go 16KB
session.go 11KB
graph.go 11KB
genop_test.go 10KB
example_inception_inference_test.go 9KB
tensor_test.go 8KB
session_test.go 7KB
attrs.go 7KB
operation.go 6KB
operation_test.go 6KB
scope.go 6KB
scope_test.go 5KB
attrs_test.go 4KB
op_test.go 4KB
api_def_map.go 3KB
shape.go 3KB
saved_model.go 2KB
shape_test.go 2KB
main.go 2KB
graph_test.go 2KB
status.go 2KB
util_test.go 2KB
op.go 2KB
saved_model_test.go 1KB
doc.go 1KB
version.go 834B
lib.go 779B
generate.go 741B
android.go 715B
lib.go 688B
schema_generated.h 449KB
optimized_ops.h 269KB
reference_ops.h 196KB
depthwiseconv_uint8_3x3_filter.h 137KB
blas.h 137KB
hlo_evaluator_typed_visitor.h 114KB
stream.h 113KB
xla_builder.h 104KB
legacy_reference_ops.h 102KB
dnn.h 101KB
legacy_optimized_ops.h 89KB
MatMatProductAVX2.h 89KB
depthwiseconv_uint8.h 86KB
mkl_util.h 81KB
c_api.h 80KB
hlo_instruction.h 73KB
model.h 67KB
eigen_cuboid_convolution.h 67KB
op_kernel.h 65KB
hlo_instructions.h 58KB
eigen_spatial_convolutions.h 55KB
pattern_matcher.h 52KB
literal.h 47KB
depthwiseconv_float.h 42KB
quantization_utils.h 42KB
cuda_dnn.h 39KB
NeuralNetworksShim.h 38KB
reduction_gpu_kernels.cu.h 38KB
stream_executor_pimpl.h 36KB
cwise_ops.h 34KB
types.h 33KB
shape_util.h 33KB
pooling_ops_3d_sycl.h 32KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
threenewbee
- 粉丝: 3w+
- 资源: 1754
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功