* Refactor code for speed and clarity

* Update dataloaders.py

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>

---------

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Glenn Jocher authored and GitHub committed 2024-07-01 00:17:24 +02:00
1 parent b9019671f4
commit 100a423b66
14 files changed
+30

No files matched your search

+4
View File
@@ -157,6 +157,7 @@ def random_perspective(
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10))
# targets = [cls, xyxy]
"""Applies random perspective transformation to an image, modifying the image and corresponding labels."""
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
width = im.shape[1] + border[1] * 2
@@ -336,6 +337,9 @@ def classify_albumentations(
auto_aug=False,
):
# YOLOv5 classification Albumentations (optional, only used if package is installed)
"""Sets up and returns Albumentations transforms for YOLOv5 classification tasks depending on augmentation
settings.
"""
prefix = colorstr("albumentations: ")
try:
import albumentations as A
+3
View File
@@ -174,6 +174,7 @@ def create_dataloader(
shuffle=False,
seed=0,
):
"""Creates and returns a configured DataLoader instance for loading and processing image datasets."""
if rect and shuffle:
LOGGER.warning("WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False")
shuffle = False
@@ -552,6 +553,7 @@ class LoadImagesAndLabels(Dataset):
rank=-1,
seed=0,
):
"""Initializes the YOLOv5 dataset loader, handling images and their labels, caching, and preprocessing."""
self.img_size = img_size
self.augment = augment
self.hyp = hyp
@@ -1351,6 +1353,7 @@ def create_classification_dataloader(
path, imgsz=224, batch_size=16, augment=True, cache=False, rank=-1, workers=8, shuffle=True
):
# Returns Dataloader object to be used with YOLOv5 Classifier
"""Creates a DataLoader for image classification, supporting caching, augmentation, and distributed training."""
with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP
dataset = ClassificationDataset(root=path, imgsz=imgsz, augment=augment, cache=cache)
batch_size = min(batch_size, len(dataset))
+1
View File
@@ -30,6 +30,7 @@ def random_perspective(
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
# targets = [cls, xyxy]
"""Applies random perspective, rotation, scale, shear, and translation augmentations to an image and targets."""
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
width = im.shape[1] + border[1] * 2
+2
View File
@@ -39,6 +39,7 @@ def create_dataloader(
overlap_mask=False,
seed=0,
):
"""Creates a dataloader for training, validating, or testing YOLO models with various dataset options."""
if rect and shuffle:
LOGGER.warning("WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False")
shuffle = False
@@ -102,6 +103,7 @@ class LoadImagesAndLabelsAndMasks(LoadImagesAndLabels): # for training/testing
rank=-1,
seed=0,
):
"""Initializes the dataset with image, label, and mask loading capabilities for training/testing."""
super().__init__(
path,
img_size,