diff --git a/ultralytics/yolo/data/build.py b/ultralytics/yolo/data/build.py index 3f3b881..3a15670 100644 --- a/ultralytics/yolo/data/build.py +++ b/ultralytics/yolo/data/build.py @@ -65,7 +65,7 @@ def build_dataloader(cfg, batch_size, img_path, stride=32, label_path=None, rank img_size=cfg.img_size, batch_size=batch_size, augment=True if mode == "train" else False, # augmentation - hyp=cfg.get("augment_hyp", None), + hyp=cfg, # TODO: probably add a get_hyps_from_cfg function rect=cfg.rect if mode == "train" else True, # rectangular batches cache=None if cfg.noval else cfg.get("cache", None), single_cls=cfg.get("single_cls", False), diff --git a/ultralytics/yolo/utils/configs/default.yaml b/ultralytics/yolo/utils/configs/default.yaml index ed63482..fd1986b 100644 --- a/ultralytics/yolo/utils/configs/default.yaml +++ b/ultralytics/yolo/utils/configs/default.yaml @@ -83,20 +83,19 @@ fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5) label_smoothing: 0.0 nbs: 64 # nominal batch size # anchors: 3 -augment_hyp: - hsv_h: 0.015 # image HSV-Hue augmentation (fraction) - hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) - hsv_v: 0.4 # image HSV-Value augmentation (fraction) - degrees: 0.0 # image rotation (+/- deg) - translate: 0.1 # image translation (+/- fraction) - scale: 0.5 # image scale (+/- gain) - shear: 0.0 # image shear (+/- deg) - perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 - flipud: 0.0 # image flip up-down (probability) - fliplr: 0.5 # image flip left-right (probability) - mosaic: 1.0 # image mosaic (probability) - mixup: 0.0 # image mixup (probability) - copy_paste: 0.0 # segment copy-paste (probability) +hsv_h: 0.015 # image HSV-Hue augmentation (fraction) +hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) +hsv_v: 0.4 # image HSV-Value augmentation (fraction) +degrees: 0.0 # image rotation (+/- deg) +translate: 0.1 # image translation (+/- fraction) +scale: 0.5 # image scale (+/- gain) +shear: 0.0 # image shear (+/- deg) +perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 +flipud: 0.0 # image flip up-down (probability) +fliplr: 0.5 # image flip left-right (probability) +mosaic: 1.0 # image mosaic (probability) +mixup: 0.0 # image mixup (probability) +copy_paste: 0.0 # segment copy-paste (probability) # Hydra configs -------------------------------------------------------------------------------------------------------- hydra: diff --git a/ultralytics/yolo/v8/detect/val.py b/ultralytics/yolo/v8/detect/val.py index 56c98b9..546219d 100644 --- a/ultralytics/yolo/v8/detect/val.py +++ b/ultralytics/yolo/v8/detect/val.py @@ -9,7 +9,7 @@ from ultralytics.yolo.data import build_dataloader from ultralytics.yolo.engine.trainer import DEFAULT_CONFIG from ultralytics.yolo.engine.validator import BaseValidator from ultralytics.yolo.utils import ops -from ultralytics.yolo.utils.checks import check_file, check_requirements +from ultralytics.yolo.utils.checks import check_file from ultralytics.yolo.utils.files import yaml_load from ultralytics.yolo.utils.metrics import ConfusionMatrix, DetMetrics, box_iou from ultralytics.yolo.utils.plotting import output_to_target, plot_images @@ -20,15 +20,16 @@ class DetectionValidator(BaseValidator): def __init__(self, dataloader=None, save_dir=None, pbar=None, logger=None, args=None): super().__init__(dataloader, save_dir, pbar, logger, args) - if self.args.save_json: - check_requirements(['pycocotools']) - self.process = ops.process_mask_upsample # more accurate - else: - self.process = ops.process_mask # faster self.data_dict = yaml_load(check_file(self.args.data)) if self.args.data else None self.is_coco = False self.class_map = None self.targets = None + self.metrics = DetMetrics(save_dir=self.save_dir, plot=self.args.plots) + self.iouv = torch.linspace(0.5, 0.95, 10, device=self.device) # iou vector for mAP@0.5:0.95 + self.niou = self.iouv.numel() + self.seen = 0 + self.jdict = [] + self.stats = [] def preprocess(self, batch): batch["img"] = batch["img"].to(self.device, non_blocking=True) @@ -44,11 +45,7 @@ class DetectionValidator(BaseValidator): return batch def init_metrics(self, model): - if self.training: - head = de_parallel(model).model[-1] - else: - head = de_parallel(model).model.model[-1] - + head = model.model[-1] if self.training else model.model.model[-1] if self.data: self.is_coco = isinstance(self.data.get('val'), str) and self.data['val'].endswith(f'coco{os.sep}val2017.txt') @@ -57,15 +54,8 @@ class DetectionValidator(BaseValidator): self.names = model.names if isinstance(self.names, (list, tuple)): # old format self.names = dict(enumerate(self.names)) - - self.iouv = torch.linspace(0.5, 0.95, 10, device=self.device) # iou vector for mAP@0.5:0.95 - self.niou = self.iouv.numel() - self.seen = 0 + self.metrics.names = self.names self.confusion_matrix = ConfusionMatrix(nc=self.nc) - self.metrics = DetMetrics(save_dir=self.save_dir, plot=self.args.plots, names=self.names) - self.loss = torch.zeros(3, device=self.device) - self.jdict = [] - self.stats = [] def get_desc(self): return ('%22s' + '%11s' * 6) % ('Class', 'Images', 'Instances', 'Box(P', "R", "mAP50", "mAP50-95)") @@ -135,7 +125,7 @@ class DetectionValidator(BaseValidator): return metrics def print_results(self): - pf = '%22s' + '%11i' * 2 + '%11.3g' * 4 # print format + pf = '%22s' + '%11i' * 2 + '%11.3g' * len(self.metric_keys) # print format self.logger.info(pf % ("all", self.seen, self.nt_per_class.sum(), *self.metrics.mean_results())) if self.nt_per_class.sum() == 0: self.logger.warning( diff --git a/ultralytics/yolo/v8/segment/val.py b/ultralytics/yolo/v8/segment/val.py index 56bc038..6c0082c 100644 --- a/ultralytics/yolo/v8/segment/val.py +++ b/ultralytics/yolo/v8/segment/val.py @@ -8,8 +8,7 @@ import torch.nn.functional as F from ultralytics.yolo.data import build_dataloader from ultralytics.yolo.engine.trainer import DEFAULT_CONFIG from ultralytics.yolo.utils import ops -from ultralytics.yolo.utils.checks import check_file, check_requirements -from ultralytics.yolo.utils.files import yaml_load +from ultralytics.yolo.utils.checks import check_requirements from ultralytics.yolo.utils.metrics import ConfusionMatrix, SegmentMetrics, box_iou, mask_iou from ultralytics.yolo.utils.plotting import output_to_target, plot_images from ultralytics.yolo.utils.torch_utils import de_parallel @@ -26,10 +25,7 @@ class SegmentationValidator(DetectionValidator): self.process = ops.process_mask_upsample # more accurate else: self.process = ops.process_mask # faster - self.data_dict = yaml_load(check_file(self.args.data)) if self.args.data else None - self.is_coco = False - self.class_map = None - self.targets = None + self.metrics = SegmentMetrics(save_dir=self.save_dir, plot=self.args.plots) def preprocess(self, batch): batch["img"] = batch["img"].to(self.device, non_blocking=True) @@ -46,29 +42,18 @@ class SegmentationValidator(DetectionValidator): return batch def init_metrics(self, model): - if self.training: - head = de_parallel(model).model[-1] - else: - head = de_parallel(model).model.model[-1] - + head = model.model[-1] if self.training else model.model.model[-1] if self.data: self.is_coco = isinstance(self.data.get('val'), str) and self.data['val'].endswith(f'coco{os.sep}val2017.txt') self.class_map = ops.coco80_to_coco91_class() if self.is_coco else list(range(1000)) - self.nm = head.nm if hasattr(head, "nm") else 32 self.nc = head.nc + self.nm = head.nm if hasattr(head, "nm") else 32 self.names = model.names if isinstance(self.names, (list, tuple)): # old format self.names = dict(enumerate(self.names)) - - self.iouv = torch.linspace(0.5, 0.95, 10, device=self.device) # iou vector for mAP@0.5:0.95 - self.niou = self.iouv.numel() - self.seen = 0 + self.metrics.names = self.names self.confusion_matrix = ConfusionMatrix(nc=self.nc) - self.metrics = SegmentMetrics(save_dir=self.save_dir, plot=self.args.plots, names=self.names) - self.loss = torch.zeros(4, device=self.device) - self.jdict = [] - self.stats = [] self.plot_masks = [] def get_desc(self): @@ -150,21 +135,6 @@ class SegmentationValidator(DetectionValidator): # callbacks.run('on_val_image_end', pred, predn, path, names, im[si]) ''' - def print_results(self): - pf = '%22s' + '%11i' * 2 + '%11.3g' * 8 # print format - self.logger.info(pf % ("all", self.seen, self.nt_per_class.sum(), *self.metrics.mean_results())) - if self.nt_per_class.sum() == 0: - self.logger.warning( - f'WARNING ⚠️ no labels found in {self.args.task} set, can not compute metrics without labels') - - # Print results per class - if (self.args.verbose or (self.nc < 50 and not self.training)) and self.nc > 1 and len(self.stats): - for i, c in enumerate(self.metrics.ap_class_index): - self.logger.info(pf % (self.names[c], self.seen, self.nt_per_class[c], *self.metrics.class_result(i))) - - if self.args.plots: - self.confusion_matrix.plot(save_dir=self.save_dir, names=list(self.names.values())) - def _process_batch(self, detections, labels, iouv, pred_masks=None, gt_masks=None, overlap=False, masks=False): """ Return correct prediction matrix @@ -202,12 +172,6 @@ class SegmentationValidator(DetectionValidator): correct[matches[:, 1].astype(int), i] = True return torch.tensor(correct, dtype=torch.bool, device=iouv.device) - def get_dataloader(self, dataset_path, batch_size): - # TODO: manage splits differently - # calculate stride - check if model is initialized - gs = max(int(de_parallel(self.model).stride if self.model else 0), 32) - return build_dataloader(self.args, batch_size, img_path=dataset_path, stride=gs, mode="val")[0] - # TODO: probably add this to class Metrics @property def metric_keys(self):