1

我已经训练了 resnet50v2 和 densenet 169 模型。TensorFlow 每晚 2.3.0-dev20200608。该模型运行良好,我尝试了一些优化,例如“简单”tf lite、tf lite 动态范围、tf lite 16float,它们都运行良好(精度与原始模型相同或略低于预期)。我想将我的模型转换为使用 uint8 的全整数训练后量化。我将模型从 SavedModel 格式转换为:

converter = tf.lite.TFLiteConverter.from_saved_model('/path/to/my/saved_models')
converter.optimizations = [tf.lite.Optimize.DEFAULT]
def representative_dataset_gen():
    for i in range(100):
        yield [x_train[i].astype(np.float32)]
converter.representative_dataset = representative_data_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()

with open('resnet.tflite', 'wb') as f:
  f.write(tflite_model)

正如 TensorFlow lite 网站中所写。然后我编译了边缘 tpu 的模型。它可以工作,因为 edge tpu 允许我在没有错误的情况下运行它,但结果是乱码。它总是预测相同的值。然后我尝试在 cpu 上使用 tf lite 解释器。输入/输出张量是正确的 uint8,但它再次预测相同的值。在带有 tf lite 的 cpu 上,问题仍然存在移动到 int8。有没有其他人遇到过同样的问题?

请在此处找到一个 Google 文件夹,其中包含我用于转换的代码、转换前后的模型以及转换后的模型。

https://drive.google.com/drive/folders/11XruNeJzdIm9DTn7FnuIWYaSalqg2F0B?usp=sharing

4

0 回答 0