Tianxiaomo / pytorch-YOLOv4

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

how to convert yolov4-csp,mish into .pth format? #424

Open akashAD98 opened 3 years ago

akashAD98 commented 3 years ago

can you please make a code for these models? thanks in advance

frenky-strasak commented 3 years ago

@akashAD98 Did you find any solution?

akashAD98 commented 3 years ago

yes i did , first clone the original GitHub repo & use this code

from tool import darknet2pytorch
import torch
import os
from torch.autograd import Variable
import torch.onnx
import torchvision
import torch
model_param_path = r"converted_models"
# load weights from darknet format
model_config = os.path.join(model_param_path,'yolov4x-mish_custom.cfg')
model = darknet2pytorch.Darknet(model_config, inference=True)
weights = os.path.join(model_param_path,"yolov4x-mish_custom.weights")
model.load_weights(weights)

# save weights to pytorch format
torch.save(model.state_dict(), os.path.join(model_param_path,"savedmodel.pth"))

# reload weights from pytorch format
model_pt = darknet2pytorch.Darknet(model_config, inference=True)
model_pt.load_state_dict(torch.load(os.path.join(model_param_path,"savedmodel.pth")))
dummy_input = Variable(torch.randn(10, 3, 416, 416))     #10, 3, 416, 416))
torch.onnx.export(model, dummy_input, "apperals_chaneel.onnx",input_names = ['input'], output_names = ['output',"boxes"],
 dynamic_axes= {'input' : {0 : "batch"},'output' : {0 : "batch"},'boxes' : {0 : "boxes"}})
frenky-strasak commented 3 years ago

@akashAD98 thank you for your response!

My goal is to create onnx model from yolov4-csp and yolov4-mish. I used your code to generate the onnx model "apperals_chaneel.onnx". But the result is the same like from this command: python demo_darknet2onnx.py <cfgFile> <weightFile> <imageFile> <batchSize> (command from this repository): predictions_onnx There are many wrong bboxes.

Also this implementation does not contain "logistic" function here required for yolov4-csp, so I tried to implement there, but the wrong bboxes are still there.

Do you have any idea what's wrong here? Thank you!

akashAD98 commented 3 years ago

@frenky-strasak yes there is issue ,i was able to convert it into .pth but for .onnx im also getting the same issue, here we need to write our own code for mish function, i havnt did it, i attached a few issues here, if you get something please let me know. https://github.com/onnx/onnx/issues/3475 https://github.com/hunglc007/tensorflow-yolov4-tflite/issues/280 https://github.com/onnx/onnx/issues/2818

frenky-strasak commented 3 years ago

@akashAD98 , I found this repo, where the converting yolov4-csp darknet to onnx format works.

akashAD98 commented 3 years ago

Thanks. have you tried converting yolov4 CSP into .PB format ? I'm looking for this script.

On Tue, 13 Jul, 2021, 2:54 pm Frenky Střasák, @.***> wrote:

@akashAD98 https://github.com/akashAD98 , I found this repo https://github.com/linghu8812/tensorrt_inference/tree/master/Yolov4, where the converting yolov4-csp darknet to onnx format works.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Tianxiaomo/pytorch-YOLOv4/issues/424#issuecomment-878927123, or unsubscribe https://github.com/notifications/unsubscribe-auth/AO5PB2VRATASHJBZLSNPVM3TXQA5DANCNFSM44OQHMHA .

akashAD98 commented 3 years ago

@frenky-strasak i was able to convert yolox-mish model into onnx, Do you have the script to testing inferencing of onxx models?? Thanks