eriklindernoren / PyTorch-YOLOv3

Minimal PyTorch implementation of YOLOv3
GNU General Public License v3.0
7.29k stars 2.64k forks source link

Fix problematic header read/write for *.weights file #842

Open zjuyzj opened 10 months ago

zjuyzj commented 10 months ago

Proposed changes

In the latest version 1.8.0, Darknet.save_darknet_weights() generates a problematic *.weights file, in which "minor" field is always 0, however the length of "seen" field is 8 bytes.

This may cause abnormal offset calculation when try to use official Darknet or other third-party Darknet-compatible repository (e.g. rknn-toolkit by Rockchip) to load the exported *.weights file, corrupting the whole weights and biases behind the header, then leading to totally wrong inference!

Reference to the read and write of *.weights header, from Darknet's official implementation:

Related issues

Resolves #292, and all other problems likewise caused by wrong exported *.weights are totally resolved.

However, saved module state_dict (.pth file) does not contain the value of "seen" class member, which is 0 by default. Convert a .pth to .weights will fill the "seen" field zero, losing the original "seen" from any pretrained .weights, which is just a little problem and not related to inference BTW.

Necessary checks