| """Joint augmentation for fingerprint images and minutiae. |
| |
| Applies the same geometric transform to both the image and minutiae |
| coordinates simultaneously, ensuring spatial consistency. |
| |
| Augmentations: |
| - Random rotation: rotates image + rotation matrix on (x,y) + adds angle to θ |
| - Random translation: shifts image + adds offset to (x,y) |
| - Minutia dropout: randomly drops points (keeps ≥ min_keep) |
| - Coordinate jitter: Gaussian noise on (x,y) — minutiae only |
| """ |
|
|
| import math |
| import random |
|
|
| import torch |
| import torch.nn.functional as F |
|
|
| from ..configs.default import AugmentConfig |
|
|
|
|
| class JointAugmentor: |
| """Applies the same geometric transform to image and minutiae simultaneously. |
| |
| Parameters |
| ---------- |
| cfg : AugmentConfig |
| Augmentation configuration. |
| """ |
|
|
| def __init__(self, cfg: AugmentConfig): |
| self.cfg = cfg |
|
|
| def __call__( |
| self, |
| image: torch.Tensor, |
| minutiae: torch.Tensor, |
| ) -> tuple[torch.Tensor, torch.Tensor]: |
| """ |
| Args: |
| image: ``(1, H, W)`` normalized [0, 1] fingerprint image. |
| minutiae: ``(N, 3)`` raw ``[x, y, θ]`` in pixel coordinates. |
| |
| Returns: |
| image: ``(1, H, W)`` augmented image. |
| minutiae: ``(N', 3)`` augmented minutiae (N' ≤ N after dropout). |
| """ |
| _, H, W = image.shape |
| m = minutiae.clone() |
|
|
| |
| cx, cy = (W - 1) / 2.0, (H - 1) / 2.0 |
|
|
| |
| if self.cfg.rotate: |
| angle_deg = random.uniform(-self.cfg.rotate_range, self.cfg.rotate_range) |
| angle_rad = math.radians(angle_deg) |
| cos_a, sin_a = math.cos(angle_rad), math.sin(angle_rad) |
|
|
| |
| |
| theta = torch.tensor( |
| [ |
| [cos_a, -sin_a, 0.0], |
| [sin_a, cos_a, 0.0], |
| ], |
| dtype=image.dtype, |
| ).unsqueeze(0) |
|
|
| grid = F.affine_grid(theta, [1, 1, H, W], align_corners=True) |
| image = F.grid_sample( |
| image.unsqueeze(0), |
| grid, |
| mode="bilinear", |
| padding_mode="zeros", |
| align_corners=True, |
| ).squeeze(0) |
|
|
| |
| x_centered = m[:, 0] - cx |
| y_centered = m[:, 1] - cy |
| m[:, 0] = cos_a * x_centered - sin_a * y_centered + cx |
| m[:, 1] = sin_a * x_centered + cos_a * y_centered + cy |
|
|
| |
| m[:, 2] = m[:, 2] + angle_rad |
| m[:, 2] = torch.atan2(torch.sin(m[:, 2]), torch.cos(m[:, 2])) |
|
|
| |
| if self.cfg.translate and self.cfg.translate_range > 0: |
| tx = random.uniform(-self.cfg.translate_range, self.cfg.translate_range) |
| ty = random.uniform(-self.cfg.translate_range, self.cfg.translate_range) |
|
|
| |
| theta = torch.tensor( |
| [ |
| [1.0, 0.0, -2.0 * tx / (W - 1)], |
| [0.0, 1.0, -2.0 * ty / (H - 1)], |
| ], |
| dtype=image.dtype, |
| ).unsqueeze(0) |
|
|
| grid = F.affine_grid(theta, [1, 1, H, W], align_corners=True) |
| image = F.grid_sample( |
| image.unsqueeze(0), |
| grid, |
| mode="bilinear", |
| padding_mode="zeros", |
| align_corners=True, |
| ).squeeze(0) |
|
|
| |
| m[:, 0] += tx |
| m[:, 1] += ty |
|
|
| |
| if self.cfg.minutia_dropout > 0 and m.shape[0] > self.cfg.min_keep: |
| keep = torch.rand(m.shape[0]) > self.cfg.minutia_dropout |
| if keep.sum() < self.cfg.min_keep: |
| keep[: self.cfg.min_keep] = True |
| m = m[keep] |
|
|
| |
| if self.cfg.jitter_std > 0: |
| noise = torch.randn(m.shape[0], 2) * self.cfg.jitter_std |
| m[:, :2] += noise |
|
|
| |
| spurious_rate = getattr(self.cfg, "spurious_rate", 0.0) |
| if spurious_rate > 0 and m.shape[0] > 0: |
| n_spurious = max(1, int(m.shape[0] * spurious_rate)) |
| xy_min = m[:, :2].min(dim=0).values |
| xy_max = m[:, :2].max(dim=0).values |
| xy_range = (xy_max - xy_min).clamp(min=1.0) |
| fake_xy = xy_min + torch.rand(n_spurious, 2) * xy_range |
| fake_theta = torch.rand(n_spurious, 1) * 2 * math.pi - math.pi |
| fake = torch.cat([fake_xy, fake_theta], dim=-1) |
| m = torch.cat([m, fake], dim=0) |
|
|
| return image, m |
|
|