tensorflow 如何量化除特定节点之外的所有节点?

iqxoj9l9  于 2022-12-04  发布在  其他
关注(0)|答案(2)|浏览(101)

我正在使用tensorflow图形转换工具来量化图形,使用

input_names = ["prefix/input"]
output_names = ["final_result"]

transforms1 = ["strip_unused_nodes","fold_constants(ignore_errors=true)",  "fold_batch_norms",  "fold_old_batch_norms","quantize_weights" ]

transformed_graph_def = TransformGraph(graph.as_graph_def(), input_names,output_names, transforms1)

我使用选项quantize_weights来量化图中的权重,我知道通过改变quantize_weights中的阈值minimum_size,某些节点可以保持未量化,因此保留一些节点未量化当然是可能的。
我想量化所有节点的权重,除了一个名字为K的特定节点或一组在K(集合)中有名字的节点。

xmakbtuz

xmakbtuz1#

EDIT: the previous answer refered to Tensorflow Lite code. I updated it to refer to Tensorflow.
Looking at the implementation of Tensorflow's quantize_weights , these are the instances where weights don't get quantized:

  1. tensor that is not type float
  2. tensor that has fewer than 1024 weights (or another number specified by the parameter minimum_size )
    If you are able to modify nodes in the graph so that they are excluded by one of the above rules, then quantize, then revert the nodes to the pre-quantized state, you might be able to do this.
kmb7vmvb

kmb7vmvb2#

若要从量化中排除特定节点或节点集,可以使用quantize_weights变换的op_types参数。此参数允许您指定应量化的节点类型列表。默认情况下,将量化所有节点类型,但您可以通过提供不包括要排除的节点类型的列表来排除特定节点或节点集。
例如,如果要从量化中排除名称为“K”的节点,可以使用以下代码:

transforms1 = ["strip_unused_nodes","fold_constants(ignore_errors=true)",  "fold_batch_norms",  "fold_old_batch_norms", "quantize_weights(op_types=['!K'])" ]
transformed_graph_def = TransformGraph(graph.as_graph_def(), input_names,output_names, transforms1)

这将从量化中排除具有名称“K”的任何节点,同时仍量化图中的所有其它节点。
或者,如果要排除多个节点类型,可以在如下列表中指定它们:

transforms1 = ["strip_unused_nodes","fold_constants(ignore_errors=true)",  "fold_batch_norms",  "fold_old_batch_norms", "quantize_weights(op_types=['!K1', '!K2', '!K3'])" ]
transformed_graph_def = TransformGraph(graph.as_graph_def(), input_names,output_names, transforms1)

这将从量化中排除具有名称“K1”、“K2”或“K3”的任何节点,同时仍量化图中的所有其它节点。
请注意,op_types列表中节点名称前面的“!”字符表示这些节点类型应该从量化中排除。如果省略“!",则默认情况下将量化所有节点类型,并且需要明确排除任何不希望量化的节点类型。

相关问题