# 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-1.10.1-源码
需积分: 1 0 下载量 165 浏览量
2024-03-05
12:57:02
上传
评论
收藏 23.36MB GZ 举报
温馨提示
共2000个文件
h:919个
py:535个
md:283个
Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码 Tensorflow-1.10.1-源码
资源推荐
资源详情
资源评论
收起资源包目录
Tensorflow-1.10.1-源码 (2000个子文件)
libtensorflow_test.c 937B
wrappers.go 986KB
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
c_api.h 77KB
mkl_util.h 75KB
op_kernel.h 63KB
eigen_spatial_convolutions.h 51KB
quantization_utils.h 42KB
reduction_gpu_kernels.cu.h 38KB
pooling_ops_3d_sycl.h 32KB
cwise_ops.h 32KB
shape_inference.h 32KB
graph.h 31KB
tensor.h 29KB
optional.h 29KB
message_wrappers.h 29KB
depthwiseconv_float.h 29KB
tfprof_node.h 28KB
sparse_tensor.h 27KB
random_distributions.h 27KB
function.h 27KB
eigen_volume_patch.h 26KB
shared_batch_scheduler.h 26KB
pooling_ops_common.h 26KB
eigen_backward_cuboid_convolutions.h 25KB
batch_matmul_op_impl.h 25KB
dataset.h 25KB
eigen_pooling.h 24KB
tensor_format.h 24KB
tape.h 24KB
variant_op_registry.h 24KB
cwise_ops_common.h 24KB
tensor_array.h 24KB
cuda_device_functions.h 23KB
decode.h 22KB
adaptive_shared_batch_scheduler.h 22KB
eigen_backward_spatial_convolutions.h 21KB
image-inl.h 21KB
serial_device_batch_scheduler.h 20KB
tensor_shape.h 20KB
inlined_vector.h 20KB
resource_mgr.h 20KB
c_api.h 20KB
cuda_solvers.h 19KB
scatter_functor.h 19KB
env.h 18KB
sparse_conditional_accumulator.h 18KB
mkl_conv_ops.h 17KB
types.h 17KB
bfloat16.h 17KB
direct_session.h 17KB
sparse_matmul_op.h 16KB
mkl_pooling_ops_common.h 16KB
mirror_pad_op.h 16KB
ctc_beam_search.h 16KB
sqlite.h 15KB
int_type.h 15KB
list_kernels.h 15KB
bfc_allocator.h 15KB
debug_io_utils.h 15KB
remote_fused_graph_execute_utils.h 15KB
ctc_loss_calculator.h 14KB
node_def_util.h 14KB
sdca_internal.h 14KB
debug_ops.h 14KB
allocator.h 14KB
strided_slice_op_impl.h 14KB
common_shape_fns.h 14KB
tensor_bundle.h 13KB
top_n.h 13KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
alin、m
- 粉丝: 104
- 资源: 49
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功