Skip to content

Reference for ultralytics/data/dataset.py

Note

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/dataset.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!


ultralytics.data.dataset.YOLODataset

YOLODataset(*args, data=None, task='detect', **kwargs)

Bases: BaseDataset

Dataset class for loading object detection and/or segmentation labels in YOLO format.

This class supports loading data for object detection, segmentation, pose estimation, and oriented bounding box (OBB) tasks using the YOLO format.

Attributes:

Name Type Description
use_segments bool

Indicates if segmentation masks should be used.

use_keypoints bool

Indicates if keypoints should be used for pose estimation.

use_obb bool

Indicates if oriented bounding boxes should be used.

data dict

Dataset configuration dictionary.

Methods:

Name Description
cache_labels

Cache dataset labels, check images and read shapes.

get_labels

Returns dictionary of labels for YOLO training.

build_transforms

Builds and appends transforms to the list.

close_mosaic

Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.

update_labels_info

Updates label format for different tasks.

collate_fn

Collates data samples into batches.

Examples:

>>> dataset = YOLODataset(img_path="path/to/images", data={"names": {0: "person"}}, task="detect")
>>> dataset.get_labels()

Parameters:

Name Type Description Default
data dict

Dataset configuration dictionary.

None
task str

Task type, one of 'detect', 'segment', 'pose', or 'obb'.

'detect'
*args Any

Additional positional arguments for the parent class.

()
**kwargs Any

Additional keyword arguments for the parent class.

{}
Source code in ultralytics/data/dataset.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def __init__(self, *args, data=None, task="detect", **kwargs):
    """
    Initialize the YOLODataset.

    Args:
        data (dict, optional): Dataset configuration dictionary.
        task (str): Task type, one of 'detect', 'segment', 'pose', or 'obb'.
        *args (Any): Additional positional arguments for the parent class.
        **kwargs (Any): Additional keyword arguments for the parent class.
    """
    self.use_segments = task == "segment"
    self.use_keypoints = task == "pose"
    self.use_obb = task == "obb"
    self.data = data
    assert not (self.use_segments and self.use_keypoints), "Can not use both segments and keypoints."
    super().__init__(*args, channels=self.data["channels"], **kwargs)

build_transforms

build_transforms(hyp=None)

Builds and appends transforms to the list.

Parameters:

Name Type Description Default
hyp dict

Hyperparameters for transforms.

None

Returns:

Type Description
Compose

Composed transforms.

Source code in ultralytics/data/dataset.py
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
def build_transforms(self, hyp=None):
    """
    Builds and appends transforms to the list.

    Args:
        hyp (dict, optional): Hyperparameters for transforms.

    Returns:
        (Compose): Composed transforms.
    """
    if self.augment:
        hyp.mosaic = hyp.mosaic if self.augment and not self.rect else 0.0
        hyp.mixup = hyp.mixup if self.augment and not self.rect else 0.0
        hyp.cutmix = hyp.cutmix if self.augment and not self.rect else 0.0
        transforms = v8_transforms(self, self.imgsz, hyp)
    else:
        transforms = Compose([LetterBox(new_shape=(self.imgsz, self.imgsz), scaleup=False)])
    transforms.append(
        Format(
            bbox_format="xywh",
            normalize=True,
            return_mask=self.use_segments,
            return_keypoint=self.use_keypoints,
            return_obb=self.use_obb,
            batch_idx=True,
            mask_ratio=hyp.mask_ratio,
            mask_overlap=hyp.overlap_mask,
            bgr=hyp.bgr if self.augment else 0.0,  # only affect training.
        )
    )
    return transforms

cache_labels

cache_labels(path=Path('./labels.cache'))

Cache dataset labels, check images and read shapes.

Parameters:

Name Type Description Default
path Path

Path where to save the cache file.

Path('./labels.cache')

Returns:

Type Description
dict

Dictionary containing cached labels and related information.

