You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@Pro100rus32 YOLOv5 PyTorch Hub models automatically use CUDA backends if detected. If you are not observing this behavior your code is out of date. You can update your Hub cache with
model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='last.pt', force_reload=True)
Also note you can pass a filename directly to model() for inference, you don't need to convert to numpy array first.
@Pro100rus32 YOLOv5 PyTorch Hub models automatically use CUDA backends if detected. If you are not observing this behavior your code is out of date. You can update your Hub cache with
model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='last.pt', force_reload=True)
Also note you can pass a filename directly to model() for inference, you don't need to convert to numpy array first.
I suppose I need to create a separate topic. Since my cuda is not working with this code:
import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# Images
dir = 'https://github.com/ultralytics/yolov5/raw/master/data/images/'
imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')] # batch of images
# Inference
results = model(imgs)
results.print() # or .show(), .save()
@Pro100rus32 I just pushed a major improvement for all YOLOv5 PyTorch Hub models that enabled AMP autocasting by default. In my YOLOV5s on T4 GPU test, in comparison with my earlier result I now get 8.8ms inference, 1.2ms NMS vs 12.9ms and 1.1ms before.
FYI only Turing and Ampere generation cards can exploit autocasting/FP16 inference, Pascal generation cards generally can not (P4, P100, 1080ti).
@Pro100rus32 hmm not familiar with a 940M, it's probably a laptop card.
Also remember if you are creating a video inference pipeline your speed will improve significantly if you run batched inference instead of running inference on images one at a time. i.e. wait for 30 frames and then run batched inference on them all simultaneously for faster results.
detect.py is mostly hard-coded for batch-size 1 dataloading, but the PyTorch Hub models allow for batched inference.
Yes, this is a laptop graphics card. Alas, in my task, frames are submitted one at a time. And I was already disappointed with the speed: 30ms for 1 frame 96x96 But I understand that this is due to the fact that I have a weak video card and a laptop in general.
Can I use mps instead of cuda when loading the model or is this not possible?
Edit
So I tried experimenting. It still says YOLOv5 🚀 2024-4-28 Python-3.12.2 torch-2.3.0 CPU on the logs, but when I check model.parameters()).is_mps, it seems to be true, so I'm guessing it works?
This is my code:
# MODEL_PATHfrompathlibimportPathMODEL_PATH=Path("./best.pt").resolve() # A yolov5 modelYOLO_PATH=Path("./yolov5").resolve()
# Model Instance# 1. Try to change the device by default. (Doesn't work)print("Device", torch.get_default_device()) # 😪 doesn't worktorch.set_default_device(torch.device("mps")) # 😪 doesn't workprint("Device", torch.get_default_device()) # It changes "mps:0" for the default, but not the model it seems.# 2. Load the modelmodel=torch.hub.load(YOLO_PATH.as_posix(), 'custom', MODEL_PATH.as_posix(), source='local', force_reload=True) # local repo# 3. Change to mps.model.map_location=torch.device("mps") # 😪 doesn't work (Idk if it even has this API but this doesn't work lol, worth the shot).model.to(torch.device("mps")) # ✅ This is what worked.# Check if running on cuda/mps/cpuprint("is cuda??", next(model.parameters()).is_cuda) # returns Falseprint("is cpu??", next(model.parameters()).is_cpu) # returns Falseprint("is mps??", next(model.parameters()).is_mps) # returns True
Hey there! 👋 It looks like you've done some solid experimenting with using MPS on your YOLOv5 model. Based on your code and results, you're indeed on the right track!
Using .to(torch.device("mps")) is indeed the correct way to move your model to leverage MPS (Metal Performance Shaders) on supported hardware. Although setting the default device before loading the model didn't directly affect the model's device, applying .to() method directly on the model, as you discovered, is effective.
Your check with model.parameters()).is_mps yielding True confirms the model is utilizing MPS for computations, which is great news! This setup is a good approach if you're working with MPS compatible hardware and aiming for optimized performance.
To sum up, your experimentation has led you to the correct method for utilizing MPS with YOLOv5 models. Just remember to ensure your entire pipeline, including data loaders, are aligned with using MPS to maximize efficiency. Happy coding! 🚀
Activity
glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 YOLOv5 PyTorch Hub models automatically use CUDA backends if detected. If you are not observing this behavior your code is out of date. You can update your Hub cache with
Also note you can pass a filename directly to model() for inference, you don't need to convert to numpy array first.
glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 also note that results.print() includes profiling results, so you don't need to run your own timing logic.
Pro100rus32 commentedon Mar 28, 2021
But why is it so slow?)
glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 I've already answered your question, you might want to read my response in #2630 (comment)
Pro100rus32 commentedon Mar 28, 2021
Did I do the right thing?
Out:
glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 for examples on proper usage see PyTorch Hub tutorial:
YOLOv5 Tutorials
Pro100rus32 commentedon Mar 28, 2021
I suppose I need to create a separate topic. Since my cuda is not working with this code:
But works with detect.py file
p.s. I'll try to figure it out
Pro100rus32 commentedon Mar 28, 2021
I found the problem! It doesn't work with a custom model.
model = torch.hub.load('ultralytics/yolov5', 'custom', path_or_model='last.pt')Pro100rus32 commentedon Mar 28, 2021
custom:

