You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

906 lines
37KB

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. """
  3. General utils
  4. """
  5. import contextlib
  6. import glob
  7. import logging
  8. import math
  9. import os
  10. import platform
  11. import random
  12. import re
  13. import shutil
  14. import signal
  15. import time
  16. import urllib
  17. from datetime import datetime
  18. from itertools import repeat
  19. from multiprocessing.pool import ThreadPool
  20. from pathlib import Path
  21. from subprocess import check_output
  22. from zipfile import ZipFile
  23. import cv2
  24. import numpy as np
  25. import pandas as pd
  26. import pkg_resources as pkg
  27. import torch
  28. import torchvision
  29. import yaml
  30. from utils.downloads import gsutil_getsize
  31. from utils.metrics import box_iou, fitness
  32. # Settings
  33. FILE = Path(__file__).resolve()
  34. ROOT = FILE.parents[1] # YOLOv5 root directory
  35. DATASETS_DIR = ROOT.parent / 'datasets' # YOLOv5 datasets directory
  36. NUM_THREADS = min(8, max(1, os.cpu_count() - 1)) # number of YOLOv5 multiprocessing threads
  37. VERBOSE = str(os.getenv('YOLOv5_VERBOSE', True)).lower() == 'true' # global verbose mode
  38. FONT = 'Arial.ttf' # https://ultralytics.com/assets/Arial.ttf
  39. torch.set_printoptions(linewidth=320, precision=5, profile='long')
  40. np.set_printoptions(linewidth=320, formatter={'float_kind': '{:11.5g}'.format}) # format short g, %precision=5
  41. pd.options.display.max_columns = 10
  42. cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with PyTorch DataLoader)
  43. os.environ['NUMEXPR_MAX_THREADS'] = str(NUM_THREADS) # NumExpr max threads
  44. os.environ['OMP_NUM_THREADS'] = str(NUM_THREADS) # OpenMP max threads (PyTorch and SciPy)
  45. def is_kaggle():
  46. # Is environment a Kaggle Notebook?
  47. try:
  48. assert os.environ.get('PWD') == '/kaggle/working'
  49. assert os.environ.get('KAGGLE_URL_BASE') == 'https://www.kaggle.com'
  50. return True
  51. except AssertionError:
  52. return False
  53. def is_writeable(dir, test=False):
  54. # Return True if directory has write permissions, test opening a file with write permissions if test=True
  55. if test: # method 1
  56. file = Path(dir) / 'tmp.txt'
  57. try:
  58. with open(file, 'w'): # open file with write permissions
  59. pass
  60. file.unlink() # remove file
  61. return True
  62. except OSError:
  63. return False
  64. else: # method 2
  65. return os.access(dir, os.R_OK) # possible issues on Windows
  66. def set_logging(name=None, verbose=VERBOSE):
  67. # Sets level and returns logger
  68. if is_kaggle():
  69. for h in logging.root.handlers:
  70. logging.root.removeHandler(h) # remove all handlers associated with the root logger object
  71. rank = int(os.getenv('RANK', -1)) # rank in world for Multi-GPU trainings
  72. logging.basicConfig(format="%(message)s", level=logging.INFO if (verbose and rank in (-1, 0)) else logging.WARNING)
  73. return logging.getLogger(name)
  74. LOGGER = set_logging('yolov5') # define globally (used in train.py, val.py, detect.py, etc.)
  75. def user_config_dir(dir='Ultralytics', env_var='YOLOV5_CONFIG_DIR'):
  76. # Return path of user configuration directory. Prefer environment variable if exists. Make dir if required.
  77. env = os.getenv(env_var)
  78. if env:
  79. path = Path(env) # use environment variable
  80. else:
  81. cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 OS dirs
  82. path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir
  83. path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable
  84. path.mkdir(exist_ok=True) # make if required
  85. return path
  86. CONFIG_DIR = user_config_dir() # Ultralytics settings dir
  87. class Profile(contextlib.ContextDecorator):
  88. # Usage: @Profile() decorator or 'with Profile():' context manager
  89. def __enter__(self):
  90. self.start = time.time()
  91. def __exit__(self, type, value, traceback):
  92. print(f'Profile results: {time.time() - self.start:.5f}s')
  93. class Timeout(contextlib.ContextDecorator):
  94. # Usage: @Timeout(seconds) decorator or 'with Timeout(seconds):' context manager
  95. def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True):
  96. self.seconds = int(seconds)
  97. self.timeout_message = timeout_msg
  98. self.suppress = bool(suppress_timeout_errors)
  99. def _timeout_handler(self, signum, frame):
  100. raise TimeoutError(self.timeout_message)
  101. def __enter__(self):
  102. if platform.system() != 'Windows': # not supported on Windows
  103. signal.signal(signal.SIGALRM, self._timeout_handler) # Set handler for SIGALRM
  104. signal.alarm(self.seconds) # start countdown for SIGALRM to be raised
  105. def __exit__(self, exc_type, exc_val, exc_tb):
  106. if platform.system() != 'Windows':
  107. signal.alarm(0) # Cancel SIGALRM if it's scheduled
  108. if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError
  109. return True
  110. class WorkingDirectory(contextlib.ContextDecorator):
  111. # Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager
  112. def __init__(self, new_dir):
  113. self.dir = new_dir # new dir
  114. self.cwd = Path.cwd().resolve() # current dir
  115. def __enter__(self):
  116. os.chdir(self.dir)
  117. def __exit__(self, exc_type, exc_val, exc_tb):
  118. os.chdir(self.cwd)
  119. def try_except(func):
  120. # try-except function. Usage: @try_except decorator
  121. def handler(*args, **kwargs):
  122. try:
  123. func(*args, **kwargs)
  124. except Exception as e:
  125. print(e)
  126. return handler
  127. def methods(instance):
  128. # Get class/instance methods
  129. return [f for f in dir(instance) if callable(getattr(instance, f)) and not f.startswith("__")]
  130. def print_args(name, opt):
  131. # Print argparser arguments
  132. LOGGER.info(colorstr(f'{name}: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items()))
  133. def init_seeds(seed=0):
  134. # Initialize random number generator (RNG) seeds https://pytorch.org/docs/stable/notes/randomness.html
  135. # cudnn seed 0 settings are slower and more reproducible, else faster and less reproducible
  136. import torch.backends.cudnn as cudnn
  137. random.seed(seed)
  138. np.random.seed(seed)
  139. torch.manual_seed(seed)
  140. cudnn.benchmark, cudnn.deterministic = (False, True) if seed == 0 else (True, False)
  141. def intersect_dicts(da, db, exclude=()):
  142. # Dictionary intersection of matching keys and shapes, omitting 'exclude' keys, using da values
  143. return {k: v for k, v in da.items() if k in db and not any(x in k for x in exclude) and v.shape == db[k].shape}
  144. def get_latest_run(search_dir='.'):
  145. # Return path to most recent 'last.pt' in /runs (i.e. to --resume from)
  146. last_list = glob.glob(f'{search_dir}/**/last*.pt', recursive=True)
  147. return max(last_list, key=os.path.getctime) if last_list else ''
  148. def is_docker():
  149. # Is environment a Docker container?
  150. return Path('/workspace').exists() # or Path('/.dockerenv').exists()
  151. def is_colab():
  152. # Is environment a Google Colab instance?
  153. try:
  154. import google.colab
  155. return True
  156. except ImportError:
  157. return False
  158. def is_pip():
  159. # Is file in a pip package?
  160. return 'site-packages' in Path(__file__).resolve().parts
  161. def is_ascii(s=''):
  162. # Is string composed of all ASCII (no UTF) characters? (note str().isascii() introduced in python 3.7)
  163. s = str(s) # convert list, tuple, None, etc. to str
  164. return len(s.encode().decode('ascii', 'ignore')) == len(s)
  165. def is_chinese(s='人工智能'):
  166. # Is string composed of any Chinese characters?
  167. return True if re.search('[\u4e00-\u9fff]', str(s)) else False
  168. def emojis(str=''):
  169. # Return platform-dependent emoji-safe version of string
  170. return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str
  171. def file_age(path=__file__):
  172. # Return days since last file update
  173. dt = (datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime)) # delta
  174. return dt.days # + dt.seconds / 86400 # fractional days
  175. def file_update_date(path=__file__):
  176. # Return human-readable file modification date, i.e. '2021-3-26'
  177. t = datetime.fromtimestamp(Path(path).stat().st_mtime)
  178. return f'{t.year}-{t.month}-{t.day}'
  179. def file_size(path):
  180. # Return file/dir size (MB)
  181. mb = 1 << 20 # bytes to MiB (1024 ** 2)
  182. path = Path(path)
  183. if path.is_file():
  184. return path.stat().st_size / mb
  185. elif path.is_dir():
  186. return sum(f.stat().st_size for f in path.glob('**/*') if f.is_file()) / mb
  187. else:
  188. return 0.0
  189. def check_online():
  190. # Check internet connectivity
  191. import socket
  192. try:
  193. socket.create_connection(("1.1.1.1", 443), 5) # check host accessibility
  194. return True
  195. except OSError:
  196. return False
  197. def git_describe(path=ROOT): # path must be a directory
  198. # Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe
  199. try:
  200. return check_output(f'git -C {path} describe --tags --long --always', shell=True).decode()[:-1]
  201. except Exception:
  202. return ''
  203. @try_except
  204. @WorkingDirectory(ROOT)
  205. def check_git_status():
  206. # Recommend 'git pull' if code is out of date
  207. msg = ', for updates see https://github.com/ultralytics/yolov5'
  208. s = colorstr('github: ') # string
  209. assert Path('.git').exists(), s + 'skipping check (not a git repository)' + msg
  210. assert not is_docker(), s + 'skipping check (Docker image)' + msg
  211. assert check_online(), s + 'skipping check (offline)' + msg
  212. cmd = 'git fetch && git config --get remote.origin.url'
  213. url = check_output(cmd, shell=True, timeout=5).decode().strip().rstrip('.git') # git fetch
  214. branch = check_output('git rev-parse --abbrev-ref HEAD', shell=True).decode().strip() # checked out
  215. n = int(check_output(f'git rev-list {branch}..origin/master --count', shell=True)) # commits behind
  216. if n > 0:
  217. s += f"⚠️ YOLOv5 is out of date by {n} commit{'s' * (n > 1)}. Use `git pull` or `git clone {url}` to update."
  218. else:
  219. s += f'up to date with {url} ✅'
  220. LOGGER.info(emojis(s)) # emoji-safe
  221. def check_python(minimum='3.6.2'):
  222. # Check current python version vs. required python version
  223. check_version(platform.python_version(), minimum, name='Python ', hard=True)
  224. def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=False, hard=False, verbose=False):
  225. # Check version vs. required version
  226. current, minimum = (pkg.parse_version(x) for x in (current, minimum))
  227. result = (current == minimum) if pinned else (current >= minimum) # bool
  228. s = f'{name}{minimum} required by YOLOv5, but {name}{current} is currently installed' # string
  229. if hard:
  230. assert result, s # assert min requirements met
  231. if verbose and not result:
  232. LOGGER.warning(s)
  233. return result
  234. @try_except
  235. def check_requirements(requirements=ROOT / 'requirements.txt', exclude=(), install=True):
  236. # Check installed dependencies meet requirements (pass *.txt file or list of packages)
  237. prefix = colorstr('red', 'bold', 'requirements:')
  238. check_python() # check python version
  239. if isinstance(requirements, (str, Path)): # requirements.txt file
  240. file = Path(requirements)
  241. assert file.exists(), f"{prefix} {file.resolve()} not found, check failed."
  242. with file.open() as f:
  243. requirements = [f'{x.name}{x.specifier}' for x in pkg.parse_requirements(f) if x.name not in exclude]
  244. else: # list or tuple of packages
  245. requirements = [x for x in requirements if x not in exclude]
  246. n = 0 # number of packages updates
  247. for r in requirements:
  248. try:
  249. pkg.require(r)
  250. except Exception: # DistributionNotFound or VersionConflict if requirements not met
  251. s = f"{prefix} {r} not found and is required by YOLOv5"
  252. if install:
  253. LOGGER.info(f"{s}, attempting auto-update...")
  254. try:
  255. assert check_online(), f"'pip install {r}' skipped (offline)"
  256. LOGGER.info(check_output(f"pip install '{r}'", shell=True).decode())
  257. n += 1
  258. except Exception as e:
  259. LOGGER.warning(f'{prefix} {e}')
  260. else:
  261. LOGGER.info(f'{s}. Please install and rerun your command.')
  262. if n: # if packages updated
  263. source = file.resolve() if 'file' in locals() else requirements
  264. s = f"{prefix} {n} package{'s' * (n > 1)} updated per {source}\n" \
  265. f"{prefix} ⚠️ {colorstr('bold', 'Restart runtime or rerun command for updates to take effect')}\n"
  266. LOGGER.info(emojis(s))
  267. def check_img_size(imgsz, s=32, floor=0):
  268. # Verify image size is a multiple of stride s in each dimension
  269. if isinstance(imgsz, int): # integer i.e. img_size=640
  270. new_size = max(make_divisible(imgsz, int(s)), floor)
  271. else: # list i.e. img_size=[640, 480]
  272. new_size = [max(make_divisible(x, int(s)), floor) for x in imgsz]
  273. if new_size != imgsz:
  274. LOGGER.warning(f'WARNING: --img-size {imgsz} must be multiple of max stride {s}, updating to {new_size}')
  275. return new_size
  276. def check_imshow():
  277. # Check if environment supports image displays
  278. try:
  279. assert not is_docker(), 'cv2.imshow() is disabled in Docker environments'
  280. assert not is_colab(), 'cv2.imshow() is disabled in Google Colab environments'
  281. cv2.imshow('test', np.zeros((1, 1, 3)))
  282. cv2.waitKey(1)
  283. cv2.destroyAllWindows()
  284. cv2.waitKey(1)
  285. return True
  286. except Exception as e:
  287. LOGGER.warning(f'WARNING: Environment does not support cv2.imshow() or PIL Image.show() image displays\n{e}')
  288. return False
  289. def check_suffix(file='yolov5s.pt', suffix=('.pt',), msg=''):
  290. # Check file(s) for acceptable suffix
  291. if file and suffix:
  292. if isinstance(suffix, str):
  293. suffix = [suffix]
  294. for f in file if isinstance(file, (list, tuple)) else [file]:
  295. s = Path(f).suffix.lower() # file suffix
  296. if len(s):
  297. assert s in suffix, f"{msg}{f} acceptable suffix is {suffix}"
  298. def check_yaml(file, suffix=('.yaml', '.yml')):
  299. # Search/download YAML file (if necessary) and return path, checking suffix
  300. return check_file(file, suffix)
  301. def check_file(file, suffix=''):
  302. # Search/download file (if necessary) and return path
  303. check_suffix(file, suffix) # optional
  304. file = str(file) # convert to str()
  305. if Path(file).is_file() or file == '': # exists
  306. return file
  307. elif file.startswith(('http:/', 'https:/')): # download
  308. url = str(Path(file)).replace(':/', '://') # Pathlib turns :// -> :/
  309. file = Path(urllib.parse.unquote(file).split('?')[0]).name # '%2F' to '/', split https://url.com/file.txt?auth
  310. if Path(file).is_file():
  311. LOGGER.info(f'Found {url} locally at {file}') # file already exists
  312. else:
  313. LOGGER.info(f'Downloading {url} to {file}...')
  314. torch.hub.download_url_to_file(url, file)
  315. assert Path(file).exists() and Path(file).stat().st_size > 0, f'File download failed: {url}' # check
  316. return file
  317. else: # search
  318. files = []
  319. for d in 'data', 'models', 'utils': # search directories
  320. files.extend(glob.glob(str(ROOT / d / '**' / file), recursive=True)) # find file
  321. assert len(files), f'File not found: {file}' # assert file was found
  322. assert len(files) == 1, f"Multiple files match '{file}', specify exact path: {files}" # assert unique
  323. return files[0] # return file
  324. def check_font(font=FONT):
  325. # Download font to CONFIG_DIR if necessary
  326. font = Path(font)
  327. if not font.exists() and not (CONFIG_DIR / font.name).exists():
  328. url = "https://ultralytics.com/assets/" + font.name
  329. LOGGER.info(f'Downloading {url} to {CONFIG_DIR / font.name}...')
  330. torch.hub.download_url_to_file(url, str(font), progress=False)
  331. def check_dataset(data, autodownload=True):
  332. # Download and/or unzip dataset if not found locally
  333. # Usage: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128_with_yaml.zip
  334. # Download (optional)
  335. extract_dir = ''
  336. if isinstance(data, (str, Path)) and str(data).endswith('.zip'): # i.e. gs://bucket/dir/coco128.zip
  337. download(data, dir=DATASETS_DIR, unzip=True, delete=False, curl=False, threads=1)
  338. data = next((DATASETS_DIR / Path(data).stem).rglob('*.yaml'))
  339. extract_dir, autodownload = data.parent, False
  340. # Read yaml (optional)
  341. if isinstance(data, (str, Path)):
  342. with open(data, errors='ignore') as f:
  343. data = yaml.safe_load(f) # dictionary
  344. # Resolve paths
  345. path = Path(extract_dir or data.get('path') or '') # optional 'path' default to '.'
  346. if not path.is_absolute():
  347. path = (ROOT / path).resolve()
  348. for k in 'train', 'val', 'test':
  349. if data.get(k): # prepend path
  350. data[k] = str(path / data[k]) if isinstance(data[k], str) else [str(path / x) for x in data[k]]
  351. # Parse yaml
  352. assert 'nc' in data, "Dataset 'nc' key missing."
  353. if 'names' not in data:
  354. data['names'] = [f'class{i}' for i in range(data['nc'])] # assign class names if missing
  355. train, val, test, s = (data.get(x) for x in ('train', 'val', 'test', 'download'))
  356. if val:
  357. val = [Path(x).resolve() for x in (val if isinstance(val, list) else [val])] # val path
  358. if not all(x.exists() for x in val):
  359. LOGGER.info('\nDataset not found, missing paths: %s' % [str(x) for x in val if not x.exists()])
  360. if s and autodownload: # download script
  361. root = path.parent if 'path' in data else '..' # unzip directory i.e. '../'
  362. if s.startswith('http') and s.endswith('.zip'): # URL
  363. f = Path(s).name # filename
  364. LOGGER.info(f'Downloading {s} to {f}...')
  365. torch.hub.download_url_to_file(s, f)
  366. Path(root).mkdir(parents=True, exist_ok=True) # create root
  367. ZipFile(f).extractall(path=root) # unzip
  368. Path(f).unlink() # remove zip
  369. r = None # success
  370. elif s.startswith('bash '): # bash script
  371. LOGGER.info(f'Running {s} ...')
  372. r = os.system(s)
  373. else: # python script
  374. r = exec(s, {'yaml': data}) # return None
  375. LOGGER.info(f"Dataset autodownload {f'success, saved to {root}' if r in (0, None) else 'failure'}\n")
  376. else:
  377. raise Exception('Dataset not found.')
  378. return data # dictionary
  379. def url2file(url):
  380. # Convert URL to filename, i.e. https://url.com/file.txt?auth -> file.txt
  381. url = str(Path(url)).replace(':/', '://') # Pathlib turns :// -> :/
  382. file = Path(urllib.parse.unquote(url)).name.split('?')[0] # '%2F' to '/', split https://url.com/file.txt?auth
  383. return file
  384. def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1):
  385. # Multi-threaded file download and unzip function, used in data.yaml for autodownload
  386. def download_one(url, dir):
  387. # Download 1 file
  388. f = dir / Path(url).name # filename
  389. if Path(url).is_file(): # exists in current path
  390. Path(url).rename(f) # move to dir
  391. elif not f.exists():
  392. LOGGER.info(f'Downloading {url} to {f}...')
  393. if curl:
  394. os.system(f"curl -L '{url}' -o '{f}' --retry 9 -C -") # curl download, retry and resume on fail
  395. else:
  396. torch.hub.download_url_to_file(url, f, progress=True) # torch download
  397. if unzip and f.suffix in ('.zip', '.gz'):
  398. LOGGER.info(f'Unzipping {f}...')
  399. if f.suffix == '.zip':
  400. ZipFile(f).extractall(path=dir) # unzip
  401. elif f.suffix == '.gz':
  402. os.system(f'tar xfz {f} --directory {f.parent}') # unzip
  403. if delete:
  404. f.unlink() # remove zip
  405. dir = Path(dir)
  406. dir.mkdir(parents=True, exist_ok=True) # make directory
  407. if threads > 1:
  408. pool = ThreadPool(threads)
  409. pool.imap(lambda x: download_one(*x), zip(url, repeat(dir))) # multi-threaded
  410. pool.close()
  411. pool.join()
  412. else:
  413. for u in [url] if isinstance(url, (str, Path)) else url:
  414. download_one(u, dir)
  415. def make_divisible(x, divisor):
  416. # Returns nearest x divisible by divisor
  417. if isinstance(divisor, torch.Tensor):
  418. divisor = int(divisor.max()) # to int
  419. return math.ceil(x / divisor) * divisor
  420. def clean_str(s):
  421. # Cleans a string by replacing special characters with underscore _
  422. return re.sub(pattern="[|@#!¡·$€%&()=?¿^*;:,¨´><+]", repl="_", string=s)
  423. def one_cycle(y1=0.0, y2=1.0, steps=100):
  424. # lambda function for sinusoidal ramp from y1 to y2 https://arxiv.org/pdf/1812.01187.pdf
  425. return lambda x: ((1 - math.cos(x * math.pi / steps)) / 2) * (y2 - y1) + y1
  426. def colorstr(*input):
  427. # Colors a string https://en.wikipedia.org/wiki/ANSI_escape_code, i.e. colorstr('blue', 'hello world')
  428. *args, string = input if len(input) > 1 else ('blue', 'bold', input[0]) # color arguments, string
  429. colors = {'black': '\033[30m', # basic colors
  430. 'red': '\033[31m',
  431. 'green': '\033[32m',
  432. 'yellow': '\033[33m',
  433. 'blue': '\033[34m',
  434. 'magenta': '\033[35m',
  435. 'cyan': '\033[36m',
  436. 'white': '\033[37m',
  437. 'bright_black': '\033[90m', # bright colors
  438. 'bright_red': '\033[91m',
  439. 'bright_green': '\033[92m',
  440. 'bright_yellow': '\033[93m',
  441. 'bright_blue': '\033[94m',
  442. 'bright_magenta': '\033[95m',
  443. 'bright_cyan': '\033[96m',
  444. 'bright_white': '\033[97m',
  445. 'end': '\033[0m', # misc
  446. 'bold': '\033[1m',
  447. 'underline': '\033[4m'}
  448. return ''.join(colors[x] for x in args) + f'{string}' + colors['end']
  449. def labels_to_class_weights(labels, nc=80):
  450. # Get class weights (inverse frequency) from training labels
  451. if labels[0] is None: # no labels loaded
  452. return torch.Tensor()
  453. labels = np.concatenate(labels, 0) # labels.shape = (866643, 5) for COCO
  454. classes = labels[:, 0].astype(np.int) # labels = [class xywh]
  455. weights = np.bincount(classes, minlength=nc) # occurrences per class
  456. # Prepend gridpoint count (for uCE training)
  457. # gpi = ((320 / 32 * np.array([1, 2, 4])) ** 2 * 3).sum() # gridpoints per image
  458. # weights = np.hstack([gpi * len(labels) - weights.sum() * 9, weights * 9]) ** 0.5 # prepend gridpoints to start
  459. weights[weights == 0] = 1 # replace empty bins with 1
  460. weights = 1 / weights # number of targets per class
  461. weights /= weights.sum() # normalize
  462. return torch.from_numpy(weights)
  463. def labels_to_image_weights(labels, nc=80, class_weights=np.ones(80)):
  464. # Produces image weights based on class_weights and image contents
  465. class_counts = np.array([np.bincount(x[:, 0].astype(np.int), minlength=nc) for x in labels])
  466. image_weights = (class_weights.reshape(1, nc) * class_counts).sum(1)
  467. # index = random.choices(range(n), weights=image_weights, k=1) # weight image sample
  468. return image_weights
  469. def coco80_to_coco91_class(): # converts 80-index (val2014) to 91-index (paper)
  470. # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
  471. # a = np.loadtxt('data/coco.names', dtype='str', delimiter='\n')
  472. # b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter='\n')
  473. # x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco
  474. # x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet
  475. x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34,
  476. 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  477. 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]
  478. return x
  479. def xyxy2xywh(x):
  480. # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right
  481. y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
  482. y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center
  483. y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center
  484. y[:, 2] = x[:, 2] - x[:, 0] # width
  485. y[:, 3] = x[:, 3] - x[:, 1] # height
  486. return y
  487. def xywh2xyxy(x):
  488. # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
  489. y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
  490. y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x
  491. y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y
  492. y[:, 2] = x[:, 0] + x[:, 2] / 2 # bottom right x
  493. y[:, 3] = x[:, 1] + x[:, 3] / 2 # bottom right y
  494. return y
  495. def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
  496. # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
  497. y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
  498. y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw # top left x
  499. y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh # top left y
  500. y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw # bottom right x
  501. y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh # bottom right y
  502. return y
  503. def xyxy2xywhn(x, w=640, h=640, clip=False, eps=0.0):
  504. # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right
  505. if clip:
  506. clip_coords(x, (h - eps, w - eps)) # warning: inplace clip
  507. y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
  508. y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w # x center
  509. y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h # y center
  510. y[:, 2] = (x[:, 2] - x[:, 0]) / w # width
  511. y[:, 3] = (x[:, 3] - x[:, 1]) / h # height
  512. return y
  513. def xyn2xy(x, w=640, h=640, padw=0, padh=0):
  514. # Convert normalized segments into pixel segments, shape (n,2)
  515. y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
  516. y[:, 0] = w * x[:, 0] + padw # top left x
  517. y[:, 1] = h * x[:, 1] + padh # top left y
  518. return y
  519. def segment2box(segment, width=640, height=640):
  520. # Convert 1 segment label to 1 box label, applying inside-image constraint, i.e. (xy1, xy2, ...) to (xyxy)
  521. x, y = segment.T # segment xy
  522. inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height)
  523. x, y, = x[inside], y[inside]
  524. return np.array([x.min(), y.min(), x.max(), y.max()]) if any(x) else np.zeros((1, 4)) # xyxy
  525. def segments2boxes(segments):
  526. # Convert segment labels to box labels, i.e. (cls, xy1, xy2, ...) to (cls, xywh)
  527. boxes = []
  528. for s in segments:
  529. x, y = s.T # segment xy
  530. boxes.append([x.min(), y.min(), x.max(), y.max()]) # cls, xyxy
  531. return xyxy2xywh(np.array(boxes)) # cls, xywh
  532. def resample_segments(segments, n=1000):
  533. # Up-sample an (n,2) segment
  534. for i, s in enumerate(segments):
  535. x = np.linspace(0, len(s) - 1, n)
  536. xp = np.arange(len(s))
  537. segments[i] = np.concatenate([np.interp(x, xp, s[:, i]) for i in range(2)]).reshape(2, -1).T # segment xy
  538. return segments
  539. def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
  540. # Rescale coords (xyxy) from img1_shape to img0_shape
  541. if ratio_pad is None: # calculate from img0_shape
  542. gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new
  543. pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding
  544. else:
  545. gain = ratio_pad[0][0]
  546. pad = ratio_pad[1]
  547. coords[:, [0, 2]] -= pad[0] # x padding
  548. coords[:, [1, 3]] -= pad[1] # y padding
  549. coords[:, :4] /= gain
  550. clip_coords(coords, img0_shape)
  551. return coords
  552. def clip_coords(boxes, shape):
  553. # Clip bounding xyxy bounding boxes to image shape (height, width)
  554. if isinstance(boxes, torch.Tensor): # faster individually
  555. boxes[:, 0].clamp_(0, shape[1]) # x1
  556. boxes[:, 1].clamp_(0, shape[0]) # y1
  557. boxes[:, 2].clamp_(0, shape[1]) # x2
  558. boxes[:, 3].clamp_(0, shape[0]) # y2
  559. else: # np.array (faster grouped)
  560. boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, shape[1]) # x1, x2
  561. boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, shape[0]) # y1, y2
  562. def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, multi_label=False,
  563. labels=(), max_det=300):
  564. """Runs Non-Maximum Suppression (NMS) on inference results
  565. Returns:
  566. list of detections, on (n,6) tensor per image [xyxy, conf, cls]
  567. """
  568. nc = prediction.shape[2] - 5 # number of classes
  569. xc = prediction[..., 4] > conf_thres # candidates
  570. # Checks
  571. assert 0 <= conf_thres <= 1, f'Invalid Confidence threshold {conf_thres}, valid values are between 0.0 and 1.0'
  572. assert 0 <= iou_thres <= 1, f'Invalid IoU {iou_thres}, valid values are between 0.0 and 1.0'
  573. # Settings
  574. min_wh, max_wh = 2, 7680 # (pixels) minimum and maximum box width and height
  575. max_nms = 30000 # maximum number of boxes into torchvision.ops.nms()
  576. time_limit = 10.0 # seconds to quit after
  577. redundant = True # require redundant detections
  578. multi_label &= nc > 1 # multiple labels per box (adds 0.5ms/img)
  579. merge = False # use merge-NMS
  580. t = time.time()
  581. output = [torch.zeros((0, 6), device=prediction.device)] * prediction.shape[0]
  582. for xi, x in enumerate(prediction): # image index, image inference
  583. # Apply constraints
  584. x[((x[..., 2:4] < min_wh) | (x[..., 2:4] > max_wh)).any(1), 4] = 0 # width-height
  585. x = x[xc[xi]] # confidence
  586. # Cat apriori labels if autolabelling
  587. if labels and len(labels[xi]):
  588. lb = labels[xi]
  589. v = torch.zeros((len(lb), nc + 5), device=x.device)
  590. v[:, :4] = lb[:, 1:5] # box
  591. v[:, 4] = 1.0 # conf
  592. v[range(len(lb)), lb[:, 0].long() + 5] = 1.0 # cls
  593. x = torch.cat((x, v), 0)
  594. # If none remain process next image
  595. if not x.shape[0]:
  596. continue
  597. # Compute conf
  598. x[:, 5:] *= x[:, 4:5] # conf = obj_conf * cls_conf
  599. # Box (center x, center y, width, height) to (x1, y1, x2, y2)
  600. box = xywh2xyxy(x[:, :4])
  601. # Detections matrix nx6 (xyxy, conf, cls)
  602. if multi_label:
  603. i, j = (x[:, 5:] > conf_thres).nonzero(as_tuple=False).T
  604. x = torch.cat((box[i], x[i, j + 5, None], j[:, None].float()), 1)
  605. else: # best class only
  606. conf, j = x[:, 5:].max(1, keepdim=True)
  607. x = torch.cat((box, conf, j.float()), 1)[conf.view(-1) > conf_thres]
  608. # Filter by class
  609. if classes is not None:
  610. x = x[(x[:, 5:6] == torch.tensor(classes, device=x.device)).any(1)]
  611. # Apply finite constraint
  612. # if not torch.isfinite(x).all():
  613. # x = x[torch.isfinite(x).all(1)]
  614. # Check shape
  615. n = x.shape[0] # number of boxes
  616. if not n: # no boxes
  617. continue
  618. elif n > max_nms: # excess boxes
  619. x = x[x[:, 4].argsort(descending=True)[:max_nms]] # sort by confidence
  620. # Batched NMS
  621. c = x[:, 5:6] * (0 if agnostic else max_wh) # classes
  622. boxes, scores = x[:, :4] + c, x[:, 4] # boxes (offset by class), scores
  623. i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS
  624. if i.shape[0] > max_det: # limit detections
  625. i = i[:max_det]
  626. if merge and (1 < n < 3E3): # Merge NMS (boxes merged using weighted mean)
  627. # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
  628. iou = box_iou(boxes[i], boxes) > iou_thres # iou matrix
  629. weights = iou * scores[None] # box weights
  630. x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True) # merged boxes
  631. if redundant:
  632. i = i[iou.sum(1) > 1] # require redundancy
  633. output[xi] = x[i]
  634. if (time.time() - t) > time_limit:
  635. LOGGER.warning(f'WARNING: NMS time limit {time_limit}s exceeded')
  636. break # time limit exceeded
  637. return output
  638. def strip_optimizer(f='best.pt', s=''): # from utils.general import *; strip_optimizer()
  639. # Strip optimizer from 'f' to finalize training, optionally save as 's'
  640. x = torch.load(f, map_location=torch.device('cpu'))
  641. if x.get('ema'):
  642. x['model'] = x['ema'] # replace model with ema
  643. for k in 'optimizer', 'best_fitness', 'wandb_id', 'ema', 'updates': # keys
  644. x[k] = None
  645. x['epoch'] = -1
  646. x['model'].half() # to FP16
  647. for p in x['model'].parameters():
  648. p.requires_grad = False
  649. torch.save(x, s or f)
  650. mb = os.path.getsize(s or f) / 1E6 # filesize
  651. LOGGER.info(f"Optimizer stripped from {f},{(' saved as %s,' % s) if s else ''} {mb:.1f}MB")
  652. def print_mutation(results, hyp, save_dir, bucket, prefix=colorstr('evolve: ')):
  653. evolve_csv = save_dir / 'evolve.csv'
  654. evolve_yaml = save_dir / 'hyp_evolve.yaml'
  655. keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95',
  656. 'val/box_loss', 'val/obj_loss', 'val/cls_loss') + tuple(hyp.keys()) # [results + hyps]
  657. keys = tuple(x.strip() for x in keys)
  658. vals = results + tuple(hyp.values())
  659. n = len(keys)
  660. # Download (optional)
  661. if bucket:
  662. url = f'gs://{bucket}/evolve.csv'
  663. if gsutil_getsize(url) > (evolve_csv.stat().st_size if evolve_csv.exists() else 0):
  664. os.system(f'gsutil cp {url} {save_dir}') # download evolve.csv if larger than local
  665. # Log to evolve.csv
  666. s = '' if evolve_csv.exists() else (('%20s,' * n % keys).rstrip(',') + '\n') # add header
  667. with open(evolve_csv, 'a') as f:
  668. f.write(s + ('%20.5g,' * n % vals).rstrip(',') + '\n')
  669. # Save yaml
  670. with open(evolve_yaml, 'w') as f:
  671. data = pd.read_csv(evolve_csv)
  672. data = data.rename(columns=lambda x: x.strip()) # strip keys
  673. i = np.argmax(fitness(data.values[:, :4])) #
  674. generations = len(data)
  675. f.write('# YOLOv5 Hyperparameter Evolution Results\n' +
  676. f'# Best generation: {i}\n' +
  677. f'# Last generation: {generations - 1}\n' +
  678. '# ' + ', '.join(f'{x.strip():>20s}' for x in keys[:7]) + '\n' +
  679. '# ' + ', '.join(f'{x:>20.5g}' for x in data.values[i, :7]) + '\n\n')
  680. yaml.safe_dump(data.loc[i][7:].to_dict(), f, sort_keys=False)
  681. # Print to screen
  682. LOGGER.info(prefix + f'{generations} generations finished, current result:\n' +
  683. prefix + ', '.join(f'{x.strip():>20s}' for x in keys) + '\n' +
  684. prefix + ', '.join(f'{x:20.5g}' for x in vals) + '\n\n')
  685. if bucket:
  686. os.system(f'gsutil cp {evolve_csv} {evolve_yaml} gs://{bucket}') # upload
  687. def apply_classifier(x, model, img, im0):
  688. # Apply a second stage classifier to YOLO outputs
  689. # Example model = torchvision.models.__dict__['efficientnet_b0'](pretrained=True).to(device).eval()
  690. im0 = [im0] if isinstance(im0, np.ndarray) else im0
  691. for i, d in enumerate(x): # per image
  692. if d is not None and len(d):
  693. d = d.clone()
  694. # Reshape and pad cutouts
  695. b = xyxy2xywh(d[:, :4]) # boxes
  696. b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # rectangle to square
  697. b[:, 2:] = b[:, 2:] * 1.3 + 30 # pad
  698. d[:, :4] = xywh2xyxy(b).long()
  699. # Rescale boxes from img_size to im0 size
  700. scale_coords(img.shape[2:], d[:, :4], im0[i].shape)
  701. # Classes
  702. pred_cls1 = d[:, 5].long()
  703. ims = []
  704. for j, a in enumerate(d): # per item
  705. cutout = im0[i][int(a[1]):int(a[3]), int(a[0]):int(a[2])]
  706. im = cv2.resize(cutout, (224, 224)) # BGR
  707. # cv2.imwrite('example%i.jpg' % j, cutout)
  708. im = im[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
  709. im = np.ascontiguousarray(im, dtype=np.float32) # uint8 to float32
  710. im /= 255 # 0 - 255 to 0.0 - 1.0
  711. ims.append(im)
  712. pred_cls2 = model(torch.Tensor(ims).to(d.device)).argmax(1) # classifier prediction
  713. x[i] = x[i][pred_cls1 == pred_cls2] # retain matching class detections
  714. return x
  715. def increment_path(path, exist_ok=False, sep='', mkdir=False):
  716. # Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.
  717. path = Path(path) # os-agnostic
  718. if path.exists() and not exist_ok:
  719. path, suffix = (path.with_suffix(''), path.suffix) if path.is_file() else (path, '')
  720. dirs = glob.glob(f"{path}{sep}*") # similar paths
  721. matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs]
  722. i = [int(m.groups()[0]) for m in matches if m] # indices
  723. n = max(i) + 1 if i else 2 # increment number
  724. path = Path(f"{path}{sep}{n}{suffix}") # increment path
  725. if mkdir:
  726. path.mkdir(parents=True, exist_ok=True) # make directory
  727. return path
  728. # Variables
  729. NCOLS = 0 if is_docker() else shutil.get_terminal_size().columns # terminal window size for tqdm