Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Sudhakar17 opened this issue May 27, 2019 · 1 comment

Comments

@Sudhakar17
Copy link

Sudhakar17 commented May 27, 2019

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
Copy link
Owner

@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: #2914 (comment)
and this repository: https://github.com/ultralytics/yolov3

  • Darknet -> Pytorch: python3 -c "from models import *; convert('cfg/yolov3-spp-sm2-1cls.cfg', '../darknet/backup/yolov3-spp-sm2-1cls_5000.weights')"

  • Pytroch -> Darknet: python3 -c "from models import *; convert('cfg/yolov3-spp-sm2-1cls.cfg', 'converted.pt')"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants