|
|
|
""" |
|
Logging utils |
|
""" |
|
|
|
import os |
|
import warnings |
|
|
|
import pkg_resources as pkg |
|
import torch |
|
from torch.utils.tensorboard import SummaryWriter |
|
|
|
from utils.general import colorstr, cv2 |
|
from utils.loggers.clearml.clearml_utils import ClearmlLogger |
|
from utils.loggers.wandb.wandb_utils import WandbLogger |
|
from utils.plots import plot_images, plot_results |
|
from utils.torch_utils import de_parallel |
|
|
|
LOGGERS = ('csv', 'tb', 'wandb', 'clearml') |
|
RANK = int(os.getenv('RANK', -1)) |
|
|
|
try: |
|
import wandb |
|
|
|
assert hasattr(wandb, '__version__') |
|
if pkg.parse_version(wandb.__version__) >= pkg.parse_version('0.12.2') and RANK in {0, -1}: |
|
try: |
|
wandb_login_success = wandb.login(timeout=30) |
|
except wandb.errors.UsageError: |
|
wandb_login_success = False |
|
if not wandb_login_success: |
|
wandb = None |
|
except (ImportError, AssertionError): |
|
wandb = None |
|
|
|
try: |
|
import clearml |
|
|
|
assert hasattr(clearml, '__version__') |
|
except (ImportError, AssertionError): |
|
clearml = None |
|
|
|
|
|
class Loggers(): |
|
|
|
def __init__(self, save_dir=None, weights=None, opt=None, hyp=None, logger=None, include=LOGGERS): |
|
self.save_dir = save_dir |
|
self.weights = weights |
|
self.opt = opt |
|
self.hyp = hyp |
|
self.logger = logger |
|
self.include = include |
|
self.keys = [ |
|
'train/box_loss', |
|
'train/obj_loss', |
|
'train/cls_loss', |
|
'metrics/precision', |
|
'metrics/recall', |
|
'metrics/mAP_0.5', |
|
'metrics/mAP_0.5:0.95', |
|
'val/box_loss', |
|
'val/obj_loss', |
|
'val/cls_loss', |
|
'x/lr0', |
|
'x/lr1', |
|
'x/lr2'] |
|
self.best_keys = ['best/epoch', 'best/precision', 'best/recall', 'best/mAP_0.5', 'best/mAP_0.5:0.95'] |
|
for k in LOGGERS: |
|
setattr(self, k, None) |
|
self.csv = True |
|
|
|
|
|
if not wandb: |
|
prefix = colorstr('Weights & Biases: ') |
|
s = f"{prefix}run 'pip install wandb' to automatically track and visualize YOLOv5 🚀 runs in Weights & Biases" |
|
self.logger.info(s) |
|
if not clearml: |
|
prefix = colorstr('ClearML: ') |
|
s = f"{prefix}run 'pip install clearml' to automatically track, visualize and remotely train YOLOv5 🚀 runs in ClearML" |
|
self.logger.info(s) |
|
|
|
|
|
s = self.save_dir |
|
if 'tb' in self.include and not self.opt.evolve: |
|
prefix = colorstr('TensorBoard: ') |
|
self.logger.info(f"{prefix}Start with 'tensorboard --logdir {s.parent}', view at http://localhost:6006/") |
|
self.tb = SummaryWriter(str(s)) |
|
|
|
|
|
if wandb and 'wandb' in self.include: |
|
wandb_artifact_resume = isinstance(self.opt.resume, str) and self.opt.resume.startswith('wandb-artifact://') |
|
run_id = torch.load(self.weights).get('wandb_id') if self.opt.resume and not wandb_artifact_resume else None |
|
self.opt.hyp = self.hyp |
|
self.wandb = WandbLogger(self.opt, run_id) |
|
|
|
if pkg.parse_version(wandb.__version__) >= pkg.parse_version('0.12.11'): |
|
s = "YOLOv5 temporarily requires wandb version 0.12.10 or below. Some features may not work as expected." |
|
self.logger.warning(s) |
|
else: |
|
self.wandb = None |
|
|
|
|
|
if clearml and 'clearml' in self.include: |
|
self.clearml = ClearmlLogger(self.opt, self.hyp) |
|
else: |
|
self.clearml = None |
|
|
|
def on_train_start(self): |
|
|
|
pass |
|
|
|
def on_pretrain_routine_end(self): |
|
|
|
paths = self.save_dir.glob('*labels*.jpg') |
|
if self.wandb: |
|
self.wandb.log({"Labels": [wandb.Image(str(x), caption=x.name) for x in paths]}) |
|
if self.clearml: |
|
pass |
|
|
|
def on_train_batch_end(self, ni, model, imgs, targets, paths, plots): |
|
|
|
|
|
if plots: |
|
if ni == 0: |
|
if self.tb and not self.opt.sync_bn: |
|
with warnings.catch_warnings(): |
|
warnings.simplefilter('ignore') |
|
self.tb.add_graph(torch.jit.trace(de_parallel(model), imgs[0:1], strict=False), []) |
|
if ni < 3: |
|
f = self.save_dir / f'train_batch{ni}.jpg' |
|
plot_images(imgs, targets, paths, f) |
|
if (self.wandb or self.clearml) and ni == 10: |
|
files = sorted(self.save_dir.glob('train*.jpg')) |
|
if self.wandb: |
|
self.wandb.log({'Mosaics': [wandb.Image(str(f), caption=f.name) for f in files if f.exists()]}) |
|
if self.clearml: |
|
self.clearml.log_debug_samples(files, title='Mosaics') |
|
|
|
def on_train_epoch_end(self, epoch): |
|
|
|
if self.wandb: |
|
self.wandb.current_epoch = epoch + 1 |
|
|
|
def on_val_image_end(self, pred, predn, path, names, im): |
|
|
|
if self.wandb: |
|
self.wandb.val_one_image(pred, predn, path, names, im) |
|
if self.clearml: |
|
self.clearml.log_image_with_boxes(path, pred, names, im) |
|
|
|
def on_val_end(self): |
|
|
|
if self.wandb or self.clearml: |
|
files = sorted(self.save_dir.glob('val*.jpg')) |
|
if self.wandb: |
|
self.wandb.log({"Validation": [wandb.Image(str(f), caption=f.name) for f in files]}) |
|
if self.clearml: |
|
self.clearml.log_debug_samples(files, title='Validation') |
|
|
|
def on_fit_epoch_end(self, vals, epoch, best_fitness, fi): |
|
|
|
x = dict(zip(self.keys, vals)) |
|
if self.csv: |
|
file = self.save_dir / 'results.csv' |
|
n = len(x) + 1 |
|
s = '' if file.exists() else (('%20s,' * n % tuple(['epoch'] + self.keys)).rstrip(',') + '\n') |
|
with open(file, 'a') as f: |
|
f.write(s + ('%20.5g,' * n % tuple([epoch] + vals)).rstrip(',') + '\n') |
|
|
|
if self.tb: |
|
for k, v in x.items(): |
|
self.tb.add_scalar(k, v, epoch) |
|
elif self.clearml: |
|
for k, v in x.items(): |
|
title, series = k.split('/') |
|
self.clearml.task.get_logger().report_scalar(title, series, v, epoch) |
|
|
|
if self.wandb: |
|
if best_fitness == fi: |
|
best_results = [epoch] + vals[3:7] |
|
for i, name in enumerate(self.best_keys): |
|
self.wandb.wandb_run.summary[name] = best_results[i] |
|
self.wandb.log(x) |
|
self.wandb.end_epoch(best_result=best_fitness == fi) |
|
|
|
if self.clearml: |
|
self.clearml.current_epoch_logged_images = set() |
|
self.clearml.current_epoch += 1 |
|
|
|
def on_model_save(self, last, epoch, final_epoch, best_fitness, fi): |
|
|
|
if self.wandb: |
|
if ((epoch + 1) % self.opt.save_period == 0 and not final_epoch) and self.opt.save_period != -1: |
|
self.wandb.log_model(last.parent, self.opt, epoch, fi, best_model=best_fitness == fi) |
|
|
|
if self.clearml: |
|
if ((epoch + 1) % self.opt.save_period == 0 and not final_epoch) and self.opt.save_period != -1: |
|
self.clearml.task.update_output_model(model_path=str(last), |
|
model_name='Latest Model', |
|
auto_delete_file=False) |
|
|
|
def on_train_end(self, last, best, plots, epoch, results): |
|
|
|
if plots: |
|
plot_results(file=self.save_dir / 'results.csv') |
|
files = ['results.png', 'confusion_matrix.png', *(f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R'))] |
|
files = [(self.save_dir / f) for f in files if (self.save_dir / f).exists()] |
|
self.logger.info(f"Results saved to {colorstr('bold', self.save_dir)}") |
|
|
|
if self.tb and not self.clearml: |
|
for f in files: |
|
self.tb.add_image(f.stem, cv2.imread(str(f))[..., ::-1], epoch, dataformats='HWC') |
|
|
|
if self.wandb: |
|
self.wandb.log(dict(zip(self.keys[3:10], results))) |
|
self.wandb.log({"Results": [wandb.Image(str(f), caption=f.name) for f in files]}) |
|
|
|
if not self.opt.evolve: |
|
wandb.log_artifact(str(best if best.exists() else last), |
|
type='model', |
|
name=f'run_{self.wandb.wandb_run.id}_model', |
|
aliases=['latest', 'best', 'stripped']) |
|
self.wandb.finish_run() |
|
|
|
if self.clearml: |
|
|
|
if not self.opt.evolve: |
|
self.clearml.task.update_output_model(model_path=str(best if best.exists() else last), |
|
name='Best Model') |
|
|
|
def on_params_update(self, params): |
|
|
|
|
|
if self.wandb: |
|
self.wandb.wandb_run.config.update(params, allow_val_change=True) |
|
|