如何将yolo权重转换为tflite文件 [英] How can I convert yolo weights to tflite file

查看:861
本文介绍了如何将yolo权重转换为tflite文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在android中使用yolo权重,因此我计划将yolo权重文件转换为tflite文件.

I will use yolo weights in android so I plan to convert yolo weights file to tflite file.

我在anaconda提示符中使用此代码,因为我在环境中下载了keras库.

I use this code in anaconda prompt because I downloaded keras library in env.

activate env   
python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

最后做到了,将Keras模型保存到model_data/yolo.h5

Finally, it did.Saved Keras model to model_data/yolo.h5

然后我将使用此代码在jupyter笔记本中将此h5文件转换为tflite文件.

And I'm going to convert this h5 file to tflite file in jupyter notebook with this code.

model = tf.keras.models.load_model("./yolo/yolo.h5", compile=False)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
open("keras_model.tflite", "wb").write(tflite_model)

但是会发生此错误.

ValueError                                Traceback (most recent call last)

<ipython-input-3-964a59978091> in <module>()

  1 model = tf.keras.models.load_model("./yolo/yolo.h5", compile=False)

  2 converter = tf.lite.TFLiteConverter.from_keras_model(model)

----> 3 tflite_model = converter.convert()

  4 open("keras_model.tflite", "wb").write(tflite_model)



~\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\lite\python\lite.py in convert(self)

426         raise ValueError(

427             "None is only supported in the 1st dimension. Tensor '{0}' has "

--> 428             "invalid shape '{1}'.".format(_get_tensor_name(tensor), shape_list))

429       elif shape_list and shape_list[0] is None:

430         # Set the batch size to 1 if undefined.



ValueError: None is only supported in the 1st dimension. Tensor 'input_1' has invalid shape '[None, None, None, 3]'.

我该如何解决?

我们的模型摘要是

型号:"model_1"

Model: "model_1"

input_1(InputLayer)[(无,无,无,0

input_1 (InputLayer) [(None, None, None, 0

conv2d_1(Conv2D)(无,无,无,3 864 input_1 [0] [0]

conv2d_1 (Conv2D) (None, None, None, 3 864 input_1[0][0]

batch_normalization_1(BatchNor(无,无,无,3 128 conv2d_1 [0] [0]

batch_normalization_1 (BatchNor (None, None, None, 3 128 conv2d_1[0][0]

leaky_re_lu_1(LeakyReLU)(无,无,无,3 0 batch_normalization_1 [0] [0]

leaky_re_lu_1 (LeakyReLU) (None, None, None, 3 0 batch_normalization_1[0][0]

zero_padding2d_1(ZeroPadding2D(无,无,无,3 0 Leaky_re_lu_1 [0] [0]

zero_padding2d_1 (ZeroPadding2D (None, None, None, 3 0 leaky_re_lu_1[0][0]

conv2d_2(Conv2D)(无,无,无,6 18432 zero_padding2d_1 [0] [0]

conv2d_2 (Conv2D) (None, None, None, 6 18432 zero_padding2d_1[0][0]

batch_normalization_2(BatchNor(无,无,无,6 256 conv2d_2 [0] [0]

batch_normalization_2 (BatchNor (None, None, None, 6 256 conv2d_2[0][0]

leaky_re_lu_2(LeakyReLU)(无,无,无,6 0 batch_normalization_2 [0] [0]

leaky_re_lu_2 (LeakyReLU) (None, None, None, 6 0 batch_normalization_2[0][0]

conv2d_3(Conv2D)(无,无,无,3 2048 Leaky_re_lu_2 [0] [0]

conv2d_3 (Conv2D) (None, None, None, 3 2048 leaky_re_lu_2[0][0]

. . .

batch_normalization_65(BatchNo(无,无,无,5 2048 conv2d_66 [0] [0]

batch_normalization_65 (BatchNo (None, None, None, 5 2048 conv2d_66[0][0]

batch_normalization_72(BatchNo(无,无,无,2 1024 conv2d_74 [0] [0]

batch_normalization_72 (BatchNo (None, None, None, 2 1024 conv2d_74[0][0]

leaky_re_lu_58(LeakyReLU)(无,无,无,1 0 batch_normalization_58 [0] [0]

leaky_re_lu_58 (LeakyReLU) (None, None, None, 1 0 batch_normalization_58[0][0]

leaky_re_lu_65(LeakyReLU)(无,无,无,5 0 batch_normalization_65 [0] [0]

leaky_re_lu_65 (LeakyReLU) (None, None, None, 5 0 batch_normalization_65[0][0]

leaky_re_lu_72(LeakyReLU)(无,无,无,2 0 batch_normalization_72 [0] [0]

leaky_re_lu_72 (LeakyReLU) (None, None, None, 2 0 batch_normalization_72[0][0]

conv2d_59(Conv2D)(无,无,无,2 261375 lossy_re_lu_58 [0] [0]

conv2d_59 (Conv2D) (None, None, None, 2 261375 leaky_re_lu_58[0][0]

conv2d_67(Conv2D)(无,无,无,2 130815泄漏_re_lu_65 [0] [0]

conv2d_67 (Conv2D) (None, None, None, 2 130815 leaky_re_lu_65[0][0]

总参数:62,001,757 可训练的参数:61,949,149 不可训练的参数:52,608

Total params: 62,001,757 Trainable params: 61,949,149 Non-trainable params: 52,608

推荐答案

我建议这样做:

  1. 将Darknet权重(.weights)转换为TensorFlow冻结图格式(.pb).
  2. 将此.pb文件转换为tflite.
  1. Convert Darknet weights (.weights) to TensorFlow frozen graph format (.pb).
  2. Convert this .pb file to tflite.

此过程更简单.我已经记录了3-4种将Darknet转换为TensorFlow的方法.请在此处找到它们.

This process is simpler. I have documented some 3-4 methods to convert Darknet to TensorFlow. Please find them here.

这篇关于如何将yolo权重转换为tflite文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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