New CSV Logger (#4148)
* New CSV Logger * cleanup * move batch plots into Logger * rename comment * Remove total loss from progress bar * mloss :-1 bug fix * Update plot_results() * Update plot_results() * plot_results bug fix
This commit is contained in:
1 parent
3764277f95
commit
96e36a7c91
6 files changed
+68
-109
No files matched your search
@@ -12,7 +12,6 @@ import sys
|
||||
import time
|
||||
from copy import deepcopy
|
||||
from pathlib import Path
|
||||
from threading import Thread
|
||||
|
||||
import math
|
||||
import numpy as np
|
||||
@@ -38,7 +37,7 @@ from utils.general import labels_to_class_weights, increment_path, labels_to_ima
|
||||
check_requirements, print_mutation, set_logging, one_cycle, colorstr
|
||||
from utils.google_utils import attempt_download
|
||||
from utils.loss import ComputeLoss
|
||||
from utils.plots import plot_images, plot_labels, plot_results, plot_evolution
|
||||
from utils.plots import plot_labels, plot_evolution
|
||||
from utils.torch_utils import ModelEMA, select_device, intersect_dicts, torch_distributed_zero_first, de_parallel
|
||||
from utils.loggers.wandb.wandb_utils import check_wandb_resume
|
||||
from utils.metrics import fitness
|
||||
@@ -61,7 +60,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
# Directories
|
||||
w = save_dir / 'weights' # weights dir
|
||||
w.mkdir(parents=True, exist_ok=True) # make dir
|
||||
last, best, results_file = w / 'last.pt', w / 'best.pt', save_dir / 'results.txt'
|
||||
last, best = w / 'last.pt', w / 'best.pt'
|
||||
|
||||
# Hyperparameters
|
||||
if isinstance(hyp, str):
|
||||
@@ -88,7 +87,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
|
||||
# Loggers
|
||||
if RANK in [-1, 0]:
|
||||
loggers = Loggers(save_dir, results_file, weights, opt, hyp, data_dict, LOGGER).start() # loggers dict
|
||||
loggers = Loggers(save_dir, weights, opt, hyp, data_dict, LOGGER).start() # loggers dict
|
||||
if loggers.wandb and resume:
|
||||
weights, epochs, hyp, data_dict = opt.weights, opt.epochs, opt.hyp, loggers.wandb.data_dict
|
||||
|
||||
@@ -167,10 +166,6 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
ema.ema.load_state_dict(ckpt['ema'].float().state_dict())
|
||||
ema.updates = ckpt['updates']
|
||||
|
||||
# Results
|
||||
if ckpt.get('training_results') is not None:
|
||||
results_file.write_text(ckpt['training_results']) # write results.txt
|
||||
|
||||
# Epochs
|
||||
start_epoch = ckpt['epoch'] + 1
|
||||
if resume:
|
||||
@@ -275,11 +270,11 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
# b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs)
|
||||
# dataset.mosaic_border = [b - imgsz, -b] # height, width borders
|
||||
|
||||
mloss = torch.zeros(4, device=device) # mean losses
|
||||
mloss = torch.zeros(3, device=device) # mean losses
|
||||
if RANK != -1:
|
||||
train_loader.sampler.set_epoch(epoch)
|
||||
pbar = enumerate(train_loader)
|
||||
LOGGER.info(('\n' + '%10s' * 8) % ('Epoch', 'gpu_mem', 'box', 'obj', 'cls', 'total', 'labels', 'img_size'))
|
||||
LOGGER.info(('\n' + '%10s' * 7) % ('Epoch', 'gpu_mem', 'box', 'obj', 'cls', 'labels', 'img_size'))
|
||||
if RANK in [-1, 0]:
|
||||
pbar = tqdm(pbar, total=nb) # progress bar
|
||||
optimizer.zero_grad()
|
||||
@@ -327,20 +322,13 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
ema.update(model)
|
||||
last_opt_step = ni
|
||||
|
||||
# Print
|
||||
# Log
|
||||
if RANK in [-1, 0]:
|
||||
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
|
||||
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
||||
s = ('%10s' * 2 + '%10.4g' * 6) % (
|
||||
f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1])
|
||||
pbar.set_description(s)
|
||||
|
||||
# Plot
|
||||
if plots:
|
||||
if ni < 3:
|
||||
f = save_dir / f'train_batch{ni}.jpg' # filename
|
||||
Thread(target=plot_images, args=(imgs, targets, paths, f), daemon=True).start()
|
||||
loggers.on_train_batch_end(ni, model, imgs)
|
||||
pbar.set_description(('%10s' * 2 + '%10.4g' * 5) % (
|
||||
f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
||||
loggers.on_train_batch_end(ni, model, imgs, targets, paths, plots)
|
||||
|
||||
# end batch ------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -371,13 +359,12 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, mAP@.5, mAP@.5-.95]
|
||||
if fi > best_fitness:
|
||||
best_fitness = fi
|
||||
loggers.on_train_val_end(mloss, results, lr, epoch, s, best_fitness, fi)
|
||||
loggers.on_train_val_end(mloss, results, lr, epoch, best_fitness, fi)
|
||||
|
||||
# Save model
|
||||
if (not nosave) or (final_epoch and not evolve): # if save
|
||||
ckpt = {'epoch': epoch,
|
||||
'best_fitness': best_fitness,
|
||||
'training_results': results_file.read_text(),
|
||||
'model': deepcopy(de_parallel(model)).half(),
|
||||
'ema': deepcopy(ema.ema).half(),
|
||||
'updates': ema.updates,
|
||||
@@ -395,9 +382,6 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
# end training -----------------------------------------------------------------------------------------------------
|
||||
if RANK in [-1, 0]:
|
||||
LOGGER.info(f'{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.\n')
|
||||
if plots:
|
||||
plot_results(save_dir=save_dir) # save as results.png
|
||||
|
||||
if not evolve:
|
||||
if is_coco: # COCO dataset
|
||||
for m in [last, best] if best.exists() else [last]: # speed, mAP tests
|
||||
@@ -411,13 +395,11 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
||||
save_dir=save_dir,
|
||||
save_json=True,
|
||||
plots=False)
|
||||
|
||||
# Strip optimizers
|
||||
for f in last, best:
|
||||
if f.exists():
|
||||
strip_optimizer(f) # strip optimizers
|
||||
|
||||
loggers.on_train_end(last, best)
|
||||
loggers.on_train_end(last, best, plots)
|
||||
|
||||
torch.cuda.empty_cache()
|
||||
return results
|
||||
|
||||
Reference in new issue
Block a user