我已经使用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
发布于 2021-04-07 19:13:32
首先,我建议使用最新的TF版本,例如,2.4.1或夜间版本进行转换,这在TFLite转换中有很多改进。
以上yolov4 github也需要tf 2.3.0或更高版本。
如果您在升级TF版本后仍有创建int8量化模型的经验。请分享您收到的错误消息的更多详细信息。
发布于 2021-04-08 13:09:52
我建议您首先使用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
https://stackoverflow.com/questions/66984379
复制相似问题