Source code in ultralytics/data/dataset.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def cache_labels(self, path=Path("./labels.cache")):
    """
    Cache dataset labels, check images and read shapes.

    Args:
        path (Path): Path where to save the cache file.

    Returns:
        (dict): Dictionary containing cached labels and related information.
    """
    x = {"labels": []}
    nm, nf, ne, nc, msgs = 0, 0, 0, 0, []  # number missing, found, empty, corrupt, messages
    desc = f"{self.prefix}Scanning {path.parent / path.stem}..."
    total = len(self.im_files)
    nkpt, ndim = self.data.get("kpt_shape", (0, 0))
    if self.use_keypoints and (nkpt <= 0 or ndim not in {2, 3}):
        raise ValueError(
            "'kpt_shape' in data.yaml missing or incorrect. Should be a list with [number of "
            "keypoints, number of dims (2 for x,y or 3 for x,y,visible)], i.e. 'kpt_shape: [17, 3]'"
        )
    with ThreadPool(NUM_THREADS) as pool:
        results = pool.imap(
            func=verify_image_label,
            iterable=zip(
                self.im_files,
                self.label_files,
                repeat(self.prefix),
                repeat(self.use_keypoints),
                repeat(len(self.data["names"])),
                repeat(nkpt),
                repeat(ndim),
                repeat(self.single_cls),
            ),
        )
        pbar = TQDM(results, desc=desc, total=total)
        for im_file, lb, shape, segments, keypoint, nm_f, nf_f, ne_f, nc_f, msg in pbar:
            nm += nm_f
            nf += nf_f
            ne += ne_f
            nc += nc_f
            if im_file:
                x["labels"].append(
                    {
                        "im_file": im_file,
                        "shape": shape,
                        "cls": lb[:, 0:1],  # n, 1
                        "bboxes": lb[:, 1:],  # n, 4
                        "segments": segments,
                        "keypoints": keypoint,
                        "normalized": True,
                        "bbox_format": "xywh",
                    }
                )
            if msg:
                msgs.append(msg)
            pbar.desc = f"{desc} {nf} images, {nm + ne} backgrounds, {nc} corrupt"
        pbar.close()

    if msgs:
        LOGGER.info("\n".join(msgs))
    if nf == 0:
        LOGGER.warning(f"{self.prefix}No labels found in {path}. {HELP_URL}")
    x["hash"] = get_hash(self.label_files + self.im_files)
    x["results"] = nf, nm, ne, nc, len(self.im_files)
    x["msgs"] = msgs  # warnings
    save_dataset_cache_file(self.prefix, path, x, DATASET_CACHE_VERSION)
    return x

close_mosaic

close_mosaic(hyp)

Disable mosaic, copy_paste, mixup and cutmix augmentations by setting their probabilities to 0.0.

Parameters:

Name Type Description Default
hyp dict

Hyperparameters for transforms.

required
Source code in ultralytics/data/dataset.py
239
240
241
242
243
244
245
246
247
248
249
250
def close_mosaic(self, hyp):
    """
    Disable mosaic, copy_paste, mixup and cutmix augmentations by setting their probabilities to 0.0.

    Args:
        hyp (dict): Hyperparameters for transforms.
    """
    hyp.mosaic = 0.0
    hyp.copy_paste = 0.0
    hyp.mixup = 0.0
    hyp.cutmix = 0.0
    self.transforms = self.build_transforms(hyp)

collate_fn staticmethod

collate_fn(batch)

Collates data samples into batches.

Parameters:

Name Type Description Default
batch List[dict]

List of dictionaries containing sample data.

required

Returns:

Type Description
dict

Collated batch with stacked tensors.

Source code in ultralytics/data/dataset.py
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
@staticmethod
def collate_fn(batch):
    """
    Collates data samples into batches.

    Args:
        batch (List[dict]): List of dictionaries containing sample data.

    Returns:
        (dict): Collated batch with stacked tensors.
    """
    new_batch = {}
    batch = [dict(sorted(b.items())) for b in batch]  # make sure the keys are in the same order
    keys = batch[0].keys()
    values = list(zip(*[list(b.values()) for b in batch]))
    for i, k in enumerate(keys):
        value = values[i]
        if k in {"img", "text_feats"}:
            value = torch.stack(value, 0)
        elif k == "visuals":
            value = torch.nn.utils.rnn.pad_sequence(value, batch_first=True)
        if k in {"masks", "keypoints", "bboxes", "cls", "segments", "obb"}:
            value = torch.cat(value, 0)
        new_batch[k] = value
    new_batch["batch_idx"] = list(new_batch["batch_idx"])
    for i in range(len(new_batch["batch_idx"])):
        new_batch["batch_idx"][i] += i  # add target image index for build_targets()
    new_batch["batch_idx"] = torch.cat(new_batch["batch_idx"], 0)
    return new_batch

