"""RAQEEB trainer: Ultralytics YOLO-seg with partial-label supervision across merged datasets. Every image belongs to a source (id prefix before "__"). label_sets.json lists, per source, which of the 11 canonical classes are exhaustively annotated ("full"). During training: * classification negatives are only counted for "full" classes of that image; positives always count; * mosaic partners are drawn from images with the same label-set signature, so a mosaic never mixes a doors-only image with a fully annotated one (keeps the per-image mask exact); * validation ignores predictions and ground truth of classes the image's source does not annotate. Saved checkpoints are plain Ultralytics SegmentationModel objects (the criterion is stripped on save). """ import json import os import random import threading import time from copy import copy from datetime import datetime, timezone from pathlib import Path import subprocess import numpy as np import torch import torch.nn.functional as F from ultralytics.data.augment import Compose, Mosaic from ultralytics.data.dataset import YOLODataset from ultralytics.models.yolo.segment.train import SegmentationTrainer from ultralytics.models.yolo.segment.val import SegmentationValidator from ultralytics.nn.tasks import SegmentationModel from ultralytics.utils import LOCAL_RANK, LOGGER, RANK, colorstr from ultralytics.utils.loss import v8SegmentationLoss from ultralytics.utils.torch_utils import unwrap_model NC = 11 def load_label_sets(data_yaml_dir): d = json.loads((Path(data_yaml_dir) / 'label_sets.json').read_text()) return d['sources'], d.get('image_full_overrides', {}) def source_of(path): return Path(path).name.split('__', 1)[0] # ----------------------------------------------------------------------------- dataset class GroupMosaic(Mosaic): """Mosaic whose partner images share the primary image's label-set signature.""" def get_params(self, labels): self._mix_group = labels.get('mix_group') return super().get_params(labels) def get_indexes(self): pool = getattr(self.dataset, 'group_members', {}).get(getattr(self, '_mix_group', None)) if not pool: return super().get_indexes() return [pool[random.randrange(len(pool))] for _ in range(self.n - 1)] def _swap_mosaic(t): if isinstance(t, Mosaic) and not isinstance(t, GroupMosaic): t.__class__ = GroupMosaic for child in getattr(t, 'transforms', []) or []: _swap_mosaic(child) pre = getattr(t, 'pre_transform', None) if pre is not None: _swap_mosaic(pre) class RaqeebDataset(YOLODataset): def __init__(self, *args, data=None, **kwargs): super().__init__(*args, data=data, **kwargs) sets, overrides = load_label_sets(Path(data['yaml_file']).parent if data.get('yaml_file') else data['path']) sig_ids, self.group_members = {}, {} self.cls_full = np.zeros((len(self.labels), NC), np.float32) for i, lab in enumerate(self.labels): src = source_of(lab['im_file']) s = sets.get(src, {'full': [], 'partial': []}) full = overrides.get(Path(lab['im_file']).stem, s['full']) for c in full: self.cls_full[i, c] = 1.0 sig = (tuple(sorted(full)), tuple(sorted(s['partial']))) g = sig_ids.setdefault(sig, len(sig_ids)) lab['mix_group'] = g self.group_members.setdefault(g, []).append(i) if RANK in {-1, 0}: LOGGER.info(f"{self.prefix}RAQEEB label-set groups: " + ', '.join(f'{len(v)} imgs full={k[0]}' for k, v in sorted(((k, self.group_members[g]) for k, g in sig_ids.items()), key=lambda kv: -len(kv[1])))) def build_transforms(self, hyp=None): t = super().build_transforms(hyp) _swap_mosaic(t) return t def __getitem__(self, index): out = self.transforms(self.get_image_and_label(index)) out.pop('mix_group', None) out['cls_mask'] = torch.from_numpy(self.cls_full[index]) return out @staticmethod def collate_fn(batch): masks = torch.stack([b.pop('cls_mask') for b in batch], 0) out = YOLODataset.collate_fn(batch) out['cls_mask'] = masks return out # ----------------------------------------------------------------------------- loss class MaskedBCE: """BCE whose negatives are ignored for classes the image's source does not annotate.""" def __init__(self): self.mask = None # (B, nc) def __call__(self, pred, target): loss = F.binary_cross_entropy_with_logits(pred, target, reduction='none') if self.mask is not None and self.mask.shape[0] == loss.shape[0]: w = torch.maximum(self.mask[:, None, :].to(loss.dtype), (target > 0).to(loss.dtype)) loss = loss * w return loss class RaqeebSegLoss(v8SegmentationLoss): def __init__(self, model, *a, **k): super().__init__(model, *a, **k) self.bce = MaskedBCE() def loss(self, preds, batch): m = batch.get('cls_mask') if isinstance(batch, dict) else None self.bce.mask = m.to(self.device).float() if m is not None else None return super().loss(preds, batch) _orig_init_criterion = SegmentationModel.init_criterion def _raqeeb_init_criterion(self): head = self.model[-1] if getattr(head, 'one2one_cv2', None) is None and type(head).__name__ == 'Segment': return RaqeebSegLoss(self) LOGGER.warning('RAQEEB: model head is not a plain YOLO11 Segment head; partial-label masking disabled') return _orig_init_criterion(self) SegmentationModel.init_criterion = _raqeeb_init_criterion # class-level; criterion is stripped from checkpoints # ----------------------------------------------------------------------------- validator class RaqeebSegValidator(SegmentationValidator): """Scores each image only on the classes its source exhaustively annotates.""" def __call__(self, trainer=None, model=None): # Upstream runs validation under torch.inference_mode. With the gloo backend (the only DDP backend on # Windows) collective results are written from a worker thread, which PyTorch forbids for inference # tensors. Plain no_grad gives identical numbers and keeps distributed validation working. from ultralytics.engine.validator import BaseValidator with torch.inference_mode(False), torch.no_grad(): return BaseValidator.__call__.__wrapped__(self, trainer, model) def _prepare_batch(self, si, batch): pb = super()._prepare_batch(si, batch) cm = batch.get('cls_mask') n = pb['cls'].shape[0] if cm is not None and n: keep = cm[si].to(pb['cls'].device)[pb['cls'].long().view(-1)] > 0 pb['cls'] = pb['cls'][keep] pb['bboxes'] = pb['bboxes'][keep] if isinstance(pb.get('masks'), torch.Tensor) and pb['masks'].shape[0] == n: pb['masks'] = pb['masks'][keep] return pb def update_metrics(self, preds, batch): cm = batch.get('cls_mask') if cm is not None: out = [] for si, p in enumerate(preds): n = p['cls'].shape[0] if n: keep = cm[si].to(p['cls'].device)[p['cls'].long().view(-1)] > 0 p = {k: (v[keep] if isinstance(v, torch.Tensor) and v.shape[:1] == (n,) else v) for k, v in p.items()} out.append(p) preds = out return super().update_metrics(preds, batch) # ----------------------------------------------------------------------------- telemetry def query_gpus(): q = 'index,temperature.gpu,power.draw,memory.used,memory.total,utilization.gpu,display_active' r = subprocess.run(['nvidia-smi', f'--query-gpu={q}', '--format=csv,noheader,nounits'], capture_output=True, text=True, timeout=20) rows = [] for line in r.stdout.strip().splitlines(): v = [x.strip() for x in line.split(',')] rows.append(dict(index=int(v[0]), temp=float(v[1]), power=float(v[2]), mem_used=float(v[3]), mem_total=float(v[4]), util=float(v[5]), display=v[6])) return rows class Telemetry(threading.Thread): def __init__(self, log_path, stop_file, max_temp=89, every=30): super().__init__(daemon=True) self.log_path, self.stop_file, self.max_temp, self.every = Path(log_path), Path(stop_file), max_temp, every self.halt = threading.Event() def run(self): hot = 0 while not self.halt.is_set(): try: g = query_gpus() with self.log_path.open('a') as f: f.write(json.dumps(dict(utc=datetime.now(timezone.utc).isoformat(), gpus=g)) + '\n') hot = hot + 1 if any(x['temp'] >= self.max_temp for x in g) else 0 if hot >= 4: # sustained ~2 min over limit -> graceful stop at end of epoch self.stop_file.write_text(f'thermal stop {datetime.now().isoformat()} {g}') LOGGER.warning(f'RAQEEB telemetry: sustained GPU temperature >= {self.max_temp} C, requesting stop') except Exception as e: # telemetry must never kill training LOGGER.warning(f'RAQEEB telemetry error: {e}') self.halt.wait(self.every) # ----------------------------------------------------------------------------- trainer def _epoch_end_stop_check(trainer): stop_file = Path(trainer.save_dir) / 'STOP' flag = [stop_file.exists() if RANK in {-1, 0} else None] if trainer.world_size > 1: import torch.distributed as dist dist.broadcast_object_list(flag, 0) if flag[0]: LOGGER.warning(f'RAQEEB: STOP file found, stopping after this epoch ({stop_file})') trainer.stop = True def _epoch_log(trainer): if RANK not in {-1, 0}: return rec = dict(utc=datetime.now(timezone.utc).isoformat(), epoch=int(trainer.epoch) + 1, fitness=None if trainer.fitness is None else float(trainer.fitness), metrics={k: float(v) for k, v in (trainer.metrics or {}).items()}, peak_mem_gb=round(torch.cuda.max_memory_reserved(trainer.device) / 2**30, 2) if trainer.device.type == 'cuda' else 0) with (Path(trainer.save_dir) / 'epochs.jsonl').open('a') as f: f.write(json.dumps(rec) + '\n') class RaqeebTrainer(SegmentationTrainer): def __init__(self, *a, **k): super().__init__(*a, **k) for ev, fn in (('on_train_epoch_end', _epoch_end_stop_check), ('on_fit_epoch_end', _epoch_log)): self.callbacks[ev] = [f for f in self.callbacks[ev] if getattr(f, '__name__', '') != fn.__name__] + [fn] self._telemetry = None def build_dataset(self, img_path, mode='train', batch=None): gs = max(int(unwrap_model(self.model).stride.max()), 32) if self.model else 32 return RaqeebDataset( img_path=img_path, imgsz=self.args.imgsz, batch_size=batch, augment=mode == 'train', hyp=self.args, rect=self.args.rect or mode == 'val', cache=self.args.cache or None, single_cls=self.args.single_cls or False, stride=gs, pad=0.0 if mode == 'train' else 0.5, prefix=colorstr(f'{mode}: '), task=self.args.task, classes=self.args.classes, data=self.data, fraction=self.args.fraction if mode == 'train' else 1.0, tile=getattr(self.args, 'tile', None)) def get_dataloader(self, dataset_path, batch_size=16, rank=0, mode='train'): if mode == 'val': # upstream doubles the val batch; keep it equal to the train batch (display-GPU headroom) batch_size = max(1, batch_size // 2) return super().get_dataloader(dataset_path, batch_size=batch_size, rank=rank, mode=mode) def get_validator(self): self.loss_names = 'box_loss', 'seg_loss', 'cls_loss', 'dfl_loss', 'sem_loss' return RaqeebSegValidator(self.test_loader, save_dir=self.save_dir, args=copy(self.args), _callbacks=self.callbacks) def _setup_train(self): super()._setup_train() if self.world_size > 1 and os.environ.get('RAQEEB_FP16_COMM', '1') == '1': try: from torch.distributed.algorithms.ddp_comm_hooks import default_hooks self.model.register_comm_hook(None, default_hooks.fp16_compress_hook) LOGGER.info('RAQEEB: DDP fp16 gradient compression enabled') except Exception as e: LOGGER.warning(f'RAQEEB: could not register fp16 comm hook: {e}') if RANK in {-1, 0} and self._telemetry is None: self._telemetry = Telemetry(Path(self.save_dir) / 'gpu_telemetry.jsonl', Path(self.save_dir) / 'STOP') self._telemetry.start() def final_eval(self): if self._telemetry is not None: self._telemetry.halt.set() return super().final_eval()