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

improve is_label_first error handling #217

Merged
Merged
Changes from all commits
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
18 changes: 17 additions & 1 deletion src/data_gradients/dataset_adapters/formatters/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ def format(self, images: Tensor, labels: Tensor) -> Tuple[Tensor, List[Tensor]]:

# Labels format transformations are only relevant if we have labels
if labels.numel() > 0:

if self.label_first is None:
# If possible, we try to infer the value of `label_first`.
first_col, last_col = labels[..., :1], labels[..., -1:]
if check_all_integers(first_col) and not check_all_integers(last_col):
self.label_first = True
elif not check_all_integers(first_col) and check_all_integers(last_col):
self.label_first = False

# This condition is not required because self.data_config caches answers,
# But adding this condition avoids unnecessary compute.
if self.label_first is None or self.xyxy_converter is None:
Expand Down Expand Up @@ -147,7 +156,14 @@ def convert_to_label_xyxy(annotated_bboxes: Tensor, image_shape: Tuple[int, int]
labels, bboxes = annotated_bboxes[..., -1:], annotated_bboxes[..., :-1]

if not check_all_integers(labels):
raise RuntimeError(f"Labels should all be integers, but got {labels}")
label_order_str = "label is first" if label_first else "label is last"
raise ValueError(
f"Labels should all be integers. Found non-integer labels.\n"
f"Annotations=\n{annotated_bboxes}\n"
f"It was previously said that '{label_order_str}', so Labels=\n{labels}."
f"If the labels are another column of your annotations, please re-run and answer properly the question on that topic. "
f"Otherwise, please make sure that your dataset encodes your labels (class ids) in `int` format."
)

xyxy_bboxes = xyxy_converter(bboxes)

Expand Down