我试图在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,20,128和1,19,19,128都不是broadcastable.Node编号68 (ADD),无法准备.
不确定我应该如何为该数据集执行量化,也不确定需要如何更新前面的代码。任何帮助都将不胜感激。
发布于 2022-04-09 11:23:20
解决了。事实上,问题是我将我的图像尺寸设置为300x300,而模型(SSD V2)需要320x320。
https://stackoverflow.com/questions/71803405
复制相似问题