Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class_inclusion_list in DetectionDataset #1327

Merged
merged 5 commits into from
Aug 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename
Louis-Dupont committed Jul 31, 2023
commit 8f5324f6ae7f39f1836f5cef7630f5c8cf80c417
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
from super_gradients.common.object_names import Datasets, Processings
from super_gradients.common.registry.registry import register_dataset
from super_gradients.common.decorators.factory_decorator import resolve_param
from super_gradients.training.utils.detection_utils import get_cls_posx_in_target
from super_gradients.training.utils.detection_utils import get_class_index_in_target
from super_gradients.common.abstractions.abstract_logger import get_logger
from super_gradients.training.transforms.transforms import DetectionTransform, DetectionTargetsFormatTransform, DetectionTargetsFormat
from super_gradients.common.exceptions.dataset_exceptions import EmptyDatasetException, DatasetValidationException
@@ -232,26 +232,26 @@ def _sub_class_annotation(self, annotation: dict) -> Union[dict, None]:
:param annotation: Dict representing the annotation of a specific image
:return: Subclassed annotation if non empty after subclassing, otherwise None
"""
cls_index = _get_cls_index_in_target(target_format=self.original_target_format)
class_index = _get_class_index_in_target(target_format=self.original_target_format)
for field in self.target_fields:
annotation[field] = self._sub_class_target(targets=annotation[field], cls_posx=cls_index)
annotation[field] = self._sub_class_target(targets=annotation[field], class_index=class_index)
return annotation

def _sub_class_target(self, targets: np.ndarray, cls_posx: int) -> np.ndarray:
def _sub_class_target(self, targets: np.ndarray, class_index: int) -> np.ndarray:
"""Sublass targets of a specific image.

:param targets: Target array to subclass of shape [n_targets, 5], 5 representing a bbox
:param cls_posx: Position of the class id in a bbox
:param class_index: Position of the class id in a bbox
ex: 0 if bbox of format label_xyxy | -1 if bbox of format xyxy_label
:return: Subclassed target
"""
targets_kept = []
for target in targets:
cls_id = int(target[cls_posx])
cls_id = int(target[class_index])
cls_name = self.all_classes_list[cls_id]
if cls_name in self.class_inclusion_list:
# Replace the target cls_id in self.all_classes_list by cls_id in self.class_inclusion_list
target[cls_posx] = self.class_inclusion_list.index(cls_name)
target[class_index] = self.class_inclusion_list.index(cls_name)
targets_kept.append(target)

return np.array(targets_kept) if len(targets_kept) > 0 else np.zeros((0, 5), dtype=np.float32)
@@ -507,11 +507,15 @@ def get_dataset_preprocessing_params(self):
return params


def _get_cls_index_in_target(target_format: DetectionTargetsFormat) -> int:
def _get_class_index_in_target(target_format: DetectionTargetsFormat) -> int:
"""Get the index of the class in the target format.
:param target_format: format of the target. E.g. XYXY_LABEL, LABEL_NORMALIZED_XYXY, ect...
:return: index of the class in the target format. E.g. XYXY_LABEL -> 4, LABEL_NORMALIZED_XYXY -> 0, ect....
"""
if isinstance(target_format, ConcatenatedTensorFormat):
return target_format.indexes[LabelTensorSliceItem.NAME][0]
elif isinstance(target_format, DetectionTargetsFormat):
return get_cls_posx_in_target(target_format)
return get_class_index_in_target(target_format)
else:
raise NotImplementedError(
f"{target_format} is not supported. Supported formats are: {ConcatenatedTensorFormat.__name__}, {DetectionTargetsFormat.__name__}"
2 changes: 1 addition & 1 deletion src/super_gradients/training/utils/detection_utils.py
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ class DetectionTargetsFormat(Enum):
NORMALIZED_CXCYWH_LABEL = "NORMALIZED_CXCYWH_LABEL"


def get_cls_posx_in_target(target_format: DetectionTargetsFormat) -> int:
def get_class_index_in_target(target_format: DetectionTargetsFormat) -> int:
"""Get the label of a given target
:param target_format: Representation of the target (ex: LABEL_XYXY)
:return: Position of the class id in a bbox