Spaces:
Build error
Build error
commit
Browse files- yolov6/utils/envs.py +54 -0
yolov6/utils/envs.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import torch.backends.cudnn as cudnn
|
| 9 |
+
from yolov6.utils.events import LOGGER
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_envs():
|
| 13 |
+
"""Get PyTorch needed environments from system envirionments."""
|
| 14 |
+
local_rank = int(os.getenv('LOCAL_RANK', -1))
|
| 15 |
+
rank = int(os.getenv('RANK', -1))
|
| 16 |
+
world_size = int(os.getenv('WORLD_SIZE', 1))
|
| 17 |
+
return local_rank, rank, world_size
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def select_device(device):
|
| 21 |
+
"""Set devices' information to the program.
|
| 22 |
+
Args:
|
| 23 |
+
device: a string, like 'cpu' or '1,2,3,4'
|
| 24 |
+
Returns:
|
| 25 |
+
torch.device
|
| 26 |
+
"""
|
| 27 |
+
if device == 'cpu':
|
| 28 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
| 29 |
+
LOGGER.info('Using CPU for training... ')
|
| 30 |
+
elif device:
|
| 31 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = device
|
| 32 |
+
assert torch.cuda.is_available()
|
| 33 |
+
nd = len(device.strip().split(','))
|
| 34 |
+
LOGGER.info(f'Using {nd} GPU for training... ')
|
| 35 |
+
cuda = device != 'cpu' and torch.cuda.is_available()
|
| 36 |
+
device = torch.device('cuda:0' if cuda else 'cpu')
|
| 37 |
+
return device
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def set_random_seed(seed, deterministic=False):
|
| 41 |
+
""" Set random state to random libray, numpy, torch and cudnn.
|
| 42 |
+
Args:
|
| 43 |
+
seed: int value.
|
| 44 |
+
deterministic: bool value.
|
| 45 |
+
"""
|
| 46 |
+
random.seed(seed)
|
| 47 |
+
np.random.seed(seed)
|
| 48 |
+
torch.manual_seed(seed)
|
| 49 |
+
if deterministic:
|
| 50 |
+
cudnn.deterministic = True
|
| 51 |
+
cudnn.benchmark = False
|
| 52 |
+
else:
|
| 53 |
+
cudnn.deterministic = False
|
| 54 |
+
cudnn.benchmark = True
|