Amelia0911 / onnxruntime-for-yolov5

Using CPU to test model
31 stars 12 forks source link

That is a great proect! But the results is wrong... #1

Open Byronnar opened 3 years ago

Byronnar commented 3 years ago

I have transfer the yolov5s.pt to yolov5.onnx, But I got a bad results when I infer... Can you give me some advices?

def draw(img, boxinfo, dst, id):
    for *xyxy, conf, cls in boxinfo:
        label = '%s %.2f' % ('pest', conf)
        print('xyxy: ', xyxy)
        plot_one_box(xyxy, img, label=label, color=colors[int(cls)], line_thickness=3)

    cv2.imshow("dst", img)
    cv2.waitKey(0)
    cv2.imencode('.jpg', img)[1].tofile(os.path.join(dst, id+".jpg"))

    return 0

get the xyxy below:

xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(860.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(906.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(861.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(443.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(664.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(435.)]
xyxy:  [tensor(0.), tensor(0.), tensor(493.), tensor(720.)]

Thank you!

Amelia0911 commented 3 years ago

I hope you've tested your model with predict.py and got the right results.

Byronnar commented 3 years ago

I hope you've tested your model with predict.py and got the right results.

Thanks for u apply! Which yolov5 version have you used? I have downloaded from this link: https://github.com/ultralytics/yolov5/releases/download/v3.0/yolov5s.pt And I have transfer successful by the pytorch2onnx.py. But I get the error bellow when I run predict.py:

Fusing layers... Traceback (most recent call last):
  File "predict.py", line 14, in <module>
    model = attempt_load("yolov5s.pt", map_location=device)
  File "/home/byronnar/bigfile/projects/edge_project/onnxruntime-for-yolov5/models/experimental.py", line 133, in attempt_load
    model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval())  # load FP32 model
  File "/home/byronnar/bigfile/projects/edge_project/onnxruntime-for-yolov5/models/yolo.py", line 151, in fuse
    m.conv = torch_utils.fuse_conv_and_bn(m.conv, m.bn)  # update conv
  File "/home/byronnar/anaconda3/envs/demo/lib/python3.6/site-packages/torch/nn/modules/module.py", line 802, in __setattr__
    remove_from(self.__dict__, self._parameters, self._buffers, self._non_persistent_buffers_set)
  File "/home/byronnar/anaconda3/envs/demo/lib/python3.6/site-packages/torch/nn/modules/module.py", line 772, in __getattr__
    type(self).__name__, name))
torch.nn.modules.module.ModuleAttributeError: 'Conv' object has no attribute '_non_persistent_buffers_set'

Can you share the model?

Amelia0911 commented 3 years ago

It is long time,I don't remember which version of yolov5 I used. Can you give me your email, I will email you the pretrained model。

Amelia0911 commented 3 years ago

I hope you've change your “anchor_list” in “onnx_cpu_detec.py”.

Byronnar commented 3 years ago

Thanks for reply! My email adress: Byronnar@163.com. Thank you!

Byronnar commented 3 years ago

In your new script, the 2_pytorch2onnx.py is:

import torch

weights = "yolov5.pt"
model = torch.load(weights, map_location=torch.device('cpu'))['model'].float()

model.eval()
# model.model[-1].export = True
input_data = torch.randn(1, 3, 320, 416)#, device=device[1, 3, 320, 416]

y = model(input_data)
print("model = ", model)
torch.onnx.export(model, input_data, "yolov5s.onnx", verbose=False, opset_version=11, input_names=['images'], output_names=['classes', 'boxes'] if y is None else ['output'])
print("Done!")

What is the yolov5.pt? You mean yolov5s,pt?

zhedahe commented 3 years ago

I have transfer the yolov5s.pt to yolov5.onnx, But I got a bad results when I infer... Can you give me some advices?

def draw(img, boxinfo, dst, id):
    for *xyxy, conf, cls in boxinfo:
        label = '%s %.2f' % ('pest', conf)
        print('xyxy: ', xyxy)
        plot_one_box(xyxy, img, label=label, color=colors[int(cls)], line_thickness=3)

    cv2.imshow("dst", img)
    cv2.waitKey(0)
    cv2.imencode('.jpg', img)[1].tofile(os.path.join(dst, id+".jpg"))

    return 0

get the xyxy below:

xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(860.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(906.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(861.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(443.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(664.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]
xyxy:  [tensor(0.), tensor(0.), tensor(1280.), tensor(435.)]
xyxy:  [tensor(0.), tensor(0.), tensor(493.), tensor(720.)]

Thank you!

I met same condition, it can work, but its result is wrong... I changed my“anchor_list” in “onnx_cpu_detec.py”. PS: I used yolov5s.pt, and transfer to yolov5s.onnx model success, test with pth and onnx for tensorrt are both ok, so I can not figure it out...

Amelia0911 commented 3 years ago

我已经更新代码了,做了一些修改,用的是我之前用的yolov5s.pt,模型也上传了,你可以再试试! 上面测试的问题就是检测有问题,可能是1和3中的图像尺寸不一致导致的。You can try it again~

At 2021-05-10 18:00:43, "hehaiming" @.***> wrote:

I have transfer the yolov5s.pt to yolov5.onnx, But I got a bad results when I infer... Can you give me some advices?

def draw(img, boxinfo, dst, id):

for *xyxy, conf, cls in boxinfo:

    label = '%s %.2f' % ('pest', conf)

    print('xyxy: ', xyxy)

    plot_one_box(xyxy, img, label=label, color=colors[int(cls)], line_thickness=3)

cv2.imshow("dst", img)

cv2.waitKey(0)

cv2.imencode('.jpg', img)[1].tofile(os.path.join(dst, id+".jpg"))

return 0

get the xyxy below:

xyxy: [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(860.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(906.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(861.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(1280.), tensor(443.)]

xyxy: [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(1280.), tensor(664.)]

xyxy: [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(1280.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(0.), tensor(720.)]

xyxy: [tensor(0.), tensor(0.), tensor(1280.), tensor(435.)]

xyxy: [tensor(0.), tensor(0.), tensor(493.), tensor(720.)]

Thank you!

I met same condition, it can work, but its result is wrong... I changed my“anchor_list” in “onnx_cpu_detec.py”. PS: I used yolov5s.pt, and transfer to yolov5s.onnx model success, test with pth and onnx for tensorrt are both ok, so I can not figure it out...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

zhedahe commented 3 years ago

我安装torch 1.8 onnx最新版本,yolov5也是master, 但onnx推断提示load model错误。

贴出具体的出错信息吧。另外你的onnx模型怎么来的?是yolov5 官方的 export.py 导出的吗?