get_labels

get_labels()

Returns dictionary of labels for YOLO training.

This method loads labels from disk or cache, verifies their integrity, and prepares them for training.

Returns:

Type Description
List[dict]

List of label dictionaries, each containing information about an image and its annotations.

Source code in ultralytics/data/dataset.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def get_labels(self):
    """
    Returns dictionary of labels for YOLO training.

    This method loads labels from disk or cache, verifies their integrity, and prepares them for training.

    Returns:
        (List[dict]): List of label dictionaries, each containing information about an image and its annotations.
    """
    self.label_files = img2label_paths(self.im_files)
    cache_path = Path(self.label_files[0]).parent.with_suffix(".cache")
    try:
        cache, exists = load_dataset_cache_file(cache_path), True  # attempt to load a *.cache file
        assert cache["version"] == DATASET_CACHE_VERSION  # matches current version
        assert cache["hash"] == get_hash(self.label_files + self.im_files)  # identical hash
    except (FileNotFoundError, AssertionError, AttributeError):
        cache, exists = self.cache_labels(cache_path), False  # run cache ops

    # Display cache
    nf, nm, ne, nc, n = cache.pop("results")  # found, missing, empty, corrupt, total
    if exists and LOCAL_RANK in {-1, 0}:
        d = f"Scanning {cache_path}... {nf} images, {nm + ne} backgrounds, {nc} corrupt"
        TQDM(None, desc=self.prefix + d, total=n, initial=n)  # display results
        if cache["msgs"]:
            LOGGER.info("\n".join(cache["msgs"]))  # display warnings

    # Read cache
    [cache.pop(k) for k in ("hash", "version", "msgs")]  # remove items
    labels = cache["labels"]
    if not labels:
        raise RuntimeError(
            f"No valid images found in {cache_path}. Images with incorrectly formatted labels are ignored. {HELP_URL}"
        )
    self.im_files = [lb["im_file"] for lb in labels]  # update im_files

    # Check if the dataset is all boxes or all segments
    lengths = ((len(lb["cls"]), len(lb["bboxes"]), len(lb["segments"])) for lb in labels)
    len_cls, len_boxes, len_segments = (sum(x) for x in zip(*lengths))
    if len_segments and len_boxes != len_segments:
        LOGGER.warning(
            f"Box and segment counts should be equal, but got len(segments) = {len_segments}, "
            f"len(boxes) = {len_boxes}. To resolve this only boxes will be used and all segments will be removed. "
            "To avoid this please supply either a detect or segment dataset, not a detect-segment mixed dataset."
        )
        for lb in labels:
            lb["segments"] = []
    if len_cls == 0:
        LOGGER.warning(f"Labels are missing or empty in {cache_path}, training may not work correctly. {HELP_URL}")
    return labels

update_labels_info

update_labels_info(label)

Custom your label format here.

Parameters:

Name Type Description Default
label dict

Label dictionary containing bboxes, segments, keypoints, etc.

required

Returns:

Type Description
dict

Updated label dictionary with instances.

Note

cls is not with bboxes now, classification and semantic segmentation need an independent cls label Can also support classification and semantic segmentation by adding or removing dict keys there.

Source code in ultralytics/data/dataset.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
def update_labels_info(self, label):
    """
    Custom your label format here.

    Args:
        label (dict): Label dictionary containing bboxes, segments, keypoints, etc.

    Returns:
        (dict): Updated label dictionary with instances.

    Note:
        cls is not with bboxes now, classification and semantic segmentation need an independent cls label
        Can also support classification and semantic segmentation by adding or removing dict keys there.
    """
    bboxes = label.pop("bboxes")
    segments = label.pop("segments", [])
    keypoints = label.pop("keypoints", None)
    bbox_format = label.pop("bbox_format")
    normalized = label.pop("normalized")

    # NOTE: do NOT resample oriented boxes
    segment_resamples = 100 if self.use_obb else 1000
    if len(segments) > 0:
        # make sure segments interpolate correctly if original length is greater than segment_resamples
        max_len = max(len(s) for s in segments)
        segment_resamples = (max_len + 1) if segment_resamples < max_len else segment_resamples
        # list[np.array(segment_resamples, 2)] * num_samples
        segments = np.stack(resample_segments(segments, n=segment_resamples), axis=0)
    else:
        segments = np.zeros((0, segment_resamples, 2), dtype=np.float32)
    label["instances"] = Instances(bboxes, segments, keypoints, bbox_format=bbox_format, normalized=normalized)
    return label





