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, pbar=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
|
pbar
|
bool
|
Display progress bar during validation. |
None
|
args
|
dict
|
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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
eval_json
eval_json(stats)
Evaluate YOLO output in JSON format and save predictions in DOTA format.
Source code in ultralytics/models/yolo/obb/val.py
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 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 |
|
init_metrics
init_metrics(model)
Initialize evaluation metrics for YOLO.
Source code in ultralytics/models/yolo/obb/val.py
60 61 62 63 64 |
|
plot_predictions
plot_predictions(batch, preds, ni)
Plot predicted bounding boxes on input images and save the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch
|
dict
|
Batch data containing images, file paths, and other metadata. |
required |
preds
|
list
|
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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
pred_to_json
pred_to_json(predn, filename)
Convert YOLO predictions to COCO JSON format with rotated bounding box information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predn
|
Tensor
|
Prediction tensor containing bounding box coordinates, confidence scores, class predictions, and rotation angles with shape (N, 6+) where the last column is the angle. |
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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
save_one_txt
save_one_txt(predn, save_conf, shape, file)
Save YOLO OBB (Oriented Bounding Box) 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
|
Original image shape in format (height, width). |
required |
file
|
Path | str
|
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
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|