TensorFlow Frontend前端
TensorFlow Frontend前端
TensorFlow前端有助于將TensorFlow模型導入TVM。
Supported versions:
? 1.12 and below
Tested models:
? Inception (V1/V2/V3/V4)
? Resnet (All)
? Mobilenet (V1/V2 All)
? Vgg (16/19)
? BERT (Base/3-layer)
Preparing a Model for Inference準備推理模型
Remove Unneeded Nodes刪除不需要的節點
導出過程將刪除許多不需要進行推理的節點,但不幸的是會留下一些剩余的節點。應該手動刪除的節點:
? Dropout, including Dropout and DropoutWrapper
? Assert
Convert None Dimensions to Constants將無尺寸Dimensions轉換為常數
TVM對動態張量形狀的支持最少。None應將尺寸替換為常量。例如,模型可以接受帶有shape的輸入(None,20)。這應轉換為的形狀(1,20)。應該相應地修改模型,以確保這些形狀在整個圖形中都匹配。
Export
TensorFlow前端需要凍結的protobuf(.pb)或保存的模型作為輸入。不支持檢查點(.ckpt)。TensorFlow前端所需的graphdef,可以從活動會話中提取,可以使用TFParser幫助器類提取。
應該導出該模型并進行許多轉換,以準備模型進行推理。設置add_shapes=True也很重要,因為這會將每個節點的輸出形狀嵌入到圖形中。這是一個給定會話將模型導出為protobuf的函數:
import tensorflow as tf
from tensorflow.tools.graph_transforms import TransformGraph
def export_pb(session):
with tf.gfile.GFile(“myexportedmodel.pb”, “wb”) as f:
inputs = [“myinput1”, “myinput2”] # replace with your input names
outputs = [“myoutput1”] # replace with your output names
graph_def = session.graph.as_graph_def(add_shapes=True)
graph_def = tf.graph.util.convert_variables_to_constants(session, graph_def, outputs)
graph_def = TransformGraph(
graph_def,
inputs,
outputs,
[
“remove_nodes(op=Identity, op=CheckNumerics, op=StopGradient)”,
“sort_by_execution_order”, # sort by execution order after each transform to ensure correct node ordering
“remove_attribute(attribute_name=_XlaSeparateCompiledGradients)”,
“remove_attribute(attribute_name=_XlaCompile)”,
“remove_attribute(attribute_name=_XlaScope)”,
“sort_by_execution_order”,
“remove_device”,
“sort_by_execution_order”,
“fold_batch_norms”,
“sort_by_execution_order”,
“fold_old_batch_norms”,
“sort_by_execution_order”
]
)
f.write(graph_def.SerializeToString())
Another method is to export and freeze the graph.
Import the Model
Explicit Shape:
確保可以在整個圖形中知道形狀,將shape參數傳遞給from_tensorflow。該詞典將輸入名稱映射到輸入形狀。
Data Layout
大多數TensorFlow模型以NHWC布局發布。NCHW布局通常提供更好的性能,尤其是在GPU上。該TensorFlow前端可以通過傳遞參數自動轉換模型的數據布局layout='NCHW'到from_tensorflow。
Best Practices
? 使用靜態張量形狀代替動態形狀(刪除None尺寸)。
? TensorArray目前尚不支持使用靜態RNN代替動態RNN。
Supported Ops
? Abs
? Add
? AddN
? All
? Any
? ArgMax
? ArgMin
? AvgPool
? BatchMatMul
? BatchMatMulV2
? BatchNormWithGlobalNormalization
? BatchToSpaceND
? BiasAdd
? BroadcastTo
? Cast
? Ceil
? CheckNumerics
? ClipByValue
? Concat
? ConcatV2
? Conv2D
? Cos
? Tan
? CropAndResize
? DecodeJpeg
? DepthwiseConv2dNative
? DepthToSpace
? Dilation2D
? Equal
? Elu
? Enter
? Erf
? Exit
? Exp
? ExpandDims
? Fill
? Floor
? FloorDiv
? FloorMod
? FusedBatchNorm
? FusedBatchNormV2
? Gather
? GatherNd
? GatherV2
? Greater
? GreaterEqual
? Identity
? IsFinite
? IsInf
? IsNan
? LeakyRelu
? LeftShift
? Less
? LessEqual
? Log
? Log1p
? LoopCond
? LogicalAnd
? LogicalOr
? LogicalNot
? LogSoftmax
? LRN
? LSTMBlockCell
? MatMul
? Max
? MaxPool
? Maximum
? Mean
? Merge
? Min
? Minimum
? MirrorPad
? Mod
? Mul
? Neg
? NextIteration
? NotEqual
? OneHot
? Pack
? Pad
? PadV2
? Pow
? Prod
? Range
? Rank
? RealDiv
? Relu
? Relu6
? Reshape
? ResizeBilinear
? ResizeBicubic
? ResizeNearestNeighbor
? ReverseV2
? RightShift
? Round
? Rsqrt
? Select
? Selu
? Shape
? Sigmoid
? Sign
? Sin
? Size
? Slice
? Softmax
? Softplus
? SpaceToBatchND
? SpaceToDepth,
? Split
? SplitV
? Sqrt
? Square
? SquareDifference
? Squeeze
? StridedSlice
? Sub
? Sum
? Switch
? Tanh
? TensorArrayV3
? TensorArrayScatterV3
? TensorArrayGatherV3
? TensorArraySizeV3
? TensorArrayWriteV3
? TensorArrayReadV3
? TensorArraySplitV3
? TensorArrayConcatV3
? Tile
? TopKV2
? Transpose
? TruncateMod
? Unpack
? UnravelIndex
? Where
? ZerosLike
總結
以上是生活随笔為你收集整理的TensorFlow Frontend前端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 向Relay添加算子
- 下一篇: VTA硬件