ultralytics.data.dataset.YOLOMultiModalDataset

YOLOMultiModalDataset(*args, data=None, task='detect', **kwargs)

Bases: YOLODataset

Dataset class for loading object detection and/or segmentation labels in YOLO format with multi-modal support.

This class extends YOLODataset to add text information for multi-modal model training, enabling models to process both image and text data.

Methods:

Name Description
update_labels_info

Adds text information for multi-modal model training.

build_transforms

Enhances data transformations with text augmentation.

Examples:

>>> dataset = YOLOMultiModalDataset(img_path="path/to/images", data={"names": {0: "person"}}, task="detect")
>>> batch = next(iter(dataset))
>>> print(batch.keys())  # Should include 'texts'

Parameters:

Name Type Description Default
data dict

Dataset configuration dictionary.

None
task str

Task type, one of 'detect', 'segment', 'pose', or 'obb'.

'detect'
*args Any

Additional positional arguments for the parent class.

()
**kwargs Any

Additional keyword arguments for the parent class.

{}
Source code in ultralytics/data/dataset.py
333
334
335
336
337
338
339
340
341
342
343
def __init__(self, *args, data=None, task="detect", **kwargs):
    """
    Initialize a YOLOMultiModalDataset.

    Args:
        data (dict, optional): Dataset configuration dictionary.
        task (str): Task type, one of 'detect', 'segment', 'pose', or 'obb'.
        *args (Any): Additional positional arguments for the parent class.
        **kwargs (Any): Additional keyword arguments for the parent class.
    """
    super().__init__(*args, data=data, task=task, **kwargs)

category_freq property

category_freq

Return frequency of each category in the dataset.

category_names property

category_names

Return category names for the dataset.

Returns:

Type Description
Set[str]

List of class names.

build_transforms

build_transforms(hyp=None)

Enhances data transformations with optional text augmentation for multi-modal training.

Parameters:

Name Type Description Default
hyp dict

Hyperparameters for transforms.

None

Returns:

Type Description
Compose

Composed transforms including text augmentation if applicable.

Source code in ultralytics/data/dataset.py
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
def build_transforms(self, hyp=None):
    """
    Enhances data transformations with optional text augmentation for multi-modal training.

    Args:
        hyp (dict, optional): Hyperparameters for transforms.

    Returns:
        (Compose): Composed transforms including text augmentation if applicable.
    """
    transforms = super().build_transforms(hyp)
    if self.augment:
        # NOTE: hard-coded the args for now.
        # NOTE: this implementation is different from official yoloe,
        # 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=min(self.data["nc"], 80),
            padding=True,
            padding_value=self._get_neg_texts(self.category_freq),
        )
        transforms.insert(-1, transform)
    return transforms

update_labels_info

update_labels_info(label)

Add texts information for multi-modal model training.

Parameters:

Name Type Description Default
label dict

Label dictionary containing bboxes, segments, keypoints, etc.

required

Returns:

Type Description
dict

Updated label dictionary with instances and texts.

Source code in ultralytics/data/dataset.py
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
def update_labels_info(self, label):
    """
    Add texts information for multi-modal model training.

    Args:
        label (dict): Label dictionary containing bboxes, segments, keypoints, etc.

    Returns:
        (dict): Updated label dictionary with instances and texts.
    """
    labels = super().update_labels_info(label)
    # NOTE: some categories are concatenated with its synonyms by `/`.
    # NOTE: and `RandomLoadText` would randomly select one of them if there are multiple words.
    labels["texts"] = [v.split("/") for _, v in self.data["names"].items()]

    return labels





ultralytics.data.dataset.GroundingDataset

GroundingDataset(*args, task='detect', json_file='', **kwargs)

Bases: YOLODataset

Handles object detection tasks by loading annotations from a specified JSON file, supporting YOLO format.

This dataset is designed for grounding tasks where annotations are provided in a JSON file rather than the standard YOLO format text files.

Attributes:

Name Type Description
json_file str

Path to the JSON file containing annotations.

Methods:

Name Description
get_img_files

Returns empty list as image files are read in get_labels.

get_labels

Loads annotations from a JSON file and prepares them for training.

build_transforms

Configures augmentations for training with optional text loading.

Examples:

