Search code examples
pythontensorflowobject-detectiontensoryolo

How to convert YOLOv4 Darknet Weights to Tensorflow format if you trained with custom anchors?


Main Question:

What changes should I do to the repo's source code to successfully convert my YOLOv4 darknet weight (with custom anchors) to Tensorflow format?

Background:

I used this repo to convert my YOLOv4 darknet weights to Tensorflow format.

I have trained YOLOv4 on a custom dataset using custom anchors (9 anchors) but the number of anchors I used per [yolo] layer is 4, 3, 2, respectively. By default, YOLOv4 uses 3 anchors each [yolo] layer.

Main Problem:

The repo I used is coded in a way that only considers the default anchors, where there are 3 anchors each [yolo] layer.

What I tried to do to solve the main problem:

  • I have tried to do some changes to the source code, which are summarized in this link.
  • I used the code below to attempt converting the darknet weight to tf format. Here is the log of the conversion process.

python save_model.py --weights data/yolov4-512.weights --output ./checkpoints/yolov4-512 --input_size 512 --model yolov4

  • I tested the resulting tf model using the code: python detect.py --weights checkpoints/yolov4-512 --size 512 --model yolov4 --image data/pear.jpg . The process failed and the error can be seen below. I have seen possible problems here but I don't know how to solve them.
2021-03-19 15:05:03.694379: W tensorflow/core/common_runtime/bfc_allocator.cc:312] Garbage collection: deallocate free memory regions (i.e., allocations) so that we can re-allocate a larger region to avoid OOM due to memory fragmentation. If you see this message frequently, you are running near the threshold of the available device memory and re-allocation may incur great performance overhead. You may try smaller batch sizes to observe the performance impact. Set TF_ENABLE_GPU_GARBAGE_COLLECTION=false if you'd like to disable this feature.
    Traceback (most recent call last):
      File "detect.py", line 90, 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 "detect.py", line 66, in main
        pred_bbox = infer(batch_data)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 1655, in __call__
        return self._call_impl(args, kwargs)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 1673, in _call_impl
        return self._call_with_flat_signature(args, kwargs, cancellation_manager)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 1722, in _call_with_flat_signature
        return self._call_flat(args, self.captured_inputs, cancellation_manager)
      File "C:\Python37\lib\site-packages\tensorflow\python\saved_model\load.py", line 106, in _call_flat
        cancellation_manager)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 1924, in _call_flat
        ctx, args, cancellation_manager=cancellation_manager))
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 550, in call
        ctx=ctx)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
        inputs, attrs, num_outputs)
    tensorflow.python.framework.errors_impl.InvalidArgumentError:  Input to reshape is a tensor with 98304 values, but the requested shape has 73728
             [[{{node StatefulPartitionedCall/functional_1/tf_op_layer_Reshape/Reshape}}]] [Op:__inference_signature_wrapper_5589]
    
    Function call stack:
    signature_wrapper

Solution

  • I posted an answer to one of your earlier question about YoloV4 (CSP) conversion. Did you try and see if that worked?

    If that worked, you can try to use your own config file and weights in the convert.py command in the notebook and see if it works