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)
導出過程将删除許多不需要進行推理的節點,但不幸的是會留下一些剩餘的節點。應該手動删除的節點:
Dropout, including Dropout and DropoutWrapper
Assert
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",
"fold_batch_norms",
"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
人工智能晶片與自動駕駛