本文介绍了训练后的Keras模型全整数量化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Keras模型进行训练后的完整8位量化,以编译并部署到EdgeTPU.我有一个保存为.h5文件的训练有素的Keras模型,正在尝试按照此处指定的步骤进行操作: https://coral.withgoogle.com/docs/edgetpu/models-intro/,以部署到Coral开发委员会.

I'm trying to do post-training full 8-bit quantization of a Keras model to compile and deploy to EdgeTPU.I have a trained Keras model saved as .h5 file, and am trying to go through the steps as specified here: https://coral.withgoogle.com/docs/edgetpu/models-intro/, for deployment to the Coral Dev Board.

我正在按照以下量化说明进行操作: https://www.tensorflow. org/lite/performance/post_training_quantization#full_integer_quantization_of_weights_and_activations )

I'm following these instructions for quantization: https://www.tensorflow.org/lite/performance/post_training_quantization#full_integer_quantization_of_weights_and_activations)

我正在尝试使用以下代码:

I’m trying to use the following code:

import tensorflow as tf

num_calibration_steps = 100
def representative_dataset_gen():
  for _ in range(num_calibration_steps):
    # Get sample input data as a numpy array in a method of your choosing.
    yield [X_train_quant_conv]

converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file('/tmp/classNN_simple.h5')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
converter.representative_dataset = representative_dataset_gen
tflite_full_integer_quant_model = converter.convert()

其中X_train_quant_conv是我的训练数据的子集,转换为np.array且类型为np.float32

where X_train_quant_conv is a subset of my training data converted to np.array and of type np.float32

运行这段代码时,出现以下错误:ValueError: Cannot set tensor: Dimension mismatch

When running this piece of code, I get the following error:ValueError: Cannot set tensor: Dimension mismatch

我尝试以不同的方式更改功能representative_dataset_gen(),但是每次遇到新错误时,我都会尝试.我不确定该功能应该如何.我也不确定num_calibration_steps should have的值是什么.

I’ve tried changing the function representative_dataset_gen() in different ways, but every time I get a new error. I’m not sure how this function should be. I’m also in doubt of what value num_calibration_steps should have.

任何建议或工作示例都非常感谢.

Any suggestions or working examples are very appreciated.

此问题与以下已回答的问题非常相似:

This question is very similar to this answered question: Convert Keras model to quantized Tensorflow Lite model that can be used on Edge TPU

推荐答案

您可能想看一下我的演示脚本进行量化,在github上.

You might want to look at my demo script for quantization, on github.

这只是一个猜测,因为我看不到X_train_quant_conv的真正含义,但是在我的工作演示中,我一次在representative_dataset_gen()中生成一张图像(即时生成随机数据).图像以大小为1的批次存储(例如,对于我的52x52x32图像,张量形状为(1、56、56、32).彩色图像有32个通道,尽管通常只有3个通道.我认为representative_dataset_gen()必须产生一个包含张量(或多个?)的列表,其第一维的长度为1.

It's just a guess since I can't see what X_train_quant_conv really is, but in my working demo, I yield one image at a time (random data created on the fly, in my case) in representative_dataset_gen(). The image is stored as batch of size 1 (e.g., tensor shape is (1, 56, 56, 32) for my 52x52x32 image). There are 32 channels, though there would typically just be 3, for a color image. I think representative_dataset_gen() has to yield a list containing a tensor (or more than one?) for which the first dimension is of length 1.

image_shape = (56, 56, 32)

def representative_dataset_gen():
    num_calibration_images = 10
    for i in range(num_calibration_images):
        image = tf.random.normal([1] + list(image_shape))
        yield [image]

这篇关于训练后的Keras模型全整数量化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:55