首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于珊瑚开发板(coral.ai)的.pb到tflite int8的转换问题

用于珊瑚开发板(coral.ai)的.pb到tflite int8的转换问题
EN

Stack Overflow用户
提问于 2021-04-07 18:40:23
回答 2查看 289关注 0票数 0

我已经使用yolov4 darkenet Alexyab训练了目标检测模型,然后使用此存储库https://github.com/hunglc007/tensorflow-yolov4-tflite将生成的权重文件转换为tensorflow lite,我能够将yolo权重转换为pb和tflite float16,但使用此存储库转换为tflite int8时存在问题。我尝试了很多东西来把它转换成tflite int8。

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

版本: tensorflow 1.15 python版本3.6.9 CUDA版本V9.1.85

EN

回答 2

Stack Overflow用户

发布于 2021-04-07 19:13:32

首先,我建议使用最新的TF版本,例如,2.4.1或夜间版本进行转换,这在TFLite转换中有很多改进。

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

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

票数 0
EN

Stack Overflow用户

发布于 2021-04-08 13:09:52

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

一旦有了.h5权重,就可以使用此代码将keras模型转换为tflite。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66984379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档