pythonpytorchyolov8

Training yolov8 with MPS on macbook


I am trying to train yolov8 on my custom dataset by this following code:

model = YOLO('yolov8s.pt')

# train
results = model.train(
    data=data,
    epochs=epochs,
    batch=batch_size,
    imgsz=img_size,
    project=project_name,
    name=model_name,
    device=torch.device('mps')
)

But I got an error:

engine/trainer: task=detect, mode=train, model=yolov8s.pt, data=/Users/btp712/Code/Human Monitor/MOT17/mot17_data.yml, epochs=30, time=None, patience=50, batch=-1, imgsz=640, save=True, save_period=-1, cache=False, device=mps, workers=8, project=models/yolo, name=yolov8s_mot17_det10, exist_ok=False, pretrained=True, optimizer=auto, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, amp=True, fraction=1.0, profile=False, freeze=None, multi_scale=False, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, vid_stride=1, stream_buffer=False, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, embed=None, show=False, save_frames=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, show_boxes=True, line_width=None, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=None, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, auto_augment=randaugment, erasing=0.4, crop_fraction=1.0, cfg=None, tracker=botsort.yaml, save_dir=models/yolo/yolov8s_mot17_det10
Overriding model.yaml nc=80 with nc=1

                   from  n    params  module                                       arguments                     
  0                  -1  1       928  ultralytics.nn.modules.conv.Conv             [3, 32, 3, 2]                 
  1                  -1  1     18560  ultralytics.nn.modules.conv.Conv             [32, 64, 3, 2]                
  2                  -1  1     29056  ultralytics.nn.modules.block.C2f             [64, 64, 1, True]             
  3                  -1  1     73984  ultralytics.nn.modules.conv.Conv             [64, 128, 3, 2]               
  4                  -1  2    197632  ultralytics.nn.modules.block.C2f             [128, 128, 2, True]           
  5                  -1  1    295424  ultralytics.nn.modules.conv.Conv             [128, 256, 3, 2]              
  6                  -1  2    788480  ultralytics.nn.modules.block.C2f             [256, 256, 2, True]           
  7                  -1  1   1180672  ultralytics.nn.modules.conv.Conv             [256, 512, 3, 2]              
  8                  -1  1   1838080  ultralytics.nn.modules.block.C2f             [512, 512, 1, True]           
  9                  -1  1    656896  ultralytics.nn.modules.block.SPPF            [512, 512, 5]                 
 10                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 11             [-1, 6]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 12                  -1  1    591360  ultralytics.nn.modules.block.C2f             [768, 256, 1]                 
 13                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 14             [-1, 4]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 15                  -1  1    148224  ultralytics.nn.modules.block.C2f             [384, 128, 1]                 
 16                  -1  1    147712  ultralytics.nn.modules.conv.Conv             [128, 128, 3, 2]              
 17            [-1, 12]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 18                  -1  1    493056  ultralytics.nn.modules.block.C2f             [384, 256, 1]                 
 19                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 20             [-1, 9]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 21                  -1  1   1969152  ultralytics.nn.modules.block.C2f             [768, 512, 1]                 
 22        [15, 18, 21]  1   2116435  ultralytics.nn.modules.head.Detect           [1, [128, 256, 512]]          
Model summary: 225 layers, 11135987 parameters, 11135971 gradients, 28.6 GFLOPs

Transferred 349/355 items from pretrained weights
TensorBoard: Start with 'tensorboard --logdir models/yolo/yolov8s_mot17_det10', view at http://localhost:6006/
Freezing layer 'model.22.dfl.conv.weight'
AutoBatch: Computing optimal batch size for imgsz=640
Traceback (most recent call last):
  File "/Users/btp712/Code/Human Monitor/main.py", line 21, in <module>
    train_yolo(model_name='yolov8s_mot17_det',
  File "/Users/btp712/Code/Human Monitor/train_yolo.py", line 8, in train_yolo
    results = model.train(
              ^^^^^^^^^^^^
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/ultralytics/engine/model.py", line 601, in train
    self.trainer.train()
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/ultralytics/engine/trainer.py", line 208, in train
    self._do_train(world_size)
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/ultralytics/engine/trainer.py", line 322, in _do_train
    self._setup_train(world_size)
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/ultralytics/engine/trainer.py", line 282, in _setup_train
    self.args.batch = self.batch_size = check_train_batch_size(self.model, self.args.imgsz, self.amp)
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/ultralytics/utils/autobatch.py", line 27, in check_train_batch_size
    return autobatch(deepcopy(model).train(), imgsz)  # compute optimal batch size
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/ultralytics/utils/autobatch.py", line 58, in autobatch
    properties = torch.cuda.get_device_properties(device)  # device properties
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/torch/cuda/__init__.py", line 453, in get_device_properties
    _lazy_init()  # will define _get_device_properties
    ^^^^^^^^^^^^
  File "/Users/btp712/Code/Human Monitor/.venv/lib/python3.11/site-packages/torch/cuda/__init__.py", line 293, in _lazy_init
    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

I have explored and known that this problem happening with macos, specially on models without support of cuda. Initially, I trained model without passing 'mps' to device and it ran too slow, about 1 hour per epoch becuase of training on cpu. Then I knew I could train on gpu by passing 'mps' to device but getting this error.

I am using macbook air m2. As I known, my laptop is not supported of CUDA.


Solution

  • The error you get makes sense. CUDA is not supported on macOS as NVIDIA GPUs and CUDA are not compatible with Mac hardware, especially since most modern MacBooks use AMD GPUs or Apple Silicon (M1, M1 Pro, M1 Max, M2). Any attempts to use CUDA-related functions (like torch.cuda.get_device_properties) will result in the error you are getting.