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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
"""Landmark input features, dropout and perturbations used by TopoRig."""
from __future__ import annotations
import math
from typing import Any, Mapping, Optional
import torch
from utils.eyelid_landmarks import BLINK_LEFT_EYELIDS, BLINK_RIGHT_EYELIDS
from utils.eye_gaze_uv_warp import LEFT_EYE_GAZE_LANDMARKS, RIGHT_EYE_GAZE_LANDMARKS
from utils.mouth_landmark_validation import MOUTH_LANDMARK_IDS

LANDMARK_FEATURE_REGION_IDS = {
    "left_eye": frozenset(
        (
            *BLINK_LEFT_EYELIDS.all_ids,
            *LEFT_EYE_GAZE_LANDMARKS.contour_ids,
            *LEFT_EYE_GAZE_LANDMARKS.iris_ids,
        )
    ),
    "right_eye": frozenset(
        (
            *BLINK_RIGHT_EYELIDS.all_ids,
            *RIGHT_EYE_GAZE_LANDMARKS.contour_ids,
            *RIGHT_EYE_GAZE_LANDMARKS.iris_ids,
        )
    ),
    "mouth": frozenset(MOUTH_LANDMARK_IDS),
}


def landmark_feature_config(config: Mapping[str, Any]) -> Mapping[str, Any]:
    model_config = config.get("model", {})
    if not isinstance(model_config, Mapping):
        return {}
    feature_config = model_config.get("landmark_features", {})
    return feature_config if isinstance(feature_config, Mapping) else {}


def landmark_features_enabled(config: Mapping[str, Any]) -> bool:
    return bool(landmark_feature_config(config).get("enabled", False))


def landmark_feature_dim(config: Mapping[str, Any]) -> int:
    feature_config = landmark_feature_config(config)
    if not bool(feature_config.get("enabled", False)):
        return 0
    k_nearest = int(feature_config.get("k_nearest", 4))
    if k_nearest < 1:
        raise ValueError("model.landmark_features.k_nearest must be at least 1.")
    channels_per_landmark = 0
    if bool(feature_config.get("include_vectors", True)):
        channels_per_landmark += 3
    if bool(feature_config.get("include_distances", True)):
        channels_per_landmark += 1
    if bool(feature_config.get("include_ids", True)):
        channels_per_landmark += 1
    if channels_per_landmark == 0:
        raise ValueError(
            "model.landmark_features must include at least one of vectors, "
            "distances, or ids."
        )
    return k_nearest * channels_per_landmark


