Search code examples
computer-visionobject-detectionfpgayolodarknet

Approach to get the weight values from the pre-trained weights from Darknet?


I'm currently trying to implement YOLOv3 object detection model in C(only detection, not training).

I have tested my convolution method with arbitrary values and it seems to be working as I expected.

Before stacking up multiple method calls to do forward propagation, I thought it would be safe to test with the actual pretrained weight file data.

When I look up Darknet's pre-trained weight file, it was a huge chunk of binary files. I tried to convert it to hex and decimals, but it still doesn't look simple to pinpoint what part of values to use.

So, my question is, what should I do to extract the decimal numbers of the weights or the filter values so that I can use them in the same order of the forward propagation happening in YOLOv3?

*I'm currently trying to build my c version of YOLOv3 using the structure image shown in https://www.itread01.com/content/1541167345.html

*My c code will be run on an FPGA board called MicroZed, along with other HDL code.

*I tried to plug some printf functions into some places of Darknet code to see what kinds of data are moving around when YOLOv3 runs, however, when I ran it on in Linux terminal, it didn't show anything new and kept outputting the same results.

Any help or advice will be really appreciated. Thank you!


Solution

  • I am not too sure if there is a direct way to read darknet weights, but you can convert it into .h5 format and obtain the weight values from it

    You can convert the darknet yolov3 weights into .h5 format (used by keras) by using the appropriate command from this repository.

    You can choose the command based on your Yolo version from the list shown in the ReadMe of the linked repo. For the standard yolov3, the command for converting is

    python tools/model_converter/convert.py cfg/yolov3.cfg weights/yolov3.weights weights/yolov3.h5 Once you have the .h5weights, you can use the below code snippet for obtaining the values from the weights. credit/source

    import h5py
    
    path = "<path to weights>.h5"
    weights = {}
    keys = []
    with h5py.File(path, 'r') as f: # open file
      f.visit(keys.append) # append all keys to list
      for key in keys:
          if ':' in key: # contains data if ':' in key
              param_name = f[key].name
              weights[f[key].name] = f[key].value
              print(param_name,weights[f[key].name])