Spaces:
Sleeping
Sleeping
Update landmarkdiff/augmentation.py to v0.3.2
Browse files- landmarkdiff/augmentation.py +16 -18
landmarkdiff/augmentation.py
CHANGED
|
@@ -21,7 +21,6 @@ import numpy as np
|
|
| 21 |
@dataclass
|
| 22 |
class AugmentationConfig:
|
| 23 |
"""Augmentation parameters."""
|
| 24 |
-
|
| 25 |
# Geometric
|
| 26 |
random_flip: bool = True
|
| 27 |
random_rotation_deg: float = 5.0
|
|
@@ -92,9 +91,8 @@ def augment_training_sample(
|
|
| 92 |
out_input = np.ascontiguousarray(out_input[:, ::-1])
|
| 93 |
out_target = np.ascontiguousarray(out_target[:, ::-1])
|
| 94 |
out_cond = np.ascontiguousarray(out_cond[:, ::-1])
|
| 95 |
-
out_mask = np.ascontiguousarray(
|
| 96 |
-
|
| 97 |
-
)
|
| 98 |
if out_lm_src is not None:
|
| 99 |
out_lm_src[:, 0] = 1.0 - out_lm_src[:, 0]
|
| 100 |
if out_lm_dst is not None:
|
|
@@ -112,13 +110,15 @@ def augment_training_sample(
|
|
| 112 |
M[0, 2] += tx
|
| 113 |
M[1, 2] += ty
|
| 114 |
|
| 115 |
-
out_input = cv2.warpAffine(out_input, M, (w, h),
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
)
|
|
|
|
| 120 |
mask_2d = out_mask if out_mask.ndim == 2 else out_mask[:, :, 0]
|
| 121 |
-
mask_2d = cv2.warpAffine(mask_2d, M, (w, h),
|
|
|
|
| 122 |
out_mask = mask_2d if out_mask.ndim == 2 else mask_2d[:, :, np.newaxis]
|
| 123 |
|
| 124 |
# Transform landmarks
|
|
@@ -183,7 +183,9 @@ def augment_training_sample(
|
|
| 183 |
return result
|
| 184 |
|
| 185 |
|
| 186 |
-
def _transform_landmarks(
|
|
|
|
|
|
|
| 187 |
"""Transform normalized landmarks with an affine matrix."""
|
| 188 |
# Convert to pixel coords
|
| 189 |
px = landmarks.copy()
|
|
@@ -260,12 +262,8 @@ class FitzpatrickBalancer:
|
|
| 260 |
target_distribution: Target fraction per type. Defaults to uniform.
|
| 261 |
"""
|
| 262 |
self.target = target_distribution or {
|
| 263 |
-
"I": 1 / 6,
|
| 264 |
-
"
|
| 265 |
-
"III": 1 / 6,
|
| 266 |
-
"IV": 1 / 6,
|
| 267 |
-
"V": 1 / 6,
|
| 268 |
-
"VI": 1 / 6,
|
| 269 |
}
|
| 270 |
self._counts: dict[str, int] = {}
|
| 271 |
|
|
@@ -284,7 +282,7 @@ class FitzpatrickBalancer:
|
|
| 284 |
for ft in fitz_types:
|
| 285 |
count = self._counts.get(ft, 1)
|
| 286 |
freq = count / total
|
| 287 |
-
target_freq = self.target.get(ft, 1
|
| 288 |
# Weight = target / actual (capped for stability)
|
| 289 |
w = min(target_freq / max(freq, 1e-6), 5.0)
|
| 290 |
weights.append(w)
|
|
|
|
| 21 |
@dataclass
|
| 22 |
class AugmentationConfig:
|
| 23 |
"""Augmentation parameters."""
|
|
|
|
| 24 |
# Geometric
|
| 25 |
random_flip: bool = True
|
| 26 |
random_rotation_deg: float = 5.0
|
|
|
|
| 91 |
out_input = np.ascontiguousarray(out_input[:, ::-1])
|
| 92 |
out_target = np.ascontiguousarray(out_target[:, ::-1])
|
| 93 |
out_cond = np.ascontiguousarray(out_cond[:, ::-1])
|
| 94 |
+
out_mask = np.ascontiguousarray(out_mask[:, ::-1] if out_mask.ndim == 2
|
| 95 |
+
else out_mask[:, ::-1, :])
|
|
|
|
| 96 |
if out_lm_src is not None:
|
| 97 |
out_lm_src[:, 0] = 1.0 - out_lm_src[:, 0]
|
| 98 |
if out_lm_dst is not None:
|
|
|
|
| 110 |
M[0, 2] += tx
|
| 111 |
M[1, 2] += ty
|
| 112 |
|
| 113 |
+
out_input = cv2.warpAffine(out_input, M, (w, h),
|
| 114 |
+
borderMode=cv2.BORDER_REFLECT_101)
|
| 115 |
+
out_target = cv2.warpAffine(out_target, M, (w, h),
|
| 116 |
+
borderMode=cv2.BORDER_REFLECT_101)
|
| 117 |
+
out_cond = cv2.warpAffine(out_cond, M, (w, h),
|
| 118 |
+
borderMode=cv2.BORDER_CONSTANT, borderValue=0)
|
| 119 |
mask_2d = out_mask if out_mask.ndim == 2 else out_mask[:, :, 0]
|
| 120 |
+
mask_2d = cv2.warpAffine(mask_2d, M, (w, h),
|
| 121 |
+
borderMode=cv2.BORDER_CONSTANT, borderValue=0)
|
| 122 |
out_mask = mask_2d if out_mask.ndim == 2 else mask_2d[:, :, np.newaxis]
|
| 123 |
|
| 124 |
# Transform landmarks
|
|
|
|
| 183 |
return result
|
| 184 |
|
| 185 |
|
| 186 |
+
def _transform_landmarks(
|
| 187 |
+
landmarks: np.ndarray, M: np.ndarray, w: int, h: int
|
| 188 |
+
) -> np.ndarray:
|
| 189 |
"""Transform normalized landmarks with an affine matrix."""
|
| 190 |
# Convert to pixel coords
|
| 191 |
px = landmarks.copy()
|
|
|
|
| 262 |
target_distribution: Target fraction per type. Defaults to uniform.
|
| 263 |
"""
|
| 264 |
self.target = target_distribution or {
|
| 265 |
+
"I": 1/6, "II": 1/6, "III": 1/6,
|
| 266 |
+
"IV": 1/6, "V": 1/6, "VI": 1/6,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
}
|
| 268 |
self._counts: dict[str, int] = {}
|
| 269 |
|
|
|
|
| 282 |
for ft in fitz_types:
|
| 283 |
count = self._counts.get(ft, 1)
|
| 284 |
freq = count / total
|
| 285 |
+
target_freq = self.target.get(ft, 1/6)
|
| 286 |
# Weight = target / actual (capped for stability)
|
| 287 |
w = min(target_freq / max(freq, 1e-6), 5.0)
|
| 288 |
weights.append(w)
|