Reference for ultralytics/data/base.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/base.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.data.base.BaseDataset
BaseDataset(
img_path,
imgsz=640,
cache=False,
augment=True,
hyp=DEFAULT_CFG,
prefix="",
rect=False,
batch_size=16,
stride=32,
pad=0.5,
single_cls=False,
classes=None,
fraction=1.0,
)
Bases: Dataset
Base dataset class for loading and processing image data.
This class provides core functionality for loading images, caching, and preparing data for training and inference in object detection tasks.
Attributes:
Name | Type | Description |
---|---|---|
img_path |
str
|
Path to the folder containing images. |
imgsz |
int
|
Target image size for resizing. |
augment |
bool
|
Whether to apply data augmentation. |
single_cls |
bool
|
Whether to treat all objects as a single class. |
prefix |
str
|
Prefix to print in log messages. |
fraction |
float
|
Fraction of dataset to utilize. |
im_files |
List[str]
|
List of image file paths. |
labels |
List[Dict]
|
List of label data dictionaries. |
ni |
int
|
Number of images in the dataset. |
rect |
bool
|
Whether to use rectangular training. |
batch_size |
int
|
Size of batches. |
stride |
int
|
Stride used in the model. |
pad |
float
|
Padding value. |
buffer |
list
|
Buffer for mosaic images. |
max_buffer_length |
int
|
Maximum buffer size. |
ims |
list
|
List of loaded images. |
im_hw0 |
list
|
List of original image dimensions (h, w). |
im_hw |
list
|
List of resized image dimensions (h, w). |
npy_files |
List[Path]
|
List of numpy file paths. |
cache |
str
|
Cache images to RAM or disk during training. |
transforms |
callable
|
Image transformation function. |
Methods:
Name | Description |
---|---|
get_img_files |
Read image files from the specified path. |
update_labels |
Update labels to include only specified classes. |
load_image |
Load an image from the dataset. |
cache_images |
Cache images to memory or disk. |
cache_images_to_disk |
Save an image as an *.npy file for faster loading. |
check_cache_disk |
Check image caching requirements vs available disk space. |
check_cache_ram |
Check image caching requirements vs available memory. |
set_rectangle |
Set the shape of bounding boxes as rectangles. |
get_image_and_label |
Get and return label information from the dataset. |
update_labels_info |
Custom label format method to be implemented by subclasses. |
build_transforms |
Build transformation pipeline to be implemented by subclasses. |
get_labels |
Get labels method to be implemented by subclasses. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
str
|
Path to the folder containing images. |
required |
imgsz
|
int
|
Image size for resizing. |
640
|
cache
|
bool | str
|
Cache images to RAM or disk during training. |
False
|
augment
|
bool
|
If True, data augmentation is applied. |
True
|
hyp
|
dict
|
Hyperparameters to apply data augmentation. |
DEFAULT_CFG
|
prefix
|
str
|
Prefix to print in log messages. |
''
|
rect
|
bool
|
If True, rectangular training is used. |
False
|
batch_size
|
int
|
Size of batches. |
16
|
stride
|
int
|
Stride used in the model. |
32
|
pad
|
float
|
Padding value. |
0.5
|
single_cls
|
bool
|
If True, single class training is used. |
False
|
classes
|
list
|
List of included classes. |
None
|
fraction
|
float
|
Fraction of dataset to utilize. |
1.0
|
Source code in ultralytics/data/base.py
__getitem__
__len__
build_transforms
cache_images
Cache images to memory or disk for faster training.
Source code in ultralytics/data/base.py
cache_images_to_disk
Save an image as an *.npy file for faster loading.
check_cache_disk
Check if there's enough disk space for caching images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
safety_margin
|
float
|
Safety margin factor for disk space calculation. |
0.5
|
Returns:
Type | Description |
---|---|
bool
|
True if there's enough disk space, False otherwise. |
Source code in ultralytics/data/base.py
check_cache_ram
Check if there's enough RAM for caching images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
safety_margin
|
float
|
Safety margin factor for RAM calculation. |
0.5
|
Returns:
Type | Description |
---|---|
bool
|
True if there's enough RAM, False otherwise. |
Source code in ultralytics/data/base.py
get_image_and_label
Get and return label information from the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
int
|
Index of the image to retrieve. |
required |
Returns:
Type | Description |
---|---|
dict
|
Label dictionary with image and metadata. |
Source code in ultralytics/data/base.py
get_img_files
Read image files from the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
str | List[str]
|
Path or list of paths to image directories or files. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List of image file paths. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If no images are found or the path doesn't exist. |
Source code in ultralytics/data/base.py
get_labels
Users can customize their own format here.
Note
Ensure output is a dictionary with the following keys:
Source code in ultralytics/data/base.py
load_image
Load an image from dataset index 'i'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i
|
int
|
Index of the image to load. |
required |
rect_mode
|
bool
|
Whether to use rectangular resizing. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
Loaded image. |
tuple
|
Original image dimensions (h, w). |
tuple
|
Resized image dimensions (h, w). |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the image file is not found. |
Source code in ultralytics/data/base.py
set_rectangle
Set the shape of bounding boxes for YOLO detections as rectangles.
Source code in ultralytics/data/base.py
update_labels
Update labels to include only specified classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
include_class
|
list
|
List of classes to include. If None, all classes are included. |
required |