Fix `save_one_box()` (#5545)
* Fix `save_one_box()` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
60c8a4f696
commit
3f64ad1760
|
|
@ -26,9 +26,9 @@ ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
||||||
from models.experimental import attempt_load
|
from models.experimental import attempt_load
|
||||||
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
|
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
|
||||||
from utils.general import (LOGGER, apply_classifier, check_file, check_img_size, check_imshow, check_requirements,
|
from utils.general import (LOGGER, apply_classifier, check_file, check_img_size, check_imshow, check_requirements,
|
||||||
check_suffix, colorstr, increment_path, non_max_suppression, print_args, save_one_box,
|
check_suffix, colorstr, increment_path, non_max_suppression, print_args, scale_coords,
|
||||||
scale_coords, strip_optimizer, xyxy2xywh)
|
strip_optimizer, xyxy2xywh)
|
||||||
from utils.plots import Annotator, colors
|
from utils.plots import Annotator, colors, save_one_box
|
||||||
from utils.torch_utils import load_classifier, select_device, time_sync
|
from utils.torch_utils import load_classifier, select_device, time_sync
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,8 @@ from PIL import Image
|
||||||
from torch.cuda import amp
|
from torch.cuda import amp
|
||||||
|
|
||||||
from utils.datasets import exif_transpose, letterbox
|
from utils.datasets import exif_transpose, letterbox
|
||||||
from utils.general import (colorstr, increment_path, make_divisible, non_max_suppression, save_one_box, scale_coords,
|
from utils.general import colorstr, increment_path, make_divisible, non_max_suppression, scale_coords, xyxy2xywh
|
||||||
xyxy2xywh)
|
from utils.plots import Annotator, colors, save_one_box
|
||||||
from utils.plots import Annotator, colors
|
|
||||||
from utils.torch_utils import time_sync
|
from utils.torch_utils import time_sync
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
|
||||||
|
|
@ -819,21 +819,6 @@ def apply_classifier(x, model, img, im0):
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False, save=True):
|
|
||||||
# Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop
|
|
||||||
xyxy = torch.tensor(xyxy).view(-1, 4)
|
|
||||||
b = xyxy2xywh(xyxy) # boxes
|
|
||||||
if square:
|
|
||||||
b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # attempt rectangle to square
|
|
||||||
b[:, 2:] = b[:, 2:] * gain + pad # box wh * gain + pad
|
|
||||||
xyxy = xywh2xyxy(b).long()
|
|
||||||
clip_coords(xyxy, im.shape)
|
|
||||||
crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)]
|
|
||||||
if save:
|
|
||||||
cv2.imwrite(str(increment_path(file, mkdir=True).with_suffix('.jpg')), crop)
|
|
||||||
return crop
|
|
||||||
|
|
||||||
|
|
||||||
def increment_path(path, exist_ok=False, sep='', mkdir=False):
|
def increment_path(path, exist_ok=False, sep='', mkdir=False):
|
||||||
# Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.
|
# Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.
|
||||||
path = Path(path) # os-agnostic
|
path = Path(path) # os-agnostic
|
||||||
|
|
|
||||||
126
utils/plots.py
126
utils/plots.py
|
|
@ -17,7 +17,7 @@ import seaborn as sn
|
||||||
import torch
|
import torch
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
|
|
||||||
from utils.general import is_ascii, is_chinese, user_config_dir, xywh2xyxy, xyxy2xywh
|
from utils.general import clip_coords, increment_path, is_ascii, is_chinese, user_config_dir, xywh2xyxy, xyxy2xywh
|
||||||
from utils.metrics import fitness
|
from utils.metrics import fitness
|
||||||
|
|
||||||
# Settings
|
# Settings
|
||||||
|
|
@ -117,6 +117,33 @@ class Annotator:
|
||||||
return np.asarray(self.im)
|
return np.asarray(self.im)
|
||||||
|
|
||||||
|
|
||||||
|
def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp')):
|
||||||
|
"""
|
||||||
|
x: Features to be visualized
|
||||||
|
module_type: Module type
|
||||||
|
stage: Module stage within model
|
||||||
|
n: Maximum number of feature maps to plot
|
||||||
|
save_dir: Directory to save results
|
||||||
|
"""
|
||||||
|
if 'Detect' not in module_type:
|
||||||
|
batch, channels, height, width = x.shape # batch, channels, height, width
|
||||||
|
if height > 1 and width > 1:
|
||||||
|
f = f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename
|
||||||
|
|
||||||
|
blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels
|
||||||
|
n = min(n, channels) # number of plots
|
||||||
|
fig, ax = plt.subplots(math.ceil(n / 8), 8, tight_layout=True) # 8 rows x n/8 cols
|
||||||
|
ax = ax.ravel()
|
||||||
|
plt.subplots_adjust(wspace=0.05, hspace=0.05)
|
||||||
|
for i in range(n):
|
||||||
|
ax[i].imshow(blocks[i].squeeze()) # cmap='gray'
|
||||||
|
ax[i].axis('off')
|
||||||
|
|
||||||
|
print(f'Saving {save_dir / f}... ({n}/{channels})')
|
||||||
|
plt.savefig(save_dir / f, dpi=300, bbox_inches='tight')
|
||||||
|
plt.close()
|
||||||
|
|
||||||
|
|
||||||
def hist2d(x, y, n=100):
|
def hist2d(x, y, n=100):
|
||||||
# 2d histogram used in labels.png and evolve.png
|
# 2d histogram used in labels.png and evolve.png
|
||||||
xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
|
xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
|
||||||
|
|
@ -337,37 +364,6 @@ def plot_labels(labels, names=(), save_dir=Path('')):
|
||||||
plt.close()
|
plt.close()
|
||||||
|
|
||||||
|
|
||||||
def profile_idetection(start=0, stop=0, labels=(), save_dir=''):
|
|
||||||
# Plot iDetection '*.txt' per-image logs. from utils.plots import *; profile_idetection()
|
|
||||||
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] # clip first and last rows
|
|
||||||
n = results.shape[1] # number of rows
|
|
||||||
x = np.arange(start, min(stop, n) if stop else n)
|
|
||||||
results = results[:, x]
|
|
||||||
t = (results[0] - results[0].min()) # set t0=0s
|
|
||||||
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)')
|
|
||||||
# if fi == len(files) - 1:
|
|
||||||
# a.set_ylim(bottom=0)
|
|
||||||
for side in ['top', 'right']:
|
|
||||||
a.spines[side].set_visible(False)
|
|
||||||
else:
|
|
||||||
a.remove()
|
|
||||||
except Exception as e:
|
|
||||||
print(f'Warning: Plotting error for {f}; {e}')
|
|
||||||
ax[1].legend()
|
|
||||||
plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200)
|
|
||||||
|
|
||||||
|
|
||||||
def plot_evolve(evolve_csv='path/to/evolve.csv'): # from utils.plots import *; plot_evolve()
|
def plot_evolve(evolve_csv='path/to/evolve.csv'): # from utils.plots import *; plot_evolve()
|
||||||
# Plot evolve.csv hyp evolution results
|
# Plot evolve.csv hyp evolution results
|
||||||
evolve_csv = Path(evolve_csv)
|
evolve_csv = Path(evolve_csv)
|
||||||
|
|
@ -420,28 +416,48 @@ def plot_results(file='path/to/results.csv', dir=''):
|
||||||
plt.close()
|
plt.close()
|
||||||
|
|
||||||
|
|
||||||
def feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp')):
|
def profile_idetection(start=0, stop=0, labels=(), save_dir=''):
|
||||||
"""
|
# Plot iDetection '*.txt' per-image logs. from utils.plots import *; profile_idetection()
|
||||||
x: Features to be visualized
|
ax = plt.subplots(2, 4, figsize=(12, 6), tight_layout=True)[1].ravel()
|
||||||
module_type: Module type
|
s = ['Images', 'Free Storage (GB)', 'RAM Usage (GB)', 'Battery', 'dt_raw (ms)', 'dt_smooth (ms)', 'real-world FPS']
|
||||||
stage: Module stage within model
|
files = list(Path(save_dir).glob('frames*.txt'))
|
||||||
n: Maximum number of feature maps to plot
|
for fi, f in enumerate(files):
|
||||||
save_dir: Directory to save results
|
try:
|
||||||
"""
|
results = np.loadtxt(f, ndmin=2).T[:, 90:-30] # clip first and last rows
|
||||||
if 'Detect' not in module_type:
|
n = results.shape[1] # number of rows
|
||||||
batch, channels, height, width = x.shape # batch, channels, height, width
|
x = np.arange(start, min(stop, n) if stop else n)
|
||||||
if height > 1 and width > 1:
|
results = results[:, x]
|
||||||
f = f"stage{stage}_{module_type.split('.')[-1]}_features.png" # filename
|
t = (results[0] - results[0].min()) # set t0=0s
|
||||||
|
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)')
|
||||||
|
# if fi == len(files) - 1:
|
||||||
|
# a.set_ylim(bottom=0)
|
||||||
|
for side in ['top', 'right']:
|
||||||
|
a.spines[side].set_visible(False)
|
||||||
|
else:
|
||||||
|
a.remove()
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Warning: Plotting error for {f}; {e}')
|
||||||
|
ax[1].legend()
|
||||||
|
plt.savefig(Path(save_dir) / 'idetection_profile.png', dpi=200)
|
||||||
|
|
||||||
blocks = torch.chunk(x[0].cpu(), channels, dim=0) # select batch index 0, block by channels
|
|
||||||
n = min(n, channels) # number of plots
|
|
||||||
fig, ax = plt.subplots(math.ceil(n / 8), 8, tight_layout=True) # 8 rows x n/8 cols
|
|
||||||
ax = ax.ravel()
|
|
||||||
plt.subplots_adjust(wspace=0.05, hspace=0.05)
|
|
||||||
for i in range(n):
|
|
||||||
ax[i].imshow(blocks[i].squeeze()) # cmap='gray'
|
|
||||||
ax[i].axis('off')
|
|
||||||
|
|
||||||
print(f'Saving {save_dir / f}... ({n}/{channels})')
|
def save_one_box(xyxy, im, file='image.jpg', gain=1.02, pad=10, square=False, BGR=False, save=True):
|
||||||
plt.savefig(save_dir / f, dpi=300, bbox_inches='tight')
|
# Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop
|
||||||
plt.close()
|
xyxy = torch.tensor(xyxy).view(-1, 4)
|
||||||
|
b = xyxy2xywh(xyxy) # boxes
|
||||||
|
if square:
|
||||||
|
b[:, 2:] = b[:, 2:].max(1)[0].unsqueeze(1) # attempt rectangle to square
|
||||||
|
b[:, 2:] = b[:, 2:] * gain + pad # box wh * gain + pad
|
||||||
|
xyxy = xywh2xyxy(b).long()
|
||||||
|
clip_coords(xyxy, im.shape)
|
||||||
|
crop = im[int(xyxy[0, 1]):int(xyxy[0, 3]), int(xyxy[0, 0]):int(xyxy[0, 2]), ::(1 if BGR else -1)]
|
||||||
|
if save:
|
||||||
|
file.parent.mkdir(parents=True, exist_ok=True) # make directory
|
||||||
|
cv2.imwrite(str(increment_path(file).with_suffix('.jpg')), crop)
|
||||||
|
return crop
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue