Tianxiaomo / pytorch-YOLOv4

PyTorch ,ONNX and TensorRT implementation of YOLOv4
Apache License 2.0
4.46k stars 1.49k forks source link

Darknet yolov4-csp to ONNX #447

Open frenky-strasak opened 3 years ago

frenky-strasak commented 3 years ago

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?

chayitw commented 2 years ago

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 commented 2 years ago

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

frenky-strasak commented 2 years ago

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

jackgao0323 commented 2 years ago

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

frenky-strasak commented 2 years ago

@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)

jackgao0323 commented 2 years ago

@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).

lpkoh commented 2 years ago

@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?

dabdoue commented 3 months ago

@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?