Reference for ultralytics/data/converter.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/data/converter.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.data.converter.coco91_to_coco80_class
Converts 91-index COCO class IDs to 80-index COCO class IDs.
Returns:
Type | Description |
---|---|
list
|
A list of 91 class IDs where the index represents the 80-index class ID and the value is the corresponding 91-index class ID. |
Source code in ultralytics/data/converter.py
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 |
|
ultralytics.data.converter.coco80_to_coco91_class
Converts 80-index (val2014) to 91-index (paper). For details see https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/.
Example
import numpy as np
a = np.loadtxt("data/coco.names", dtype="str", delimiter="\n")
b = np.loadtxt("data/coco_paper.names", dtype="str", delimiter="\n")
x1 = [list(a[i] == b).index(True) + 1 for i in range(80)] # darknet to coco
x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)] # coco to darknet
Source code in ultralytics/data/converter.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 177 178 179 180 181 182 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 211 212 213 214 215 216 217 218 |
|
ultralytics.data.converter.convert_coco
convert_coco(
labels_dir="../coco/annotations/",
save_dir="coco_converted/",
use_segments=False,
use_keypoints=False,
cls91to80=True,
lvis=False,
)
Converts COCO dataset annotations to a YOLO annotation format suitable for training YOLO models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels_dir
|
str
|
Path to directory containing COCO dataset annotation files. |
'../coco/annotations/'
|
save_dir
|
str
|
Path to directory to save results to. |
'coco_converted/'
|
use_segments
|
bool
|
Whether to include segmentation masks in the output. |
False
|
use_keypoints
|
bool
|
Whether to include keypoint annotations in the output. |
False
|
cls91to80
|
bool
|
Whether to map 91 COCO class IDs to the corresponding 80 COCO class IDs. |
True
|
lvis
|
bool
|
Whether to convert data in lvis dataset way. |
False
|
Example
Output
Generates output files in the specified output directory.
Source code in ultralytics/data/converter.py
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 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 304 305 306 307 308 309 310 311 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 |
|
ultralytics.data.converter.convert_segment_masks_to_yolo_seg
Converts a dataset of segmentation mask images to the YOLO segmentation format.
This function takes the directory containing the binary format mask images and converts them into YOLO segmentation format. The converted masks are saved in the specified output directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks_dir
|
str
|
The path to the directory where all mask images (png, jpg) are stored. |
required |
output_dir
|
str
|
The path to the directory where the converted YOLO segmentation masks will be stored. |
required |
classes
|
int
|
Total classes in the dataset i.e. for COCO classes=80 |
required |
Example
Notes
The expected directory structure for the masks is:
- masks
├─ mask_image_01.png or mask_image_01.jpg
├─ mask_image_02.png or mask_image_02.jpg
├─ mask_image_03.png or mask_image_03.jpg
└─ mask_image_04.png or mask_image_04.jpg
After execution, the labels will be organized in the following structure:
- output_dir
├─ mask_yolo_01.txt
├─ mask_yolo_02.txt
├─ mask_yolo_03.txt
└─ mask_yolo_04.txt
Source code in ultralytics/data/converter.py
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 |
|
ultralytics.data.converter.convert_dota_to_yolo_obb
Converts DOTA dataset annotations to YOLO OBB (Oriented Bounding Box) format.
The function processes images in the 'train' and 'val' folders of the DOTA dataset. For each image, it reads the associated label from the original labels directory and writes new labels in YOLO OBB format to a new directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dota_root_path
|
str
|
The root directory path of the DOTA dataset. |
required |
Example
Notes
The directory structure assumed for the DOTA dataset:
- DOTA
├─ images
│ ├─ train
│ └─ val
└─ labels
├─ train_original
└─ val_original
After execution, the function will organize the labels into:
- DOTA
└─ labels
├─ train
└─ val
Source code in ultralytics/data/converter.py
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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
ultralytics.data.converter.min_index
Find a pair of indexes with the shortest distance between two arrays of 2D points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr1
|
ndarray
|
A NumPy array of shape (N, 2) representing N 2D points. |
required |
arr2
|
ndarray
|
A NumPy array of shape (M, 2) representing M 2D points. |
required |
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing the indexes of the points with the shortest distance in arr1 and arr2 respectively. |
Source code in ultralytics/data/converter.py
ultralytics.data.converter.merge_multi_segment
Merge multiple segments into one list by connecting the coordinates with the minimum distance between each segment. This function connects these coordinates with a thin line to merge all segments into one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segments
|
List[List]
|
Original segmentations in COCO's JSON file. Each element is a list of coordinates, like [segmentation1, segmentation2,...]. |
required |
Returns:
Name | Type | Description |
---|---|---|
s |
List[ndarray]
|
A list of connected segments represented as NumPy arrays. |
Source code in ultralytics/data/converter.py
ultralytics.data.converter.yolo_bbox2segment
Converts existing object detection dataset (bounding boxes) to segmentation dataset or oriented bounding box (OBB) in YOLO format. Generates segmentation data using SAM auto-annotator as needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im_dir
|
str | Path
|
Path to image directory to convert. |
required |
save_dir
|
str | Path
|
Path to save the generated labels, labels will be saved
into |
None
|
sam_model
|
str
|
Segmentation model to use for intermediate segmentation data; optional. |
'sam_b.pt'
|
device
|
int | str
|
The specific device to run SAM models. Default: None. |
None
|
Notes
The input directory structure assumed for dataset:
- im_dir
├─ 001.jpg
├─ ...
└─ NNN.jpg
- labels
├─ 001.txt
├─ ...
└─ NNN.txt
Source code in ultralytics/data/converter.py
ultralytics.data.converter.create_synthetic_coco_dataset
Creates a synthetic COCO dataset with random images based on filenames from label lists.
This function downloads COCO labels, reads image filenames from label list files, creates synthetic images for train2017 and val2017 subsets, and organizes them in the COCO dataset structure. It uses multithreading to generate images efficiently.
Examples:
>>> from ultralytics.data.converter import create_synthetic_coco_dataset
>>> create_synthetic_coco_dataset()
Notes
- Requires internet connection to download label files.
- Generates random RGB images of varying sizes (480x480 to 640x640 pixels).
- Existing test2017 directory is removed as it's not needed.
- Reads image filenames from train2017.txt and val2017.txt files.