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
14 15 16 17 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 |
|
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:
```python
import numpy as np
a = np.loadtxt('data/coco.names', dtype='str', delimiter='
') b = np.loadtxt('data/coco_paper.names', dtype='str', delimiter=' ') 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
117 118 119 120 121 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 |
|
ultralytics.data.converter.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
216 217 218 219 220 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 |
|
ultralytics.data.converter.convert_dota_to_yolo_obb(dota_root_path)
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
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 |
|
ultralytics.data.converter.min_index(arr1, arr2)
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(segments)
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(im_dir, save_dir=None, sam_model='sam_b.pt')
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'
|
Notes
The input directory structure assumed for dataset:
- im_dir
├─ 001.jpg
├─ ..
└─ NNN.jpg
- labels
├─ 001.txt
├─ ..
└─ NNN.txt