|
|
|
|
|
import glob |
|
import math |
|
import os |
|
import random |
|
from copy import copy |
|
from pathlib import Path |
|
|
|
import cv2 |
|
import matplotlib |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import pandas as pd |
|
import seaborn as sns |
|
import torch |
|
import yaml |
|
from PIL import Image, ImageDraw, ImageFont |
|
from scipy.signal import butter, filtfilt |
|
|
|
from utils.general import xywh2xyxy, xyxy2xywh |
|
from utils.metrics import fitness |
|
|
|
|
|
matplotlib.rc('font', **{'size': 11}) |
|
matplotlib.use('Agg') |
|
|
|
|
|
def color_list(): |
|
|
|
def hex2rgb(h): |
|
return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) |
|
|
|
return [hex2rgb(h) for h in matplotlib.colors.TABLEAU_COLORS.values()] |
|
|
|
|
|
def hist2d(x, y, n=100): |
|
|
|
xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n) |
|
hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges)) |
|
xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1) |
|
yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1) |
|
return np.log(hist[xidx, yidx]) |
|
|
|
|
|
def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5): |
|
|
|
def butter_lowpass(cutoff, fs, order): |
|
nyq = 0.5 * fs |
|
normal_cutoff = cutoff / nyq |
|
return butter(order, normal_cutoff, btype='low', analog=False) |
|
|
|
b, a = butter_lowpass(cutoff, fs, order=order) |
|
return filtfilt(b, a, data) |
|
|
|
|
|
def plot_one_box(x, img, color=None, label=None, line_thickness=3): |
|
|
|
tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 |
|
color = color or [random.randint(0, 255) for _ in range(3)] |
|
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3])) |
|
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA) |
|
if label: |
|
tf = max(tl - 1, 1) |
|
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] |
|
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3 |
|
cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) |
|
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA) |
|
|
|
|
|
def plot_one_box_PIL(box, img, color=None, label=None, line_thickness=None): |
|
img = Image.fromarray(img) |
|
draw = ImageDraw.Draw(img) |
|
line_thickness = line_thickness or max(int(min(img.size) / 200), 2) |
|
draw.rectangle(box, width=line_thickness, outline=tuple(color)) |
|
if label: |
|
fontsize = max(round(max(img.size) / 40), 12) |
|
font = ImageFont.truetype("Arial.ttf", fontsize) |
|
txt_width, txt_height = font.getsize(label) |
|
draw.rectangle([box[0], box[1] - txt_height + 4, box[0] + txt_width, box[1]], fill=tuple(color)) |
|
draw.text((box[0], box[1] - txt_height + 1), label, fill=(255, 255, 255), font=font) |
|
return np.asarray(img) |
|
|
|
|
|
def plot_wh_methods(): |
|
|
|
|
|
x = np.arange(-4.0, 4.0, .1) |
|
ya = np.exp(x) |
|
yb = torch.sigmoid(torch.from_numpy(x)).numpy() * 2 |
|
|
|
fig = plt.figure(figsize=(6, 3), tight_layout=True) |
|
plt.plot(x, ya, '.-', label='YOLOv3') |
|
plt.plot(x, yb ** 2, '.-', label='YOLOR ^2') |
|
plt.plot(x, yb ** 1.6, '.-', label='YOLOR ^1.6') |
|
plt.xlim(left=-4, right=4) |
|
plt.ylim(bottom=0, top=6) |
|
plt.xlabel('input') |
|
plt.ylabel('output') |
|
plt.grid() |
|
plt.legend() |
|
fig.savefig('comparison.png', dpi=200) |
|
|
|
|
|
def output_to_target(output): |
|
|
|
targets = [] |
|
for i, o in enumerate(output): |
|
for *box, conf, cls in o.cpu().numpy(): |
|
targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf]) |
|
return np.array(targets) |
|
|
|
|
|
def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=640, max_subplots=16): |
|
|
|
|
|
if isinstance(images, torch.Tensor): |
|
images = images.cpu().float().numpy() |
|
if isinstance(targets, torch.Tensor): |
|
targets = targets.cpu().numpy() |
|
|
|
|
|
if np.max(images[0]) <= 1: |
|
images *= 255 |
|
|
|
tl = 3 |
|
tf = max(tl - 1, 1) |
|
bs, _, h, w = images.shape |
|
bs = min(bs, max_subplots) |
|
ns = np.ceil(bs ** 0.5) |
|
|
|
|
|
scale_factor = max_size / max(h, w) |
|
if scale_factor < 1: |
|
h = math.ceil(scale_factor * h) |
|
w = math.ceil(scale_factor * w) |
|
|
|
colors = color_list() |
|
mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) |
|
for i, img in enumerate(images): |
|
if i == max_subplots: |
|
break |
|
|
|
block_x = int(w * (i // ns)) |
|
block_y = int(h * (i % ns)) |
|
|
|
img = img.transpose(1, 2, 0) |
|
if scale_factor < 1: |
|
img = cv2.resize(img, (w, h)) |
|
|
|
mosaic[block_y:block_y + h, block_x:block_x + w, :] = img |
|
if len(targets) > 0: |
|
image_targets = targets[targets[:, 0] == i] |
|
boxes = xywh2xyxy(image_targets[:, 2:6]).T |
|
classes = image_targets[:, 1].astype('int') |
|
labels = image_targets.shape[1] == 6 |
|
conf = None if labels else image_targets[:, 6] |
|
|
|
if boxes.shape[1]: |
|
if boxes.max() <= 1.01: |
|
boxes[[0, 2]] *= w |
|
boxes[[1, 3]] *= h |
|
elif scale_factor < 1: |
|
boxes *= scale_factor |
|
boxes[[0, 2]] += block_x |
|
boxes[[1, 3]] += block_y |
|
for j, box in enumerate(boxes.T): |
|
cls = int(classes[j]) |
|
color = colors[cls % len(colors)] |
|
cls = names[cls] if names else cls |
|
if labels or conf[j] > 0.25: |
|
label = '%s' % cls if labels else '%s %.1f' % (cls, conf[j]) |
|
plot_one_box(box, mosaic, label=label, color=color, line_thickness=tl) |
|
|
|
|
|
if paths: |
|
label = Path(paths[i]).name[:40] |
|
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] |
|
cv2.putText(mosaic, label, (block_x + 5, block_y + t_size[1] + 5), 0, tl / 3, [220, 220, 220], thickness=tf, |
|
lineType=cv2.LINE_AA) |
|
|
|
|
|
cv2.rectangle(mosaic, (block_x, block_y), (block_x + w, block_y + h), (255, 255, 255), thickness=3) |
|
|
|
if fname: |
|
r = min(1280. / max(h, w) / ns, 1.0) |
|
mosaic = cv2.resize(mosaic, (int(ns * w * r), int(ns * h * r)), interpolation=cv2.INTER_AREA) |
|
|
|
Image.fromarray(mosaic).save(fname) |
|
return mosaic |
|
|
|
|
|
def plot_lr_scheduler(optimizer, scheduler, epochs=300, save_dir=''): |
|
|
|
optimizer, scheduler = copy(optimizer), copy(scheduler) |
|
y = [] |
|
for _ in range(epochs): |
|
scheduler.step() |
|
y.append(optimizer.param_groups[0]['lr']) |
|
plt.plot(y, '.-', label='LR') |
|
plt.xlabel('epoch') |
|
plt.ylabel('LR') |
|
plt.grid() |
|
plt.xlim(0, epochs) |
|
plt.ylim(0) |
|
plt.savefig(Path(save_dir) / 'LR.png', dpi=200) |
|
plt.close() |
|
|
|
|
|
def plot_test_txt(): |
|
|
|
x = np.loadtxt('test.txt', dtype=np.float32) |
|
box = xyxy2xywh(x[:, :4]) |
|
cx, cy = box[:, 0], box[:, 1] |
|
|
|
fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=True) |
|
ax.hist2d(cx, cy, bins=600, cmax=10, cmin=0) |
|
ax.set_aspect('equal') |
|
plt.savefig('hist2d.png', dpi=300) |
|
|
|
fig, ax = plt.subplots(1, 2, figsize=(12, 6), tight_layout=True) |
|
ax[0].hist(cx, bins=600) |
|
ax[1].hist(cy, bins=600) |
|
plt.savefig('hist1d.png', dpi=200) |
|
|
|
|
|
def plot_targets_txt(): |
|
|
|
x = np.loadtxt('targets.txt', dtype=np.float32).T |
|
s = ['x targets', 'y targets', 'width targets', 'height targets'] |
|
fig, ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True) |
|
ax = ax.ravel() |
|
for i in range(4): |
|
ax[i].hist(x[i], bins=100, label='%.3g +/- %.3g' % (x[i].mean(), x[i].std())) |
|
ax[i].legend() |
|
ax[i].set_title(s[i]) |
|
plt.savefig('targets.jpg', dpi=200) |
|
|
|
|
|
def plot_study_txt(path='', x=None): |
|
|
|
fig, ax = plt.subplots(2, 4, figsize=(10, 6), tight_layout=True) |
|
|
|
|
|
fig2, ax2 = plt.subplots(1, 1, figsize=(8, 4), tight_layout=True) |
|
|
|
for f in sorted(Path(path).glob('study*.txt')): |
|
y = np.loadtxt(f, dtype=np.float32, usecols=[0, 1, 2, 3, 7, 8, 9], ndmin=2).T |
|
x = np.arange(y.shape[1]) if x is None else np.array(x) |
|
s = ['P', 'R', 'mAP@.5', 'mAP@.5:.95', 't_inference (ms/img)', 't_NMS (ms/img)', 't_total (ms/img)'] |
|
|
|
|
|
|
|
|
|
j = y[3].argmax() + 1 |
|
ax2.plot(y[6, 1:j], y[3, 1:j] * 1E2, '.-', linewidth=2, markersize=8, |
|
label=f.stem.replace('study_coco_', '').replace('yolo', 'YOLO')) |
|
|
|
ax2.plot(1E3 / np.array([209, 140, 97, 58, 35, 18]), [34.6, 40.5, 43.0, 47.5, 49.7, 51.5], |
|
'k.-', linewidth=2, markersize=8, alpha=.25, label='EfficientDet') |
|
|
|
ax2.grid(alpha=0.2) |
|
ax2.set_yticks(np.arange(20, 60, 5)) |
|
ax2.set_xlim(0, 57) |
|
ax2.set_ylim(30, 55) |
|
ax2.set_xlabel('GPU Speed (ms/img)') |
|
ax2.set_ylabel('COCO AP val') |
|
ax2.legend(loc='lower right') |
|
plt.savefig(str(Path(path).name) + '.png', dpi=300) |
|
|
|
|
|
def plot_labels(labels, names=(), save_dir=Path(''), loggers=None): |
|
|
|
print('Plotting labels... ') |
|
c, b = labels[:, 0], labels[:, 1:].transpose() |
|
nc = int(c.max() + 1) |
|
colors = color_list() |
|
x = pd.DataFrame(b.transpose(), columns=['x', 'y', 'width', 'height']) |
|
|
|
|
|
sns.pairplot(x, corner=True, diag_kind='auto', kind='hist', diag_kws=dict(bins=50), plot_kws=dict(pmax=0.9)) |
|
plt.savefig(save_dir / 'labels_correlogram.jpg', dpi=200) |
|
plt.close() |
|
|
|
|
|
matplotlib.use('svg') |
|
ax = plt.subplots(2, 2, figsize=(8, 8), tight_layout=True)[1].ravel() |
|
ax[0].hist(c, bins=np.linspace(0, nc, nc + 1) - 0.5, rwidth=0.8) |
|
ax[0].set_ylabel('instances') |
|
if 0 < len(names) < 30: |
|
ax[0].set_xticks(range(len(names))) |
|
ax[0].set_xticklabels(names, rotation=90, fontsize=10) |
|
else: |
|
ax[0].set_xlabel('classes') |
|
sns.histplot(x, x='x', y='y', ax=ax[2], bins=50, pmax=0.9) |
|
sns.histplot(x, x='width', y='height', ax=ax[3], bins=50, pmax=0.9) |
|
|
|
|
|
labels[:, 1:3] = 0.5 |
|
labels[:, 1:] = xywh2xyxy(labels[:, 1:]) * 2000 |
|
img = Image.fromarray(np.ones((2000, 2000, 3), dtype=np.uint8) * 255) |
|
for cls, *box in labels[:1000]: |
|
ImageDraw.Draw(img).rectangle(box, width=1, outline=colors[int(cls) % 10]) |
|
ax[1].imshow(img) |
|
ax[1].axis('off') |
|
|
|
for a in [0, 1, 2, 3]: |
|
for s in ['top', 'right', 'left', 'bottom']: |
|
ax[a].spines[s].set_visible(False) |
|
|
|
plt.savefig(save_dir / 'labels.jpg', dpi=200) |
|
matplotlib.use('Agg') |
|
plt.close() |
|
|
|
|
|
for k, v in loggers.items() or {}: |
|
if k == 'wandb' and v: |
|
v.log({"Labels": [v.Image(str(x), caption=x.name) for x in save_dir.glob('*labels*.jpg')]}, commit=False) |
|
|
|
|
|
def plot_evolution(yaml_file='data/hyp.finetune.yaml'): |
|
|
|
with open(yaml_file) as f: |
|
hyp = yaml.load(f, Loader=yaml.SafeLoader) |
|
x = np.loadtxt('evolve.txt', ndmin=2) |
|
f = fitness(x) |
|
|
|
plt.figure(figsize=(10, 12), tight_layout=True) |
|
matplotlib.rc('font', **{'size': 8}) |
|
for i, (k, v) in enumerate(hyp.items()): |
|
y = x[:, i + 7] |
|
|
|
mu = y[f.argmax()] |
|
plt.subplot(6, 5, i + 1) |
|
plt.scatter(y, f, c=hist2d(y, f, 20), cmap='viridis', alpha=.8, edgecolors='none') |
|
plt.plot(mu, f.max(), 'k+', markersize=15) |
|
plt.title('%s = %.3g' % (k, mu), fontdict={'size': 9}) |
|
if i % 5 != 0: |
|
plt.yticks([]) |
|
print('%15s: %.3g' % (k, mu)) |
|
plt.savefig('evolve.png', dpi=200) |
|
print('\nPlot saved as evolve.png') |
|
|
|
|
|
def profile_idetection(start=0, stop=0, labels=(), save_dir=''): |
|
|
|
ax = plt.subplots(2, 4, figsize=(12, 6), tight_layout=True)[1].ravel() |
|
s = ['Images', 'Free Storage (GB)', 'RAM Usage (GB)', 'Battery', 'dt_raw (ms)', 'dt_smooth (ms)', 'real-world FPS'] |
|
files = list(Path(save_dir).glob('frames*.txt')) |
|
for fi, f in enumerate(files): |
|
try: |
|
results = np.loadtxt(f, ndmin=2).T[:, 90:-30] |
|
n = results.shape[1] |
|
x = np.arange(start, min(stop, n) if stop else n) |
|
results = results[:, x] |
|
t = (results[0] - results[0].min()) |
|
results[0] = x |
|
for i, a in enumerate(ax): |
|
if i < len(results): |
|
label = labels[fi] if len(labels) else f.stem.replace('frames_', '') |
|
a.plot(t, results[i], marker='.', label=label, linewidth=1, markersize=5) |
|
a.set_title(s[i]) |
|
a.set_xlabel('time (s)') |
|
|
|
|
|
for side in ['top', 'right']: |
|
a.spines[side].set_visible(False) |
|
else: |
|
a.remove() |
|
except Exception as e: |
|
print('Warning: Plotting error for %s; %s' % (f, e)) |
|
|
|
ax[1].legend() |
|
plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200) |
|
|
|
|
|
def plot_results_overlay(start=0, stop=0): |
|
|
|
s = ['train', 'train', 'train', 'Precision', 'mAP@0.5', 'val', 'val', 'val', 'Recall', 'mAP@0.5:0.95'] |
|
t = ['Box', 'Objectness', 'Classification', 'P-R', 'mAP-F1'] |
|
for f in sorted(glob.glob('results*.txt') + glob.glob('../../Downloads/results*.txt')): |
|
results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T |
|
n = results.shape[1] |
|
x = range(start, min(stop, n) if stop else n) |
|
fig, ax = plt.subplots(1, 5, figsize=(14, 3.5), tight_layout=True) |
|
ax = ax.ravel() |
|
for i in range(5): |
|
for j in [i, i + 5]: |
|
y = results[j, x] |
|
ax[i].plot(x, y, marker='.', label=s[j]) |
|
|
|
|
|
|
|
ax[i].set_title(t[i]) |
|
ax[i].legend() |
|
ax[i].set_ylabel(f) if i == 0 else None |
|
fig.savefig(f.replace('.txt', '.png'), dpi=200) |
|
|
|
|
|
def plot_results(start=0, stop=0, bucket='', id=(), labels=(), save_dir=''): |
|
|
|
fig, ax = plt.subplots(2, 5, figsize=(12, 6), tight_layout=True) |
|
ax = ax.ravel() |
|
s = ['Box', 'Objectness', 'Classification', 'Precision', 'Recall', |
|
'val Box', 'val Objectness', 'val Classification', 'mAP@0.5', 'mAP@0.5:0.95'] |
|
if bucket: |
|
|
|
files = ['results%g.txt' % x for x in id] |
|
c = ('gsutil cp ' + '%s ' * len(files) + '.') % tuple('gs://%s/results%g.txt' % (bucket, x) for x in id) |
|
os.system(c) |
|
else: |
|
files = list(Path(save_dir).glob('results*.txt')) |
|
assert len(files), 'No results.txt files found in %s, nothing to plot.' % os.path.abspath(save_dir) |
|
for fi, f in enumerate(files): |
|
try: |
|
results = np.loadtxt(f, usecols=[2, 3, 4, 8, 9, 12, 13, 14, 10, 11], ndmin=2).T |
|
n = results.shape[1] |
|
x = range(start, min(stop, n) if stop else n) |
|
for i in range(10): |
|
y = results[i, x] |
|
if i in [0, 1, 2, 5, 6, 7]: |
|
y[y == 0] = np.nan |
|
|
|
label = labels[fi] if len(labels) else f.stem |
|
ax[i].plot(x, y, marker='.', label=label, linewidth=2, markersize=8) |
|
ax[i].set_title(s[i]) |
|
|
|
|
|
except Exception as e: |
|
print('Warning: Plotting error for %s; %s' % (f, e)) |
|
|
|
ax[1].legend() |
|
fig.savefig(Path(save_dir) / 'results.png', dpi=200) |
|
|
|
|
|
def output_to_keypoint(output): |
|
|
|
targets = [] |
|
for i, o in enumerate(output): |
|
kpts = o[:,6:] |
|
o = o[:,:6] |
|
for index, (*box, conf, cls) in enumerate(o.detach().cpu().numpy()): |
|
targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf, *list(kpts.detach().cpu().numpy()[index])]) |
|
return np.array(targets) |
|
|
|
|
|
def plot_skeleton_kpts(im, kpts, steps, orig_shape=None): |
|
|
|
palette = np.array([[255, 128, 0], [255, 153, 51], [255, 178, 102], |
|
[230, 230, 0], [255, 153, 255], [153, 204, 255], |
|
[255, 102, 255], [255, 51, 255], [102, 178, 255], |
|
[51, 153, 255], [255, 153, 153], [255, 102, 102], |
|
[255, 51, 51], [153, 255, 153], [102, 255, 102], |
|
[51, 255, 51], [0, 255, 0], [0, 0, 255], [255, 0, 0], |
|
[255, 255, 255]]) |
|
|
|
skeleton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], |
|
[7, 13], [6, 7], [6, 8], [7, 9], [8, 10], [9, 11], [2, 3], |
|
[1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]] |
|
|
|
pose_limb_color = palette[[9, 9, 9, 9, 7, 7, 7, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 16]] |
|
pose_kpt_color = palette[[16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9, 9]] |
|
radius = 5 |
|
num_kpts = len(kpts) // steps |
|
|
|
for kid in range(num_kpts): |
|
r, g, b = pose_kpt_color[kid] |
|
x_coord, y_coord = kpts[steps * kid], kpts[steps * kid + 1] |
|
if not (x_coord % 640 == 0 or y_coord % 640 == 0): |
|
if steps == 3: |
|
conf = kpts[steps * kid + 2] |
|
if conf < 0.5: |
|
continue |
|
cv2.circle(im, (int(x_coord), int(y_coord)), radius, (int(r), int(g), int(b)), -1) |
|
|
|
for sk_id, sk in enumerate(skeleton): |
|
r, g, b = pose_limb_color[sk_id] |
|
pos1 = (int(kpts[(sk[0]-1)*steps]), int(kpts[(sk[0]-1)*steps+1])) |
|
pos2 = (int(kpts[(sk[1]-1)*steps]), int(kpts[(sk[1]-1)*steps+1])) |
|
if steps == 3: |
|
conf1 = kpts[(sk[0]-1)*steps+2] |
|
conf2 = kpts[(sk[1]-1)*steps+2] |
|
if conf1<0.5 or conf2<0.5: |
|
continue |
|
if pos1[0]%640 == 0 or pos1[1]%640==0 or pos1[0]<0 or pos1[1]<0: |
|
continue |
|
if pos2[0] % 640 == 0 or pos2[1] % 640 == 0 or pos2[0]<0 or pos2[1]<0: |
|
continue |
|
cv2.line(im, pos1, pos2, (int(r), int(g), int(b)), thickness=2) |
|
|