Tensorflow-训练后整数量化



我正试图对在Tensorflow 2.8.0中训练的模型执行训练后整数量化,遵循此处提到的说明并进行一些调整。我把我所有的图像都放在一个名为"的目录中customTF2/data/images";。不过,我无法想出如何生成量化所需的代表性数据集。官方文档以及在线找到的大多数示例都使用Tensorflow数据集,或者使用图像已经标记并拆分到相关子文件夹的数据集(我的项目不是这样(。下面是我的代码

def representative_data_gen():
test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
test_generator = test_datagen.flow_from_directory('customTF2/data', 
target_size=(300, 300), 
batch_size=1,
classes=['images'],
class_mode='categorical')
for ind in range(len(test_generator.filenames)):
img_with_label = test_generator.next()
yield [np.array(img_with_label[0], dtype=np.float32, ndmin=2)]

converter = tf.lite.TFLiteConverter.from_saved_model('customTF2/data/tflite/new/saved_model')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
# Ensure that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.target_spec.supported_types = [tf.int8]
# Set the input and output tensors to uint8 (APIs added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model_quant = converter.convert()

失败,错误为:给定的形状[1,20,0128]和[1,19,19128]不可广播。节点编号68(ADD(准备失败

不确定我应该如何为该数据集执行量化,也不确定以前的代码需要如何更新。如有任何帮助,我们将不胜感激。

已解决。事实上,问题是我将图像尺寸设置为300x300,而型号(SSD Mobilenet V2(需要320x320。

最新更新