keep_classes#

Dataset.keep_classes(to_keep: int | Iterable[int], remove_emptied_images: bool = False) Self[source]#

Perform a simple remapping, where given classes kept, and other are removed

Notes

  • This function is equivalent to calling remap_classes() where the remapping dictionary is the identity except only kept classes appear.

  • This function is the complementary to remove_classes().

Parameters:
  • to_keep – list of class ids to keep.

  • remove_emptied_images – If set to True, will remove from self.images the images that are now empty of annotation. Note that it will keep the images that were empty before the remapping. Defaults to False.

Returns:

New dataset object where given classes have been kept, and the rest removed.

Example

>>> from lours.utils.doc_utils import dummy_dataset
>>> example = dummy_dataset(2, 2, seed=1)
>>> example
Dataset object containing 2 images and 2 objects
Name :
    shake_effort_many
Images root :
    care/suggest
Images :
    width  height        relative_path  type  split
id
0     955     229  determine/story.jpg  .jpg  train
1     131     840       air/method.bmp  .bmp  train
Annotations :
    image_id category_str  category_id  ...   box_y_min   box_width  box_height
id                                      ...
0          1       listen           14  ...  276.974642    9.718823  184.684056
1          0        reach           22  ...    6.311037  123.141689  174.239136

[2 rows x 8 columns]
Label map :
{14: 'listen', 15: 'marriage', 22: 'reach'}
>>> example.keep_classes([15, 22])
Dataset object containing 2 images and 1 object
Name :
    shake_effort_many
Images root :
    care/suggest
Images :
    width  height        relative_path  type  split
id
0     955     229  determine/story.jpg  .jpg  train
1     131     840       air/method.bmp  .bmp  train
Annotations :
    image_id category_str  category_id  ... box_y_min   box_width  box_height
id                                      ...
1          0        reach           22  ...  6.311037  123.141689  174.239136

[1 rows x 8 columns]
Label map :
{15: 'marriage', 22: 'reach'}
>>> example.keep_classes(22)
Dataset object containing 2 images and 1 object
Name :
    shake_effort_many
Images root :
    care/suggest
Images :
    width  height        relative_path  type  split
id
0     955     229  determine/story.jpg  .jpg  train
1     131     840       air/method.bmp  .bmp  train
Annotations :
    image_id category_str  category_id  ... box_y_min   box_width  box_height
id                                      ...
1          0        reach           22  ...  6.311037  123.141689  174.239136

[1 rows x 8 columns]
Label map :
{22: 'reach'}
>>> example.keep_classes([15, 22], remove_emptied_images=True)
Dataset object containing 1 image and 1 object
Name :
    shake_effort_many
Images root :
    care/suggest
Images :
    width  height        relative_path  type  split
id
0     955     229  determine/story.jpg  .jpg  train
Annotations :
    image_id category_str  category_id  ... box_y_min   box_width  box_height
id                                      ...
1          0        reach           22  ...  6.311037  123.141689  174.239136

[1 rows x 8 columns]
Label map :
{15: 'marriage', 22: 'reach'}