我正在尝试使用 UINT8 量化,同时将 tensorflow 模型转换为 tflite 模型:
如果使用post_training_quantize = True,模型大小比原始 fp32 模型低 x4,所以我假设模型权重是 uint8,但是当我加载模型并通过interpreter_aligner.get_input_details()[0]['dtype']它的 float32获取输入类型时。量化模型的输出与原始模型大致相同。
converter = tf.contrib.lite.TFLiteConverter.from_frozen_graph(
graph_def_file='tflite-models/tf_model.pb',
input_arrays=input_node_names,
output_arrays=output_node_names)
converter.post_training_quantize = True
tflite_model = converter.convert()
Run Code Online (Sandbox Code Playgroud)
转换模型的输入/输出:
print(interpreter_aligner.get_input_details())
print(interpreter_aligner.get_output_details())
[{'name': 'input_1_1', 'index': 47, 'shape': array([ 1, 128, 128, 3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]
[{'name': 'global_average_pooling2d_1_1/Mean', 'index': 45, 'shape': array([ 1, 156], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]
Run Code Online (Sandbox Code Playgroud)
另一种选择是明确指定更多参数:模型大小比原始 fp32 模型低 x4,模型输入类型为 uint8,但模型输出更像是垃圾。
converter = tf.contrib.lite.TFLiteConverter.from_frozen_graph(
graph_def_file='tflite-models/tf_model.pb',
input_arrays=input_node_names,
output_arrays=output_node_names)
converter.post_training_quantize = True …Run Code Online (Sandbox Code Playgroud) python quantization deep-learning tensorflow tensorflow-lite