-
Notifications
You must be signed in to change notification settings - Fork 486
New Metric: Upper Face Dynamics Deviation(FDD) #3238
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
Open
VijayVignesh1
wants to merge
46
commits into
Lightning-AI:master
Choose a base branch
from
VijayVignesh1:feature/face_dynamic_deviation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 43 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
551f13b
Adding Upper Face Dynamics Deviation implementation
VijayVignesh1 72febe4
Making changes to adhere to pre-commit check
VijayVignesh1 4da667c
Fixing docs
VijayVignesh1 bd879fc
Fixing unittest errors
VijayVignesh1 79806cc
Correcting class name typo
VijayVignesh1 e7c0efa
Debugging the pipeline failed cases
VijayVignesh1 b926a58
Docstring fixes
VijayVignesh1 b1f7412
Debugging docstring error
VijayVignesh1 1380672
Debugging unittest failure
VijayVignesh1 4386ea0
Removing artefacts from links.rst
VijayVignesh1 0a2cf87
Changing displacement calculation
VijayVignesh1 a73adba
Modifying test plot file
VijayVignesh1 8963cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 10774fd
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 33abbce
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 9b9ed25
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 5d786bb
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 c2b04e7
Update src/torchmetrics/multimodal/fdd.py
rittik9 af10569
Update src/torchmetrics/multimodal/fdd.py
rittik9 71e215b
Update src/torchmetrics/multimodal/fdd.py
rittik9 c1fc190
Update src/torchmetrics/multimodal/fdd.py
rittik9 22b006b
Removing same length check testcase
VijayVignesh1 3500b3f
Appling suggestions from code review
VijayVignesh1 692bb22
Adding upper_face_maps index check, removing plot lower bound, making…
VijayVignesh1 df4e9bd
Debugging readthedocs failure
VijayVignesh1 4168625
Update fdd.py
rittik9 c2a3ed3
Update fdd.py
rittik9 4cdeaac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3cfe0fb
Update fdd.py
rittik9 7f072e2
Update fdd.py
rittik9 2a48a57
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aed3c9d
Update test_fdd.py
rittik9 9a24333
Update fdd.py
rittik9 0f7a82f
Update fdd.py
rittik9 4370f53
Update fdd.py
rittik9 3213118
Update fdd.py
rittik9 75d67f4
Debugging failed testcases
VijayVignesh1 63a4e51
Update fdd.py
rittik9 efa69f6
Update fdd.py
rittik9 e1f5ce8
Update test_fdd.py
rittik9 44a4530
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 389680b
Merge branch 'master' into feature/face_dynamic_deviation
justusschock 1b8936c
Merge branch 'master' into feature/face_dynamic_deviation
VijayVignesh1 79c6018
Adding the batch size dimensions to the inputs
VijayVignesh1 9aa9ed4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6ef4bfd
fixing lint errors
VijayVignesh1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| .. customcarditem:: | ||
| :header: Upper Face Dynamics Deviation (FDD) | ||
| :image: https://pl-flash-data.s3.amazonaws.com/assets/thumbnails/image_classification.svg | ||
| :tags: Multimodal | ||
|
|
||
| .. include:: ../links.rst | ||
|
|
||
| ################################### | ||
| Upper Face Dynamics Deviation (FDD) | ||
| ################################### | ||
|
|
||
| Module Interface | ||
| ________________ | ||
|
|
||
| .. autoclass:: torchmetrics.multimodal.fdd.UpperFaceDynamicsDeviation | ||
|
|
||
| Functional Interface | ||
| ____________________ | ||
|
|
||
| .. autofunction:: torchmetrics.functional.multimodal.fdd.upper_face_dynamics_deviation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Copyright The Lightning team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from typing import List | ||
|
|
||
| import torch | ||
| from torch import Tensor | ||
|
|
||
|
|
||
| def upper_face_dynamics_deviation( | ||
| vertices_pred: Tensor, | ||
| vertices_gt: Tensor, | ||
| template: Tensor, | ||
| upper_face_map: List[int], | ||
| ) -> Tensor: | ||
| r"""Compute Upper Face Dynamics Deviation (FDD) for 3D talking head evaluation. | ||
|
|
||
| The Upper Face Dynamics Deviation (FDD) metric evaluates the quality of facial expressions in the upper | ||
| face region for 3D talking head models. It quantifies the deviation in vertex motion dynamics between the | ||
| predicted and ground truth sequences by comparing the temporal variation (standard deviation) of per-vertex | ||
| squared displacements relative to a neutral template. Lower values of FDD indicate closer alignment of the | ||
| predicted upper-face motion dynamics with the ground truth. | ||
|
|
||
| The metric is defined as: | ||
|
|
||
| .. math:: | ||
| \text{FDD} = \frac{1}{|S_U|} \sum_{v \in S_U} \Big( \text{std}(\| x_{1:T,v} - | ||
| \text{template}_v \|_2^2) - \text{std}(\| \hat{x}_{1:T,v} - \text{template}_v \|_2^2) \Big) | ||
|
|
||
| where :math:`T` is the number of frames, :math:`S_U` is the set of upper-face vertices with :math:`M = |S_U|`, | ||
| :math:`x_{t,v}` are the 3D coordinates of vertex :math:`v` at frame :math:`t` in the ground truth sequence, | ||
| and :math:`\hat{x}_{t,v} \in \mathbb{R}^3` are the corresponding predicted vertices. The neutral template coordinate | ||
| of vertex :math:`v` is denoted as :math:`\text{template}_v \in \mathbb{R}^3`. The operator :math:`\text{std}(\cdot)` | ||
| computes the standard deviation of the temporal sequence. | ||
|
|
||
| Args: | ||
| vertices_pred: Predicted vertices tensor of shape (T, V, 3) where T is number of frames, | ||
| V is number of vertices, and 3 represents XYZ coordinates. | ||
| vertices_gt: Ground truth vertices tensor of shape (T, V, 3) where T is number of frames, | ||
| V is number of vertices, and 3 represents XYZ coordinates. | ||
| template: Template mesh tensor of shape (V, 3) representing the neutral face. | ||
| upper_face_map: List of vertex indices corresponding to the upper face region. | ||
|
|
||
| Returns: | ||
| torch.Tensor: Scalar tensor containing the mean FDD value across upper-face vertices. | ||
|
|
||
| Raises: | ||
| ValueError: | ||
| If the number of dimensions of `vertices_pred` or `vertices_gt` is not 3. | ||
| If `template` does not have shape (No_of_vertices, 3). | ||
| If `vertices_pred` and `vertices_gt` do not have the same vertex and coordinate dimensions. | ||
| If `template` shape does not match the vertex-coordinate dimensions of `vertices_pred` (and `vertices_gt`). | ||
| If ``upper_face_map`` is empty or contains invalid vertex indices. | ||
|
|
||
| Example: | ||
| >>> import torch | ||
| >>> from torchmetrics.functional.multimodal import upper_face_dynamics_deviation | ||
| >>> vertices_pred = torch.randn(10, 100, 3, generator=torch.manual_seed(41)) | ||
| >>> vertices_gt = torch.randn(10, 100, 3, generator=torch.manual_seed(42)) | ||
| >>> upper_face_map = [10, 11, 12, 13, 14] | ||
| >>> template = torch.randn(100, 3, generator=torch.manual_seed(43)) | ||
| >>> upper_face_dynamics_deviation(vertices_pred, vertices_gt, template, upper_face_map) | ||
| tensor(1.0385) | ||
|
|
||
| """ | ||
| if vertices_pred.ndim != 3 or vertices_gt.ndim != 3: | ||
| raise ValueError( | ||
| f"Expected both vertices_pred and vertices_gt to have 3 dimensions but got " | ||
| f"{vertices_pred.ndim} and {vertices_gt.ndim} dimensions respectively." | ||
| ) | ||
| if template.ndim != 2 or template.shape[1] != 3: | ||
| raise ValueError(f"Expected template to have shape (V, 3) but got {template.shape}.") | ||
| if vertices_pred.shape[1:] != vertices_gt.shape[1:]: | ||
| raise ValueError( | ||
| f"Expected vertices_pred and vertices_gt to have same vertex and coordinate dimensions but got " | ||
| f"shapes {vertices_pred.shape} and {vertices_gt.shape}." | ||
| ) | ||
| if vertices_pred.shape[1:] != template.shape: | ||
| raise ValueError( | ||
| f"Shape mismatch: expected template shape {template.shape} to match " | ||
| f"vertex-coordinate dimensions of predictions {vertices_pred.shape[1:]}, " | ||
| f"but got template shape {template.shape} instead." | ||
| ) | ||
| if not upper_face_map: | ||
| raise ValueError("upper_face_map cannot be empty.") | ||
| if min(upper_face_map) < 0 or max(upper_face_map) >= template.shape[0]: | ||
| raise ValueError( | ||
| f"upper_face_map contains out-of-range vertex indices. " | ||
| f"Valid index range is [0, {template.shape[0] - 1}], " | ||
| f"but received indices in range [{min(upper_face_map)}, {max(upper_face_map)}]." | ||
| ) | ||
| min_frames = min(vertices_pred.shape[0], vertices_gt.shape[0]) | ||
| pred = vertices_pred[:min_frames, upper_face_map, :] # (T, M, 3) | ||
| gt = vertices_gt[:min_frames, upper_face_map, :] | ||
| template = template.to(pred.device)[upper_face_map, :] # (M, 3) | ||
|
|
||
| pred_disp = pred - template # (T, M, 3) | ||
| gt_disp = gt - template | ||
|
|
||
| pred_norm_sq = torch.sum(pred_disp**2, dim=-1) # (T, M) | ||
| gt_norm_sq = torch.sum(gt_disp**2, dim=-1) # (T, M) | ||
|
|
||
| pred_dyn = torch.std(pred_norm_sq, dim=0, unbiased=False) # (M,) | ||
| gt_dyn = torch.std(gt_norm_sq, dim=0, unbiased=False) | ||
|
|
||
| return torch.mean(gt_dyn - pred_dyn) # scalar | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.