Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/utils/loggers/__init__.py
Views: 475
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license1"""2Logging utils3"""45import os6import warnings7from threading import Thread89import pkg_resources as pkg10import torch11from torch.utils.tensorboard import SummaryWriter1213from utils.general import colorstr, emojis14from utils.loggers.wandb.wandb_utils import WandbLogger15from utils.plots import plot_images, plot_results16from utils.torch_utils import de_parallel1718LOGGERS = ('csv', 'tb', 'wandb') # text-file, TensorBoard, Weights & Biases19RANK = int(os.getenv('RANK', -1))2021try:22import wandb2324assert hasattr(wandb, '__version__') # verify package import not local dir25if pkg.parse_version(wandb.__version__) >= pkg.parse_version('0.12.2') and RANK in [0, -1]:26try:27wandb_login_success = wandb.login(timeout=30)28except wandb.errors.UsageError: # known non-TTY terminal issue29wandb_login_success = False30if not wandb_login_success:31wandb = None32except (ImportError, AssertionError):33wandb = None343536class Loggers():37# YOLOv5 Loggers class38def __init__(self, save_dir=None, weights=None, opt=None, hyp=None, logger=None, include=LOGGERS):39self.save_dir = save_dir40self.weights = weights41self.opt = opt42self.hyp = hyp43self.logger = logger # for printing results to console44self.include = include45# self.keys = ['train/box_loss', 'train/obj_loss', 'train/cls_loss', # train loss46# 'metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', # metrics47# 'val/box_loss', 'val/obj_loss', 'val/cls_loss', # val loss48# 'x/lr0', 'x/lr1', 'x/lr2'] # params49self.keys = ['train/box_loss', 'train/obj_loss', 'train/cls_loss', 'train/theta_loss', # train loss50'metrics/precision', 'metrics/recall', 'metrics/HBBmAP.5', 'metrics/HBBmAP.5:.95', # metrics51'val/box_loss', 'val/obj_loss', 'val/cls_loss', 'val/theta_loss', # val loss52'x/lr0', 'x/lr1', 'x/lr2'] # params53for k in LOGGERS:54setattr(self, k, None) # init empty logger dictionary55self.csv = True # always log to csv5657# Message58if not wandb:59prefix = colorstr('Weights & Biases: ')60s = f"{prefix}run 'pip install wandb' to automatically track and visualize YOLOv5 🚀 runs (RECOMMENDED)"61print(emojis(s))6263# TensorBoard64s = self.save_dir65if 'tb' in self.include and not self.opt.evolve:66prefix = colorstr('TensorBoard: ')67self.logger.info(f"{prefix}Start with 'tensorboard --logdir {s.parent}', view at http://localhost:6006/")68self.tb = SummaryWriter(str(s))6970# W&B71if wandb and 'wandb' in self.include:72wandb_artifact_resume = isinstance(self.opt.resume, str) and self.opt.resume.startswith('wandb-artifact://')73run_id = torch.load(self.weights).get('wandb_id') if self.opt.resume and not wandb_artifact_resume else None74self.opt.hyp = self.hyp # add hyperparameters75self.wandb = WandbLogger(self.opt, run_id)76else:77self.wandb = None7879def on_pretrain_routine_end(self):80# Callback runs on pre-train routine end81paths = self.save_dir.glob('*labels*.jpg') # training labels82if self.wandb:83self.wandb.log({"Labels": [wandb.Image(str(x), caption=x.name) for x in paths]})8485def on_train_batch_end(self, ni, model, imgs, targets, paths, plots, sync_bn):86"""87Args:88imgs (tensor): (b, 3, height, width)89targets (tensor): (n_targets, [img_index clsid cx cy l s theta gaussian_θ_labels])90paths (list[str,...]): (b)91"""92# Callback runs on train batch end93if plots:94if ni == 0:95if not sync_bn: # tb.add_graph() --sync known issue https://github.com/ultralytics/yolov5/issues/375496with warnings.catch_warnings():97warnings.simplefilter('ignore') # suppress jit trace warning98self.tb.add_graph(torch.jit.trace(de_parallel(model), imgs[0:1], strict=False), [])99if ni < 8:100f = self.save_dir / f'train_batch{ni}.jpg' # filename101Thread(target=plot_images, args=(imgs, targets, paths, f), daemon=True).start()102if self.wandb and ni == 10:103files = sorted(self.save_dir.glob('train*.jpg'))104self.wandb.log({'Mosaics': [wandb.Image(str(f), caption=f.name) for f in files if f.exists()]})105106def on_train_epoch_end(self, epoch):107# Callback runs on train epoch end108if self.wandb:109self.wandb.current_epoch = epoch + 1110111def on_val_image_end(self, pred, predn, path, names, im):112# Callback runs on val image end113if self.wandb:114self.wandb.val_one_image(pred, predn, path, names, im)115116def on_val_end(self):117# Callback runs on val end118if self.wandb:119files = sorted(self.save_dir.glob('val*.jpg'))120self.wandb.log({"Validation": [wandb.Image(str(f), caption=f.name) for f in files]})121122def on_fit_epoch_end(self, vals, epoch, best_fitness, fi):123# Callback runs at the end of each fit (train+val) epoch124x = {k: v for k, v in zip(self.keys, vals)} # dict125if self.csv:126file = self.save_dir / 'results.csv'127n = len(x) + 1 # number of cols128s = '' if file.exists() else (('%20s,' * n % tuple(['epoch'] + self.keys)).rstrip(',') + '\n') # add header129with open(file, 'a') as f:130f.write(s + ('%20.5g,' * n % tuple([epoch] + vals)).rstrip(',') + '\n')131132if self.tb:133for k, v in x.items():134self.tb.add_scalar(k, v, epoch)135136if self.wandb:137self.wandb.log(x)138self.wandb.end_epoch(best_result=best_fitness == fi)139140def on_model_save(self, last, epoch, final_epoch, best_fitness, fi):141# Callback runs on model save event142if self.wandb:143if ((epoch + 1) % self.opt.save_period == 0 and not final_epoch) and self.opt.save_period != -1:144self.wandb.log_model(last.parent, self.opt, epoch, fi, best_model=best_fitness == fi)145146def on_train_end(self, last, best, plots, epoch, results):147# Callback runs on training end148if plots:149plot_results(file=self.save_dir / 'results.csv') # save results.png150files = ['results.png', 'confusion_matrix.png', *(f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R'))]151files = [(self.save_dir / f) for f in files if (self.save_dir / f).exists()] # filter152153if self.tb:154import cv2155for f in files:156self.tb.add_image(f.stem, cv2.imread(str(f))[..., ::-1], epoch, dataformats='HWC')157158if self.wandb:159self.wandb.log({"Results": [wandb.Image(str(f), caption=f.name) for f in files]})160# Calling wandb.log. TODO: Refactor this into WandbLogger.log_model161if not self.opt.evolve:162wandb.log_artifact(str(best if best.exists() else last), type='model',163name='run_' + self.wandb.wandb_run.id + '_model',164aliases=['latest', 'best', 'stripped'])165self.wandb.finish_run()166else:167self.wandb.finish_run()168self.wandb = WandbLogger(self.opt)169170def on_params_update(self, params):171# Update hyperparams or configs of the experiment172# params: A dict containing {param: value} pairs173if self.wandb:174self.wandb.wandb_run.config.update(params, allow_val_change=True)175176177