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

Fix Flask REST API #7210

Merged
merged 6 commits into from
Apr 3, 2022
Merged

Fix Flask REST API #7210

merged 6 commits into from
Apr 3, 2022

Conversation

Zengyf-CVer
Copy link
Contributor

@Zengyf-CVer Zengyf-CVer commented Mar 30, 2022

@glenn-jocher
I fixed this bug.

πŸ› οΈ PR Summary

Made with ❀️ by Ultralytics Actions

🌟 Summary

Update and fix in the Flask REST API for YOLOv5 model deployment.

πŸ“Š Key Changes

  • Clarified the description by changing "yolov5s object detection model" to "Flask REST API exposing a YOLOv5s model".
  • Changed variable name from args to opt for argument parsing consistency.
  • Added a workaround to bypass a known issue with rate limiting when using torch.hub.

🎯 Purpose & Impact

  • The description change improves the readability and understanding of what the API does.
  • The variable renaming is a minor change for internal code consistency and readability.
  • The workaround for the torch.hub rate limit issue ensures that developers won't run into HTTP 403 errors when trying to load models, leading to smoother development and deployment experiences. This could be particularly impactful for users working with YOLOv5 model in cloud environments or with frequent reloads.

@glenn-jocher glenn-jocher linked an issue Mar 30, 2022 that may be closed by this pull request
2 tasks
@glenn-jocher
Copy link
Member

@Zengyf-CVer thanks for the PR! I looked into this and seems like PyTorch > 1.10 should have this bug fixed:

https://stackoverflow.com/questions/68901236/urllib-error-httperror-http-error-403-rate-limit-exceeded-when-loading-resnet1

Pytorch released a bugfix for 1.9 (i.e. 1.9.1) so updating to 1.9.1 should rectify this issue for good.

@Zengyf-CVer
Copy link
Contributor Author

@glenn-jocher My configuration is torch1.10, but this bug appears.

@glenn-jocher
Copy link
Member

@Zengyf-CVer sorry to hear that! That's very strange. Could you test 1.11 please? If there error is still present then I'll go ahead and merge with an explanation for that line, because that's the strangest lambda function I've ever seen.

@glenn-jocher glenn-jocher changed the title fix flask rest api Fix Flask REST API Mar 31, 2022
@Zengyf-CVer
Copy link
Contributor Author

@glenn-jocher
I've experimented with torch 1.11 and no similar problem occurs. But this problem still exists on torch 1.10. I checked it, and it is probably related to the reloading of github. I changed the parameter value of force_reload to False, and it can run on torch 1.10, and there is no need to add the line of lamda code.

# torch 1.10
    model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=False)  # force_reload to recache
# torch 1.11
# No need to do any operation, it can be used normally.

@glenn-jocher
Copy link
Member

@Zengyf-CVer ok, thanks for the tests! I think we should leave the code alone then as the issue seems to be fixed in latest PyTorch release.

@Zengyf-CVer
Copy link
Contributor Author

@glenn-jocher
I think so too, but it would be better to have some version hints in the code section.

@glenn-jocher
Copy link
Member

@Zengyf-CVer yes, we could do an if torch<1.11 statement. Could you do this using this example?

yolov5/models/yolo.py

Lines 79 to 82 in 71621df

if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
yv, xv = torch.meshgrid(torch.arange(ny, device=d), torch.arange(nx, device=d), indexing='ij')
else:
yv, xv = torch.meshgrid(torch.arange(ny, device=d), torch.arange(nx, device=d))

@Zengyf-CVer
Copy link
Contributor Author

@glenn-jocher
I did a few more tests and found a problem:When I delete both .cache/torch/hub/ultralytics_yolov5_master and yolov5s.pt in the root directory of the workspace, I found that whether it is torch 1.10.2 or torch 1.11.0, there will be urllib.error.HTTPError: HTTP Error 403: rate limit exceeded this problem. So I wrote the lambda code again and found that the program can be used normally.So torch.hub._validate_not_a_forked_repo = lambda a,b,c: True is useful to solve the current problem.

@glenn-jocher glenn-jocher merged commit dda669a into ultralytics:master Apr 3, 2022
@glenn-jocher
Copy link
Member

@Zengyf-CVer understood, thanks for the detailed debugging! PR is merged. Thank you for your contributions to YOLOv5 πŸš€ and Vision AI ⭐

BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
* Update restapi.py

* Update restapi.py

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

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

* Cleanup

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

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

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

About the running bug of restapi.py
2 participants