import hydra import torch import torch.nn as nn from ultralytics.yolo import v8 from ultralytics.yolo.data import build_dataloader from ultralytics.yolo.engine.trainer import DEFAULT_CONFIG, BaseTrainer from ultralytics.yolo.utils.loss import BboxLoss from ultralytics.yolo.utils.metrics import smooth_BCE from ultralytics.yolo.utils.modeling.tasks import DetectionModel from ultralytics.yolo.utils.ops import xywh2xyxy from ultralytics.yolo.utils.plotting import plot_images, plot_results from ultralytics.yolo.utils.tal import TaskAlignedAssigner, dist2bbox, make_anchors from ultralytics.yolo.utils.torch_utils import de_parallel # BaseTrainer python usage class DetectionTrainer(BaseTrainer): def get_dataloader(self, dataset_path, batch_size, mode="train", rank=0): # TODO: manage splits differently # calculate stride - check if model is initialized gs = max(int(de_parallel(self.model).stride.max() if self.model else 0), 32) return build_dataloader(self.args, batch_size, img_path=dataset_path, stride=gs, rank=rank, mode=mode)[0] def preprocess_batch(self, batch): batch["img"] = batch["img"].to(self.device, non_blocking=True).float() / 255 return batch def set_model_attributes(self): nl = de_parallel(self.model).model[-1].nl # number of detection layers (to scale hyps) self.args.box *= 3 / nl # scale to layers self.args.cls *= self.data["nc"] / 80 * 3 / nl # scale to classes and layers self.args.obj *= (self.args.imgsz / 640) ** 2 * 3 / nl # scale to image size and layers self.model.nc = self.data["nc"] # attach number of classes to model self.model.args = self.args # attach hyperparameters to model # TODO: self.model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc self.model.names = self.data["names"] def load_model(self, model_cfg=None, weights=None): model = DetectionModel(model_cfg or weights["model"].yaml, ch=3, nc=self.data["nc"]) if weights: model.load(weights) for _, v in model.named_parameters(): v.requires_grad = True # train all layers return model def get_validator(self): self.loss_names = 'box_loss', 'cls_loss', 'dfl_loss' return v8.detect.DetectionValidator(self.test_loader, save_dir=self.save_dir, logger=self.console, args=self.args) def criterion(self, preds, batch): return Loss(self.model)(preds, batch) def label_loss_items(self, loss_items=None, prefix="train"): # We should just use named tensors here in future keys = [f"{prefix}/{x}" for x in self.loss_names] return dict(zip(keys, loss_items)) if loss_items is not None else keys def progress_string(self): return ('\n' + '%11s' * 6) % \ ('Epoch', 'GPU_mem', *self.loss_names, 'Size') def plot_training_samples(self, batch, ni): images = batch["img"] cls = batch["cls"].squeeze(-1) bboxes = batch["bboxes"] paths = batch["im_file"] batch_idx = batch["batch_idx"] plot_images(images, batch_idx, cls, bboxes, paths=paths, fname=self.save_dir / f"train_batch{ni}.jpg") def plot_metrics(self): plot_results(file=self.csv) # save results.png # Criterion class for computing training losses class Loss: def __init__(self, model): device = next(model.parameters()).device # get model device h = model.args # hyperparameters # Define criteria BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h["cls_pw"]], device=device), reduction='none') # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get("label_smoothing", 0.0)) # positive, negative BCE targets m = de_parallel(model).model[-1] # Detect() module self.BCEcls = BCEcls self.hyp = h self.stride = m.stride # model strides self.nc = m.nc # number of classes self.nl = m.nl # number of layers self.device = device self.use_dfl = m.reg_max > 1 self.assigner = TaskAlignedAssigner(topk=10, num_classes=self.nc, alpha=0.5, beta=6.0) self.bbox_loss = BboxLoss(m.reg_max - 1, use_dfl=self.use_dfl).to(device) self.proj = torch.arange(m.reg_max, dtype=torch.float, device=device) def preprocess(self, targets, batch_size, scale_tensor): if targets.shape[0] == 0: out = torch.zeros(batch_size, 0, 5, device=self.device) else: i = targets[:, 0] # image index _, counts = i.unique(return_counts=True) out = torch.zeros(batch_size, counts.max(), 5, device=self.device) for j in range(batch_size): matches = i == j n = matches.sum() if n: out[j, :n] = targets[matches, 1:] out[..., 1:5] = xywh2xyxy(out[..., 1:5].mul_(scale_tensor)) return out def bbox_decode(self, anchor_points, pred_dist): if self.use_dfl: b, a, c = pred_dist.shape # batch, anchors, channels pred_dist = pred_dist.view(b, a, 4, c // 4).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = pred_dist.view(b, a, c // 4, 4).transpose(2,3).softmax(3).matmul(self.proj.type(pred_dist.dtype)) # pred_dist = (pred_dist.view(b, a, c // 4, 4).softmax(2) * self.proj.type(pred_dist.dtype).view(1, 1, -1, 1)).sum(2) return dist2bbox(pred_dist, anchor_points, xywh=False) def __call__(self, preds, batch): loss = torch.zeros(3, device=self.device) # box, cls, dfl feats, pred_distri, pred_scores = preds if len(preds) == 3 else preds[1] pred_scores = pred_scores.permute(0, 2, 1).contiguous() pred_distri = pred_distri.permute(0, 2, 1).contiguous() dtype = pred_scores.dtype batch_size, grid_size = pred_scores.shape[:2] imgsz = torch.tensor(feats[0].shape[2:], device=self.device, dtype=dtype) * self.stride[0] # image size (h,w) anchor_points, stride_tensor = make_anchors(feats, self.stride, 0.5) # targets targets = torch.cat((batch["batch_idx"].view(-1, 1), batch["cls"].view(-1, 1), batch["bboxes"]), 1) targets = self.preprocess(targets.to(self.device), batch_size, scale_tensor=imgsz[[1, 0, 1, 0]]) gt_labels, gt_bboxes = targets.split((1, 4), 2) # cls, xyxy mask_gt = gt_bboxes.sum(2, keepdim=True).gt_(0) # pboxes pred_bboxes = self.bbox_decode(anchor_points, pred_distri) # xyxy, (b, h*w, 4) target_labels, target_bboxes, target_scores, fg_mask = self.assigner( pred_scores.detach().sigmoid(), (pred_bboxes.detach() * stride_tensor).type(gt_bboxes.dtype), anchor_points * stride_tensor, gt_labels, gt_bboxes, mask_gt) target_bboxes /= stride_tensor target_scores_sum = target_scores.sum() # cls loss # loss[1] = self.varifocal_loss(pred_scores, target_scores, target_labels) / target_scores_sum # VFL way loss[1] = self.BCEcls(pred_scores, target_scores.to(dtype)).sum() / target_scores_sum # BCE # bbox loss if fg_mask.sum(): loss[0], loss[2] = self.bbox_loss(pred_distri, pred_bboxes, anchor_points, target_bboxes, target_scores, target_scores_sum, fg_mask) loss[0] *= 7.5 # box gain loss[1] *= 0.5 # cls gain loss[2] *= 1.5 # dfl gain return loss.sum() * batch_size, loss.detach() # loss(box, cls, dfl) @hydra.main(version_base=None, config_path=DEFAULT_CONFIG.parent, config_name=DEFAULT_CONFIG.name) def train(cfg): cfg.model = cfg.model or "models/yolov8n.yaml" cfg.data = cfg.data or "coco128.yaml" # or yolo.ClassificationDataset("mnist") cfg.imgsz = 160 cfg.epochs = 5 trainer = DetectionTrainer(cfg) trainer.train() if __name__ == "__main__": """ CLI usage: python ultralytics/yolo/v8/detect/train.py model=yolov8n.yaml data=coco128 epochs=100 imgsz=640 TODO: yolo task=detect mode=train model=yolov8n.yaml data=coco128.yaml epochs=100 """ train()