【问题标题】:Problem in conversion of .pb to tflite int8 for Coral Devboard (coral.ai)Coral Devboard (coral.ai) 将 .pb 转换为 tflite int8 的问题
【发布时间】:2021-07-03 03:59:01
【问题描述】:

我已经使用 yolov4 darkenet Alexyab 完成了对象检测模型的训练,然后使用此存储库将生成的权重文件转换为 tensorflow lite https://github.com/hunglc007/tensorflow-yolov4-tflite 并且我能够将 yolo 权重转换为 pb 和 tflite float16,但是使用此 repo 转换为 tflite int8 时存在问题。我尝试了很多方法将其转换为 tflite int8。

我现在正在使用需要 tflite int8 量化的珊瑚开发板,请指导我如何将 pb 权重文件转换为 tflite int8。

版本: 张量流 1.15 蟒蛇版本 3.6.9 CUDA版本V9.1.85

【问题讨论】:

    标签: object-detection tensorflow-lite object-detection-api google-coral yolov4


    【解决方案1】:

    首先,我推荐使用最新的TF版本,例如2.4.1或nightly版本进行转换,这对TFLite转换有很多改进。

    以上yolov4 github也需要tf 2.3.0以上版本。

    如果您在升级 TF 版本后仍有创建 int8 量化模型的经验。请分享您收到的错误消息的更多详细信息。

    【讨论】:

    • 我用的是tensorflow 1.15,因为较新版本的tensorflow只支持float量化,不支持int8量化。
    • 首先我是在 tensorflow 2.3.x 版本上完成的,并且能够将 .pb 文件转换为 float16,但是对于 int8 tflite 转换它是不成功的,所以我在虚拟环境中将 tensorflow 版本降级为 tf 1.15测试是否可以完成。
    • 好的,我也试试夜间版本。
    • 我在转换 tensorflow 2.3.0 时遇到问题
    【解决方案2】:

    我建议您首先使用 this 存储库将 yolov4 权重转换为 keras .h5 权重。

    获得.h5 权重后,您可以使用此代码将 keras 模型转换为 tflite。

    import tensorflow.compat.v1 as tf
    import numpy
    import cv2
    
    def representative_dataset_gen():
      num_calibration_steps = 100 # should be a subset of your dataset size
      imgs = []
      batch_size = 1
      for _ in range(num_calibration_steps):
        img = cv2.imread(img_file)
        img = img / 255.0
        img = img.astype(np.float32)
        imgs.append(img)
      
      imgs = np.array(imgs)
      images = tf.data.Dataset.from_tensor_slices(imgs).batch(1)
      for i in images.take(batch_size):
        yield [i]
    
    converter = tf.lite.TFLiteConverter.from_keras_model_file("path_to_h5_weights")
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
    converter.inference_input_type = tf.int8  # or tf.uint8
    converter.inference_output_type = tf.int8  # or tf.uint8
    converter.representative_dataset = representative_dataset_gen
    tflite_model = converter.convert()
    
    # Save the model.
    with open('model.tflite', 'wb') as f:
      f.write(tflite_model)
    

    来源 - Tensorflow Quantization

    tflite conversion

    generate representative dataset

    【讨论】:

    • 好的,我试试这个
    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多