-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsync_talk_utils.py
412 lines (339 loc) · 14.8 KB
/
sync_talk_utils.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
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
import os
import numpy as np
import torch
import tqdm
from transformers import Wav2Vec2Processor, HubertModel
from .repos.SyncTalk.nerf_triplane.network import AudioEncoder
from .repos.SyncTalk.nerf_triplane.utils import AudDataset, get_audio_features, linear_to_srgb, blend_with_mask_cuda
from .repos.SyncTalk.nerf_triplane.network import NeRFNetwork
from .repos.SyncTalk.nerf_triplane.provider import NeRFDataset
from torch.utils.data import DataLoader
from torch import Tensor
from .utils import Logger
class Opt:
def __init__(self, O: bool) -> None:
self.path = ''
self.O = False
self.test = False
self.test_train = False
self.data_range = [0, -1]
self.workspace = 'workspace'
self.seed = 0
# training options
self.iters = 200000
self.lr = 1e-2
self.lr_net = 1e-3
self.ckpt = 'latest'
self.num_rays = 4096 * 16
self.cuda_ray = False
self.max_steps = 16
self.num_steps = 16
self.upsample_steps = 0
self.update_extra_interval = 16
self.max_ray_batch = 4096
# loss set
self.warmup_step = 10000
self.amb_aud_loss = 1
self.amb_eye_loss = 1
self.unc_loss = 1
self.lambda_amb = 1e-4
self.pyramid_loss = 0
# network backbone options
self.fp16 = False
self.bg_img = ''
self.fbg = False
self.exp_eye = False
self.fix_eye = -1
self.smooth_eye = False
self.bs_area = 'upper'
self.au45 = False
self.torso_shrink = 0.8
# dataset options
self.color_space = 'srgb'
self.preload = 0
# (the default value is for the fox dataset)
self.bound = 1
self.scale = 4
self.offset = [0, 0, 0]
self.dt_gamma = 1/256
self.min_near = 0.05
self.density_thresh = 10
self.density_thresh_torso = 0.01
self.patch_size = 1
self.init_lips = False
self.finetune_lips = False
self.smooth_lips = False
self.torso = False
self.head_ckpt = ''
# else
self.att = 2
self.aud = ''
self.emb = False
self.portrait = False
self.ind_dim = 4
self.ind_num = 20000
self.ind_dim_torso = 8
self.amb_dim = 2
self.part = False
self.part2 = False
self.train_camera = False
self.smooth_path = False
self.smooth_path_window = 7
# asr
self.asr = False
self.asr_wav = ''
self.asr_play = False
self.asr_model = 'deepspeech'
self.asr_save_feats = False
# audio FPS
self.fps = 50
# sliding window left-middle-right length (unit: 20ms)
self.l = 10
self.m = 50
self.r = 10
if O:
self.cuda_ray = True
self.fp16 = True
self.exp_eye = True
@torch.no_grad()
def get_hubert_from_16k_speech(wav2vec2_processor, hubert_model, speech, device_type) -> Tensor:
if speech.ndim ==2:
speech = speech[:, 0] # [T, 2] ==> [T,]
input_values_all = wav2vec2_processor(speech, return_tensors="pt", sampling_rate=16000).input_values # [1, T]
input_values_all = input_values_all.to(device_type)
# For long audio sequence, due to the memory limitation, we cannot process them in one run
# HuBERT process the wav with a CNN of stride [5,2,2,2,2,2], making a stride of 320
# Besides, the kernel is [10,3,3,3,3,2,2], making 400 a fundamental unit to get 1 time step.
# So the CNN is euqal to a big Conv1D with kernel k=400 and stride s=320
# We have the equation to calculate out time step: T = floor((t-k)/s)
# To prevent overlap, we set each clip length of (K+S*(N-1)), where N is the expected length T of this clip
# The start point of next clip should roll back with a length of (kernel-stride) so it is stride * N
kernel = 400
stride = 320
clip_length = stride * 1000
num_iter = input_values_all.shape[1] // clip_length
expected_T = (input_values_all.shape[1] - (kernel-stride)) // stride
res_lst = []
for i in range(num_iter):
if i == 0:
start_idx = 0
end_idx = clip_length - stride + kernel
else:
start_idx = clip_length * i
end_idx = start_idx + (clip_length - stride + kernel)
input_values = input_values_all[:, start_idx: end_idx]
hidden_states = hubert_model.forward(input_values).last_hidden_state # [B=1, T=pts//320, hid=1024]
res_lst.append(hidden_states[0])
if num_iter > 0:
input_values = input_values_all[:, clip_length * num_iter:]
else:
input_values = input_values_all
# if input_values.shape[1] != 0:
if input_values.shape[1] >= kernel: # if the last batch is shorter than kernel_size, skip it
hidden_states = hubert_model(input_values).last_hidden_state # [B=1, T=pts//320, hid=1024]
res_lst.append(hidden_states[0])
ret = torch.cat(res_lst, dim=0).cpu() # [T, 1024]
# assert ret.shape[0] == expected_T
assert abs(ret.shape[0] - expected_T) <= 1
if ret.shape[0] < expected_T:
ret = torch.nn.functional.pad(ret, (0,0,0,expected_T-ret.shape[0]))
else:
ret = ret[:expected_T]
return ret
def make_even_first_dim(tensor: Tensor) -> Tensor:
size = list(tensor.size())
if size[0] % 2 == 1:
size[0] -= 1
return tensor[:size[0]]
return tensor
def aud_process_ave(model: AudioEncoder, wav_file: str, emb=False, device_type='cuda') -> Tensor:
dataset = AudDataset(wav_file)
data_loader = DataLoader(dataset, batch_size=64, shuffle=False)
outputs = []
for mel in data_loader:
mel = mel.to(device_type)
with torch.no_grad():
out = model(mel)
outputs.append(out)
outputs = torch.cat(outputs, dim=0).cpu()
first_frame, last_frame = outputs[:1], outputs[-1:]
aud_features = torch.cat([first_frame.repeat(2, 1), outputs, last_frame.repeat(2, 1)], dim=0).unsqueeze(0)
# support both [N, 16] labels and [N, 16, K] logits
if len(aud_features.shape) == 3:
aud_features = aud_features.float().permute(1, 0, 2) # [N, 16, 29] --> [N, 29, 16]
if emb:
print(f'[INFO] argmax to aud features {aud_features.shape} for --emb mode')
aud_features = aud_features.argmax(1) # [N, 16]
else:
assert emb, "aud only provide labels, must use --emb"
aud_features = aud_features.long()
Logger.log(f'[INFO] load {wav_file} aud_features: {aud_features.shape}')
return aud_features
def aud_process_hubert(wav2vec2_processor: Wav2Vec2Processor, hubert_model: HubertModel, wav_file: str, emb=False, device_type='cuda') -> Tensor:
assert wav_file.endswith(".wav")
import soundfile as sf
import librosa
from .sync_talk_utils import get_hubert_from_16k_speech, make_even_first_dim
speech, sr = sf.read(wav_file)
speech_16k = librosa.resample(speech, orig_sr=sr, target_sr=16000)
Logger.log("SR: {} to {}".format(sr, 16000))
hubert_hidden = get_hubert_from_16k_speech(wav2vec2_processor, hubert_model, speech_16k, device_type)
hubert_hidden = make_even_first_dim(hubert_hidden).reshape(-1, 2, 1024)
aud_features = hubert_hidden
# support both [N, 16] labels and [N, 16, K] logits
if len(aud_features.shape) == 3:
aud_features = aud_features.float().permute(0, 2, 1) # [N, 16, 29] --> [N, 29, 16]
if emb:
print(f'[INFO] argmax to aud features {aud_features.shape} for --emb mode')
aud_features = aud_features.argmax(1) # [N, 16]
else:
assert emb, "aud only provide labels, must use --emb"
aud_features = aud_features.long()
Logger.log(f'[INFO] load {wav_file} aud_features: {aud_features.shape}')
return aud_features
def aud_process_deepspeech(deepspeech, wav_file: str, emb=False) -> Tensor:
import tensorflow as tf
from scipy.io import wavfile
from .repos.SyncTalk.data_utils.deepspeech_features.deepspeech_features import pure_conv_audio_to_deepspeech
graph, logits_ph, input_node_ph, input_lengths_ph = deepspeech
audio_file_path = wav_file
audio_window_size=1,
audio_window_stride=1
with tf.compat.v1.Session(graph=graph) as session:
audio_sample_rate, audio = wavfile.read(audio_file_path)
if audio.ndim != 1:
Logger.log(
"[WARN] Audio has multiple channels, the first channel is used")
audio = audio[:, 0]
ds_features = pure_conv_audio_to_deepspeech(
audio=audio,
audio_sample_rate=audio_sample_rate,
audio_window_size=audio_window_size,
audio_window_stride=audio_window_stride,
num_frames=None,
net_fn=lambda x: session.run(
logits_ph,
feed_dict={
input_node_ph: x[np.newaxis, ...],
input_lengths_ph: [x.shape[0]]}))
net_output = ds_features.reshape(-1, 29)
win_size = 16
zero_pad = np.zeros((int(win_size / 2), net_output.shape[1]))
net_output = np.concatenate(
(zero_pad, net_output, zero_pad), axis=0)
windows = []
for window_index in range(0, net_output.shape[0] - win_size, 2):
windows.append(
net_output[window_index:window_index + win_size])
aud_features = windows
# support both [N, 16] labels and [N, 16, K] logits
if len(aud_features.shape) == 3:
aud_features = aud_features.float().permute(0, 2, 1) # [N, 16, 29] --> [N, 29, 16]
if emb:
print(f'[INFO] argmax to aud features {aud_features.shape} for --emb mode')
aud_features = aud_features.argmax(1) # [N, 16]
else:
assert emb, "aud only provide labels, must use --emb"
aud_features = aud_features.long()
Logger.log(f'[INFO] load {wav_file} aud_features: {aud_features.shape}')
return aud_features
def get_aud_file_features(aud: str, asr_model: str, emb = False) -> Tensor:
"""
Get audio feature from aud file.
"""
if asr_model == 'ave':
try:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = AudioEncoder().to(device).eval()
ckpt = torch.load(os.path.join(os.path.dirname(__file__), 'repos/SyncTalk/nerf_triplane/checkpoints/audio_visual_encoder.pth'))
model.load_state_dict({f'audio_encoder.{k}': v for k, v in ckpt.items()})
dataset = AudDataset(aud)
data_loader = DataLoader(dataset, batch_size=64, shuffle=False)
outputs = []
for mel in data_loader:
mel = mel.to(device)
with torch.no_grad():
out = model(mel)
outputs.append(out)
outputs = torch.cat(outputs, dim=0).cpu()
first_frame, last_frame = outputs[:1], outputs[-1:]
aud_features = torch.cat([first_frame.repeat(2, 1), outputs, last_frame.repeat(2, 1)], dim=0).numpy()
except:
print(f'[ERROR] If do not use Audio Visual Encoder, replace it with the npy file path.')
else:
try:
aud_features = np.load(aud)
except:
print(f'[ERROR] If do not use Audio Visual Encoder, replace it with the npy file path.')
if asr_model == 'ave':
aud_features = torch.from_numpy(aud_features).unsqueeze(0)
# support both [N, 16] labels and [N, 16, K] logits
if len(aud_features.shape) == 3:
aud_features = aud_features.float().permute(1, 0, 2) # [N, 16, 29] --> [N, 29, 16]
if emb:
print(f'[INFO] argmax to aud features {aud_features.shape} for --emb mode')
aud_features = aud_features.argmax(1) # [N, 16]
else:
assert emb, "aud only provide labels, must use --emb"
aud_features = aud_features.long()
print(f'[INFO] load {aud} aud_features: {aud_features.shape}')
else:
aud_features = torch.from_numpy(aud_features)
# support both [N, 16] labels and [N, 16, K] logits
if len(aud_features.shape) == 3:
aud_features = aud_features.float().permute(0, 2, 1) # [N, 16, 29] --> [N, 29, 16]
if emb:
print(f'[INFO] argmax to aud features {aud_features.shape} for --emb mode')
aud_features = aud_features.argmax(1) # [N, 16]
else:
assert emb, "aud only provide labels, must use --emb"
aud_features = aud_features.long()
print(f'[INFO] load {aud} aud_features: {aud_features.shape}')
return aud_features
def inference_step(model: NeRFNetwork, data, opt, bg_color=None, perturb=False, device_type='cuda'):
rays_o = data['rays_o'] # [B, N, 3]
rays_d = data['rays_d'] # [B, N, 3]
bg_coords = data['bg_coords'] # [1, N, 2]
poses = data['poses'] # [B, 7]
auds = data['auds'] # [B, 29, 16]
index = data['index']
H, W = data['H'], data['W']
# allow using a fixed eye area (avoid eye blink) at test
if opt.exp_eye and opt.fix_eye >= 0:
eye = torch.FloatTensor([opt.fix_eye]).view(1, 1).to(device_type)
else:
eye = data['eye'] # [B, 1]
if bg_color is not None:
bg_color = bg_color.to(device_type)
else:
bg_color = data['bg_color']
model.testing = True
outputs = model.render(rays_o, rays_d, auds, bg_coords, poses, eye=eye, index=index, staged=True, bg_color=bg_color, perturb=perturb, **vars(opt))
model.testing = False
pred_rgb = outputs['image'].reshape(-1, H, W, 3)
# pred_depth = outputs['depth'].reshape(-1, H, W)
pred_rgb = outputs['image'].reshape(-1, H, W, 3)
# pred_depth = outputs['depth'].reshape(-1, H, W)
return pred_rgb
@torch.no_grad()
def inference_auds(model: NeRFNetwork, dataset: NeRFDataset, auds: Tensor, opt: Opt, start=0, device_type='cuda'):
model.eval()
auds_size = auds.shape[0]
pbar = tqdm.tqdm(total=auds_size, bar_format='{percentage:3.0f}% {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}]')
Logger.log(f"==> Start Inference")
all_preds = []
for i in range(auds_size):
data = dataset.collate([start + i])
data['auds'] = get_audio_features(auds, opt.att, i).to(device_type)
with torch.amp.autocast(enabled=opt.fp16, device_type=device_type):
preds = inference_step(model, data, opt, device_type=device_type)
if opt.color_space == 'linear':
preds = linear_to_srgb(preds)
if opt.portrait:
pred = blend_with_mask_cuda(preds[0], data["bg_gt_images"].squeeze(0), data["bg_face_mask"].squeeze(0))
pred = (pred * 255).astype(np.uint8)
else:
pred = preds[0]
all_preds.append(pred)
pbar.update()
return all_preds