You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

量化TFLite Model Maker音频模型时SLICE节点报错的解决请求

问题描述

按照TensorFlow官方示例《Retrain a speech recognition model with TensorFlow Lite Model Maker》训练的语音识别模型可正常推理,但在将其量化为int8格式适配ESP32时触发错误。

量化代码1(单独转换脚本)

import tensorflow as tf
from tflite_model_maker import audio_classifier

spec = audio_classifier.BrowserFftSpec()

rep_dir = './dataset-rep' # 包含约100个音频样本的精简数据集
rep_data = audio_classifier.DataLoader.from_folder(
      spec, rep_dir, cache=False)

def representative_dataset():
      representative_dataset = rep_data.gen_dataset()
      for audio, label in representative_dataset:
            yield [audio]


converter = tf.lite.TFLiteConverter.from_saved_model("/path/to/saved_model")
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]

converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8

tflite_model = converter.convert()


with open ('./models/quantModel.tflite', 'wb') as f:
    f.write(tflite_model)

错误信息

运行converter.convert()时触发:

RuntimeError: Invalid begin and size.Node number 13 (SLICE) failed to invoke.

完整报错栈:

Traceback (most recent call last):
  File "./model_conversion.py", line 54, in <module>
    tflite_model = converter.convert()
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/lite.py", line 933, in wrapper
    return self._convert_and_export_metrics(convert_func, *args, **kwargs)
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/lite.py", line 911, in _convert_and_export_metrics
    result = convert_func(self, *args, **kwargs)
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/lite.py", line 1216, in convert
    return self._convert_from_saved_model(graph_def)
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/lite.py", line 1100, in _convert_from_saved_model
    return self._optimize_tflite_model(
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/convert_phase.py", line 215, in wrapper
    raise error from None  # Re-throws the exception.
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/convert_phase.py", line 205, in wrapper
    return func(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/lite.py", line 871, in _optimize_tflite_model
    model = self._quantize(model, q_in_type, q_out_type, q_activations_type,
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/lite.py", line 613, in _quantize
    calibrated = calibrate_quantize.calibrate(
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/convert_phase.py", line 215, in wrapper
    raise error from None  # Re-throws the exception.
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/convert_phase.py", line 205, in wrapper
    return func(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/optimize/calibrator.py", line 226, in calibrate
    self._feed_tensors(dataset_gen, resize_input=True)
  File "/home/user/miniconda3/lib/python3.9/site-packages/tensorflow/lite/python/optimize/calibrator.py", line 138, in _feed_tensors
    self._calibrator.FeedTensor(input_array)
RuntimeError: Invalid begin and size.Node number 13 (SLICE) failed to invoke.

量化代码2(训练后直接导出)

... # 模型训练代码
quantization_config = QuantizationConfig.for_int8(rep_data) # 直接传入原始数据而非representative_dataset()
model.export(SAVE_PATH, tflite_filename=QUANT_FILENAME, quantization_config = quantization_config)

同样触发类似错误,仅节点编号变为11。

环境信息

WSL下conda环境:

  • Python 3.9.16
  • tflite-model-maker 0.4.2
  • absl-py 1.4.0

错误原因分析

这个错误的核心是校准阶段喂入的音频数据维度与模型预期不匹配,导致SLICE算子无法正确执行切片操作:

  • 使用DataLoader.gen_dataset()生成的数据默认包含批量维度(例如(batch_size, feature_length)),但TFLite校准器期望单样本输入(无批量维度,即(feature_length,))。
  • 旧版本的tflite-model-maker(0.4.2)在处理音频数据的代表性数据集时,存在维度处理bug,未自动去除批量维度。
解决方法

方法1:修正代表性数据集的输出维度

在representative_dataset函数中手动去除音频数据的批量维度:

def representative_dataset():
    for audio, label in rep_data.gen_dataset():
        # 去除批量维度,从(batch_size, ...)转为(...)
        yield [tf.squeeze(audio, axis=0)]

方法2:升级tflite-model-maker到最新稳定版

旧版本(0.4.2)存在音频量化的已知bug,升级到0.5.0及以上版本可修复该问题:

pip install --upgrade tflite-model-maker

升级后,无论是使用QuantizationConfig.for_int8还是手动构建转换器,都能自动处理数据维度,无需额外修改代码。

方法3:手动构造无批量的代表性数据集

如果无法升级包,可直接遍历数据集中的单样本,避免批量维度:

def representative_dataset():
    for sample in rep_data:
        # sample[0]是单样本音频数据,无批量维度
        yield [sample[0]]

内容的提问来源于stack exchange,提问作者Zino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 12:15:15