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

请问一下,你是怎么转onnx的呢?可以共享一下脚本吗? #3

Open
zhigaowu opened this issue Feb 24, 2022 · 6 comments

Comments

@zhigaowu
Copy link

我修改了yolo.py里的一个变量,转是转成功了,但是,看后面还有很多逻辑,应该是转的不完全。
多谢了先

@zhigaowu
Copy link
Author

export = True # onnx export
yolo.py的29行

@hpc203
Copy link
Owner

hpc203 commented Feb 24, 2022 via email

@zhigaowu
Copy link
Author

转换onnx的时候,在yolo.py的Detect类里,不要执行后处理解码的那一段代码

---原始邮件--- 发件人: @.> 发送时间: 2022年2月24日(周四) 晚上6:31 收件人: @.>; 抄送: @.>; 主题: Re: [hpc203/yolov5-detect-car_plate_corner] 请问一下,你是怎么转onnx的呢?可以共享一下脚本吗? (Issue #3) export = True # onnx export yolo.py的29行 — Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you are subscribed to this thread.Message ID: @.>

哦,我也是这样转的,
可是,我发现后处理有很多grid ancher,而且这些ancher也不知道是怎么生成的,感觉像错过了什么,我再看看源码吧。

还是多谢了。

@zhigaowu
Copy link
Author

转换onnx的时候,在yolo.py的Detect类里,不要执行后处理解码的那一段代码

---原始邮件--- 发件人: @.> 发送时间: 2022年2月24日(周四) 晚上6:31 收件人: @.>; 抄送: @.>; 主题: Re: [hpc203/yolov5-detect-car_plate_corner] 请问一下,你是怎么转onnx的呢?可以共享一下脚本吗? (Issue #3) export = True # onnx export yolo.py的29行 — Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you are subscribed to this thread.Message ID: @.>

调试了一天代码,也没看出相关的anchor的设置,好像是模型内部生成的;
而且,我转出来的模型的输出是三个,你转出来的模型输出是一个,应该是concat的结果。
但是,恕我愚昧,还是不知道怎么转。

能共享一下best.pt转onnx的代码吗?谢谢咯。

@zhigaowu
Copy link
Author

def forward(self, x):
    # x = x.copy()  # for profiling
    z = []  # inference output
    # self.training |= self.export
    if self.export:
        for i in range(self.nl):
            x[i] = self.m[i](x[i])
            bs, _, ny, nx = x[i].shape  # x(bs,48,20,20) to x(bs,3,20,20,16)
            x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
            z.append(x[i].view(bs, -1, self.no))

        return torch.cat(z, 1)

可以了,加了最后两行。
还是多谢

@neverstoplearn
Copy link

def forward(self, x):
    # x = x.copy()  # for profiling
    z = []  # inference output
    # self.training |= self.export
    if self.export:
        for i in range(self.nl):
            x[i] = self.m[i](x[i])
            bs, _, ny, nx = x[i].shape  # x(bs,48,20,20) to x(bs,3,20,20,16)
            x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
            z.append(x[i].view(bs, -1, self.no))

        return torch.cat(z, 1)

可以了,加了最后两行。 还是多谢

按照你的加,出现了以下问题,请问你还修改了其他地方吗?谢谢。
Namespace(batch_size=1, img_size=[640, 640], weights='./weights/best.pt')
Fusing layers...
Model Summary: 305 layers, 1721262 parameters, 0 gradients, 5.6 GFLOPS

Starting ONNX export with onnx 1.10.2...
Fusing layers...
Model Summary: 305 layers, 1721262 parameters, 0 gradients, 5.6 GFLOPS
Traceback (most recent call last):
File "export.py", line 69, in
torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['data'],
File "/home/zhengxin/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/onnx/init.py", line 271, in export
return utils.export(model, args, f, export_params, verbose, training,
File "/home/zhengxin/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/onnx/utils.py", line 88, in export
_export(model, args, f, export_params, verbose, training, input_names, output_names,
File "/home/zhengxin/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/onnx/utils.py", line 694, in _export
_model_to_graph(model, args, verbose, input_names,
File "/home/zhengxin/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/onnx/utils.py", line 490, in _model_to_graph
_set_input_and_output_names(graph, input_names, output_names)
File "/home/zhengxin/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/onnx/utils.py", line 774, in _set_input_and_output_names
set_names(list(graph.outputs()), output_names, 'output')
File "/home/zhengxin/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/onnx/utils.py", line 767, in set_names
raise RuntimeError(
RuntimeError: number of output names provided (3) exceeded number of outputs (1)

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

3 participants