def build_model_landmark_features(
    record: Mapping[str, Any],
    vertices: torch.Tensor,
    config: Mapping[str, Any],
    device: torch.device,
    *,
    training: bool = False,
    perturbation: Optional[Mapping[str, Any]] = None,
    generator: Optional[torch.Generator] = None,
) -> Optional[torch.Tensor]:
    if not landmark_features_enabled(config):
        return None
    feature_dim = landmark_feature_dim(config)
    if vertices.dim() != 3 or vertices.shape[-1] != 3:
        raise ValueError("vertices must have shape [B, V, 3].")

    landmarks_3d = record.get("landmarks_3d")
    if not isinstance(landmarks_3d, Mapping):
        return vertices.new_zeros((*vertices.shape[:2], feature_dim))

    positions = landmarks_3d.get("neutral_positions")
    if not isinstance(positions, torch.Tensor) or positions.numel() == 0:
        return vertices.new_zeros((*vertices.shape[:2], feature_dim))

    mediapipe_ids = landmarks_3d.get("mediapipe_ids")
    if not isinstance(mediapipe_ids, torch.Tensor) or mediapipe_ids.numel() == 0:
        mediapipe_ids = torch.arange(
            positions.shape[-2],
            dtype=torch.long,
            device=positions.device,
        )

    positions = positions.to(device=device, dtype=vertices.dtype)
    mediapipe_ids = mediapipe_ids.to(device=device, dtype=torch.long).flatten()
    feature_config = landmark_feature_config(config)
    if training:
        training_augmentation = feature_config.get("training_augmentation", {})
        if isinstance(training_augmentation, Mapping) and bool(
            training_augmentation.get("enabled", False)
        ):
            if landmark_random_event(
                training_augmentation.get("modality_dropout_probability", 0.0),
                device=device,
                generator=generator,
                label="modality_dropout_probability",
            ):
                return vertices.new_zeros((*vertices.shape[:2], feature_dim))
            regions = normalize_landmark_feature_regions(
                training_augmentation.get(
                    "regions", ("left_eye", "right_eye", "mouth")
                )
            )
            if regions and landmark_random_event(
                training_augmentation.get("region_dropout_probability", 0.0),
                device=device,
                generator=generator,
                label="region_dropout_probability",
            ):
                region_index = int(
                    torch.randint(
                        len(regions),
                        (),
                        device=device,
                        generator=generator,
                    ).item()
                )
                positions, mediapipe_ids = drop_model_landmark_regions(
                    positions,
                    mediapipe_ids,
                    (regions[region_index],),
                )
            if landmark_random_event(
                training_augmentation.get("position_jitter_probability", 0.0),
                device=device,
                generator=generator,
                label="position_jitter_probability",
            ):
                positions = jitter_model_landmark_positions(
                    positions,
                    mediapipe_ids,
                    vertices,
                    std_head_fraction=float(
                        training_augmentation.get(
                            "position_jitter_std_head_fraction", 0.0
                        )
                    ),
                    regions=training_augmentation.get("position_jitter_regions"),
                    up_axis=int(training_augmentation.get("head_up_axis", 2)),
                    generator=generator,
                )

    if perturbation:
        if bool(perturbation.get("drop_all", False)):
            return vertices.new_zeros((*vertices.shape[:2], feature_dim))
        positions, mediapipe_ids = drop_model_landmark_regions(
            positions,
            mediapipe_ids,
            normalize_landmark_feature_regions(
                perturbation.get("drop_regions", ()),
            ),
        )
        positions = permute_model_landmark_positions(
            positions,
            mediapipe_ids,
            regions=perturbation.get("permute_regions"),
            generator=generator,
        )
        positions = jitter_model_landmark_positions(
            positions,
            mediapipe_ids,
            vertices,
            std_head_fraction=float(
                perturbation.get("position_jitter_std_head_fraction", 0.0)
            ),
            regions=perturbation.get("position_jitter_regions"),
            up_axis=int(perturbation.get("head_up_axis", 2)),
            generator=generator,
        )

    if positions.shape[-2] == 0 or mediapipe_ids.numel() == 0:
        return vertices.new_zeros((*vertices.shape[:2], feature_dim))

    return landmark_relative_vertex_features(
        vertices=vertices,
        landmark_positions=positions,
        mediapipe_ids=mediapipe_ids,
        config=feature_config,
        device=device,
    )


def normalize_landmark_feature_regions(value: Any) -> tuple[str, ...]:
    if value in (None, ""):
        return ()
    raw_values = (value,) if isinstance(value, str) else tuple(value)
    regions = tuple(dict.fromkeys(str(item).strip().lower() for item in raw_values))
    unknown = sorted(set(regions) - set(LANDMARK_FEATURE_REGION_IDS))
    if unknown:
        raise ValueError(
            "Unknown landmark feature region(s): "
            f"{', '.join(unknown)}. Expected one of: "
            f"{', '.join(sorted(LANDMARK_FEATURE_REGION_IDS))}."
        )
    return regions


def landmark_random_event(
    probability: Any,
    *,
    device: torch.device,
    generator: Optional[torch.Generator],
    label: str,
) -> bool:
    probability = float(probability)
    if not 0.0 <= probability <= 1.0:
        raise ValueError(f"model.landmark_features.{label} must be in [0, 1].")
    return probability > 0.0 and bool(
        torch.rand((), device=device, generator=generator).item() < probability
    )


