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.

737 lines
30KB

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