How can I convert yolo weights to tflite file

不羁的心 提交于 2021-01-28 05:19:27

问题


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

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

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

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)

But this error occurs.


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]'.

How can I fix it?


our model summary is

Model: "model_1"


Layer (type) Output Shape Param # Connected to

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


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


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


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


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


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


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


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


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


. . . .


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


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


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


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


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


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


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


conv2d_75 (Conv2D) (None, None, None, 2 65535 leaky_re_lu_72[0][0]

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



回答1:


I see that you're getting the H5 file of the Keras YOLO model. For TFLite models, you'll require a model that has a definite input shape like ( 256 , 256 , 3 ). Also, for an H5 model, you can't modify the input shape after the model is saved. So, you can take these measures,

  • Go to the file where the model and its layers are instantiated, modify the input shape there. Retrain the model using that fixed input size.
  • Use the TFLite Object Detection API. This has an Android sample app too.
  • Train the model using DarkNet as mentioned here.
  • If you're trying to detect one of the 20 classes present in the PASCAL, use this model.



回答2:


I would recommend doing so:

  1. Convert Darknet weights (.weights) to TensorFlow frozen graph format (.pb).
  2. Convert this .pb file to tflite.

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



来源:https://stackoverflow.com/questions/61585139/how-can-i-convert-yolo-weights-to-tflite-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!