>>> dataset = GroundingDataset(img_path="path/to/images", json_file="annotations.json", task="detect")
>>> len(dataset)  # Number of valid images with annotations

Parameters:

Name Type Description Default
json_file str

Path to the JSON file containing annotations.

''
task str

Must be 'detect' or 'segment' for GroundingDataset.

'detect'
*args Any

Additional positional arguments for the parent class.

()
**kwargs Any

Additional keyword arguments for the parent class.

{}
Source code in ultralytics/data/dataset.py
436
437
438
439
440
441
442
443
444
445
446
447
448
def __init__(self, *args, task="detect", json_file="", **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.
        *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
    super().__init__(*args, task=task, data={"channels": 3}, **kwargs)

category_freq property

category_freq

Return frequency of each category in the dataset.

category_names property

category_names

Return unique category names from the dataset.

build_transforms

build_transforms(hyp=None)

Configures augmentations for training with optional text loading.

Parameters:

Name Type Description Default
hyp dict

Hyperparameters for transforms.

None

Returns:

Type Description
Compose

Composed transforms including text augmentation if applicable.

Source code in ultralytics/data/dataset.py
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
def build_transforms(self, hyp=None):
    """
    Configures augmentations for training with optional text loading.

    Args:
        hyp (dict, optional): Hyperparameters for transforms.

    Returns:
        (Compose): Composed transforms including text augmentation if applicable.
    """
    transforms = super().build_transforms(hyp)
    if self.augment:
        # NOTE: hard-coded the args for now.
        # NOTE: this implementation is different from official yoloe,
        # 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,
            padding=True,
            padding_value=self._get_neg_texts(self.category_freq),
        )
        transforms.insert(-1, transform)
    return transforms

cache_labels

cache_labels(path=Path('./labels.cache'))

Loads annotations from a JSON file, filters, and normalizes bounding boxes for each image.

Parameters:

Name Type Description Default
path Path

Path where to save the cache file.

Path('./labels.cache')

Returns:

Type Description
dict

Dictionary containing cached labels and related information.

Source code in ultralytics/data/dataset.py
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
def cache_labels(self, path=Path("./labels.cache")):
    """
    Loads annotations from a JSON file, filters, and normalizes bounding boxes for each image.

    Args:
        path (Path): Path where to save the cache file.

    Returns:
        (dict): Dictionary containing cached labels and related information.
    """
    x = {"labels": []}
    LOGGER.info("Loading annotation file...")
    with open(self.json_file) as f:
        annotations = json.load(f)
    images = {f"{x['id']:d}": x for x in annotations["images"]}
    img_to_anns = defaultdict(list)
    for ann in annotations["annotations"]:
        img_to_anns[ann["image_id"]].append(ann)
    for img_id, anns in TQDM(img_to_anns.items(), desc=f"Reading annotations {self.json_file}"):
        img = images[f"{img_id:d}"]
        h, w, f = img["height"], img["width"], img["file_name"]
        im_file = Path(self.img_path) / f
        if not im_file.exists():
            continue
        self.im_files.append(str(im_file))
        bboxes = []
        segments = []
        cat2id = {}
        texts = []
        for ann in anns:
            if ann["iscrowd"]:
                continue
            box = np.array(ann["bbox"], dtype=np.float32)
            box[:2] += box[2:] / 2
            box[[0, 2]] /= float(w)
            box[[1, 3]] /= float(h)
            if box[2] <= 0 or box[3] <= 0:
                continue

            caption = img["caption"]
            cat_name = " ".join([caption[t[0] : t[1]] for t in ann["tokens_positive"]]).lower().strip()
            if not cat_name:
                continue

            if cat_name not in cat2id:
                cat2id[cat_name] = len(cat2id)
                texts.append([cat_name])
            cls = cat2id[cat_name]  # class
            box = [cls] + box.tolist()
            if box not in bboxes:
                bboxes.append(box)
                if ann.get("segmentation") is not None:
                    if len(ann["segmentation"]) == 0:
                        segments.append(box)
                        continue
                    elif len(ann["segmentation"]) > 1:
                        s = merge_multi_segment(ann["segmentation"])
                        s = (np.concatenate(s, axis=0) / np.array([w, h], dtype=np.float32)).reshape(-1).tolist()
                    else:
                        s = [j for i in ann["segmentation"] for j in i]  # all segments concatenated
                        s = (
                            (np.array(s, dtype=np.float32).reshape(-1, 2) / np.array([w, h], dtype=np.float32))
                            .reshape(-1)
                            .tolist()
                        )
                    s = [cls] + s
                    segments.append(s)
        lb = np.array(bboxes, dtype=np.float32) if len(bboxes) else np.zeros((0, 5), dtype=np.float32)

        if segments:
            classes = np.array([x[0] for x in segments], dtype=np.float32)
            segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in segments]  # (cls, xy1...)
            lb = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1)  # (cls, xywh)
        lb = np.array(lb, dtype=np.float32)

        x["labels"].append(
            {
                "im_file": im_file,
                "shape": (h, w),
                "cls": lb[:, 0:1],  # n, 1
                "bboxes": lb[:, 1:],  # n, 4
                "segments": segments,
                "normalized": True,
                "bbox_format": "xywh",
                "texts": texts,
            }
        )
    x["hash"] = get_hash(self.json_file)
    save_dataset_cache_file(self.prefix, path, x, DATASET_CACHE_VERSION)
    return x

