-
Notifications
You must be signed in to change notification settings - Fork 465
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
44
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 all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
2b20b66
Adding Upper Face Dynamics Deviation implementation
VijayVignesh1 a1a36b2
Making changes to adhere to pre-commit check
VijayVignesh1 6873021
Fixing docs
VijayVignesh1 5c4f879
Fixing unittest errors
VijayVignesh1 581fdb3
Correcting class name typo
VijayVignesh1 38f947a
Debugging the pipeline failed cases
VijayVignesh1 0e564e0
Docstring fixes
VijayVignesh1 bc1681f
Debugging docstring error
VijayVignesh1 010ab21
Debugging unittest failure
VijayVignesh1 99acd77
Removing artefacts from links.rst
VijayVignesh1 82c5fac
Changing displacement calculation
VijayVignesh1 d0dfa0b
Modifying test plot file
VijayVignesh1 5b6b25c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 190f743
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 57381c1
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 23668d2
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 2a928b8
Update src/torchmetrics/functional/multimodal/fdd.py
rittik9 e703ba1
Update src/torchmetrics/multimodal/fdd.py
rittik9 7dcd6b7
Update src/torchmetrics/multimodal/fdd.py
rittik9 19cda67
Update src/torchmetrics/multimodal/fdd.py
rittik9 1d0df90
Update src/torchmetrics/multimodal/fdd.py
rittik9 679e677
Removing same length check testcase
VijayVignesh1 494cbfb
Appling suggestions from code review
VijayVignesh1 7e4bba8
Adding upper_face_maps index check, removing plot lower bound, making…
VijayVignesh1 b3496d6
Debugging readthedocs failure
VijayVignesh1 bbaed68
Update fdd.py
rittik9 ca87ed5
Update fdd.py
rittik9 c316b22
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6338c15
Update fdd.py
rittik9 e2028e2
Update fdd.py
rittik9 c10084a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5832844
Update test_fdd.py
rittik9 c71d651
Update fdd.py
rittik9 14bc039
Update fdd.py
rittik9 a7c88bd
Update fdd.py
rittik9 198b75d
Update fdd.py
rittik9 86e282d
Update fdd.py
rittik9 a6454d7
Debugging failed testcases
VijayVignesh1 da141d4
Update fdd.py
rittik9 bef5ea0
Update fdd.py
rittik9 1e81659
Update test_fdd.py
rittik9 72d355b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 20c8702
Merge branch 'master' into feature/face_dynamic_deviation
justusschock a38f640
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so T is basically the batch-size here? can we name it that way?
also dies this work in 2d as well? for distance calculation it should not matter if it's R^2 or R^3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T is the time dimension here. But actually, we need to add the batch dimension to it. To answer the second question, yes this should work with 2d as well. We might need to change the corresponding checks here.
@rittik9 I think we might need to pivot. Your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some thoughts on this. I can add the time dimension, so that the input to the class and function is (B, T, V, 3) or (B, T, V, 2). However, the min_frames logic needs to be removed in that case and it's upto the user to align the T dimension of pred and gt. @justusschock @rittik9