-
Notifications
You must be signed in to change notification settings - Fork 30
/
syncnet.py
304 lines (261 loc) · 10.2 KB
/
syncnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Dict, Optional, Tuple
import numpy as np
from scipy.spatial.transform import Rotation
from torchmetrics.functional import structural_similarity_index_measure as ssim
from torchmetrics.image.fid import FrechetInceptionDistance
from mysixdrepnet import *
'''
Key components:
SyncNet Implementation:
pythonCopy- Video encoder for lip movements
- Audio encoder for speech features
- Synchronization confidence computation
CAPP Score Components:
pythonCopy- Pose sequence extraction
- Audio-pose alignment
- Contrastive learning based evaluation
Motion Analysis:
pythonCopyMetrics:
- Pose variation intensity
- Head movement statistics
- Temporal consistency
Visual Quality:
pythonCopyWhen ground truth available:
- FID Score
- SSIM
- L1/L2 distances
To use in training:
pythonCopy# Initialize evaluator
evaluator = Evaluator(device)
# During training
metrics = evaluator.evaluate_batch(batch, generated_video)
logger.log_metrics(metrics, step)
# During validation
val_metrics = {}
for batch in val_loader:
generated = model(batch)
batch_metrics = evaluator.evaluate_batch(batch, generated)
for k, v in batch_metrics.items():
val_metrics[k] = val_metrics.get(k, 0) + v
'''
class SyncNet(nn.Module):
"""
SyncNet implementation for lip-sync evaluation
As referenced in paper for computing Cl and Dl
"""
def __init__(self):
super().__init__()
# Initialize SyncNet architecture
# This should be pretrained SyncNet as used in paper
self.video_encoder = nn.Sequential(
# Conv3D layers for video processing
nn.Conv3d(3, 96, kernel_size=(5, 7, 7), stride=(1, 2, 2), padding=(2, 3, 3)),
nn.BatchNorm3d(96),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(1, 2, 2)),
nn.Conv3d(96, 256, kernel_size=(5, 5, 5), stride=(1, 2, 2), padding=(2, 2, 2)),
nn.BatchNorm3d(256),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(1, 2, 2)),
nn.Conv3d(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
nn.BatchNorm3d(256),
nn.ReLU(inplace=True),
nn.Conv3d(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
nn.BatchNorm3d(256),
nn.ReLU(inplace=True),
nn.Conv3d(256, 256, kernel_size=(3, 3, 3), padding=(1, 1, 1)),
nn.BatchNorm3d(256),
nn.ReLU(inplace=True)
)
self.audio_encoder = nn.Sequential(
# 1D Conv layers for audio processing
nn.Conv1d(1, 96, kernel_size=7, stride=2, padding=3),
nn.BatchNorm1d(96),
nn.ReLU(inplace=True),
nn.MaxPool1d(kernel_size=3, stride=2),
nn.Conv1d(96, 256, kernel_size=5, stride=2, padding=2),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.MaxPool1d(kernel_size=3, stride=2),
nn.Conv1d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.Conv1d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.Conv1d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True)
)
def forward(
self,
video: torch.Tensor,
audio: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Compute confidence score (Cl) and feature distance (Dl)
"""
# Extract features
video_features = self.video_encoder(video)
audio_features = self.audio_encoder(audio)
# Compute confidence score (cosine similarity)
confidence = F.cosine_similarity(
video_features.view(video_features.size(0), -1),
audio_features.view(audio_features.size(0), -1)
)
# Compute feature distance
distance = F.pairwise_distance(
video_features.view(video_features.size(0), -1),
audio_features.view(audio_features.size(0), -1)
)
return confidence, distance
class PoseExtractor(nn.Module):
"""Extract pose sequences from videos"""
def __init__(self):
super().__init__()
# Initialize pose estimation network (e.g., SixDRepNet)
self.pose_net = SixDRepNet_Detector()
self.eval()
@torch.no_grad()
def forward(self, video: torch.Tensor) -> torch.Tensor:
"""
Extract pose sequences from video frames
Args:
video: Video frames [B, T, C, H, W]
Returns:
poses: Pose parameters [B, T, 6] (3 rotation + 3 translation)
"""
B, T = video.shape[:2]
poses = []
for t in range(T):
frame_poses = self.pose_net(video[:, t])
poses.append(frame_poses)
return torch.stack(poses, dim=1)
def compute_pose_intensity(pose_sequences: torch.Tensor) -> float:
"""
Compute pose variation intensity
Args:
pose_sequences: Pose parameters [B, T, 6]
Returns:
intensity: Average pose variation between consecutive frames
"""
# Convert rotation vectors to matrices
rotations = Rotation.from_rotvec(
pose_sequences[..., :3].cpu().numpy()
).as_matrix()
# Compute angular differences between consecutive frames
angular_diffs = []
for t in range(rotations.shape[1] - 1):
R1 = rotations[:, t]
R2 = rotations[:, t + 1]
R_diff = np.matmul(R1.transpose(0, 2, 1), R2)
angle = np.arccos((np.trace(R_diff, axis1=1, axis2=2) - 1) / 2)
angular_diffs.append(angle)
# Compute translation differences
trans_diffs = torch.norm(
pose_sequences[..., 3:, 1:] - pose_sequences[..., 3:, :-1],
dim=-1
).mean().item()
# Combine rotation and translation variations
intensity = (np.mean(angular_diffs) + trans_diffs) / 2
return intensity
class Evaluator:
"""Comprehensive evaluation metrics for VASA"""
def __init__(self, device: torch.device):
self.device = device
# Initialize evaluation networks
self.syncnet = SyncNet().to(device)
self.capp_scorer = CAPPScore(pose_dim=6, audio_dim=768).to(device)
self.pose_extractor = PoseExtractor().to(device)
self.fid = FrechetInceptionDistance(normalize=True).to(device)
# Put networks in eval mode
for module in [self.syncnet, self.capp_scorer, self.pose_extractor]:
module.eval()
@torch.no_grad()
def compute_metrics(self,
generated_video: torch.Tensor,
audio_features: torch.Tensor,
real_video: Optional[torch.Tensor] = None) -> Dict[str, float]:
"""
Compute comprehensive evaluation metrics
Args:
generated_video: Generated video frames [B, T, C, H, W]
audio_features: Audio features [B, T, C]
real_video: Optional ground truth video for comparison
Returns:
Dictionary of computed metrics
"""
metrics = {}
# 1. Audio-Visual Synchronization
sync_conf, sync_dist = self.syncnet(generated_video, audio_features)
metrics['sync_confidence'] = sync_conf.mean().item()
metrics['sync_distance'] = sync_dist.mean().item()
# 2. Audio-Pose Alignment
pose_sequences = self.pose_extractor(generated_video)
capp_similarity = self.capp_scorer(pose_sequences, audio_features)
metrics['capp_score'] = capp_similarity.diagonal().mean().item()
# 3. Motion Analysis
# Pose variation intensity
metrics['pose_intensity'] = compute_pose_intensity(pose_sequences)
# Head movement statistics
head_motion = pose_sequences[..., :3] # rotation components
metrics.update({
'head_motion_mean': head_motion.abs().mean().item(),
'head_motion_std': head_motion.std().item(),
'head_motion_max': head_motion.abs().max().item()
})
# 4. Visual Quality
if real_video is not None:
# FID Score
self.fid.update(real_video, real=True)
self.fid.update(generated_video, real=False)
metrics['fid'] = self.fid.compute().item()
# SSIM
metrics['ssim'] = ssim(generated_video, real_video).item()
# L1 and L2 distances
metrics.update({
'l1_distance': F.l1_loss(generated_video, real_video).item(),
'l2_distance': F.mse_loss(generated_video, real_video).item()
})
# 5. Temporal Consistency
temp_diff = torch.diff(generated_video, dim=1)
metrics.update({
'temporal_consistency': -temp_diff.abs().mean().item(),
'temporal_smoothness': -temp_diff.std().item()
})
return metrics
def evaluate_batch(self,
batch: Dict[str, torch.Tensor],
generated_video: torch.Tensor) -> Dict[str, float]:
"""
Evaluate a batch of generated videos
Args:
batch: Dictionary containing ground truth data
generated_video: Generated video frames
"""
metrics = self.compute_metrics(
generated_video,
batch['audio_features'],
batch['frames']
)
# Add any batch-specific metrics
if 'emotion' in batch:
emotion_consistency = F.cosine_similarity(
batch['emotion'],
self.emotion_extractor(generated_video)
).mean()
metrics['emotion_consistency'] = emotion_consistency.item()
return metrics
def log_metrics(self,
metrics: Dict[str, float],
logger: Any,
step: int,
prefix: str = ''):
"""Log computed metrics"""
if prefix and not prefix.endswith('/'):
prefix = prefix + '/'
for name, value in metrics.items():
logger.log_metrics({f"{prefix}{name}": value}, step)