Doc*_*ven 24 python python-3.x tensorflow tensorflow-lite
这些天来,我一直在努力寻找有关在TPU支持下部署TF模型的错误。
我可以在不运行TPU支持的情况下获得模型,但是一旦启用量化功能,我就会迷路。
我处于以下情况:
最后一点,我使用了TFLiteConverter的Python API。生成功能性tflite模型的脚本是
import tensorflow as tf
graph_def_file = 'frozen_model.pb'
inputs = ['dense_input']
outputs = ['dense/BiasAdd']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.inference_type = tf.lite.constants.FLOAT
input_arrays = converter.get_input_arrays()
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_model = converter.convert()
open('model.tflite', 'wb').write(tflite_model)
Run Code Online (Sandbox Code Playgroud)
这告诉我,到目前为止我的方法似乎还可以。现在,如果我想使用Coral TPU棒,就必须对我的模型进行量化(在训练过程中考虑了这一点)。我要做的就是修改转换器脚本。我认为我必须将其更改为
import tensorflow as tf
graph_def_file = 'frozen_model.pb'
inputs = ['dense_input']
outputs = ['dense/BiasAdd']
converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.inference_type = tf.lite.constants.QUANTIZED_UINT8 ## Indicates TPU compatibility
input_arrays = converter.get_input_arrays()
converter.quantized_input_stats = {input_arrays[0]: (0., 1.)} ## mean, std_dev
converter.default_ranges_stats = (-128, 127) ## min, max values for quantization (?)
converter.allow_custom_ops = True ## not sure if this is needed
## REMOVED THE OPTIMIZATIONS ALTOGETHER TO MAKE IT WORK
tflite_model = converter.convert()
open('model.tflite', 'wb').write(tflite_model)
Run Code Online (Sandbox Code Playgroud)
当使用解释器的Python API加载时,此tflite模型会产生结果,但我无法理解它们的含义。此外,也没有(或者如果有的话,它隐藏得很好)有关如何选择均值,std_dev和最小/最大范围的文档。另外,在用edgetpu_compiler编译并部署(使用C ++ API加载)之后,我收到错误消息:
INFO: Initialized TensorFlow Lite runtime.
ERROR: Failed to prepare for TPU. generic::failed_precondition: Custom op already assigned to a different TPU.
ERROR: Node number 0 (edgetpu-custom-op) failed to prepare.
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
我想我在转换过程中错过了一个标志或其他东西。但是由于这里也缺少文档,所以我不确定。
简而言之:
感谢您的帮助或指导!
编辑:我用完整的测试代码打开了一个github问题。随意玩这个。
您永远不需要手动设置量化统计数据。
您尝试过训练后量化教程吗?
https://www.tensorflow.org/lite/performance/post_training_integer_quant
基本上他们设置量化选项:
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
Run Code Online (Sandbox Code Playgroud)
然后,他们将“代表性数据集”传递给转换器,以便转换器可以运行几批模型来收集必要的统计数据:
def representative_data_gen():
for input_value in mnist_ds.take(100):
yield [input_value]
converter.representative_dataset = representative_data_gen
Run Code Online (Sandbox Code Playgroud)
虽然有量化训练的选项,但进行训练后量化总是更容易。
| 归档时间: |
|
| 查看次数: |
368 次 |
| 最近记录: |