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

Added automatic clipping for normalized annotations #177

Merged
merged 2 commits into from
Oct 3, 2024
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
74 changes: 74 additions & 0 deletions luxonis_ml/data/datasets/annotation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
from abc import ABC, abstractmethod
from datetime import datetime
from typing import Any, ClassVar, Dict, List, Literal, Optional, Tuple, TypedDict, Union
Expand All @@ -20,6 +21,8 @@

from ..utils.enums import LabelType

logger = logging.getLogger(__name__)

KeypointVisibility: TypeAlias = Literal[0, 1, 2]
NormalizedFloat: TypeAlias = Annotated[float, Field(ge=0, le=1)]
"""C{NormalizedFloat} is a float that is restricted to the range [0, 1]."""
Expand Down Expand Up @@ -137,6 +140,25 @@ class BBoxAnnotation(Annotation):

_label_type = LabelType.BOUNDINGBOX

@model_validator(mode="before")
@classmethod
def validate_values(cls, values: Dict[str, Any]) -> Dict[str, Any]:
warn = False
for key in ["x", "y", "w", "h"]:
if values[key] < -2 or values[key] > 2:
raise ValueError(
"BBox annotation has value outside of automatic clipping range ([-2, 2]). "
"Values should be normalized based on image size to range [0, 1]."
)
if not (0 <= values[key] <= 1):
warn = True
values[key] = max(0, min(1, values[key]))
if warn:
logger.warning(
"BBox annotation has values outside of [0, 1] range. Clipping them to [0, 1]."
)
return values

def to_numpy(self, class_mapping: Dict[str, int]) -> np.ndarray:
class_ = class_mapping.get(self.class_, 0)
return np.array([class_, self.x, self.y, self.w, self.h])
Expand Down Expand Up @@ -170,6 +192,33 @@ class KeypointAnnotation(Annotation):

_label_type = LabelType.KEYPOINTS

@model_validator(mode="before")
@classmethod
def validate_values(cls, values: Dict[str, Any]) -> Dict[str, Any]:
warn = False
for i, keypoint in enumerate(values["keypoints"]):
if (keypoint[0] < -2 or keypoint[0] > 2) or (
keypoint[1] < -2 or keypoint[1] > 2
):
raise ValueError(
"Keypoint annotation has value outside of automatic clipping range ([-2, 2]). "
"Values should be normalized based on image size to range [0, 1]."
)
new_keypoint = list(keypoint)
if not (0 <= keypoint[0] <= 1):
new_keypoint[0] = max(0, min(1, keypoint[0]))
warn = True
if not (0 <= keypoint[1] <= 1):
new_keypoint[1] = max(0, min(1, keypoint[1]))
warn = True
values["keypoints"][i] = tuple(new_keypoint)

if warn:
logger.warning(
"Keypoint annotation has values outside of [0, 1] range. Clipping them to [0, 1]."
)
return values

def to_numpy(self, class_mapping: Dict[str, int]) -> np.ndarray:
class_ = class_mapping.get(self.class_, 0)
kps = np.array(self.keypoints).reshape((-1, 3)).astype(np.float32)
Expand Down Expand Up @@ -340,6 +389,31 @@ class PolylineSegmentationAnnotation(SegmentationAnnotation):

points: List[Tuple[NormalizedFloat, NormalizedFloat]] = Field(min_length=3)

@model_validator(mode="before")
@classmethod
def validate_values(cls, values: Dict[str, Any]) -> Dict[str, Any]:
warn = False
for i, point in enumerate(values["points"]):
if (point[0] < -2 or point[0] > 2) or (point[1] < -2 or point[1] > 2):
raise ValueError(
"Polyline annotation has value outside of automatic clipping range ([-2, 2]). "
"Values should be normalized based on image size to range [0, 1]."
)
new_point = list(point)
if not (0 <= point[0] <= 1):
new_point[0] = max(0, min(1, point[0]))
warn = True
if not (0 <= point[1] <= 1):
new_point[1] = max(0, min(1, point[1]))
warn = True
values["points"][i] = tuple(new_point)

if warn:
logger.warning(
"Polyline annotation has values outside of [0, 1] range. Clipping them to [0, 1]."
)
return values

def to_numpy(self, _: Dict[str, int], width: int, height: int) -> np.ndarray:
polyline = [(round(x * width), round(y * height)) for x, y in self.points]
mask = Image.new("L", (width, height), 0)
Expand Down
Loading