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

DetectMultiBackend() default stride=32 #7342

Merged
merged 10 commits into from
Apr 9, 2022
Merged

Conversation

rglkt
Copy link
Contributor

@rglkt rglkt commented Apr 8, 2022

πŸ› οΈ PR Summary

Made with ❀️ by Ultralytics Actions

🌟 Summary

Optimization of default stride setting for YOLOv5 models.

πŸ“Š Key Changes

  • Changed the default stride value from 64 to 32 in the model initialization.

🎯 Purpose & Impact

  • 🎯 Purpose: This change likely aims to improve the default configuration for better compatibility or performance.
  • πŸ’‘ Impact: Users might experience improved detection accuracy or speed due to the more optimal stride setting. This could be especially beneficial for real-time applications.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘‹ Hello @rglkt, thank you for submitting a YOLOv5 πŸš€ PR! To allow your work to be integrated as seamlessly as possible, we advise you to:

  • βœ… Verify your PR is up-to-date with upstream/master. If your PR is behind upstream/master an automatic GitHub Actions merge may be attempted by writing /rebase in a new comment, or by running the following code, replacing 'feature' with the name of your local branch:
git remote add upstream https://github.com/ultralytics/yolov5.git
git fetch upstream
# git checkout feature  # <--- replace 'feature' with local branch name
git merge upstream/master
git push -u origin -f
  • βœ… Verify all Continuous Integration (CI) checks are passing.
  • βœ… Reduce changes to the absolute minimum required for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." -Bruce Lee

@rglkt
Copy link
Contributor Author

rglkt commented Apr 8, 2022

hello, when i try to use engine model, it has some wrong. i set my imgsz 224, the multipule of 32, but the common.py set it as 64 automatically and result to wrong. i think may be the default stride should be 32, when the n/s/m/l/x stride is the multiple of 32.
b66849f0c6a2f256b1a33ca72c5899a

@@ -296,7 +296,7 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
super().__init__()
w = str(weights[0] if isinstance(weights, list) else weights)
pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs = self.model_type(w) # get backend
stride, names = 64, [f'class{i}' for i in range(1000)] # assign defaults
stride, names = 32, [f'class{i}' for i in range(1000)] # assign defaults
Copy link
Contributor

@zhiqwang zhiqwang Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the P6 series need stride 64, so it would be better to set this parameter optional here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i can't realize the meaning of P6, do you mean as follow? i think the data configuration is used to config something about dataset, it's not suitable to set stride on it?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry,i know, set it on cmd argument, i try it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P6 is yolov5n6, yolov5s6, yolov5m6 and yolov5l6, introduced from tag 5.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!and i have added argument --stride

@rglkt rglkt mentioned this pull request Apr 8, 2022
2 tasks
@glenn-jocher
Copy link
Member

@rglkt some model formats (like PyTorch) can access attached metadata (like stride) to dynamically select the minimum viable stride, while other formats (like ONNX) do not have any metadata and thus lack stride information, in which case stride 64 is assumed since it is valid for both P5 models (minimum stride 32) and P6 models (minimum stride 64).

The only alternative here is to run an image and compare the input to the output to empirically attempt a stride determination at inference time, which would add delay on DetectMultiBackend init. I'll think this over a bit and see what we can do here.

@glenn-jocher
Copy link
Member

@rglkt I've implemented ONNX metadata saving and reading now which resolves any stride issues for ONNX models. Unfortunately the rest of the formats are not resolved for this issue, like the engine issue in #7164.

One limitation in this PR is that detect.py is only one entrypoint for inference, we also have val.py and PyTorch Hub model inference.

I don't see a good solution to this other than to try to keep attaching custom metadata to each export format. So far I have PyTorch, TorchScript, and now ONNX available with stride and names metadata in DetectMultiBackend.

@rglkt
Copy link
Contributor Author

rglkt commented Apr 9, 2022

Thank you for your work, I am a senior who is doing graduation design. This code helped me a lot, I think my pr is not the best solution, but I want to report the problem that other people will also encounter in the code and hope to contribute some power to the code. Thanks again for the open source code

@glenn-jocher glenn-jocher changed the title set common default stride as 32 DetectMultiBackend() default stride=32 Apr 9, 2022
@glenn-jocher glenn-jocher merged commit aa542ce into ultralytics:master Apr 9, 2022
@glenn-jocher
Copy link
Member

@rglkt PR is merged. Thank you for your contributions to YOLOv5 πŸš€ and Vision AI ⭐

I think is probably a slight improvement over master, as now errors will appear on incorrect --imgsz rather than appearing on correct --imgsz.

The problem's not solved of course, as now errors will appear when users with P6 exported models attempt inference on stride 32 images, but at least the error will appear appropriately on an incorrect argument value.

@glenn-jocher glenn-jocher linked an issue Apr 10, 2022 that may be closed by this pull request
1 task
BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
* set common default stride as 32

* restore default stride, and set it on argument optional

* fix wrong use of opt

* fix missing parameter of stride

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix format of parameters

* Update val.py

* Update common.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
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

Successfully merging this pull request may close these issues.

Why is the default "stride" of common.DetectMultiBackend set to 64
3 participants