DetectionDataset
Source code in detection_datasets/detection_dataset.py
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 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 214 215 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 336 337 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 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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
|
__init__(data=None)
#
Initialize the dataset.
Don't call the constructr directly, use from_hub()
or from_disk()
methods instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
pd.DataFrame
|
The data used to initialize the dataset. Defaults to None. |
None
|
Source code in detection_datasets/detection_dataset.py
categories()
property
#
Creates a DataFrame containing the categories found in the data with their id.
Returns:
Type | Description |
---|---|
pd.DataFrame
|
A dataframe containing the categories with the category_id as index. |
Source code in detection_datasets/detection_dataset.py
category_names()
property
#
Returns the categories names.
Returns:
Type | Description |
---|---|
list[str]
|
The categories names. |
data()
property
#
Getter for the data, with annotations grouped by images.
Returns:
Type | Description |
---|---|
pd.DataFrame
|
The data contained in the dataset as a Pandas DataFrame. |
delete()
#
Delete the instance and the temporary directory it may use.
The temporary directory is created by a DetectionDataset instance when calling the from_hub()
method, and is
used to store image files.
Source code in detection_datasets/detection_dataset.py
format()
property
#
Getter for the current format of the data, which can either be "image" or "bbox".
Returns:
Type | Description |
---|---|
str
|
The current format of the data. |
from_disk(dataset_format, path, **kwargs)
#
Load a dataset from disk.
This is a factory method that can read the dataset from different formats, when the dataset is already in a local directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_format |
str
|
Format of the dataset. Currently supported values and formats: - "coco": COCO format |
required |
path |
str
|
Path to the dataset on the local filesystem. |
required |
**kwargs |
Keyword arguments specific to the dataset_format. |
{}
|
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |
Example
Source code in detection_datasets/detection_dataset.py
from_hub(dataset_name, repo_name=ORGANISATION)
#
Load a dataset from the Hugging Face Hub.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset, without the organisation's prefix. |
required |
repo_name |
str
|
name of the Hugging Face profile or organisation where the dataset is stored. Defaults to "detection-datasets". |
ORGANISATION
|
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |
Source code in detection_datasets/detection_dataset.py
get_data(index='image')
#
Getter for the data, with the possibility to specify the format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
str
|
The desired format of the data. Can be either "image" or "bbox". Defaults to "image". |
'image'
|
Returns:
Type | Description |
---|---|
pd.DataFrame
|
The data contained in the dataset as a Pandas DataFrame in the specified format. |
Source code in detection_datasets/detection_dataset.py
map_categories(mapping)
#
Maps the categories to the new categories.
The new categoy names replace the existing ones. Annotations with categories not present in the mapping are dropped. The new category_ids correspond the the rank of the new categories in alphabetical order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mapping |
dict[str, str]
|
A dictionnary mapping original categories to new categories. |
required |
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |
Source code in detection_datasets/detection_dataset.py
n_bbox()
property
#
Returns the number of images in the dataset.
Returns:
Type | Description |
---|---|
int
|
The number of images in the dataset. |
n_categories()
property
#
Returns the number of categories.
Returns:
Type | Description |
---|---|
int
|
The number of categories. |
n_images()
property
#
Returns the number of images in the dataset.
Returns:
Type | Description |
---|---|
int
|
The number of images in the dataset. |
select(n_images, seed=42)
#
Limits the number of images to n_images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_images |
int
|
Number of images to include in the dataset. The original proportion of images between splits will be respected. |
required |
seed |
int
|
Random seed. |
42
|
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |
Source code in detection_datasets/detection_dataset.py
set_format(index)
#
Set the format of the data.
The data contained in the dataset can either have: - One row per image, with the annotations grouped as a list - One row per annotation, with each image appearing on multiple rows
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
str
|
How to organise the data, can be "image" or "bbox". |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the specified format is unknown. |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
Data contained in the dataset. |
Source code in detection_datasets/detection_dataset.py
show(image_id=None)
#
Show the image with bounding boxes and labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_id |
int
|
Id of the image. If not provided, a random image is selected. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
PILImage
|
Image with bounding boxes and labels. |
Source code in detection_datasets/detection_dataset.py
shuffle(seed=42)
#
Shuffles the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed |
int
|
Random seed. |
42
|
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |
Source code in detection_datasets/detection_dataset.py
split(splits)
#
Splits the dataset into train, val and test.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
splits |
Iterable[float]
|
Iterable containing the proportion of images to include in the train, val and test splits. The sum of the values in the iterable must be equal to 1. The original splits will be overwritten. |
required |
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |
Source code in detection_datasets/detection_dataset.py
split_proportions()
property
#
Returns the proportion of images in the train, val and test splits.
Returns:
Type | Description |
---|---|
pd.DataFrame
|
The proportion of images in the train, val and test splits. |
Source code in detection_datasets/detection_dataset.py
splits()
property
#
Returns the splits of the dataset.
Returns:
Type | Description |
---|---|
list[str]
|
The splits present in the dataset. |
temp_dir_instance()
property
#
Name of the temporary directory used by the DetectionDataset instance.
When downloading images from the Hub, images are first downloaded as parquet files in the Hugging Face cache directory, before being extracted as jpeg files in the detection_dataset cache. Each instance creates its own sub-directory in the detection_dataset cache. The subdirectory for an instance is named after its id.
Returns:
Type | Description |
---|---|
str
|
Path to the ssubdirectory for the instance in the detection_dataset cache. |
Source code in detection_datasets/detection_dataset.py
to_disk(dataset_format, name, absolute_path)
#
Write the dataset to disk.
This is a factory method that can write the dataset to disk in the selected format (e.g. COCO, MMDET, YOLO)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_format |
str
|
Format of the dataset. Currently supported formats: - "yolo": YOLO format - "mmdet": MMDET internal format, see: https://mmdetection.readthedocs.io/en/latest/tutorials/customize_dataset.html#reorganize-new-data-format-to-middle-format - "coco": COCO format |
required |
name |
str
|
Name of the dataset to be created in the "path" directory. |
required |
absolute_path |
str
|
Absolute path to the directory where the dataset will be created. |
required |
**kwargs |
Keyword arguments specific to the dataset_format. |
required |
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |
Source code in detection_datasets/detection_dataset.py
to_hub(dataset_name, repo_name, **kwargs)
#
Push the dataset to the hub as a Parquet dataset.
This method wraps Hugging Face's DatasetDict.push_to_hub() method.
The dataset is pushed as a DatasetDict, meaning the each split (train, val, test), if present, will be a separate Dataset instance inside this DatasetDict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset inside the user/organisation's repository. |
required |
repo_name |
str
|
user of organisation to push the dataset to. |
required |
Returns:
Type | Description |
---|---|
DetectionDataset
|
The DetectionDataset instance. This allows for method cascading. |