将Keras模型转换为可在Edge TPU上使用的量化Tensorflow Lite模型 [英] Convert Keras model to quantized Tensorflow Lite model that can be used on Edge TPU

查看:465
本文介绍了将Keras模型转换为可在Edge TPU上使用的量化Tensorflow Lite模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Keras model that I want to run on the Coral Edge TPU device. To do this, it needs to be a Tensorflow Lite model with full integer quantization. I was able to convert the model to a TFLite model:

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 converted_model.tflite时,出现此错误:

But when I run edgetpu_compiler converted_model.tflite, I get this error:

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

这是因为我需要对模型进行量化,但是我不确定如何做到这一点.我发现此页面,该页面告诉我如何执行此操作,但它需要我制作一个输入数据生成器.这是它提供的示例:

This is because I need to quantize the model, but I'm not sure how to do that. I found this page which tells me how to do this, but it wants me to make an input data generator. This is the example it provides:

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的引用,但已弃用了它)

How can I adapt this code to work with my input data? Where does num_calibration_steps come from? Is there a better way to do this? (I saw references to tf.contrib.tpu.keras_to_tpu_model but it has been deprecated)

推荐答案

我相信num_calibration_steps只是转换器使用您的代表集确定量化级别的次数.只是一个猜测,但它可能会从您的代表集中多次采样(自举或大刀阔斧).我仍在亲自调查整个过程,但是如果我仅将每个产量传递给单个图像,并使用num_calibration_steps约100倍(例如100张代表性图像),这似乎对我有用.您可以在github 上看到我的演示脚本.

I believe num_calibration_steps is just the number of times the converter uses your rep set to determine the quantization levels. Just a guess, but maybe it subsamples from your rep set multiple times (bootstrapping or jackknifing). I'm still investigating the whole process myself, but it seems to work for me if I just pass it a single image for each yield, and use num_calibration_steps at about 100 (e.g., 100 representative images). You can see my demo script 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]

也可以看到我对这个问题的类似回答: Keras模型的训练后全整数量化

Also see my similar response to this question: Post-training full integer quantization of Keras model

这篇关于将Keras模型转换为可在Edge TPU上使用的量化Tensorflow Lite模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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