Reference for ultralytics/models/rtdetr/val.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/rtdetr/val.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.rtdetr.val.RTDETRDataset
RTDETRDataset(*args, data=None, **kwargs)
Bases: YOLODataset
Real-Time DEtection and TRacking (RT-DETR) dataset class extending the base YOLODataset class.
This specialized dataset class is designed for use with the RT-DETR object detection model and is optimized for real-time detection and tracking tasks.
Attributes:
Name | Type | Description |
---|---|---|
augment |
bool
|
Whether to apply data augmentation. |
rect |
bool
|
Whether to use rectangular training. |
use_segments |
bool
|
Whether to use segmentation masks. |
use_keypoints |
bool
|
Whether to use keypoint annotations. |
imgsz |
int
|
Target image size for training. |
Methods:
Name | Description |
---|---|
load_image |
Load one image from dataset index. |
build_transforms |
Build transformation pipeline for the dataset. |
Examples:
Initialize an RT-DETR dataset
>>> dataset = RTDETRDataset(img_path="path/to/images", imgsz=640)
>>> image, hw = dataset.load_image(0)
This constructor sets up a dataset specifically optimized for the RT-DETR (Real-Time DEtection and TRacking) model, building upon the base YOLODataset functionality.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Variable length argument list passed to the parent YOLODataset class. |
()
|
data
|
dict | None
|
Dictionary containing dataset information. If None, default values will be used. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to the parent YOLODataset class. |
{}
|
Source code in ultralytics/models/rtdetr/val.py
39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
build_transforms
build_transforms(hyp=None)
Build transformation pipeline for the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hyp
|
dict
|
Hyperparameters for transformations. |
None
|
Returns:
Type | Description |
---|---|
Compose
|
Composition of transformation functions. |
Source code in ultralytics/models/rtdetr/val.py
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 |
|
load_image
load_image(i, rect_mode=False)
Load one 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 mode for batch inference. |
False
|
Returns:
Name | Type | Description |
---|---|---|
im |
Tensor
|
The loaded image. |
resized_hw |
tuple
|
Height and width of the resized image with shape (2,). |
Examples:
Load an image from the dataset
>>> dataset = RTDETRDataset(img_path="path/to/images")
>>> image, hw = dataset.load_image(0)
Source code in ultralytics/models/rtdetr/val.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
ultralytics.models.rtdetr.val.RTDETRValidator
RTDETRValidator(dataloader=None, save_dir=None, args=None, _callbacks=None)
Bases: DetectionValidator
RTDETRValidator extends the DetectionValidator class to provide validation capabilities specifically tailored for the RT-DETR (Real-Time DETR) object detection model.
The class allows building of an RTDETR-specific dataset for validation, applies Non-maximum suppression for post-processing, and updates evaluation metrics accordingly.
Attributes:
Name | Type | Description |
---|---|---|
args |
Namespace
|
Configuration arguments for validation. |
data |
dict
|
Dataset configuration dictionary. |
Methods:
Name | Description |
---|---|
build_dataset |
Build an RTDETR Dataset for validation. |
postprocess |
Apply Non-maximum suppression to prediction outputs. |
Examples:
Initialize and run RT-DETR validation
>>> from ultralytics.models.rtdetr import RTDETRValidator
>>> args = dict(model="rtdetr-l.pt", data="coco8.yaml")
>>> validator = RTDETRValidator(args=args)
>>> validator()
Notes
For further details on the attributes and methods, refer to the parent DetectionValidator class.
Source code in ultralytics/models/yolo/detect/val.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
build_dataset
build_dataset(img_path, mode='val', batch=None)
Build an RTDETR Dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_path
|
str
|
Path to the folder containing images. |
required |
mode
|
str
|
|
'val'
|
batch
|
int
|
Size of batches, this is for |
None
|
Returns:
Type | Description |
---|---|
RTDETRDataset
|
Dataset configured for RT-DETR validation. |
Source code in ultralytics/models/rtdetr/val.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
postprocess
postprocess(
preds: Union[Tensor, List[Tensor], Tuple[Tensor]],
) -> List[Dict[str, torch.Tensor]]
Apply Non-maximum suppression to prediction outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tensor | List | Tuple
|
Raw predictions from the model. If tensor, should have shape (batch_size, num_predictions, num_classes + 4) where last dimension contains bbox coords and class scores. |
required |
Returns:
Type | Description |
---|---|
List[Dict[str, Tensor]]
|
List of dictionaries for each image, each containing: - 'bboxes': Tensor of shape (N, 4) with bounding box coordinates - 'conf': Tensor of shape (N,) with confidence scores - 'cls': Tensor of shape (N,) with class indices |
Source code in ultralytics/models/rtdetr/val.py
156 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 |
|