AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.65k stars 7.96k forks source link

Saving yolo-v3 weights using python/pytorch in .weights format after pruning #3248

Open Sudhakar17 opened 5 years ago

Sudhakar17 commented 5 years ago

https://github.com/talebolano/yolov3-network-slimming Implemented in PyTorch. It uses the yolo-v3 weights from the darknet and retrained the model by following l-1 norm of BN weights. (not important info at the moment)

I am using this code to train the yolo-v3 by sparsity approach. After training the model, it saves the weight into ".weights" file. As we know that in pytorch, the weights can be written in numpy format and save into the file.

My question is that pruned_yolo_v3.weights are not using the GPU to calculate the mAP in the darknet but it works with the CPU and it is dead slow.

I checked the original code in darknet framework, I don't understand the part "cuda_set_device". Is this one impacts the model of the weight to run on GPU?

darknet code

void save_weights_upto(network net, char *filename, int cutoff)
{
#ifdef GPU
    if(net.gpu_index >= 0){
        cuda_set_device(net.gpu_index);
    }
#endif
    fprintf(stderr, "Saving weights to %s\n", filename);
    FILE *fp = fopen(filename, "wb");
    if(!fp) file_error(filename);

    int major = 0;
    int minor = 1;
    int revision = 0;
    fwrite(&major, sizeof(int), 1, fp);
    fwrite(&minor, sizeof(int), 1, fp);
    fwrite(&revision, sizeof(int), 1, fp);
    fwrite(net.seen, sizeof(int), 1, fp);

 --------------------
}

Additional Question:

  1. If you know any way to convert the tensorflow, pytorch, keras model to write as a .weights file, please share the method here. I found the links to convert the model in the other way around. (.weights --> .pb/.h5 model)

@AlexeyAB

AlexeyAB commented 5 years ago

@Sudhakar17 Hi,

I checked the original code in darknet framework, I don't understand the part "cuda_set_device". Is this one impacts the model of the weight to run on GPU?

weights-file can't affect on will be used GPU or not.

If you compiled Darknet with GPU=1 CUDNN=1 in the Makefile, then it will use GPU in any cases.


You can try to convert weights -> pt -> weights by using this way: https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-495905317 and this repository: https://github.com/ultralytics/yolov3