將 TensorFlow 模型轉換為 output_format
。
用法
tf.compat.v1.lite.TFLiteConverter(
graph_def, input_tensors, output_tensors, input_arrays_with_shape=None,
output_arrays=None, experimental_debug_info_func=None
)
參數
-
graph_def
凍結的 TensorFlow GraphDef。 -
input_tensors
輸入張量列表。使用foo.shape
和foo.dtype
計算類型和形狀。 -
output_tensors
輸出張量列表(此處僅使用 .name)。 -
input_arrays_with_shape
表示輸入張量名稱的字符串元組和表示輸入形狀的整數列表(例如,[("foo":[1, 16, 16, 3])])。僅當圖形無法加載到 TensorFlow 以及input_tensors
和output_tensors
為 None 時使用。 (默認無) -
output_arrays
用於凍結圖形的輸出張量列表。僅當圖形無法加載到 TensorFlow 以及input_tensors
和output_tensors
為 None 時使用。 (默認無) -
experimental_debug_info_func
一個實驗函數,用於從graph_def
檢索一組節點的圖形調試信息。
拋出
-
ValueError
無效參數。
屬性
-
optimizations
實驗標誌,可能會發生變化。要應用的優化集。例如 {tf.lite.Optimize.DEFAULT}。 (默認無,必須是無或一組類型的值tf.lite.Optimize
) -
representative_dataset
用於整數量化的生成器函數,其中每個生成的樣本具有與模型輸入相同的順序、類型和形狀。通常,這是從訓練或評估數據集中隨機選擇的數百個樣本的一小部分,沒有特定的順序。這是一個可選屬性,但對於完整整數量化是必需的,即,如果tf.int8
是target_spec.supported_types
中唯一支持的類型。請參閱tf.lite.RepresentativeDataset
。 (默認無) -
target_spec
實驗標誌,可能會發生變化。目標設備的規格,包括支持的操作集、支持的類型和一組用戶定義的 TensorFlow Lite 運行時所需的 TensorFlow 算子。請參閱tf.lite.TargetSpec
。 -
inference_type
數值數組的數據類型,不包括輸入層。 (默認 tf.float32,必須在 {tf.float32, tf.int8, tf.uint8} 中) -
inference_input_type
輸入層中數值數組的數據類型。如果inference_input_type
在 {tf.int8, tf.uint8} 中,則必須提供quantized_input_stats
。 (默認是分配給inference_type
的值,必須在 {tf.float32, tf.int8, tf.uint8} 中) -
inference_output_type
輸出層中數值數組的數據類型。 (默認是分配給inference_type
的值,必須在 {tf.float32, tf.int8, tf.uint8} 中) -
quantized_input_stats
輸入張量名稱到浮點元組的映射,表示訓練數據的均值和標準差。 (例如,{"foo":(0., 1.)})。如果inference_input_type
是 tf.int8 或 tf.uint8,則為必需。 (默認無) -
default_ranges_stats
整數元組 (min, max) 表示沒有指定範圍的所有數值數組的範圍值。旨在通過"dummy quantization" 進行量化實驗。 (默認無) -
allow_custom_ops
指示是否允許自定義操作的布爾值。當為 False 時,任何未知操作都是錯誤。當為 True 時,將為任何未知的操作創建自定義操作。開發人員需要使用自定義解析器將這些提供給 TensorFlow Lite 運行時。 (默認為假) -
drop_control_dependency
布爾值,指示是否靜默刪除控製依賴項。這是因為 TFLite 不支持控製依賴。 (默認為真) -
reorder_across_fake_quant
布爾值,指示是否在意外位置重新排序 FakeQuant 節點。當 FakeQuant 節點的位置阻止轉換圖形所需的圖形轉換時使用。生成與量化訓練圖不同的圖,可能導致不同的算術行為。 (默認為假) -
change_concat_input_ranges
用於更改量化模型的 concat 運算符的輸入和輸出的最小/最大範圍行為的布爾值。當為 true 時更改 concat 運算符重疊的範圍。 (默認為假) -
output_format
輸出文件格式。 (默認 tf.compat.v1.lite.constants.TFLITE,必須在 {tf.compat.v1.lite.constants.TFLITE, tf.compat.v1.lite.constants.GRAPHVIZ_DOT}) -
dump_graphviz_dir
文件夾的完整文件路徑,用於在處理 GraphViz .dot 文件的各個階段轉儲圖形。優先於output_format=tf.compat.v1.lite.constants.GRAPHVIZ_DOT
為了保持輸出文件的要求。 (默認無) -
dump_graphviz_video
布爾值,指示是否在每次圖形轉換後轉儲 GraphViz .dot 文件。需要指定dump_graphviz_dir
標誌。 (默認為假) -
conversion_summary_dir
存儲轉換日誌的目錄的完整路徑。 (默認無) -
target_ops
已棄用。請改用target_spec.supported_ops
。 -
post_training_quantize
已棄用。請改用optimizations
並將其設置為{tf.lite.Optimize.DEFAULT}
。 (默認為假) -
experimental_new_converter
實驗標誌,可能會發生變化。啟用基於 MLIR 的轉換。 (默認為真) -
experimental_new_quantizer
實驗標誌,可能會發生變化。啟用基於 MLIR 的量化轉換,而不是基於 Flatbuffer 的轉換。 (默認為真)
這用於從 TensorFlow GraphDef、SavedModel 或 tf.keras 模型轉換為 TFLite FlatBuffer 或圖形可視化。
示例用法:
# Converting a GraphDef from session.
converter = tf.compat.v1.lite.TFLiteConverter.from_session(
sess, in_tensors, out_tensors)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
# Converting a GraphDef from file.
converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
# Converting a SavedModel.
converter = tf.compat.v1.lite.TFLiteConverter.from_saved_model(
saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
# Converting a tf.keras model.
converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file(
keras_model)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
相關用法
- Python tf.compat.v1.layers.conv3d用法及代碼示例
- Python tf.compat.v1.layers.Conv3D用法及代碼示例
- Python tf.compat.v1.layers.dense用法及代碼示例
- Python tf.compat.v1.losses.softmax_cross_entropy用法及代碼示例
- Python tf.compat.v1.layers.AveragePooling3D用法及代碼示例
- Python tf.compat.v1.layers.Conv2DTranspose用法及代碼示例
- Python tf.compat.v1.layers.max_pooling3d用法及代碼示例
- Python tf.compat.v1.layers.average_pooling1d用法及代碼示例
- Python tf.compat.v1.layers.experimental.keras_style_scope用法及代碼示例
- Python tf.compat.v1.layers.flatten用法及代碼示例
- Python tf.compat.v1.layers.conv1d用法及代碼示例
- Python tf.compat.v1.layers.experimental.set_keras_style用法及代碼示例
- Python tf.compat.v1.layers.conv2d_transpose用法及代碼示例
- Python tf.compat.v1.layers.dropout用法及代碼示例
- Python tf.compat.v1.layers.batch_normalization用法及代碼示例
- Python tf.compat.v1.layers.average_pooling2d用法及代碼示例
- Python tf.compat.v1.losses.mean_squared_error用法及代碼示例
- Python tf.compat.v1.layers.MaxPooling1D用法及代碼示例
- Python tf.compat.v1.layers.conv2d用法及代碼示例
- Python tf.compat.v1.layers.Conv2D用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.lite.TFLiteConverter。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。