Fix `select_device()` for Multi-GPU (#6434)
Browse files* Fix `select_device()` for Multi-GPU
Possible fix for https://github.com/ultralytics/yolov5/issues/6431
* Update torch_utils.py
* Update torch_utils.py
* Update torch_utils.py
* Update torch_utils.py
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
- utils/datasets.py +2 -2
- utils/torch_utils.py +12 -3
utils/datasets.py
CHANGED
@@ -29,13 +29,13 @@ from tqdm import tqdm
|
|
29 |
from utils.augmentations import Albumentations, augment_hsv, copy_paste, letterbox, mixup, random_perspective
|
30 |
from utils.general import (LOGGER, NUM_THREADS, check_dataset, check_requirements, check_yaml, clean_str,
|
31 |
segments2boxes, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn)
|
32 |
-
from utils.torch_utils import torch_distributed_zero_first
|
33 |
|
34 |
# Parameters
|
35 |
HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
|
36 |
IMG_FORMATS = ['bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp'] # include image suffixes
|
37 |
VID_FORMATS = ['asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'wmv'] # include video suffixes
|
38 |
-
DEVICE_COUNT = max(
|
39 |
|
40 |
# Get orientation exif tag
|
41 |
for orientation in ExifTags.TAGS.keys():
|
|
|
29 |
from utils.augmentations import Albumentations, augment_hsv, copy_paste, letterbox, mixup, random_perspective
|
30 |
from utils.general import (LOGGER, NUM_THREADS, check_dataset, check_requirements, check_yaml, clean_str,
|
31 |
segments2boxes, xyn2xy, xywh2xyxy, xywhn2xyxy, xyxy2xywhn)
|
32 |
+
from utils.torch_utils import device_count, torch_distributed_zero_first
|
33 |
|
34 |
# Parameters
|
35 |
HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
|
36 |
IMG_FORMATS = ['bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp'] # include image suffixes
|
37 |
VID_FORMATS = ['asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'wmv'] # include video suffixes
|
38 |
+
DEVICE_COUNT = max(device_count(), 1) # number of CUDA devices
|
39 |
|
40 |
# Get orientation exif tag
|
41 |
for orientation in ExifTags.TAGS.keys():
|
utils/torch_utils.py
CHANGED
@@ -53,6 +53,15 @@ def git_describe(path=Path(__file__).parent): # path must be a directory
|
|
53 |
return '' # not a git repository
|
54 |
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def select_device(device='', batch_size=0, newline=True):
|
57 |
# device = 'cpu' or '0' or '0,1,2,3'
|
58 |
s = f'YOLOv5 π {git_describe() or date_modified()} torch {torch.__version__} ' # string
|
@@ -61,10 +70,10 @@ def select_device(device='', batch_size=0, newline=True):
|
|
61 |
if cpu:
|
62 |
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
|
63 |
elif device: # non-cpu device requested
|
64 |
-
nd =
|
65 |
-
assert torch.cuda.is_available(), 'CUDA is not available, use `--device cpu` or do not pass a --device'
|
66 |
assert nd > int(max(device.split(','))), f'Invalid `--device {device}` request, valid devices are 0 - {nd - 1}'
|
67 |
-
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable
|
|
|
68 |
|
69 |
cuda = not cpu and torch.cuda.is_available()
|
70 |
if cuda:
|
|
|
53 |
return '' # not a git repository
|
54 |
|
55 |
|
56 |
+
def device_count():
|
57 |
+
# Returns number of CUDA devices available. Safe version of torch.cuda.device_count().
|
58 |
+
try:
|
59 |
+
cmd = 'nvidia-smi -L | wc -l'
|
60 |
+
return int(subprocess.run(cmd, shell=True, capture_output=True, check=True).stdout.decode().split()[-1])
|
61 |
+
except Exception as e:
|
62 |
+
return 0
|
63 |
+
|
64 |
+
|
65 |
def select_device(device='', batch_size=0, newline=True):
|
66 |
# device = 'cpu' or '0' or '0,1,2,3'
|
67 |
s = f'YOLOv5 π {git_describe() or date_modified()} torch {torch.__version__} ' # string
|
|
|
70 |
if cpu:
|
71 |
os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
|
72 |
elif device: # non-cpu device requested
|
73 |
+
nd = device_count() # number of CUDA devices
|
|
|
74 |
assert nd > int(max(device.split(','))), f'Invalid `--device {device}` request, valid devices are 0 - {nd - 1}'
|
75 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable - must be before assert is_available()
|
76 |
+
assert torch.cuda.is_available(), 'CUDA is not available, use `--device cpu` or do not pass a --device'
|
77 |
|
78 |
cuda = not cpu and torch.cuda.is_available()
|
79 |
if cuda:
|