diff --git a/ultralytics/data/build.py b/ultralytics/data/build.py index d9ce0982..62aabf10 100644 --- a/ultralytics/data/build.py +++ b/ultralytics/data/build.py @@ -3,13 +3,14 @@ import os import random from pathlib import Path -from typing import Any, Iterator +from typing import Any, Dict, Iterator import numpy as np import torch from PIL import Image from torch.utils.data import dataloader, distributed +from ultralytics.cfg import IterableSimpleNamespace from ultralytics.data.dataset import GroundingDataset, YOLODataset, YOLOMultiModalDataset from ultralytics.data.loaders import ( LOADERS, @@ -111,7 +112,16 @@ def seed_worker(worker_id: int): # noqa random.seed(worker_seed) -def build_yolo_dataset(cfg, img_path, batch, data, mode="train", rect=False, stride=32, multi_modal=False): +def build_yolo_dataset( + cfg: IterableSimpleNamespace, + img_path: str, + batch: int, + data: Dict[str, Any], + mode: str = "train", + rect: bool = False, + stride: int = 32, + multi_modal: bool = False, +): """Build and return a YOLO dataset based on configuration parameters.""" dataset = YOLOMultiModalDataset if multi_modal else YOLODataset return dataset( @@ -133,11 +143,21 @@ def build_yolo_dataset(cfg, img_path, batch, data, mode="train", rect=False, str ) -def build_grounding(cfg, img_path, json_file, batch, mode="train", rect=False, stride=32): +def build_grounding( + cfg: IterableSimpleNamespace, + img_path: str, + json_file: str, + batch: int, + mode: str = "train", + rect: bool = False, + stride: int = 32, + max_samples: int = 80, +): """Build and return a GroundingDataset based on configuration parameters.""" return GroundingDataset( img_path=img_path, json_file=json_file, + max_samples=max_samples, imgsz=cfg.imgsz, batch_size=batch, augment=mode == "train", # augmentation diff --git a/ultralytics/data/dataset.py b/ultralytics/data/dataset.py index 732d90bd..14e13bd2 100644 --- a/ultralytics/data/dataset.py +++ b/ultralytics/data/dataset.py @@ -411,6 +411,7 @@ class YOLOMultiModalDataset(YOLODataset): @staticmethod def _get_neg_texts(category_freq: Dict, threshold: int = 100) -> List[str]: """Get negative text samples based on frequency threshold.""" + threshold = min(max(category_freq.values()), 100) return [k for k, v in category_freq.items() if v >= threshold] @@ -434,18 +435,20 @@ class GroundingDataset(YOLODataset): >>> len(dataset) # Number of valid images with annotations """ - def __init__(self, *args, task: str = "detect", json_file: str = "", **kwargs): + def __init__(self, *args, task: str = "detect", json_file: str = "", max_samples: int = 80, **kwargs): """ Initialize a GroundingDataset for object detection. Args: json_file (str): Path to the JSON file containing annotations. task (str): Must be 'detect' or 'segment' for GroundingDataset. + max_samples (int): Maximum number of samples to load for text augmentation. *args (Any): Additional positional arguments for the parent class. **kwargs (Any): Additional keyword arguments for the parent class. """ assert task in {"detect", "segment"}, "GroundingDataset currently only supports `detect` and `segment` tasks" self.json_file = json_file + self.max_samples = max_samples super().__init__(*args, task=task, data={"channels": 3}, **kwargs) def get_img_files(self, img_path: str) -> List: @@ -625,7 +628,7 @@ class GroundingDataset(YOLODataset): # the strategy of selecting negative is restricted in one dataset, # while official pre-saved neg embeddings from all datasets at once. transform = RandomLoadText( - max_samples=80, + max_samples=min(self.max_samples, 80), padding=True, padding_value=self._get_neg_texts(self.category_freq), ) @@ -651,6 +654,7 @@ class GroundingDataset(YOLODataset): @staticmethod def _get_neg_texts(category_freq: Dict, threshold: int = 100) -> List[str]: """Get negative text samples based on frequency threshold.""" + threshold = min(max(category_freq.values()), 100) return [k for k, v in category_freq.items() if v >= threshold] diff --git a/ultralytics/models/yolo/world/train_world.py b/ultralytics/models/yolo/world/train_world.py index a1233d0b..bcf13334 100644 --- a/ultralytics/models/yolo/world/train_world.py +++ b/ultralytics/models/yolo/world/train_world.py @@ -107,7 +107,15 @@ class WorldTrainerFromScratch(WorldTrainer): datasets = [ build_yolo_dataset(self.args, im_path, batch, self.training_data[im_path], stride=gs, multi_modal=True) if isinstance(im_path, str) - else build_grounding(self.args, im_path["img_path"], im_path["json_file"], batch, stride=gs) + else build_grounding( + # assign `nc` from validation set to max number of text samples for training consistency + self.args, + im_path["img_path"], + im_path["json_file"], + batch, + stride=gs, + max_samples=self.data["nc"], + ) for im_path in img_path ] self.set_text_embeddings(datasets, batch) # cache text embeddings to accelerate training