Reference for ultralytics/models/yolo/obb/val.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/yolo/obb/val.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.yolo.obb.val.OBBValidator
OBBValidator(dataloader=None, save_dir=None, args=None, _callbacks=None)
Bases: DetectionValidator
A class extending the DetectionValidator class for validation based on an Oriented Bounding Box (OBB) model.
This validator specializes in evaluating models that predict rotated bounding boxes, commonly used for aerial and satellite imagery where objects can appear at various orientations.
Attributes:
Name | Type | Description |
---|---|---|
args |
dict
|
Configuration arguments for the validator. |
metrics |
OBBMetrics
|
Metrics object for evaluating OBB model performance. |
is_dota |
bool
|
Flag indicating whether the validation dataset is in DOTA format. |
Methods:
Name | Description |
---|---|
init_metrics |
Initialize evaluation metrics for YOLO. |
_process_batch |
Process batch of detections and ground truth boxes to compute IoU matrix. |
_prepare_batch |
Prepare batch data for OBB validation. |
_prepare_pred |
Prepare predictions with scaled and padded bounding boxes. |
plot_predictions |
Plot predicted bounding boxes on input images. |
pred_to_json |
Serialize YOLO predictions to COCO json format. |
save_one_txt |
Save YOLO detections to a txt file in normalized coordinates. |
eval_json |
Evaluate YOLO output in JSON format and return performance statistics. |
Examples:
>>> from ultralytics.models.yolo.obb import OBBValidator
>>> args = dict(model="yolo11n-obb.pt", data="dota8.yaml")
>>> validator = OBBValidator(args=args)
>>> validator(model=args["model"])
This constructor initializes an OBBValidator instance for validating Oriented Bounding Box (OBB) models. It extends the DetectionValidator class and configures it specifically for the OBB task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataloader
|
DataLoader
|
Dataloader to be used for validation. |
None
|
save_dir
|
str | Path
|
Directory to save results. |
None
|
args
|
dict | SimpleNamespace
|
Arguments containing validation parameters. |
None
|
_callbacks
|
list
|
List of callback functions to be called during validation. |
None
|
Source code in ultralytics/models/yolo/obb/val.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
eval_json
eval_json(stats: Dict[str, Any]) -> Dict[str, Any]
Evaluate YOLO output in JSON format and save predictions in DOTA format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stats
|
Dict[str, Any]
|
Performance statistics dictionary. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Updated performance statistics. |
Source code in ultralytics/models/yolo/obb/val.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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 |
|
init_metrics
init_metrics(model: Module) -> None
Initialize evaluation metrics for YOLO obb validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
Module
|
Model to validate. |
required |
Source code in ultralytics/models/yolo/obb/val.py
60 61 62 63 64 65 66 67 68 69 |
|
plot_predictions
plot_predictions(batch: Dict[str, Any], preds: List[Tensor], ni: int) -> None
Plot predicted bounding boxes on input images and save the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
Dict[str, Any]
|
Batch data containing images, file paths, and other metadata. |
required |
preds
|
List[Tensor]
|
List of prediction tensors for each image in the batch. |
required |
ni
|
int
|
Batch index used for naming the output file. |
required |
Examples:
>>> validator = OBBValidator()
>>> batch = {"img": images, "im_file": paths}
>>> preds = [torch.rand(10, 7)] # Example predictions for one image
>>> validator.plot_predictions(batch, preds, 0)
Source code in ultralytics/models/yolo/obb/val.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
postprocess
postprocess(preds: Tensor) -> List[Dict[str, torch.Tensor]]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tensor
|
Raw predictions from the model. |
required |
Returns:
Type | Description |
---|---|
List[Dict[str, Tensor]]
|
Processed predictions with angle information concatenated to bboxes. |
Source code in ultralytics/models/yolo/obb/val.py
97 98 99 100 101 102 103 104 105 106 107 108 |
|
pred_to_json
pred_to_json(predn: Dict[str, Tensor], filename: Union[str, Path]) -> None
Convert YOLO predictions to COCO JSON format with rotated bounding box information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Dict[str, Tensor]
|
Prediction dictionary containing 'bboxes', 'conf', and 'cls' keys with bounding box coordinates, confidence scores, and class predictions. |
required |
filename
|
str | Path
|
Path to the image file for which predictions are being processed. |
required |
Notes
This method processes rotated bounding box predictions and converts them to both rbox format (x, y, w, h, angle) and polygon format (x1, y1, x2, y2, x3, y3, x4, y4) before adding them to the JSON dictionary.
Source code in ultralytics/models/yolo/obb/val.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
save_one_txt
save_one_txt(
predn: Dict[str, Tensor],
save_conf: bool,
shape: Tuple[int, int],
file: Path,
) -> None
Save YOLO OBB detections to a text file in normalized coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Tensor
|
Predicted detections with shape (N, 7) containing bounding boxes, confidence scores, class predictions, and angles in format (x, y, w, h, conf, cls, angle). |
required |
save_conf
|
bool
|
Whether to save confidence scores in the text file. |
required |
shape
|
Tuple[int, int]
|
Original image shape in format (height, width). |
required |
file
|
Path
|
Output file path to save detections. |
required |
Examples:
>>> validator = OBBValidator()
>>> predn = torch.tensor([[100, 100, 50, 30, 0.9, 0, 45]]) # One detection: x,y,w,h,conf,cls,angle
>>> validator.save_one_txt(predn, True, (640, 480), "detection.txt")
Source code in ultralytics/models/yolo/obb/val.py
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 |
|