def drop_model_landmark_regions(
    positions: torch.Tensor,
    mediapipe_ids: torch.Tensor,
    regions: Sequence[str],
) -> tuple[torch.Tensor, torch.Tensor]:
    regions = normalize_landmark_feature_regions(regions)
    if not regions:
        return positions, mediapipe_ids
    dropped_ids = set().union(
        *(LANDMARK_FEATURE_REGION_IDS[region] for region in regions)
    )
    keep = torch.tensor(
        [int(value) not in dropped_ids for value in mediapipe_ids.detach().cpu()],
        device=mediapipe_ids.device,
        dtype=torch.bool,
    )
    return positions.index_select(-2, keep.nonzero().flatten()), mediapipe_ids[keep]


def jitter_model_landmark_positions(
    positions: torch.Tensor,
    mediapipe_ids: torch.Tensor,
    vertices: torch.Tensor,
    *,
    std_head_fraction: float,
    regions: Any = None,
    up_axis: int = 2,
    generator: Optional[torch.Generator] = None,
) -> torch.Tensor:
    std_head_fraction = float(std_head_fraction)
    if std_head_fraction < 0.0 or not math.isfinite(std_head_fraction):
        raise ValueError("Landmark position jitter must be finite and non-negative.")
    if std_head_fraction == 0.0 or positions.shape[-2] == 0:
        return positions
    if not 0 <= up_axis < 3:
        raise ValueError("Landmark position jitter head_up_axis must be 0, 1, or 2.")
    if positions.dim() == 2:
        batched_positions = positions.unsqueeze(0)
        squeeze_batch = True
    elif positions.dim() == 3:
        batched_positions = positions
        squeeze_batch = False
    else:
        raise ValueError("Landmark positions must have shape [L, 3] or [B, L, 3].")
    if batched_positions.shape[0] not in (1, vertices.shape[0]):
        raise ValueError("Landmark position batch size must match vertices.")

    selected_regions = normalize_landmark_feature_regions(regions)
    if selected_regions:
        selected_ids = set().union(
            *(LANDMARK_FEATURE_REGION_IDS[region] for region in selected_regions)
        )
        selected = torch.tensor(
            [int(value) in selected_ids for value in mediapipe_ids.detach().cpu()],
            device=positions.device,
            dtype=torch.bool,
        )
    else:
        selected = torch.ones(
            mediapipe_ids.shape,
            device=positions.device,
            dtype=torch.bool,
        )
    if not bool(selected.any()):
        return positions

    head_height = (
        vertices[..., up_axis].amax(dim=1)
        - vertices[..., up_axis].amin(dim=1)
    ).clamp_min(torch.finfo(vertices.dtype).eps)
    if batched_positions.shape[0] == 1 and vertices.shape[0] > 1:
        head_height = head_height[:1]
    noise = torch.randn(
        batched_positions.shape,
        device=positions.device,
        dtype=positions.dtype,
        generator=generator,
    )
    noise = noise * head_height.reshape(-1, 1, 1) * std_head_fraction
    noise = noise * selected.reshape(1, -1, 1)
    jittered = batched_positions + noise
    return jittered.squeeze(0) if squeeze_batch else jittered


def permute_model_landmark_positions(
    positions: torch.Tensor,
    mediapipe_ids: torch.Tensor,
    *,
    regions: Any,
    generator: Optional[torch.Generator] = None,
) -> torch.Tensor:
    selected_regions = normalize_landmark_feature_regions(regions)
    if not selected_regions or positions.shape[-2] < 2:
        return positions
    selected_ids = set().union(
        *(LANDMARK_FEATURE_REGION_IDS[region] for region in selected_regions)
    )
    selected = torch.tensor(
        [int(value) in selected_ids for value in mediapipe_ids.detach().cpu()],
        device=positions.device,
        dtype=torch.bool,
    ).nonzero().flatten()
    if selected.numel() < 2:
        return positions
    permutation = torch.randperm(
        selected.numel(),
        device=positions.device,
        generator=generator,
    )
    identity = torch.arange(selected.numel(), device=positions.device)
    if bool(torch.equal(permutation, identity)):
        permutation = permutation.roll(1)
    source = selected.index_select(0, permutation)
    output = positions.clone()
    output[..., selected, :] = positions.index_select(-2, source)
    return output


