我正在尝试推断tinyYOLO-V2
with INT8
权重和激活。我可以使用 TFliteConverter 将权重转换为 INT8。为了INT8
激活,我必须提供代表性数据集来估计缩放因子。我创建此类数据集的方法似乎是错误的。
正确的程序是什么?
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:无法设置张量:获得 STRING 类型的张量,但输入 27 的预期类型为 FLOAT32,名称:input_1
我使用您的代码读取数据集并发现错误:
img = img.astype('float32') 应该是
img = img.astype(np.float32)
希望这可以帮助
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)