Data API¶
Datasets¶
modern_yolonas.data.coco.COCODetectionDataset
¶
Bases: Dataset
COCO-format detection dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str | Path
|
Path to image directory (e.g., |
required |
ann_file
|
str | Path
|
Path to annotation JSON (e.g., |
required |
transforms
|
Transform | None
|
|
None
|
input_size
|
int
|
Target input size (used by transforms). |
640
|
cat_id_to_label
|
dict[int, int] | None
|
Category id → label index to use instead of deriving one from this file. Required when this dataset trains alongside another: the derived mapping only counts categories that have annotations, so a file missing one rare class would shift every later label by one. |
None
|
Source code in src/modern_yolonas/data/coco.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 139 140 | |
load_raw(index)
¶
Load image and targets without transforms.
Source code in src/modern_yolonas/data/coco.py
modern_yolonas.data.yolo.YOLODetectionDataset
¶
Bases: Dataset
Source code in src/modern_yolonas/data/yolo.py
Transforms¶
modern_yolonas.data.transforms
¶
Detection-aware data augmentations.
Each transform operates on (image, targets) where:
- image: HWC uint8 BGR numpy array
- targets: [N, 5] numpy array with [class_id, x_center, y_center, w, h] (normalized)
HSVAugment, HorizontalFlip, RandomAffine, RandomResizedCrop, and
RandomChannelSwap are backed by Albumentations <https://albumentations.ai>_
(MIT license, v2+).
Mosaic, Mixup, LetterboxResize, and Normalize use native implementations
because they have no direct Albumentations equivalent.
Compose
¶
Chain multiple transforms sequentially.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
list
|
List of callables, each accepting |
required |
Source code in src/modern_yolonas/data/transforms.py
disable_mosaic_mixup()
¶
Turn off any Mosaic or Mixup in the chain (called by CloseMosaicCallback).
TrainTransformPipeline holds them in named slots; a plain Compose just has
them somewhere in the list, so find them by type. Idempotent.
Source code in src/modern_yolonas/data/transforms.py
HSVAugment
¶
Randomly shift hue, saturation and value.
Implemented with three 256-entry lookup tables rather than through
Albumentations. The shift is constant across the image, so it is exactly a
per-channel table lookup, and cv2.LUT applies one in a single pass. The
Albumentations route cost two whole-image copies to swap BGR/RGB around a call
that converted colour space again internally — 5.32 ms against 2.32 ms per
640x640 image, measured 2026-09-19.
The shift ranges are unchanged: OpenCV's uint8 HSV encoding puts hue in
[0, 179] and saturation and value in [0, 255], which is the same
convention HueSaturationValue used, so recipes keep their meaning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hgain
|
int
|
Max hue shift, in OpenCV hue units. Matches the super-gradients
|
18
|
sgain
|
int
|
Max saturation shift in absolute units. Default: 30. |
30
|
vgain
|
int
|
Max value shift in absolute units. Default: 30. |
30
|
p
|
float
|
Probability of applying the transform. |
0.5
|
Source code in src/modern_yolonas/data/transforms.py
HorizontalFlip
¶
Randomly flip the image and bounding boxes horizontally via Albumentations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Probability of applying the flip. |
0.5
|
Source code in src/modern_yolonas/data/transforms.py
RandomChannelShuffle
¶
Randomly permute BGR channels (equivalent to super-gradients DetectionRGB2BGR).
Source code in src/modern_yolonas/data/transforms.py
RandomCrop
¶
Random crop with bbox clipping and re-normalization.
Source code in src/modern_yolonas/data/transforms.py
RandomAffine
¶
Apply random rotation, scale, translation, and shear via Albumentations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
degrees
|
float
|
Maximum rotation in degrees. |
0.0
|
translate
|
float
|
Maximum translation as a fraction of image size. |
0.25
|
scale
|
tuple[float, float]
|
Scale range |
(0.5, 1.5)
|
shear
|
float
|
Maximum shear in degrees. |
0.0
|
Source code in src/modern_yolonas/data/transforms.py
RandomResizedCrop
¶
Randomly crop a region of the image and resize it to size via Albumentations.
Mirrors torchvision.transforms.RandomResizedCrop but is bounding-box
aware. Boxes whose area falls below min_width / min_height pixels
after cropping are automatically discarded by _BBOX_PARAMS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Output square side length in pixels. |
640
|
scale
|
tuple[float, float]
|
Range of fraction of the original image area to crop.
Default |
(0.08, 1.0)
|
ratio
|
tuple[float, float]
|
Range of aspect ratio of the crop.
Default |
(0.75, 1.333)
|
interpolation
|
int
|
OpenCV interpolation flag (default |
INTER_LINEAR
|
p
|
float
|
Probability of applying the transform. |
1.0
|
Source code in src/modern_yolonas/data/transforms.py
RandomResizedCropFlipAffine
¶
RandomResizedCrop → HorizontalFlip → RandomAffine, as one warp.
The three transforms it replaces are each affine, so their matrices multiply and
a single cv2.warpAffine produces the same output. That is worth doing twice
over: it resamples the image once instead of three times, and it drops two full
passes over a 640x640 array plus two Albumentations bbox round-trips.
The parameters are drawn by calling Albumentations' own samplers, so the
augmentation distribution is identical by construction rather than by
reimplementation — A.Affine samples x and y scale independently, for one,
which a hand-rolled version would be unlikely to reproduce.
Two matrices, deliberately. OpenCV's resize and warpAffine map pixel
centres: the source coordinate for output pixel d is (d + 0.5) * s - 0.5.
Albumentations' bbox crop is geometric — (src - x1) * S / crop with no half-pixel
term — which is also why A.Affine returns a separate bbox_matrix. Composing
the image chain with the geometric convention shifts the picture by
0.5 * (1 - S / crop) pixels, half a pixel when the crop is upscaled 2x, while
the boxes stay put. Nothing raises; the labels simply stop matching the pixels.
Measured: with the geometric matrix the 99th percentile of the difference against
the chain is up to 14 grey levels, with the pixel-centre matrix it is 1.
So _image_matrix carries the half-pixel terms and _box_matrix does not.
They are not meant to agree.
Only axis-aligned chains are supported. With rotation or shear, clipping a box once at the end is not the same as clipping it after each step, and this class would silently produce different boxes; it raises instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Output square side. |
640
|
scale
|
tuple[float, float]
|
Crop area fraction range, as |
(0.05, 0.8)
|
ratio
|
tuple[float, float]
|
Crop aspect ratio range, as |
(0.75, 1.33)
|
flip_prob
|
float
|
Probability of the horizontal flip. |
0.5
|
translate
|
float
|
Affine translation as a fraction of output size. |
0.25
|
affine_scale
|
tuple[float, float]
|
Affine scale range. |
(0.5, 1.5)
|
degrees
|
float
|
Rotation. Must be 0. |
0.0
|
shear
|
float
|
Shear. Must be 0. |
0.0
|
pad_value
|
int
|
Fill for pixels pulled in from outside the source. |
114
|
min_box_size
|
float
|
Drop boxes thinner than this, in output pixels. Matches the
|
2.0
|
Source code in src/modern_yolonas/data/transforms.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
sample_params(height, width)
¶
Draw one set of parameters. Split out so tests can drive both paths alike.
Source code in src/modern_yolonas/data/transforms.py
RandomChannelSwap
¶
Randomly swap BGR channel order to RGB (and vice-versa) via Albumentations.
Adds photometric variety without touching bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Probability of swapping channels. |
0.5
|
Source code in src/modern_yolonas/data/transforms.py
CenterCrop
¶
Crop the center of the image to size × size pixels.
Bounding boxes that fall outside the cropped region are discarded;
those that overlap are clipped to the new canvas by _BBOX_PARAMS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Output square side length in pixels. |
640
|
Source code in src/modern_yolonas/data/transforms.py
Mosaic
¶
4-image mosaic augmentation.
Source code in src/modern_yolonas/data/transforms.py
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 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 | |
Mixup
¶
Mixup augmentation for detection.
Should be placed in the pipeline after LetterboxResize and
before Normalize, so both images are already square uint8.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
BaseDetectionDataset
|
Dataset exposing a |
required |
alpha
|
float
|
Beta distribution |
1.5
|
beta
|
float
|
Beta distribution |
1.5
|
prob
|
float
|
Per-sample probability of applying mixup. |
0.5
|
Source code in src/modern_yolonas/data/transforms.py
LetterboxResize
¶
Resize with aspect ratio preservation and center padding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_size
|
int
|
Output square dimension. |
640
|
pad_value
|
int
|
Pixel value for padding (default 114, matching YOLO convention). |
114
|
Source code in src/modern_yolonas/data/transforms.py
Normalize
¶
Convert HWC BGR uint8 to CHW RGB, optionally leaving the scaling to the GPU.
With dtype="uint8" the array stays 1.23 MB per 640x640 sample instead of
4.92 MB, which is what then crosses the collate function, the pin_memory
thread and the PCIe bus; YoloNASLightningModule.on_after_batch_transfer
divides by 255 on the device, where it is free. Measured 2026-09-19:
3.23 ms to 0.28 ms per sample, and the reconstructed float32 is bit-identical.
The default stays "float32" so that every consumer that reads batches
directly — yolonas eval, yolonas quantize, the examples — is unaffected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
str
|
|
'float32'
|
Source code in src/modern_yolonas/data/transforms.py
TrainTransformPipeline
¶
Full training augmentation pipeline with Mosaic and Mixup support.
Pipeline order (matching super-gradients): Mosaic → per_image_transforms (RandomAffine, RandomChannelShuffle, HSV, HorizontalFlip) → Mixup → final_transforms (LetterboxResize, Normalize)
Source code in src/modern_yolonas/data/transforms.py
apply(index, load_raw_fn)
¶
Dataset-aware pipeline entry point called from getitem.
Source code in src/modern_yolonas/data/transforms.py
__call__(image, targets)
¶
Fallback: per-image + final only (no Mosaic/Mixup).
Source code in src/modern_yolonas/data/transforms.py
disable_mosaic_mixup()
¶
Disable Mosaic and Mixup (called by CloseMosaicCallback).