foss-xtensa / darknet2onnx

Converts Darknet YOLO models to PyTorch/ONNX

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Darknet to ONNX Converter for NeuroWeave SDK

# Folder Structure
.
├── LICENSE.txt
├── README.md
├── __ini__.py
├── config.py
├── darknet2onnx.py # Main file to execute the conversion from darknet to onnx
├── darknet2pytorch.py
├── region_loss.py
├── torch_utils.py
├── utils.py
├── yolo_layer.py

Conversion from Darknet 2 ONNX

Modifications made for compatiblity with NeuroWeave SDK

  • Removes the final post processing YOLO Head from the model
  • Makes the output as the feature outputs expected by NW-SDK
  • Expand operation over tensor used 6D tensors, which are not compatible with NW-SDK, hence replaced the custom implementation of upsample op with nn.Upsample official implementation in torch for upsampling the tensor.
  • Matches the output with Upsample_interpolate and Darkflow's op for upsample.
  • NW-SDK's post-processing subgraph uses image features as NHWC whereas pytorch results in NCHW features, added a transpose in the end to create NHWC outputs - this transpose should be optimized away in the compilation of the model

Steps

  1. Install all standard dependencies of XNNC 3.x which includes python 3.10 and pytorch 2.0
  2. Download weights and cfgs of YOLO v2/v3/v4 models (or their variants)
  3. Execute following command to execute conversion from darknet cfg/weights to onnx model
    • python darknet2onnx.py <path-to-cfg> <path-to-weights> --batch_size 1 --onnx_file_path <path-to-onnx-model>
    • For example: python darknet2onnx.py darknet/cfg/yolov3-spp.cfg darknet/weights/yolov3-spp.weights --batch_size 1 --onnx_file_path yolov3-spp.onnx

Reference & Citations

@misc{bochkovskiy2020yolov4,
      title={YOLOv4: Optimal Speed and Accuracy of Object Detection}, 
      author={Alexey Bochkovskiy and Chien-Yao Wang and Hong-Yuan Mark Liao},
      year={2020},
      eprint={2004.10934},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}
@InProceedings{Wang_2021_CVPR,
    author    = {Wang, Chien-Yao and Bochkovskiy, Alexey and Liao, Hong-Yuan Mark},
    title     = {{Scaled-YOLOv4}: Scaling Cross Stage Partial Network},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2021},
    pages     = {13029-13038}
}

About

Converts Darknet YOLO models to PyTorch/ONNX

License:Apache License 2.0


Languages

Language:Python 100.0%