Standart

glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 ok thanks for the info! I'll try to reproduce here with a custom model and see what's going wrong.
glenn-jocher commentedon Mar 28, 2021
Yes I am able to reproduce. I'll try to get this fixed later today, adding a TODO.
11 remaining items
Pro100rus32 commentedon Mar 28, 2021
(108/355) 1.mp4: 256x256 Done. (0.016s)
Speed: 4.0ms pre-process, 18.9ms inference, 1.0ms NMS per image at shape (1, 3, 256, 256)
16ms vs 23.9ms (Scanned over 100 frames+)
glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 thanks for the numbers, but its not clear what you are testing. Could you provide code to reproduce, i.e. a minimum reproducible example?
https://docs.ultralytics.com/help/minimum_reproducible_example/
Pro100rus32 commentedon Mar 28, 2021
Thanks, I solved the problem :)
glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 I just pushed a major improvement for all YOLOv5 PyTorch Hub models that enabled AMP autocasting by default. In my YOLOV5s on T4 GPU test, in comparison with my earlier result I now get 8.8ms inference, 1.2ms NMS vs 12.9ms and 1.1ms before.
FYI only Turing and Ampere generation cards can exploit autocasting/FP16 inference, Pascal generation cards generally can not (P4, P100, 1080ti).
Pro100rus32 commentedon Mar 28, 2021
I use 940M xD
glenn-jocher commentedon Mar 28, 2021
@Pro100rus32 hmm not familiar with a 940M, it's probably a laptop card.
Also remember if you are creating a video inference pipeline your speed will improve significantly if you run batched inference instead of running inference on images one at a time. i.e. wait for 30 frames and then run batched inference on them all simultaneously for faster results.
detect.py is mostly hard-coded for batch-size 1 dataloading, but the PyTorch Hub models allow for batched inference.
Pro100rus32 commentedon Mar 28, 2021
Yes, this is a laptop graphics card. Alas, in my task, frames are submitted one at a time. And I was already disappointed with the speed: 30ms for 1 frame 96x96 But I understand that this is due to the fact that I have a weak video card and a laptop in general.
PyTorch Hub custom model to CUDA device fix (ultralytics#2636)
pull updates from original repo (release v5) + reimplement UI (#21)
PyTorch Hub custom model to CUDA device fix (ultralytics#2636)
PyTorch Hub custom model to CUDA device fix (ultralytics#2636)
PyTorch Hub custom model to CUDA device fix (ultralytics#2636)
Blankeos commentedon Apr 29, 2024
Can I use
mpsinstead ofcudawhen loading the model or is this not possible?Edit
So I tried experimenting. It still says
YOLOv5 🚀 2024-4-28 Python-3.12.2 torch-2.3.0 CPUon the logs, but when I checkmodel.parameters()).is_mps, it seems to be true, so I'm guessing it works?This is my code:
Logs
glenn-jocher commentedon Apr 29, 2024
Hey there! 👋 It looks like you've done some solid experimenting with using MPS on your YOLOv5 model. Based on your code and results, you're indeed on the right track!
Using
.to(torch.device("mps"))is indeed the correct way to move your model to leverage MPS (Metal Performance Shaders) on supported hardware. Although setting the default device before loading the model didn't directly affect the model's device, applying.to()method directly on the model, as you discovered, is effective.Your check with
model.parameters()).is_mpsyieldingTrueconfirms the model is utilizing MPS for computations, which is great news! This setup is a good approach if you're working with MPS compatible hardware and aiming for optimized performance.To sum up, your experimentation has led you to the correct method for utilizing MPS with YOLOv5 models. Just remember to ensure your entire pipeline, including data loaders, are aligned with using MPS to maximize efficiency. Happy coding! 🚀