Hub models `map_location=device` (#3894)
Browse files* Hub models `map_location=device`
* cleanup
- hubconf.py +4 -3
- utils/torch_utils.py +3 -2
hubconf.py
CHANGED
@@ -36,13 +36,15 @@ def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbo
|
|
36 |
|
37 |
fname = Path(name).with_suffix('.pt') # checkpoint filename
|
38 |
try:
|
|
|
|
|
39 |
if pretrained and channels == 3 and classes == 80:
|
40 |
-
model = attempt_load(fname, map_location=
|
41 |
else:
|
42 |
cfg = list((Path(__file__).parent / 'models').rglob(f'{name}.yaml'))[0] # model.yaml path
|
43 |
model = Model(cfg, channels, classes) # create model
|
44 |
if pretrained:
|
45 |
-
ckpt = torch.load(attempt_download(fname), map_location=
|
46 |
msd = model.state_dict() # model state_dict
|
47 |
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
48 |
csd = {k: v for k, v in csd.items() if msd[k].shape == v.shape} # filter
|
@@ -51,7 +53,6 @@ def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbo
|
|
51 |
model.names = ckpt['model'].names # set class names attribute
|
52 |
if autoshape:
|
53 |
model = model.autoshape() # for file/URI/PIL/cv2/np inputs and NMS
|
54 |
-
device = select_device('0' if torch.cuda.is_available() else 'cpu') if device is None else torch.device(device)
|
55 |
return model.to(device)
|
56 |
|
57 |
except Exception as e:
|
|
|
36 |
|
37 |
fname = Path(name).with_suffix('.pt') # checkpoint filename
|
38 |
try:
|
39 |
+
device = select_device(('0' if torch.cuda.is_available() else 'cpu') if device is None else device)
|
40 |
+
|
41 |
if pretrained and channels == 3 and classes == 80:
|
42 |
+
model = attempt_load(fname, map_location=device) # download/load FP32 model
|
43 |
else:
|
44 |
cfg = list((Path(__file__).parent / 'models').rglob(f'{name}.yaml'))[0] # model.yaml path
|
45 |
model = Model(cfg, channels, classes) # create model
|
46 |
if pretrained:
|
47 |
+
ckpt = torch.load(attempt_download(fname), map_location=device) # load
|
48 |
msd = model.state_dict() # model state_dict
|
49 |
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
50 |
csd = {k: v for k, v in csd.items() if msd[k].shape == v.shape} # filter
|
|
|
53 |
model.names = ckpt['model'].names # set class names attribute
|
54 |
if autoshape:
|
55 |
model = model.autoshape() # for file/URI/PIL/cv2/np inputs and NMS
|
|
|
56 |
return model.to(device)
|
57 |
|
58 |
except Exception as e:
|
utils/torch_utils.py
CHANGED
@@ -2,7 +2,6 @@
|
|
2 |
|
3 |
import datetime
|
4 |
import logging
|
5 |
-
import math
|
6 |
import os
|
7 |
import platform
|
8 |
import subprocess
|
@@ -11,6 +10,7 @@ from contextlib import contextmanager
|
|
11 |
from copy import deepcopy
|
12 |
from pathlib import Path
|
13 |
|
|
|
14 |
import torch
|
15 |
import torch.backends.cudnn as cudnn
|
16 |
import torch.distributed as dist
|
@@ -64,7 +64,8 @@ def git_describe(path=Path(__file__).parent): # path must be a directory
|
|
64 |
def select_device(device='', batch_size=None):
|
65 |
# device = 'cpu' or '0' or '0,1,2,3'
|
66 |
s = f'YOLOv5 π {git_describe() or date_modified()} torch {torch.__version__} ' # string
|
67 |
-
|
|
|
68 |
if cpu:
|
69 |
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
|
70 |
elif device: # non-cpu device requested
|
|
|
2 |
|
3 |
import datetime
|
4 |
import logging
|
|
|
5 |
import os
|
6 |
import platform
|
7 |
import subprocess
|
|
|
10 |
from copy import deepcopy
|
11 |
from pathlib import Path
|
12 |
|
13 |
+
import math
|
14 |
import torch
|
15 |
import torch.backends.cudnn as cudnn
|
16 |
import torch.distributed as dist
|
|
|
64 |
def select_device(device='', batch_size=None):
|
65 |
# device = 'cpu' or '0' or '0,1,2,3'
|
66 |
s = f'YOLOv5 π {git_describe() or date_modified()} torch {torch.__version__} ' # string
|
67 |
+
device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
|
68 |
+
cpu = device == 'cpu'
|
69 |
if cpu:
|
70 |
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
|
71 |
elif device: # non-cpu device requested
|