get_img_files

get_img_files(img_path)

The image files would be read in get_labels function, return empty list here.

Parameters:

Name Type Description Default
img_path str

Path to the directory containing images.

required

Returns:

Type Description
list

Empty list as image files are read in get_labels.

Source code in ultralytics/data/dataset.py
450
451
452
453
454
455
456
457
458
459
460
def get_img_files(self, img_path):
    """
    The image files would be read in `get_labels` function, return empty list here.

    Args:
        img_path (str): Path to the directory containing images.

    Returns:
        (list): Empty list as image files are read in get_labels.
    """
    return []

get_labels

get_labels()

Load labels from cache or generate them from JSON file.

Returns:

Type Description
List[dict]

List of label dictionaries, each containing information about an image and its annotations.

Source code in ultralytics/data/dataset.py
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
def get_labels(self):
    """
    Load labels from cache or generate them from JSON file.

    Returns:
        (List[dict]): List of label dictionaries, each containing information about an image and its annotations.
    """
    cache_path = Path(self.json_file).with_suffix(".cache")
    try:
        cache, _ = load_dataset_cache_file(cache_path), True  # attempt to load a *.cache file
        assert cache["version"] == DATASET_CACHE_VERSION  # matches current version
        assert cache["hash"] == get_hash(self.json_file)  # identical hash
    except (FileNotFoundError, AssertionError, AttributeError):
        cache, _ = self.cache_labels(cache_path), False  # run cache ops
    [cache.pop(k) for k in ("hash", "version")]  # remove items
    labels = cache["labels"]
    # self.verify_labels(labels)
    self.im_files = [str(label["im_file"]) for label in labels]
    if LOCAL_RANK in {-1, 0}:
        LOGGER.info(f"Load {self.json_file} from cache file {cache_path}")
    return labels

verify_labels

verify_labels(labels)

Verify the number of instances in the dataset matches expected counts.

Source code in ultralytics/data/dataset.py
462
463
464
465
466
467
468
469
470
471
472
473
474
def verify_labels(self, labels):
    """Verify the number of instances in the dataset matches expected counts."""
    instance_count = sum(label["bboxes"].shape[0] for label in labels)
    if "final_mixed_train_no_coco_segm" in self.json_file:
        assert instance_count == 3662344
    elif "final_mixed_train_no_coco" in self.json_file:
        assert instance_count == 3681235
    elif "final_flickr_separateGT_train_segm" in self.json_file:
        assert instance_count == 638214
    elif "final_flickr_separateGT_train" in self.json_file:
        assert instance_count == 640704
    else:
        assert False





ultralytics.data.dataset.YOLOConcatDataset

Bases: ConcatDataset

Dataset as a concatenation of multiple datasets.

This class is useful to assemble different existing datasets for YOLO training, ensuring they use the same collation function.

Methods:

Name Description
collate_fn

Static method that collates data samples into batches using YOLODataset's collation function.

Examples:

>>> dataset1 = YOLODataset(...)
>>> dataset2 = YOLODataset(...)
>>> combined_dataset = YOLOConcatDataset([dataset1, dataset2])

close_mosaic

close_mosaic(hyp)

Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.

Parameters:

Name Type Description Default
hyp dict

Hyperparameters for transforms.

