YOLOv5 Apple Metal Performance Shader (MPS) support (#7878)

* Apple Metal Performance Shader (MPS) device support

Following https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/

Should work with Apple M1 devices with PyTorch nightly installed with command `--device mps`. Usage examples:
```bash
python train.py --device mps
python detect.py --device mps
python val.py --device mps
```

* Update device strategy to fix MPS issue
This commit is contained in:
Glenn Jocher authored and GitHub committed 2022-05-24 13:34:32 +02:00
1 parent 27911dc824
commit c215878f11
5 files changed
+11 -8

No files matched your search

+1 -1
View File
@@ -331,7 +331,7 @@ class DetectMultiBackend(nn.Module):
names = yaml.safe_load(f)['names']
if pt: # PyTorch
model = attempt_load(weights if isinstance(weights, list) else w, map_location=device)
model = attempt_load(weights if isinstance(weights, list) else w, device=device)
stride = max(int(model.stride.max()), 32) # model stride
names = model.module.names if hasattr(model, 'module') else model.names # get class names
model.half() if fp16 else model.float()
+3 -3
View File
@@ -71,14 +71,14 @@ class Ensemble(nn.ModuleList):
return y, None # inference, train output
def attempt_load(weights, map_location=None, inplace=True, fuse=True):
def attempt_load(weights, device=None, inplace=True, fuse=True):
from models.yolo import Detect, Model
# Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
model = Ensemble()
for w in weights if isinstance(weights, list) else [weights]:
ckpt = torch.load(attempt_download(w), map_location=map_location) # load
ckpt = (ckpt.get('ema') or ckpt['model']).float() # FP32 model
ckpt = torch.load(attempt_download(w))
ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model
model.append(ckpt.fuse().eval() if fuse else ckpt.eval()) # fused or un-fused model in eval mode
# Compatibility updates
+1 -1
View File
@@ -536,7 +536,7 @@ def run(
):
# PyTorch model
im = torch.zeros((batch_size, 3, *imgsz)) # BCHW image
model = attempt_load(weights, map_location=torch.device('cpu'), inplace=True, fuse=False)
model = attempt_load(weights, device=torch.device('cpu'), inplace=True, fuse=False)
_ = model(im) # inference
model.info()