[Python] How can I convert yolo weights to tflite file

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página