required
Source code in ultralytics/data/dataset.py
664
665
666
667
668
669
670
671
672
673
674
def close_mosaic(self, hyp):
    """
    Sets mosaic, copy_paste and mixup options to 0.0 and builds transformations.

    Args:
        hyp (dict): Hyperparameters for transforms.
    """
    for dataset in self.datasets:
        if not hasattr(dataset, "close_mosaic"):
            continue
        dataset.close_mosaic(hyp)

collate_fn staticmethod

collate_fn(batch)

Collates data samples into batches.

Parameters:

Name Type Description Default
batch List[dict]

List of dictionaries containing sample data.

required

Returns:

Type Description
dict

Collated batch with stacked tensors.

Source code in ultralytics/data/dataset.py
651
652
653
654
655
656
657
658
659
660
661
662
@staticmethod
def collate_fn(batch):
    """
    Collates data samples into batches.

    Args:
        batch (List[dict]): List of dictionaries containing sample data.

    Returns:
        (dict): Collated batch with stacked tensors.
    """
    return YOLODataset.collate_fn(batch)





ultralytics.data.dataset.SemanticDataset

SemanticDataset()

Bases: BaseDataset

Semantic Segmentation Dataset.

Source code in ultralytics/data/dataset.py
681
682
683
def __init__(self):
    """Initialize a SemanticDataset object."""
    super().__init__()





ultralytics.data.dataset.ClassificationDataset

ClassificationDataset(root, args, augment=False, prefix='')

Extends torchvision ImageFolder to support YOLO classification tasks.

This class offers functionalities like image augmentation, caching, and verification. It's designed to efficiently handle large datasets for training deep learning models, with optional image transformations and caching mechanisms to speed up training.

Attributes:

Name Type Description
cache_ram bool

Indicates if caching in RAM is enabled.

cache_disk bool

Indicates if caching on disk is enabled.

samples list

A list of tuples, each containing the path to an image, its class index, path to its .npy cache file (if caching on disk), and optionally the loaded image array (if caching in RAM).

torch_transforms callable

PyTorch transforms to be applied to the images.

root str

Root directory of the dataset.

prefix str

Prefix for logging and cache filenames.

Methods:

Name Description
__getitem__

Returns subset of data and targets corresponding to given indices.

__len__

Returns the total number of samples in the dataset.

verify_images

Verifies all images in dataset.

Parameters:

Name Type Description Default
root str

Path to the dataset directory where images are stored in a class-specific folder structure.

required
args Namespace

Configuration containing dataset-related settings such as image size, augmentation parameters, and cache settings.

required
augment bool

Whether to apply augmentations to the dataset.

False
prefix str

Prefix for logging and cache filenames, aiding in dataset identification.

''
Source code in ultralytics/data/dataset.py
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
def __init__(self, root, args, augment=False, prefix=""):
    """
    Initialize YOLO object with root, image size, augmentations, and cache settings.

    Args:
        root (str): Path to the dataset directory where images are stored in a class-specific folder structure.
        args (Namespace): Configuration containing dataset-related settings such as image size, augmentation
            parameters, and cache settings.
        augment (bool, optional): Whether to apply augmentations to the dataset.
        prefix (str, optional): Prefix for logging and cache filenames, aiding in dataset identification.
    """
    import torchvision  # scope for faster 'import ultralytics'

    # Base class assigned as attribute rather than used as base class to allow for scoping slow torchvision import
    if TORCHVISION_0_18:  # 'allow_empty' argument first introduced in torchvision 0.18
        self.base = torchvision.datasets.ImageFolder(root=root, allow_empty=True)
    else:
        self.base = torchvision.datasets.ImageFolder(root=root)
    self.samples = self.base.samples
    self.root = self.base.root

    # Initialize attributes
    if augment and args.fraction < 1.0:  # reduce training fraction
        self.samples = self.samples[: round(len(self.samples) * args.fraction)]
    self.prefix = colorstr(f"{prefix}: ") if prefix else ""
    self.cache_ram = args.cache is True or str(args.cache).lower() == "ram"  # cache images into RAM
    if self.cache_ram:
        LOGGER.warning(
            "Classification `cache_ram` training has known memory leak in "
            "https://github.com/ultralytics/ultralytics/issues/9824, setting `cache_ram=False`."
        )
        self.cache_ram = False
    self.cache_disk = str(args.cache).lower() == "disk"  # cache images on hard drive as uncompressed *.npy files
    self.samples = self.verify_images()  # filter out bad images
    self.samples = [list(x) + [Path(x[0]).with_suffix(".npy"), None] for x in self.samples]  # file, index, npy, im
    scale = (1.0 - args.scale, 1.0)  # (0.08, 1.0)
    self.torch_transforms = (
        classify_augmentations(
            size=args.imgsz,
            scale=scale,
            hflip=args.fliplr,
            vflip=args.flipud,
            erasing=args.erasing,
            auto_augment=args.auto_augment,
            hsv_h=args.hsv_h,
            hsv_s=args.hsv_s,
            hsv_v=args.hsv_v,
        )
        if augment
        else classify_transforms(size=args.imgsz)
    )

