Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TF-Lite'的说明;用于量化感知训练的s-Toco转换器args_Python_Python 3.x_Tensorflow_Tensorflow Lite - Fatal编程技术网

Python TF-Lite'的说明;用于量化感知训练的s-Toco转换器args

Python TF-Lite'的说明;用于量化感知训练的s-Toco转换器args,python,python-3.x,tensorflow,tensorflow-lite,Python,Python 3.x,Tensorflow,Tensorflow Lite,这些天来,我正试图追踪一个关于部署带有TPU支持的TF模型的错误 我可以在没有TPU支持的情况下运行模型,但一旦启用量化,我就会迷失方向 我的情况如下: 创建了一个模型并对其进行了培训 创建模型的评估图 冻结模型并将结果保存为协议缓冲区 在没有TPU支持的情况下成功转换并部署了它 最后,我使用了TFLiteConverter的Python API。生成函数式tflite模型的脚本是 import tensorflow as tf graph_def_file = 'frozen_model.p

这些天来,我正试图追踪一个关于部署带有TPU支持的TF模型的错误

我可以在没有TPU支持的情况下运行模型,但一旦启用量化,我就会迷失方向

我的情况如下:

  • 创建了一个模型并对其进行了培训
  • 创建模型的评估图
  • 冻结模型并将结果保存为协议缓冲区
  • 在没有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)
    
    这告诉我,到目前为止,我的方法似乎还可以。现在,如果我想使用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)
    
    这个tflite模型在加载解释器的Python API时会产生结果,但我无法理解它们的含义。此外,没有关于如何选择平均值、标准偏差和最小/最大范围的文档(如果有,则隐藏得很好)。此外,在用EdGePuxEng编译器编译它并部署它(用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
    
    我想我在转换过程中错过了一个标志或其他东西。但是由于这里也缺少文档,我不能肯定

    简言之:

  • 参数是什么意思,标准偏差,最小/最大值,以及它们如何相互作用
  • 在转换过程中我做错了什么
  • 我非常感谢您的帮助和指导


    编辑:我已经打开了一个完整的测试代码。您可以随意使用它。

    您不需要手动设置量化统计信息

    你试过培训后的量化教程吗

    基本上,他们设置了量化选项:

    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    converter.inference_input_type = tf.uint8
    converter.inference_output_type = tf.uint8
    
    然后,他们将“代表性数据集”传递给转换器,以便转换器可以运行模型几批,以收集必要的统计信息:

    def representative_data_gen():
      for input_value in mnist_ds.take(100):
        yield [input_value]
    
    converter.representative_dataset = representative_data_gen
    

    虽然有量化训练的选项,但进行训练后量化总是比较容易。

    稍后可能会解释这些选项,但根据我的经验,后量化不是很好,只能用于在量化后查看模型的性能。为了最大限度地利用量化例程,您需要执行量化感知训练。@FalconUA:我以为我执行了量化感知训练(参见github链接)。如果你决定写一个答案,也许你可以解释训练后量化和量化感知训练之间的主要区别,因为我对这个问题还不熟悉。那太好了!请参阅此示例可能会有所帮助:请参阅以了解均值和STDEV的解释