def landmark_relative_vertex_features(
    vertices: torch.Tensor,
    landmark_positions: torch.Tensor,
    mediapipe_ids: torch.Tensor,
    config: Mapping[str, Any],
    device: torch.device,
) -> torch.Tensor:
    dtype = vertices.dtype
    vertices = vertices.to(device=device)
    landmark_positions = landmark_positions.to(device=device, dtype=dtype)
    mediapipe_ids = mediapipe_ids.to(device=device, dtype=torch.long).flatten()
    if landmark_positions.dim() == 2:
        landmark_positions = landmark_positions.unsqueeze(0).expand(
            vertices.shape[0],
            -1,
            -1,
        )
    if landmark_positions.dim() != 3 or landmark_positions.shape[-1] != 3:
        raise ValueError("landmark_positions must have shape [L, 3] or [B, L, 3].")
    if landmark_positions.shape[0] != vertices.shape[0]:
        raise ValueError("landmark_positions batch size must match vertices.")
    if mediapipe_ids.numel() != landmark_positions.shape[1]:
        raise ValueError("mediapipe_ids must match the landmark count.")

    k_nearest = int(config.get("k_nearest", 4))
    include_vectors = bool(config.get("include_vectors", True))
    include_distances = bool(config.get("include_distances", True))
    include_ids = bool(config.get("include_ids", True))
    id_normalizer = float(config.get("id_normalizer", 467.0))
    chunk_size = int(config.get("chunk_size", 50000))
    feature_dim = k_nearest * (
        (3 if include_vectors else 0)
        + (1 if include_distances else 0)
        + (1 if include_ids else 0)
    )

    if k_nearest < 1:
        raise ValueError("model.landmark_features.k_nearest must be at least 1.")
    if feature_dim <= 0:
        raise ValueError(
            "model.landmark_features must include at least one of vectors, "
            "distances, or ids."
        )
    if chunk_size < 1:
        raise ValueError("model.landmark_features.chunk_size must be at least 1.")

    output_batches = []
    for batch_index in range(vertices.shape[0]):
        batch_vertices = vertices[batch_index]
        batch_landmarks = landmark_positions[batch_index]
        if batch_landmarks.numel() == 0:
            output_batches.append(
                vertices.new_zeros((batch_vertices.shape[0], feature_dim))
            )
            continue
        actual_k = min(k_nearest, batch_landmarks.shape[0])
        chunks = []
        for start in range(0, batch_vertices.shape[0], chunk_size):
            chunk = batch_vertices[start : start + chunk_size]
            distances = torch.cdist(
                chunk.unsqueeze(0),
                batch_landmarks.unsqueeze(0),
            ).squeeze(0)
            nearest_distances, nearest_indices = distances.topk(
                actual_k,
                largest=False,
                dim=-1,
            )
            nearest_positions = batch_landmarks.index_select(
                0,
                nearest_indices.reshape(-1),
            ).reshape(chunk.shape[0], actual_k, 3)
            nearest_ids = mediapipe_ids.index_select(
                0,
                nearest_indices.reshape(-1),
            ).reshape(chunk.shape[0], actual_k)

            components = []
            if include_vectors:
                components.append(
                    (nearest_positions - chunk.unsqueeze(1)).reshape(
                        chunk.shape[0],
                        actual_k * 3,
                    )
                )
            if include_distances:
                components.append(nearest_distances)
            if include_ids:
                components.append(
                    nearest_ids.to(dtype=dtype)
                    .div(max(id_normalizer, 1.0))
                    .clamp(0.0, 1.0)
                )
            chunk_features = torch.cat(components, dim=-1)
            if actual_k < k_nearest:
                padding = vertices.new_zeros(
                    (chunk.shape[0], feature_dim - chunk_features.shape[-1])
                )
                chunk_features = torch.cat((chunk_features, padding), dim=-1)
            chunks.append(chunk_features)
        output_batches.append(torch.cat(chunks, dim=0))
    return torch.stack(output_batches, dim=0)