__getitem__

__getitem__(i)

Returns subset of data and targets corresponding to given indices.

Parameters:

Name Type Description Default
i int

Index of the sample to retrieve.

required

Returns:

Type Description
dict

Dictionary containing the image and its class index.

Source code in ultralytics/data/dataset.py
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
def __getitem__(self, i):
    """
    Returns subset of data and targets corresponding to given indices.

    Args:
        i (int): Index of the sample to retrieve.

    Returns:
        (dict): Dictionary containing the image and its class index.
    """
    f, j, fn, im = self.samples[i]  # filename, index, filename.with_suffix('.npy'), image
    if self.cache_ram:
        if im is None:  # Warning: two separate if statements required here, do not combine this with previous line
            im = self.samples[i][3] = cv2.imread(f)
    elif self.cache_disk:
        if not fn.exists():  # load npy
            np.save(fn.as_posix(), cv2.imread(f), allow_pickle=False)
        im = np.load(fn)
    else:  # read image
        im = cv2.imread(f)  # BGR
    # Convert NumPy array to PIL image
    im = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
    sample = self.torch_transforms(im)
    return {"img": sample, "cls": j}

__len__

__len__() -> int

Return the total number of samples in the dataset.

Source code in ultralytics/data/dataset.py
786
787
788
def __len__(self) -> int:
    """Return the total number of samples in the dataset."""
    return len(self.samples)

verify_images

verify_images()

Verify all images in dataset.

Returns:

Type Description
list

List of valid samples after verification.

Source code in ultralytics/data/dataset.py
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
def verify_images(self):
    """
    Verify all images in dataset.

    Returns:
        (list): List of valid samples after verification.
    """
    desc = f"{self.prefix}Scanning {self.root}..."
    path = Path(self.root).with_suffix(".cache")  # *.cache file path

    try:
        check_file_speeds([file for (file, _) in self.samples[:5]], prefix=self.prefix)  # check image read speeds
        cache = load_dataset_cache_file(path)  # attempt to load a *.cache file
        assert cache["version"] == DATASET_CACHE_VERSION  # matches current version
        assert cache["hash"] == get_hash([x[0] for x in self.samples])  # identical hash
        nf, nc, n, samples = cache.pop("results")  # found, missing, empty, corrupt, total
        if LOCAL_RANK in {-1, 0}:
            d = f"{desc} {nf} images, {nc} corrupt"
            TQDM(None, desc=d, total=n, initial=n)
            if cache["msgs"]:
                LOGGER.info("\n".join(cache["msgs"]))  # display warnings
        return samples

    except (FileNotFoundError, AssertionError, AttributeError):
        # Run scan if *.cache retrieval failed
        nf, nc, msgs, samples, x = 0, 0, [], [], {}
        with ThreadPool(NUM_THREADS) as pool:
            results = pool.imap(func=verify_image, iterable=zip(self.samples, repeat(self.prefix)))
            pbar = TQDM(results, desc=desc, total=len(self.samples))
            for sample, nf_f, nc_f, msg in pbar:
                if nf_f:
                    samples.append(sample)
                if msg:
                    msgs.append(msg)
                nf += nf_f
                nc += nc_f
                pbar.desc = f"{desc} {nf} images, {nc} corrupt"
            pbar.close()
        if msgs:
            LOGGER.info("\n".join(msgs))
        x["hash"] = get_hash([x[0] for x in self.samples])
        x["results"] = nf, nc, len(samples), samples
        x["msgs"] = msgs  # warnings
        save_dataset_cache_file(self.prefix, path, x, DATASET_CACHE_VERSION)
        return samples





📅 Created 1 year ago ✏️ Updated 8 months ago