Zed Examples YoloV5 custom detection needs update

When following the instructions on zed-examples/object detection/custom detector/python/pytorch_yolov5 at master · stereolabs/zed-examples · GitHub we encountered an issue:

The installation went flawlessly but there seems to be an incompatibility issue with the newest version (commit #10220 SHA: 6992dde4bd628f6bffe7d4c5025afadf79ed679b of yoloV5:

When executing python detector.py with the specified parameters, we encountered:

ImportError: cannot import name ‘scale_coords’ from ‘utils.general’

The detection window did not open.

In some version of yoloV5, presumably commit #9570 on Sept. 24 2022:

“new scale_segments() function” the scale_coords function was changed to the scale_segments function. We tried earlier commits which at least opened the detection window but with a black screen and further issues arose:

“/home/(my paths)…/yolov5/detector.py”, line 100, in torch_thread
model = attempt_load(weights, map_location=device) # load FP32
TypeError: attempt_load() got an unexpected keyword argument ‘map_location’

The attempt_load() function apparently does not accept this argument.
We went down the road of even older commits but at one point there was an issue with the pytorch version, because the yoloV5 repo we chose was so old.

If you have any help or further questions please do not hesitate to ask. We think that an updated version of detector.py might fix the problem.

Thank you!!

Hello, thank you for the notice. We’ll check this out.

Antoine

1 Like

Thank you! FYI I think I forgot to mention that we use linux (pop!_OS), but you probably recognized it was linux from the Folder path.

Moritz

HI there! i am facing the similar issue with the detector.py which is
ImportError: cannot import name ‘scale_coords’ from ‘utils.general’
as someone mentioned in this post, after scale_coords changes to scale_segments still there are problems arose. did anyone solve detector.py errors without any problem ? if yes please feel free to suggest me.
My task is to track the objects along with their 3D information using Zed-2i. if anyone has any idea how it can be achieved please share your inputs.
Thank you
Parvathi

Hello @parvathi , still waiting for a fix, since this is beyond my experience :confused:

Hi, I faced this exact issue and appear to have got it working by checking out an older version of the yolov5 repo, this one worked for me -

Also in detector.py the line
model = attempt_load(weights, map_location=device)
needs to be changed to
model = attempt_load(weights, device=device)

This post was helpful for finding that out - 关于“TypeError: attempt_load() got an unexpected keyword argument 'map_location'” · Issue #8178 · ultralytics/yolov5 · GitHub

hope this helps

1 Like

@sttteephen Thank you very much, this solved the Problem!
@alassagne Perhaps an adjustment is still required, this is a very good lead.

This is the actual solution. It’s difficult for us to follow every version of every yolov(x), so we have to stay at a specific commit. The problem is, the documentation seems better on the C++ sample, sorry for that. We’ll improve the python documentation.

1 Like

Hello, can you be more specific about the exact yolov5 branch that worked for your python code? I’ve tried a few but always had mismatches in the interpreter or a blank screen in a window.
Thank you for the information and for helping out.

@sttteephen already pointed out the Version in his reply:

Just click on the link in his reply in this very same thread and adapt detector.py accordingly, as he says.

When you click “Browse files” on the right side, it leads you to the code which you can download as a ZIP file: GitHub - ultralytics/yolov5 at d669a74623f273f74213a88b5233964d1ab3ea08

We need to use YOLOv5-6.1version, Beacuse the new version ‘scale_coords’ rename to ‘scale_boxes’ .

Sorry for the late response…
Verified on linux and jetson SDK computers.
Many thanks for the tips to every one of this chain.
momi193, your tip on the link to the code is great!

1 Like

I am not sure if the following issue analysis is valid for this chain.

Bounding box of the detected object gets far longer height calculation in the xywh2abcd(xywh, im_shape) function. I’ve tested this from the latest sdk 4.0 down to 3.6.5 .
What could be the root cause for this?

A problematic bounding box display:

A good bounding box display:

Hi, I encountered the same problem, did you solve it?

No, I’m waiting for the next release or fix, meawhile I would test the CPP code.

I solved this problem today.
We cannot just change the name of the function in the “detector.py”(from “scale_coords” to “scale_segments”).
We should also change something of the function “scale_segments” in “utils.general” according to “scale_coords”.

Thus, in general.py, “def scale_segments” change

segments[:, 0] -= pad[0]  # x padding
segments[:, 1] -= pad[1]  # y padding
segments /= gain

to

segments[:, [0, 2]] -= pad[0]  # x padding
segments[:, [1, 3]] -= pad[1]  # y padding
segments[:, :4] /= gain

Unfortunately the CPP solution build fails compiling the code…

Hello ustwm and thank for the fix! I have verified in Ubuntu 20.04 desktop as well as on Ubuntu 22.04 desktop.

I followed the tutorial and now I have this problem:
CAMERA FAILED TO SETUP
How should I handle this?

There is already a solution with a newer version, this is from February 2022, the solution in post#6 is from September 2022. Zed Examples YoloV5 custom detection needs update - #6 by sttteephen

Remember that in detector.py the line
model = attempt_load(weights, map_location=device)
needs to be changed to
model = attempt_load(weights, device=device)