pytorch-YOLOv4 icon indicating copy to clipboard operation
pytorch-YOLOv4 copied to clipboard

Darknet yolov4-csp to ONNX

Open frenky-strasak opened this issue 3 years ago • 8 comments

Hi, I try to convert yolov4-csp to ONNX by this command, but I get following result: predictions_onnx

Then I have realized that the script does not contain any logistic function. But when I add it to here: elif activation == 'logistic': model.add_module('logistic{0}'.format(conv_id), nn.Sigmoid()) I got results containing more wrong bboxes: predictions_onnx

What should I change in the code to have a working yolov4-csp onnx model?

frenky-strasak avatar Jul 02 '21 15:07 frenky-strasak

Hi Frenky & friends in Github, Thank for sharing the tool of "Darknet2ONNX". I also meet the same issue about using yolov4-csp onnx model, could u give me some suggestion?

  1. yolov4-csp onnx model used: predictions_onnx_0 4_0 6_csp

  2. yolov4 onnx model used: (seems better) predictions_onnx_0 4_0 6_street

chayitw avatar Jul 16 '21 10:07 chayitw

Hi Frenky & friends in Github, I use demo.py to observe the situation when just converting "yolov4-csp" into a Pytorch model, and there are many Bonding Boxs that have appeared in the results, so it should not be the isssue of converting a Pytorch into a ONNX model. ( I am not sure this is normal about the original yolov4-csp XD. But "converting a Pytorch into a ONNX model" may be OK. ) predictions

Thanks, -chayi

chayitw avatar Jul 22 '21 02:07 chayitw

Hi, it should be enough to change this line according to this answer. It works for me, but I have to explore more.

frenky-strasak avatar Jul 28 '21 07:07 frenky-strasak

Hi, I try to convert yolov4-csp to ONNX by this command, but I get following result: predictions_onnx

Then I have realized that the script does not contain any logistic function. But when I add it to here: elif activation == 'logistic': model.add_module('logistic{0}'.format(conv_id), nn.Sigmoid()) I got results containing more wrong bboxes: predictions_onnx

What should I change in the code to have a working yolov4-csp onnx model?

Add Sigmoid is correct, and next you need to change bxy, bwy, det_confs and cls_confs to

image

jackgao0323 avatar Aug 16 '21 10:08 jackgao0323

@jackgao0323 thanks. As far as det_confs and cls_confs, they should stay unchanged, right? det_confs = torch.sigmoid(det_confs) cls_confs = torch.sigmoid(cls_confs)

frenky-strasak avatar Aug 17 '21 07:08 frenky-strasak

@jackgao0323 thanks. As far as det_confs and cls_confs, they should stay unchanged, right? det_confs = torch.sigmoid(det_confs) cls_confs = torch.sigmoid(cls_confs)

No, det & cls they don't need the sigmoid. Just comment the four lines on the top, and add the two lines (bxy and bwh).

jackgao0323 avatar Aug 17 '21 14:08 jackgao0323

@jackgao0323 @frenky-strasak Thanks so much for the discussion above, the solution worked to improve the performance of yolov4-csp. However, I realized that there is still a discrepancy between the results from running inference in darknet and onnx. Can I check if the above equations are correct?

For example, shouldnt's bwh = torch.pow(torch.sigmoid(bwh) * 2, 2) based on the equation change from yolov4 to yolov4-csp? Also shouldn't we have torch.sigmoid in the bxy equation too?

***Actually I have changed it and the results are nonsensical again. @jackgao0323 would you mind explaining how you arrived at your equation or refer me to a resource?

Also is there a way to make my results exactly the same?

lpkoh avatar Jan 16 '22 05:01 lpkoh

@jackgao0323 @frenky-strasak Thanks so much for the discussion above, the solution worked to improve the performance of yolov4-csp. However, I realized that there is still a discrepancy between the results from running inference in darknet and onnx. Can I check if the above equations are correct?

For example, shouldnt's bwh = torch.pow(torch.sigmoid(bwh) * 2, 2) based on the equation change from yolov4 to yolov4-csp? Also shouldn't we have torch.sigmoid in the bxy equation too?

***Actually I have changed it and the results are nonsensical again. @jackgao0323 would you mind explaining how you arrived at your equation or refer me to a resource?

Also is there a way to make my results exactly the same?

I'm running into the same issue. Were you able to find a solution?

dabdoue avatar Mar 15 '24 18:03 dabdoue