运行整数量化时出现TFLiteConverter分段错误

时间:2020-08-20 23:31:02

标签: tensorflow tensorflow-lite quantization

我正在使用tensorflow == 1.15.3,并且遇到了尝试int8 post-training quantization的分段错误。可以在here中找到有关TFLiteConverter 1.15版本的文档。

我找到了a similar issue on github,但是他们提供--add_postprocessing_op=true的解决方案并没有解决分段错误。

我已经使用PDB对其进行了调试,并准确找到了崩溃的位置。它永远不会到达我的representative_dataset函数。运行CreateWrapperCPPFromBuffer(model_content)时出错:

> .../python3.6/site-packages/tensorflow_core/lite/python/optimize/calibrator.py(51)__init__()
-> .CreateWrapperCPPFromBuffer(model_content))
(Pdb) s
Fatal Python error: Segmentation fault

Current thread 0x00007ff40ee9f740 (most recent call first):
  File ".../python3.6/site-packages/tensorflow_core/lite/python/optimize/calibrator.py", line 51 in __init__
  File ".../python3.6/site-packages/tensorflow_core/lite/python/lite.py", line 236 in _calibrate_quantize_model
  File ".../python3.6/site-packages/tensorflow_core/lite/python/lite.py", line 993 in convert
  File ".../convert_model_to_tflite_int8.py", line 97 in <module>
  File "<string>", line 1 in <module>
  File "/usr/lib/python3.6/bdb.py", line 434 in run
  File "/usr/lib/python3.6/pdb.py", line 1548 in _runscript
  File "/usr/lib/python3.6/pdb.py", line 1667 in main
  File "/usr/lib/python3.6/pdb.py", line 1694 in <module>
  File "/usr/lib/python3.6/runpy.py", line 85 in _run_code
  File "/usr/lib/python3.6/runpy.py", line 193 in _run_module_as_main
[1]    17668 segmentation fault (core dumped)  python -m pdb convert_model_to_tflite_int8.py  --add_postprocessing_op=true

这是我的转换代码:

converter = tf.lite.TFLiteConverter.from_frozen_graph(
  graph_def_file=pb_model_path,
  input_arrays=["device_0/input_node_name:1"],
  output_arrays=["device_0/output_node_name"],
  input_shapes={"device_0/input_node_name:1": [100, 16384]}
)
converter.allow_custom_ops = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8

def test():
  pdb.set_trace()
  print(' ! ! ! representative_dataset_gen ! ! ! ')
  zeros = np.zeros(shape=(1, 100, 16384), dtype='int8')
  ds = tf.data.Dataset.from_tensor_slices((zeros)).batch(1)
  for input_value in ds.take(1):
    yield [input_value]
converter.representative_dataset = test

pdb.set_trace()
tflite_model = converter.convert()

tflite_model_size = open(model_name, 'wb').write(tflite_model)
print('TFLite Model is %d bytes' % tflite_model_size)

首先,我的模型转换适用于tf.float16(尽管那里没有使用representative_dataset)。

1 个答案:

答案 0 :(得分:0)

将我的tf版本升级到2.3解决了分段错误。我的模型代码尚未与tf == 2.x兼容,但幸运的是,转换代码与此无关,因此升级进行得很顺利。

相关问题