remap_classes#
- Dataset.remap_classes(class_mapping: dict[int, int], new_names: dict[int, str] | None = None, remove_not_mapped: bool = True, remove_emptied_images: bool = False) Self[source]#
Remap classes ids and names according to a dictionary
Note
In case of class fusion, the class name of the last category_id with respect to
class_mappingorder will be deduced.Note
if
remove_not_mappedis True, Classes that are not present in the dictionary are removed from the dataset altogether. Otherwise, they are kept as if the identity mapping was in the bottom ofclass_mappingfor this particular class. For potential class fusion, the name of the unmapped class will be used.See also
- Parameters:
class_mapping –
old_id->new_idmappingnew_names – Optimal
new_id->new_namemapping, essentially the new label_map. If category_id is missing from keys, will deduce it from the former one. Defaults to None.remove_not_mapped – If set to True, will remove classes that are not in class mapping. Otherwise, keep them as is (with potential class fusion). Defaults to True.
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 with updated label maps, category ids and category_names
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.remap_classes({14: 1}) 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 ... 0 1 listen 1 ... 276.974642 9.718823 184.684056 [1 rows x 8 columns] Label map : {1: 'listen'}
>>> example.remap_classes({14: 1}, remove_not_mapped=False) 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 1 ... 276.974642 9.718823 184.684056 1 0 reach 22 ... 6.311037 123.141689 174.239136 [2 rows x 8 columns] Label map : {1: 'listen', 15: 'marriage', 22: 'reach'}
>>> example.remap_classes( ... {14: 1}, ... remove_not_mapped=False, ... new_names={1: "new_listen", 15: "new_marriage"}, ... ) 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 new_listen 1 ... 276.974642 9.718823 184.684056 1 0 reach 22 ... 6.311037 123.141689 174.239136 [2 rows x 8 columns] Label map : {1: 'new_listen', 15: 'new_marriage', 22: 'reach'}
>>> example.remap_classes( ... {14: 1}, ... 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 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 1 ... 276.974642 9.718823 184.684056 [1 rows x 8 columns] Label map : {1: 'listen'}
Note that only empited images are removed. Images that were already empty before are kept
>>> example = dummy_dataset(2, 2) >>> example Dataset object containing 2 images and 2 objects Name : inside_else_memory Images root : such/serious Images : width height relative_path type split id 0 342 136 help/me.jpeg .jpeg train 1 377 167 whatever/wait.png .png train Annotations : image_id category_str category_id ... box_y_min box_width box_height id ... 0 0 step 15 ... 73.932999 71.552480 42.673983 1 0 why 19 ... 4.567638 248.551257 122.602211 [2 rows x 8 columns] Label map : {15: 'step', 19: 'why', 25: 'interview'} >>> example.remap_classes({25: 1}, remove_emptied_images=True) Dataset object containing 1 image and 0 object Name : inside_else_memory Images root : such/serious Images : width height relative_path type split id 1 377 167 whatever/wait.png .png train Annotations : Empty DataFrame Columns: [image_id, category_str, category_id, split, box_x_min, box_y_min, box_width, box_height] Index: [] Label map : {1: 'interview'}