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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 |
|
__getitem__
__getitem__(index)
Return transformed label information for given index.
Source code in ultralytics/data/base.py
367 368 369 |
|
__len__
__len__()
Return the length of the labels list for the dataset.
Source code in ultralytics/data/base.py
392 393 394 |
|
build_transforms
build_transforms(hyp=None)
Users can customize augmentations here.
Examples:
>>> if self.augment:
... # Training transforms
... return Compose([])
>>> else:
... # Val transforms
... return Compose([])
Source code in ultralytics/data/base.py
400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
cache_images
cache_images()
Cache images to memory or disk for faster training.
Source code in ultralytics/data/base.py
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
cache_images_to_disk
cache_images_to_disk(i)
Save an image as an *.npy file for faster loading.
Source code in ultralytics/data/base.py
270 271 272 273 274 |
|
check_cache_disk
check_cache_disk(safety_margin=0.5)
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
276 277 278 279 280 281 282 283 284 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 |
|
check_cache_ram
check_cache_ram(safety_margin=0.5)
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
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
get_image_and_label
get_image_and_label(index)
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
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
get_img_files
get_img_files(img_path)
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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
|
get_labels
get_labels()
Users can customize their own format here.
Examples:
Ensure output is a dictionary with the following keys:
>>> dict(
... im_file=im_file,
... shape=shape, # format: (height, width)
... cls=cls,
... bboxes=bboxes, # xywh
... segments=segments, # xy
... keypoints=keypoints, # xy
... normalized=True, # or False
... bbox_format="xyxy", # or xywh, ltwh
... )
Source code in ultralytics/data/base.py
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
load_image
load_image(i, rect_mode=True)
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
202 203 204 205 206 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
|
set_rectangle
set_rectangle()
Set the shape of bounding boxes for YOLO detections as rectangles.
Source code in ultralytics/data/base.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
update_labels
update_labels(include_class: Optional[list])
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 |
Source code in ultralytics/data/base.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
update_labels_info
update_labels_info(label)
Custom your label format here.
Source code in ultralytics/data/base.py
396 397 398 |
|