为 TFliteconverter 创建代表性数据集的正确方法是什么? [英] What is the correct way to create representative dataset for TFliteconverter?

查看:26
本文介绍了为 TFliteconverter 创建代表性数据集的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 INT8 权重和激活来推断 tinyYOLO-V2.我可以使用 TFliteConverter 将权重转换为 INT8.对于 INT8 激活,我必须给出代表性数据集来估计比例因子.我创建此类数据集的方法似乎是错误的.

正确的程序是什么?

def rep_data_gen():一个 = []对于我在范围内(160):inst = anns[i]文件名 = inst['文件名']img = cv2.imread(img_dir + file_name)img = cv2.resize(img, (NORM_H, NORM_W))图像 = 图像/255.0img = img.astype('float32')a.append(img)a = np.array(a)print(a.shape) # a 是 160 个 3D 图像的 np 数组img = tf.data.Dataset.from_tensor_slices(a).batch(1)对于我在 img.take(BATCH_SIZE):打印(一)产量 [i]# https://www.tensorflow.org/lite/performance/post_training_quantization转换器 = tf.lite.TFLiteConverter.from_keras_model_file("./yolo.h5")converter.optimizations = [tf.lite.Optimize.DEFAULT]converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]转换器.inference_input_type = [tf.int8]转换器.inference_output_type = [tf.int8]converter.representative_dataset=rep_data_gentflite_quant_model = converter.convert()

<块引用>

ValueError: 无法设置张量:得到了 STRING 类型的张量,但输入 27 的预期类型为 FLOAT32,名称:input_1

解决方案

我使用你的代码读取数据集,发现错误:

img = img.astype('float32') 应该是

img = img.astype(np.float32)

希望对你有帮助

I am trying to infer tinyYOLO-V2 with INT8 weights and activation. I can convert the weights to INT8 with TFliteConverter. For INT8 activation, I have to give representative dataset to estimate the scaling factor. My method of creating such dataset seems wrong.

What is the correct procedure ?

def rep_data_gen():
    a = []
    for i in range(160):
        inst = anns[i]
        file_name = inst['filename']
        img = cv2.imread(img_dir + file_name)
        img = cv2.resize(img, (NORM_H, NORM_W))
        img = img / 255.0
        img = img.astype('float32')
        a.append(img)
    a = np.array(a)
    print(a.shape) # a is np array of 160 3D images
    img = tf.data.Dataset.from_tensor_slices(a).batch(1)
    for i in img.take(BATCH_SIZE):
        print(i)
        yield [i]
# https://www.tensorflow.org/lite/performance/post_training_quantization
converter = tf.lite.TFLiteConverter.from_keras_model_file("./yolo.h5")
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = [tf.int8]
converter.inference_output_type = [tf.int8]
converter.representative_dataset=rep_data_gen
tflite_quant_model = converter.convert()

ValueError: Cannot set tensor: Got tensor of type STRING but expected type FLOAT32 for input 27, name: input_1

解决方案

I used your code for reading in a dataset and found the error:

img = img.astype('float32') should be

img = img.astype(np.float32)

Hope this helps

这篇关于为 TFliteconverter 创建代表性数据集的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