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.

438 lines
18KB

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. """
  3. Plotting utils
  4. """
  5. import math
  6. from copy import copy
  7. from pathlib import Path
  8. import cv2
  9. import matplotlib
  10. import matplotlib.pyplot as plt
  11. import numpy as np
  12. import pandas as pd
  13. import seaborn as sn
  14. import torch
  15. from PIL import Image, ImageDraw, ImageFont
  16. from utils.general import is_ascii, xyxy2xywh, xywh2xyxy
  17. from utils.metrics import fitness
  18. # Settings
  19. matplotlib.rc('font', **{'size': 11})
  20. matplotlib.use('Agg') # for writing to files only
  21. FILE = Path(__file__).absolute()
  22. ROOT = FILE.parents[1] # yolov5/ dir
  23. class Colors:
  24. # Ultralytics color palette https://ultralytics.com/
  25. def __init__(self):
  26. # hex = matplotlib.colors.TABLEAU_COLORS.values()
  27. hex = ('FF3838', 'FF9D97', 'FF701F', 'FFB21D', 'CFD231', '48F90A', '92CC17', '3DDB86', '1A9334', '00D4BB',
  28. '2C99A8', '00C2FF', '344593', '6473FF', '0018EC', '8438FF', '520085', 'CB38FF', 'FF95C8', 'FF37C7')
  29. self.palette = [self.hex2rgb('#' + c) for c in hex]
  30. self.n = len(self.palette)
  31. def __call__(self, i, bgr=False):
  32. c = self.palette[int(i) % self.n]
  33. return (c[2], c[1], c[0]) if bgr else c
  34. @staticmethod
  35. def hex2rgb(h): # rgb order (PIL)
  36. return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4))
  37. colors = Colors() # create instance for 'from utils.plots import colors'
  38. def check_font(font='Arial.ttf', size=10):
  39. # Return a PIL TrueType Font, downloading to ROOT dir if necessary
  40. font = Path(font)
  41. font = font if font.exists() else (ROOT / font.name)
  42. try:
  43. return ImageFont.truetype(str(font) if font.exists() else font.name, size)
  44. except Exception as e: # download if missing
  45. url = "https://ultralytics.com/assets/" + font.name
  46. print(f'Downloading {url} to {font}...')
  47. torch.hub.download_url_to_file(url, str(font))
  48. return ImageFont.truetype(str(font), size)
  49. class Annotator:
  50. check_font() # download TTF if necessary
  51. # YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
  52. def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
  53. assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
  54. self.pil = pil
  55. if self.pil: # use PIL
  56. self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
  57. self.draw = ImageDraw.Draw(self.im)
  58. self.font = check_font(font, size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
  59. self.fh = self.font.getsize('a')[1] - 3 # font height
  60. else: # use cv2
  61. self.im = im
  62. self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
  63. def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
  64. # Add one xyxy box to image with label
  65. if self.pil or not is_ascii(label):
  66. self.draw.rectangle(box, width=self.lw, outline=color) # box
  67. if label:
  68. w = self.font.getsize(label)[0] # text width
  69. self.draw.rectangle([box[0], box[1] - self.fh, box[0] + w + 1, box[1] + 1], fill=color)
  70. self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls')
  71. else: # cv2
  72. c1, c2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
  73. cv2.rectangle(self.im, c1, c2, color, thickness=self.lw, lineType=cv2.LINE_AA)
  74. if label:
  75. tf = max(self.lw - 1, 1) # font thickness
  76. w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0]
  77. c2 = c1[0] + w, c1[1] - h - 3
  78. cv2.rectangle(self.im, c1, c2, color, -1, cv2.LINE_AA) # filled
  79. cv2.putText(self.im, label, (c1[0], c1[1] - 2), 0, self.lw / 3, txt_color, thickness=tf,
  80. lineType=cv2.LINE_AA)
  81. def rectangle(self, xy, fill=None, outline=None, width=1):
  82. # Add rectangle to image (PIL-only)
  83. self.draw.rectangle(xy, fill, outline, width)
  84. def text(self, xy, text, txt_color=(255, 255, 255)):
  85. # Add text to image (PIL-only)
  86. w, h = self.font.getsize(text) # text width, height
  87. self.draw.text((xy[0], xy[1] - h + 1), text, fill=txt_color, font=self.font)
  88. def result(self):
  89. # Return annotated image as array
  90. return np.asarray(self.im)
  91. def hist2d(x, y, n=100):
  92. # 2d histogram used in labels.png and evolve.png
  93. xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
  94. hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
  95. xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
  96. yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
  97. return np.log(hist[xidx, yidx])
  98. def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5):
  99. from scipy.signal import butter, filtfilt
  100. # https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy
  101. def butter_lowpass(cutoff, fs, order):
  102. nyq = 0.5 * fs
  103. normal_cutoff = cutoff / nyq
  104. return butter(order, normal_cutoff, btype='low', analog=False)
  105. b, a = butter_lowpass(cutoff, fs, order=order)
  106. return filtfilt(b, a, data) # forward-backward filter
  107. def output_to_target(output):
  108. # Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
  109. targets = []
  110. for i, o in enumerate(output):
  111. for *box, conf, cls in o.cpu().numpy():
  112. targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf])
  113. return np.array(targets)
  114. def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=1920, max_subplots=16):
  115. # Plot image grid with labels
  116. if isinstance(images, torch.Tensor):
  117. images = images.cpu().float().numpy()
  118. if isinstance(targets, torch.Tensor):
  119. targets = targets.cpu().numpy()
  120. if np.max(images[0]) <= 1:
  121. images *= 255.0 # de-normalise (optional)
  122. bs, _, h, w = images.shape # batch size, _, height, width
  123. bs = min(bs, max_subplots) # limit plot images
  124. ns = np.ceil(bs ** 0.5) # number of subplots (square)
  125. # Build Image
  126. mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) # init
  127. for i, im in enumerate(images):
  128. if i == max_subplots: # if last batch has fewer images than we expect
  129. break
  130. x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin
  131. im = im.transpose(1, 2, 0)
  132. mosaic[y:y + h, x:x + w, :] = im
  133. # Resize (optional)
  134. scale = max_size / ns / max(h, w)
  135. if scale < 1:
  136. h = math.ceil(scale * h)
  137. w = math.ceil(scale * w)
  138. mosaic = cv2.resize(mosaic, tuple(int(x * ns) for x in (w, h)))
  139. # Annotate
  140. fs = int((h + w) * ns * 0.01) # font size
  141. annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs)
  142. for i in range(i + 1):
  143. x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin
  144. annotator.rectangle([x, y, x + w, y + h], None, (255, 255, 255), width=2) # borders
  145. if paths:
  146. annotator.text((x + 5, y + 5 + h), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220)) # filenames
  147. if len(targets) > 0:
  148. ti = targets[targets[:, 0] == i] # image targets
  149. boxes = xywh2xyxy(ti[:, 2:6]).T
  150. classes = ti[:, 1].astype('int')
  151. labels = ti.shape[1] == 6 # labels if no conf column
  152. conf = None if labels else ti[:, 6] # check for confidence presence (label vs pred)
  153. if boxes.shape[1]:
  154. if boxes.max() <= 1.01: # if normalized with tolerance 0.01
  155. boxes[[0, 2]] *= w # scale to pixels
  156. boxes[[1, 3]] *= h
  157. elif scale < 1: # absolute coords need scale if image scales
  158. boxes *= scale
  159. boxes[[0, 2]] += x
  160. boxes[[1, 3]] += y
  161. for j, box in enumerate(boxes.T.tolist()):
  162. cls = classes[j]
  163. color = colors(cls)
  164. cls = names[cls] if names else cls
  165. if labels or conf[j] > 0.25: # 0.25 conf thresh
  166. label = f'{cls}' if labels else f'{cls} {conf[j]:.1f}'
  167. annotator.box_label(box, label, color=color)
  168. annotator.im.save(fname) # save
  169. def plot_lr_scheduler(optimizer, scheduler, epochs=300, save_dir=''):
  170. # Plot LR simulating training for full epochs
  171. optimizer, scheduler = copy(optimizer), copy(scheduler) # do not modify originals
  172. y = []
  173. for _ in range(epochs):
  174. scheduler.step()
  175. y.append(optimizer.param_groups[0]['lr'])
  176. plt.plot(y, '.-', label='LR')
  177. plt.xlabel('epoch')
  178. plt.ylabel('LR')
  179. plt.grid()
  180. plt.xlim(0, epochs)
  181. plt.ylim(0)
  182. plt.savefig(Path(save_dir) / 'LR.png', dpi=200)
  183. plt.close()
  184. def plot_val_txt(): # from utils.plots import *; plot_val()
  185. # Plot val.txt histograms
  186. x = np.loadtxt('val.txt', dtype=np.float32)
  187. box = xyxy2xywh(x[:, :4])
  188. cx, cy = box[:, 0], box[:, 1]
  189. fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=True)
  190. ax.hist2d(cx, cy, bins=600, cmax=10, cmin=0)
  191. ax.set_aspect('equal')
  192. plt.savefig('hist2d.png', dpi=300)
  193. fig, ax = plt.subplots(1, 2, figsize=(12, 6), tight_layout=True)
  194. ax[0].hist(cx, bins=600)
  195. ax[1].hist(cy, bins=600)
  196. plt.savefig('hist1d.png', dpi=200)
  197. def plot_targets_txt(): # from utils.plots import *; plot_targets_txt()
  198. # Plot targets.txt histograms
  199. x = np.loadtxt('targets.txt', dtype=np.float32).T
  200. s = ['x targets', 'y targets', 'width targets', 'height targets']
  201. fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)
  202. ax = ax.ravel()
  203. for i in range(4):
  204. ax[i].hist(x[i], bins=100, label='%.3g +/- %.3g' % (x[i].mean(), x[i].std()))
  205. ax[i].legend()
  206. ax[i].set_title(s[i])
  207. plt.savefig('targets.jpg', dpi=200)
  208. def plot_study_txt(path='', x=None): # from utils.plots import *; plot_study_txt()
  209. # Plot study.txt generated by val.py
  210. plot2 = False # plot additional results
  211. if plot2:
  212. ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True)[1].ravel()
  213. fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True)
  214. # for f in [Path(path) / f'study_coco_{x}.txt' for x in ['yolov5s6', 'yolov5m6', 'yolov5l6', 'yolov5x6']]:
  215. for f in sorted(Path(path).glob('study*.txt')):
  216. y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T
  217. x = np.arange(y.shape[1]) if x is None else np.array(x)
  218. if plot2:
  219. s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_preprocess (ms/img)', 't_inference (ms/img)', 't_NMS (ms/img)']
  220. for i in range(7):
  221. ax[i].plot(x, y[i], '.-', linewidth=2, markersize=8)
  222. ax[i].set_title(s[i])
  223. j = y[3].argmax() + 1
  224. ax2.plot(y[5, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8,
  225. label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO'))
  226. ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5],
  227. 'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet')
  228. ax2.grid(alpha=0.2)
  229. ax2.set_yticks(np.arange(20, 60, 5))
  230. ax2.set_xlim(0, 57)
  231. ax2.set_ylim(30, 55)
  232. ax2.set_xlabel('GPU Speed (ms/img)')
  233. ax2.set_ylabel('COCO AP val')
  234. ax2.legend(loc='lower right')
  235. plt.savefig(str(Path(path).name) + '.png', dpi=300)
  236. def plot_labels(labels, names=(), save_dir=Path('')):
  237. # plot dataset labels
  238. print('Plotting labels... ')
  239. c, b = labels[:, 0], labels[:, 1:].transpose() # classes, boxes
  240. nc = int(c.max() + 1) # number of classes
  241. x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height'])
  242. # seaborn correlogram
  243. sn.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9))
  244. plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200)
  245. plt.close()
  246. # matplotlib labels
  247. matplotlib.use('svg') # faster
  248. ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel()
  249. y = ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8)
  250. # [y[2].patches[i].set_color([x / 255 for x in colors(i)]) for i in range(nc)] # update colors bug #3195
  251. ax[0].set_ylabel('instances')
  252. if 0 < len(names) < 30:
  253. ax[0].set_xticks(range(len(names)))
  254. ax[0].set_xticklabels(names, rotation=90, fontsize=10)
  255. else:
  256. ax[0].set_xlabel('classes')
  257. sn.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9)
  258. sn.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9)
  259. # rectangles
  260. labels[:, 1:3] = 0.5 # center
  261. labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000
  262. img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255)
  263. for cls, *box in labels[:1000]:
  264. ImageDraw.Draw(img).rectangle(box, width=1, outline=colors(cls)) # plot
  265. ax[1].imshow(img)
  266. ax[1].axis('off')
  267. for a in [0, 1, 2, 3]:
  268. for s in ['top', 'right', 'left', 'bottom']:
  269. ax[a].spines[s].set_visible(False)
  270. plt.savefig(save_dir / 'labels.jpg', dpi=200)
  271. matplotlib.use('Agg')
  272. plt.close()
  273. def profile_idetection(start=0, stop=0, labels=(), save_dir=''):
  274. # Plot iDetection '*.txt' per-image logs. from utils.plots import *; profile_idetection()
  275. ax = plt.subplots(2, 4, figsize=(12, 6), tight_layout=True)[1].ravel()
  276. s = ['Images', 'Free Storage (GB)', 'RAM Usage (GB)', 'Battery', 'dt_raw (ms)', 'dt_smooth (ms)', 'real-world FPS']
  277. files = list(Path(save_dir).glob('frames*.txt'))
  278. for fi, f in enumerate(files):
  279. try:
  280. results = np.loadtxt(f, ndmin=2).T[:, 90:-30] # clip first and last rows
  281. n = results.shape[1] # number of rows
  282. x = np.arange(start, min(stop, n) if stop else n)
  283. results = results[:, x]
  284. t = (results[0] - results[0].min()) # set t0=0s
  285. results[0] = x
  286. for i, a in enumerate(ax):
  287. if i < len(results):
  288. label = labels[fi] if len(labels) else f.stem.replace('frames_', '')
  289. a.plot(t, results[i], marker='.', label=label, linewidth=1, markersize=5)
  290. a.set_title(s[i])
  291. a.set_xlabel('time (s)')
  292. # if fi == len(files) - 1:
  293. # a.set_ylim(bottom=0)
  294. for side in ['top', 'right']:
  295. a.spines[side].set_visible(False)
  296. else:
  297. a.remove()
  298. except Exception as e:
  299. print('Warning: Plotting error for %s; %s' % (f, e))
  300. ax[1].legend()
  301. plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200)
  302. def plot_evolve(evolve_csv='path/to/evolve.csv'): # from utils.plots import *; plot_evolve()
  303. # Plot evolve.csv hyp evolution results
  304. evolve_csv = Path(evolve_csv)
  305. data = pd.read_csv(evolve_csv)
  306. keys = [x.strip() for x in data.columns]
  307. x = data.values
  308. f = fitness(x)
  309. j = np.argmax(f) # max fitness index
  310. plt.figure(figsize=(10, 12), tight_layout=True)
  311. matplotlib.rc('font', **{'size': 8})
  312. for i, k in enumerate(keys[7:]):
  313. v = x[:, 7 + i]
  314. mu = v[j] # best single result
  315. plt.subplot(6, 5, i + 1)
  316. plt.scatter(v, f, c=hist2d(v, f, 20), cmap='viridis', alpha=.8, edgecolors='none')
  317. plt.plot(mu, f.max(), 'k+', markersize=15)
  318. plt.title('%s = %.3g' % (k, mu), fontdict={'size': 9}) # limit to 40 characters
  319. if i % 5 != 0:
  320. plt.yticks([])
  321. print('%15s: %.3g' % (k, mu))
  322. f = evolve_csv.with_suffix('.png') # filename
  323. plt.savefig(f, dpi=200)
  324. plt.close()
  325. print(f'Saved {f}')
  326. def plot_results(file='path/to/results.csv', dir=''):
  327. # Plot training results.csv. Usage: from utils.plots import *; plot_results('path/to/results.csv')
  328. save_dir = Path(file).parent if file else Path(dir)
  329. fig, ax = plt.subplots(2, 5, figsize=(12, 6), tight_layout=True)
  330. ax = ax.ravel()
  331. files = list(save_dir.glob('results*.csv'))
  332. assert len(files), f'No results.csv files found in {save_dir.resolve()}, nothing to plot.'
  333. for fi, f in enumerate(files):
  334. try:
  335. data = pd.read_csv(f)
  336. s = [x.strip() for x in data.columns]
  337. x = data.values[:, 0]
  338. for i, j in enumerate([1, 2, 3, 4, 5, 8, 9, 10, 6, 7]):
  339. y = data.values[:, j]
  340. # y[y == 0] = np.nan # don't show zero values
  341. ax[i].plot(x, y, marker='.', label=f.stem, linewidth=2, markersize=8)
  342. ax[i].set_title(s[j], fontsize=12)
  343. # if j in [8, 9, 10]: # share train and val loss y axes
  344. # ax[i].get_shared_y_axes().join(ax[i], ax[i - 5])
  345. except Exception as e:
  346. print(f'Warning: Plotting error for {f}: {e}')
  347. ax[1].legend()
  348. fig.savefig(save_dir / 'results.png', dpi=200)
  349. plt.close()
  350. def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp')):
  351. """
  352. x: Features to be visualized
  353. module_type: Module type
  354. stage: Module stage within model
  355. n: Maximum number of feature maps to plot
  356. save_dir: Directory to save results
  357. """
  358. if 'Detect' not in module_type:
  359. batch, channels, height, width = x.shape # batch, channels, height, width
  360. if height > 1 and width > 1:
  361. f = f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename
  362. blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels
  363. n = min(n, channels) # number of plots
  364. fig, ax = plt.subplots(math.ceil(n / 8), 8, tight_layout=True) # 8 rows x n/8 cols
  365. ax = ax.ravel()
  366. plt.subplots_adjust(wspace=0.05, hspace=0.05)
  367. for i in range(n):
  368. ax[i].imshow(blocks[i].squeeze()) # cmap='gray'
  369. ax[i].axis('off')
  370. print(f'Saving {save_dir / f}... ({n}/{channels})')
  371. plt.savefig(save_dir / f, dpi=300, bbox_inches='tight')
  372. plt.close()