gpt4 book ai didi

python - 将 Keras 模型转换为可在 Edge TPU 上使用的量化 Tensorflow Lite 模型

转载 作者:太空宇宙 更新时间:2023-11-03 20:21:42 26 4
gpt4 key购买 nike

我有一个 Keras 模型,想要在 Coral Edge TPU 设备上运行。为此,它需要是具有完整整数量化的 Tensorflow Lite 模型。我能够将模型转换为 TFLite 模型:

model.save('keras_model.h5')

converter = tf.lite.TFLiteConverter.from_keras_model_file("keras_model.h5")
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

但是当我运行 edgetpu_compiler returned_model.tflite 时,我收到此错误:

Edge TPU Compiler version 2.0.267685300
Invalid model: converted_model.tflite
Model not quantized

这是因为我需要量化模型,但我不知道该怎么做。我发现this page它告诉我如何做到这一点,但它希望我制作一个输入数据生成器。这是它提供的示例:

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 [input]

如何调整此代码以处理我的输入数据? num_calibration_steps 从哪里来?有一个更好的方法吗? (我看到了对 tf.contrib.tpu.keras_to_tpu_model 的引用,但它已被弃用)

最佳答案

我相信num_calibration_steps只是转换器使用您的代表集来确定量化级别的次数。只是一个猜测,但也许它会多次从您的代表集中进行子采样(引导或折叠)。我仍在亲自研究整个过程,但如果我只为每个产量传递一个图像,并在大约 100 个(例如 100 个代表性图像)时使用 num_calibration_steps ,它似乎对我有用。你可以看我的演示脚本on github .

关键部分是:

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]

另请参阅我对此问题的类似回答: Post-training full integer quantization of Keras model

关于python - 将 Keras 模型转换为可在 Edge TPU 上使用的量化 Tensorflow Lite 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58123573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com