Search code examples
pythonyolo

Running yolov8 pre-trained model on a free GPU on a CPU for detection for a single class


As a newbie trying object detection for a single class i will be using a free GPU. Can the yolov8 trained model on a free GPU to detect a single class after i have downloaded it on a CPU?


Solution

  • The model will load on GPU if available. Hence, just have to force the inference on CPU by using the device argument in predict.

    from ultralytics.yolo.engine.model import YOLO
    
    
    model = YOLO("yolov8n.pt")
    
    # force to run on CPU by using device flag
    results = model.predict(source="0", show=True, stream=True, classes=0, device='cpu')
    for i, result in enumerate(results):
        boxes = result.boxes
        for box in boxes:
            xyxy = box.xyxy[0] 
    
    

    You can double check that you are actually running on CPU by checking your GPU usage with nvtop