Search code examples
tensorflowobject-detectionyolodarknet

How to convert YOLOv4-CSP darknet weight to Tensorflow format?


How to convert YOLOv4-CSP darknet weights to Tensorflow (tf) format?

I have tried using this repo but it didn't work.

I had this error message:

Traceback (most recent call last):
  File "save_model.py", line 58, in <module>
    app.run(main)
  File "C:\Python37\lib\site-packages\absl\app.py", line 303, in run
    _run_main(main, args)
  File "C:\Python37\lib\site-packages\absl\app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "save_model.py", line 54, in main
    save_tf()
  File "save_model.py", line 49, in save_tf
    utils.load_weights(model, FLAGS.weights, FLAGS.model, FLAGS.tiny)
  File "D:\swap\20210319\tensorflow-yolov4-tflite\core\utils.py", line 63, in load_weights
    conv_weights = conv_weights.reshape(conv_shape).transpose([2, 3, 1, 0])
ValueError: cannot reshape array of size 3791890 into shape (1024,512,3,3)

Solution

  • The repository that you are using doesn't support conversion of Scaled YoloV4 or Yolov4-csp yet. It's still a feature request according to this issue

    There's luckily a workaround. I found this repository that does the same thing, only difference being it converts the model to .h5 (keras format) before converting into tensorflow format. This also supports yolov4-csp.

    I made a Google Colab notebook that does the conversion, which can be found here.