update write_video with ffmpeg
This commit is contained in:
1 parent
4d4015350c
commit
0d5689979f
8 files changed
+186
-173
No files matched your search
@@ -182,6 +182,7 @@ config.TRAIN = edict()
|
||||
config.TRAIN.apply_validation = False
|
||||
config.TRAIN.apply_testing = False
|
||||
config.TRAIN.apply_update_parameters = False
|
||||
config.TRAIN.apply_lr_scheduler = False
|
||||
|
||||
config.TRAIN.lr = 0.001
|
||||
config.TRAIN.lr_step = [50, 100]
|
||||
|
||||
@@ -241,7 +241,7 @@ class Audio2MeshModel(Base):
|
||||
print(f"save infer_result shape = {batch_pdt_geo.shape} -> {path_save_npy}")
|
||||
|
||||
|
||||
def infer(self, batch_cond, raw_speech, suffix="", save_npy=True):
|
||||
def infer(self, batch_cond, batch_speech, suffix="", save_npy=True):
|
||||
self.eval()
|
||||
self.cond = batch_cond.float().to(self.device) # (nsteps, dim_cond)
|
||||
|
||||
@@ -255,7 +255,7 @@ class Audio2MeshModel(Base):
|
||||
batch_pdt_geo = batch_pdt_geo - self.temp_verts + self.temp_verts_specific
|
||||
|
||||
# render & and save
|
||||
self.save_reconstruct(batch_pdt_geo, raw_speech, path_save=Path(self.opt.OUTPUT_DIR),
|
||||
self.save_reconstruct(batch_pdt_geo, batch_speech, path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=suffix, vis_landmarks=False)
|
||||
|
||||
if save_npy:
|
||||
@@ -402,7 +402,7 @@ class Audio2MeshModel(Base):
|
||||
|
||||
return loss, loss_dict
|
||||
|
||||
def save_reconstruct(self, batch_pdt_geo, raw_speech, path_save, suffix="", vis_landmarks=False):
|
||||
def save_reconstruct(self, batch_pdt_geo, batch_speech, path_save, suffix="", vis_landmarks=False):
|
||||
# batch_pdt_geo, [B x N x dim_motion]
|
||||
|
||||
B, N, *_ = batch_pdt_geo.shape
|
||||
@@ -441,7 +441,7 @@ class Audio2MeshModel(Base):
|
||||
|
||||
# write video
|
||||
ipath_save = path_save.joinpath(f"{suffix}_{i:02d}_pt.mp4")
|
||||
write_video(v_render_pt, raw_speech, ipath_save, fps=50)
|
||||
write_video(v_render_pt, batch_speech, ipath_save, fps=50)
|
||||
self.logger.info(f"save video to -> {str(ipath_save)}")
|
||||
|
||||
|
||||
@@ -465,7 +465,7 @@ class Audio2MeshModel(Base):
|
||||
return images
|
||||
|
||||
@torch.no_grad()
|
||||
def save_reconstruct_with_gt2(self, batch_pdt_geo, batch_tgt_geo, raw_speech, path_save, suffix="", vis_landmarks=True):
|
||||
def save_reconstruct_with_gt2(self, batch_pdt_geo, batch_tgt_geo, batch_speech, path_save, suffix="", vis_landmarks=True):
|
||||
# batch_pdt_geo, [1 x N x 5023 x 3]
|
||||
# batch_tgt_geo, [1 x N x 5023 x 3]
|
||||
|
||||
@@ -518,7 +518,7 @@ class Audio2MeshModel(Base):
|
||||
|
||||
|
||||
# save animation
|
||||
write_video_with_gt(v_render_pt, v_render_gt, raw_speech, ipath_save, fps=50, sr=16000)
|
||||
write_video_with_gt(v_render_pt, v_render_gt, batch_speech, ipath_save, fps=50, sr=16000)
|
||||
# print(f"testing v_render_pt = {len(v_render_pt)}, gt shape = {len(v_render_gt)}")
|
||||
|
||||
|
||||
@@ -587,8 +587,7 @@ class Audio2MeshModel(Base):
|
||||
jstart_time = (jstart_frame + 7) / self.opt.DATASET.fps # xxx.yyy min
|
||||
jend_time = (jstart_frame + 7 + self.nseq) / self.opt.DATASET.fps # xxx.yyy min
|
||||
# print(f"jvid={jvid}, jnfram={jnframe}, jstart_frame={jstart_frame}, jpath_audio={jpath_audio}")
|
||||
# print(f"jstart_time={jstart_time}, jend_time={jend_time}")
|
||||
|
||||
# print(f"jstart_time={jstart_time}, jend_time={jend_time}")
|
||||
|
||||
# save animation
|
||||
# print(f"testing v_render_pt = {len(v_render_pt)}, gt shape = {len(v_render_gt)}")
|
||||
|
||||
+74
-145
@@ -15,7 +15,7 @@ from src.optims import get_optimizer, get_scheduler
|
||||
from src.utils import util
|
||||
|
||||
# common
|
||||
from common.animation import save_animation_with_gt2, save_images_with_audio
|
||||
from common.animation import write_video, write_video_with_gt
|
||||
from common.debug import debug
|
||||
from common.losses import WingLoss
|
||||
|
||||
@@ -104,10 +104,10 @@ class Audio2PoseModel(Base):
|
||||
|
||||
# padding half frame length for bed and end
|
||||
npad = int(0.5 * self.opt.DATASET.sr * ( 1 / self.opt.DATASET.fps) + 0.5)
|
||||
raw_pad = np.pad(raw_speech, pad_width=((0,0), (npad, npad)), mode="edge")
|
||||
debug(self.opt, f"after padding raw_pad = {raw_pad.shape}, dtype={raw_pad.dtype}", verbose=False)
|
||||
pad_speech = np.pad(raw_speech, pad_width=((0,0), (npad, npad)), mode="edge")
|
||||
debug(self.opt, f"after padding pad_speech = {pad_speech.shape}, dtype={pad_speech.dtype}", verbose=False)
|
||||
|
||||
inputs = self.processor(list(raw_pad), sampling_rate=16000, return_tensors="pt")
|
||||
inputs = self.processor(list(pad_speech), sampling_rate=16000, return_tensors="pt")
|
||||
with torch.no_grad():
|
||||
cond = self.extractor(**inputs.to(self.device)).last_hidden_state # B x nsteps x nc_feat
|
||||
debug(self.opt, f"cond = {cond.shape}, dtype={cond.dtype}, device={cond.device}", verbose=False)
|
||||
@@ -120,8 +120,11 @@ class Audio2PoseModel(Base):
|
||||
self.c = cond # (B, nsteps, 16, nc_speech)
|
||||
self.B, self.nsteps, self.nwind, self.dim_speech = cond.size()
|
||||
self.x = self.x[:, 7:7+self.nsteps, :] # (B, nsteps, nc_motion)
|
||||
self.raw_speech = raw_speech[:, 7*self.opt.DATASET.sr: (7+self.nsteps)*self.opt.DATASET.sr] # (B, nsteps * sr)
|
||||
|
||||
|
||||
# cut audio clips
|
||||
idx_beg = int(7 * self.opt.DATASET.sr / self.opt.DATASET.fps + 0.5)
|
||||
idx_end = int((7 + self.nsteps) * self.opt.DATASET.sr / self.opt.DATASET.fps + 0.5)
|
||||
self.raw_speech = raw_speech[:, idx_beg: idx_end] # (B, nsteps * sr / fps)
|
||||
|
||||
self.idn = self.idn[:, 7:7+self.nsteps, :]
|
||||
self.exp = self.exp[:, 7:7+self.nsteps, :]
|
||||
@@ -170,7 +173,6 @@ class Audio2PoseModel(Base):
|
||||
self.logger.info(f"\t[UPDATE] lambda_kl = {self.opt.LOSS.lambda_kl}")
|
||||
|
||||
|
||||
|
||||
def training(self, batch_input, idx_epoch=0, idx_batch=0):
|
||||
self.train() # set train model
|
||||
self.set_input(batch_input)
|
||||
@@ -265,61 +267,58 @@ class Audio2PoseModel(Base):
|
||||
|
||||
return loss_dict
|
||||
|
||||
def infer(self, batch_cond, path_audio, suffix="", save_npy=True):
|
||||
|
||||
def infer(self, batch_cond, batch_speech, suffix="", save_npy=True):
|
||||
self.eval()
|
||||
c = batch_cond.float().to(self.device) # (B, nsteps, 16, nc_speech)
|
||||
nbtach, nstep, *_ = c.shape
|
||||
self.cond = batch_cond.float().to(self.device) # (nsteps, dim_cond)
|
||||
|
||||
with torch.no_grad():
|
||||
batch_pdt_geo = self.netG(self.cond) # B x N x dim_motion
|
||||
batch_pdt_geo += self.temp_verts # B x N x 5023 x 3
|
||||
|
||||
batch_pos = self.netG(self.cond) # B x N x dim_motion
|
||||
batch_pos += self.temp_verts # B x N x 5023 x 3
|
||||
|
||||
print(f"testing pt shape = {batch_pos.shape}")
|
||||
print(f"testing pt shape = {batch_pdt_geo.shape}")
|
||||
|
||||
# !! add specific when render & output !!
|
||||
batch_pos = batch_pos - self.temp_verts + self.temp_verts_specific
|
||||
batch_pdt_geo = batch_pdt_geo - self.temp_verts + self.temp_verts_specific
|
||||
|
||||
# render & and save
|
||||
self.save_reconstruct(batch_pos, path_audio,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=suffix)
|
||||
|
||||
self.save_reconstruct(batch_pdt_geo, batch_speech, path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=suffix, vis_landmarks=False)
|
||||
|
||||
if save_npy:
|
||||
# save result
|
||||
batch_pos = batch_pos.cpu().numpy()
|
||||
batch_pdt_geo = batch_pdt_geo.cpu().numpy()
|
||||
path_save_npy = Path(self.opt.OUTPUT_DIR).joinpath(f"{suffix}.npy")
|
||||
np.save(path_save_npy, batch_pos)
|
||||
print(f"save infer_result shape = {batch_pos.shape} -> {path_save_npy}")
|
||||
|
||||
np.save(path_save_npy, batch_pdt_geo)
|
||||
print(f"save infer_result shape = {batch_pdt_geo.shape} -> {path_save_npy}")
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def testing(self, batch_input, idx_epoch=0, idx_batch=0):
|
||||
self.eval() # set eval model
|
||||
self.set_input(batch_input)
|
||||
|
||||
with torch.no_grad():
|
||||
|
||||
batch_pt = torch.zeros_like(self.x) # [B, nsteps, nc_motion]
|
||||
# batch_pt[:, 0, 3] = self.x[:, 0, 3] # T-pose + first scale from input data
|
||||
batch_pt[:, 0] = self.x[:, 0] # T-pose + first scale from input data
|
||||
batch_pt = torch.zeros_like(self.x) # [B, nsteps, nc_motion]
|
||||
# batch_pt[:, 0, 3] = self.x[:, 0, 3] # T-pose + first scale from input data
|
||||
batch_pt[:, 0] = self.x[:, 0] # T-pose + first scale from input data
|
||||
|
||||
for istep in range(1, self.nsteps):
|
||||
ibatch_z = torch.randn((self.B, self.dim_latent), device=self.device) # [B, dim_latent]
|
||||
ibatch_c = self.c[:, istep] # [B, nwind, nc_speech]
|
||||
ibatch_x_prev = batch_pt[:, istep-1] # [B, nc_motion]
|
||||
for istep in range(1, self.nsteps):
|
||||
ibatch_z = torch.randn((self.B, self.dim_latent), device=self.device) # [B, dim_latent]
|
||||
ibatch_c = self.c[:, istep] # [B, nwind, nc_speech]
|
||||
ibatch_x_prev = batch_pt[:, istep-1] # [B, nc_motion]
|
||||
|
||||
# forward
|
||||
ibatch_pt = self.netG.sample(ibatch_z, ibatch_x_prev, ibatch_c) # [B, nc_motion]
|
||||
# forward
|
||||
ibatch_pt = self.netG.sample(ibatch_z, ibatch_x_prev, ibatch_c) # [B, nc_motion]
|
||||
|
||||
debug(self.opt, f"[test] ibatch_pt = {ibatch_pt.shape}")
|
||||
batch_pt[:, istep] = ibatch_pt.detach().clone() # update batch_pt
|
||||
debug(self.opt, f"[test] ibatch_pt = {ibatch_pt.shape}")
|
||||
batch_pt[:, istep] = ibatch_pt.detach().clone() # update batch_pt
|
||||
|
||||
|
||||
# save video
|
||||
# print(f"testing pt shape = {batch_pt.shape}, gt shape = {self.x.shape}")
|
||||
|
||||
self.save_reconstruct_with_gt(batch_pt, self.x,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=f'sampled_{idx_epoch:05d}_{idx_batch:03d}')
|
||||
# save video
|
||||
# print(f"testing pt shape = {batch_pt.shape}, gt shape = {self.x.shape}")
|
||||
self.save_reconstruct_with_gt(batch_pt, self.x, self.raw_speech,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=f'sampled_{idx_epoch:05d}_{idx_batch:03d}')
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
@@ -345,6 +344,28 @@ class Audio2PoseModel(Base):
|
||||
return verts, ldmk2d, ldmk3d
|
||||
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def render_geometry(self, verts, cam=None, device=None):
|
||||
"""
|
||||
verts: [N x 5023 x 3]
|
||||
"""
|
||||
batch_size = verts.size(0)
|
||||
|
||||
if cam is None:
|
||||
cam = torch.zeros(batch_size, 3).to(device) # (batch_size, 3)
|
||||
|
||||
# orth_proj
|
||||
verts_proj = util.batch_orth_proj(verts, cam);
|
||||
verts_proj[:,:,1:] = -verts_proj[:,:,1:]
|
||||
|
||||
# render
|
||||
images = self.netR.render_shape(verts, verts_proj) # (batch_size, 3, 512, 512)
|
||||
|
||||
return images
|
||||
|
||||
|
||||
|
||||
def eval_losses(self, batch_pt, batch_gt, batch_mu, batch_lv):
|
||||
""" Computes the VAE loss function.
|
||||
pramas:
|
||||
@@ -387,8 +408,9 @@ class Audio2PoseModel(Base):
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def save_reconstruct(self, batch_pdt_pos, raw_speech, path_save, suffix="", vis_landmarks=True):
|
||||
def save_reconstruct(self, batch_pdt_pos, batch_speech, path_save, suffix="", vis_landmarks=True):
|
||||
# batch_pdt_pos, [B x N x dim_motion]
|
||||
# batch_speech, [B x N*sr ]
|
||||
|
||||
B, N, *_ = batch_pdt_pos.shape
|
||||
nbatch_render = 10
|
||||
@@ -428,99 +450,18 @@ class Audio2PoseModel(Base):
|
||||
for m in range(nbatch_real):
|
||||
v_render_pt.append(tensor2image(render_pt[m, ...]))
|
||||
|
||||
# write tmp audio
|
||||
tmp_audio = tempfile.NamedTemporaryFile(suffix='.wav')
|
||||
wavfile.write(tmp_audio.name, rate=16000, data=raw_speech)
|
||||
|
||||
# save animation
|
||||
save_images_with_audio(v_render_pt, tmp_audio.name, ipath_save)
|
||||
# write video
|
||||
ipath_save = path_save.joinpath(f"{suffix}_{i:02d}_pt.mp4")
|
||||
write_video(v_render_pt, batch_speech, ipath_save, fps=50)
|
||||
self.logger.info(f"save video to -> {str(ipath_save)}")
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def render_geometry(self, verts, cam=None, device=None):
|
||||
"""
|
||||
verts: [N x 5023 x 3]
|
||||
"""
|
||||
batch_size = verts.size(0)
|
||||
|
||||
if cam is None:
|
||||
cam = torch.zeros(batch_size, 3).to(device) # (batch_size, 3)
|
||||
|
||||
# orth_proj
|
||||
verts_proj = util.batch_orth_proj(verts, cam);
|
||||
verts_proj[:,:,1:] = -verts_proj[:,:,1:]
|
||||
|
||||
# render
|
||||
images = self.netR.render_shape(verts, verts_proj) # (batch_size, 3, 512, 512)
|
||||
|
||||
return images
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def save_reconstruct_with_gt2(self, batch_pdt_pos, batch_tgt_geo, raw_speech, path_save, suffix="", vis_landmarks=True):
|
||||
# batch_pdt_pos, [1 x N x 5023 x 3]
|
||||
# batch_tgt_geo, [1 x N x 5023 x 3]
|
||||
|
||||
B, N, *_ = batch_pdt_pos.shape
|
||||
|
||||
ipos_pt = batch_pdt_pos[0, ...] # [N x 5023 x 3]
|
||||
ipos_gt = batch_tgt_geo[0, ...] # [N x 5023 x 3]
|
||||
ipath_save = path_save.joinpath(f"{suffix}_{0:02d}.mp4")
|
||||
|
||||
v_render_pt = []
|
||||
v_render_gt = []
|
||||
nbatch_render = 10
|
||||
for k in range(0, N, nbatch_render):
|
||||
# split
|
||||
kpos_pt = ipos_pt[k:k+nbatch_render, ...] # [nbatch_real x 5023 x 3]
|
||||
kpos_gt = ipos_gt[k:k+nbatch_render, ...] # [nbatch_real x 5023 x 3]
|
||||
|
||||
nbatch_real = kpos_pt.size(0)
|
||||
|
||||
# cam
|
||||
cam = torch.tensor([5, 0, 0], device=self.device).repeat(nbatch_real, 1) # (nbatch_real, 3)
|
||||
# print(f"cam shape = {cam.shape}")
|
||||
|
||||
# render
|
||||
render_pt = self.render_geometry(kpos_pt, cam, self.device) # (nbatch_real, 3, 512, 512)
|
||||
render_gt = self.render_geometry(kpos_gt, cam, self.device) # (nbatch_real, 3, 512, 512)
|
||||
# print(f"render_pt ={render_pt.shape}, render_gt={render_gt.shape}")
|
||||
|
||||
if vis_landmarks:
|
||||
# landmarks 3d
|
||||
kldmk3d_pt = self.netF.seletec_3d68(kgeo_pt) # [nbatch_real x 68 x 3]
|
||||
kldmk3d_gt = self.netF.seletec_3d68(kgeo_gt) # [nbatch_real x 68 x 3]
|
||||
|
||||
kldmk3d_pt = util.batch_orth_proj(kldmk3d_pt, cam);
|
||||
kldmk3d_pt[:,:,1:] = -kldmk3d_pt[:,:,1:];
|
||||
|
||||
kldmk3d_gt = util.batch_orth_proj(kldmk3d_gt, cam);
|
||||
kldmk3d_gt[:,:,1:] = -kldmk3d_gt[:,:,1:];
|
||||
|
||||
render_pt = util.tensor_vis_landmarks(render_pt, kldmk3d_pt, gt_landmarks=kldmk3d_gt, isScale=True, color = (255, 0, 0), color_gt=(0, 255, 0))
|
||||
render_gt = util.tensor_vis_landmarks(render_gt, kldmk3d_gt, isScale=True, color = (0, 255, 0))
|
||||
else:
|
||||
render_pt = render_pt.cpu()
|
||||
render_gt = render_gt.cpu()
|
||||
# append each frame
|
||||
for m in range(nbatch_real):
|
||||
v_render_pt.append(tensor2image(render_pt[m, ...]))
|
||||
v_render_gt.append(tensor2image(render_gt[m, ...]))
|
||||
|
||||
|
||||
# write tmp audio
|
||||
tmp_audio = tempfile.NamedTemporaryFile(suffix='.wav')
|
||||
wavfile.write(tmp_audio.name, rate=16000, data=raw_speech)
|
||||
|
||||
# save animation
|
||||
# print(f"testing v_render_pt = {len(v_render_pt)}, gt shape = {len(v_render_gt)}")
|
||||
save_animation_with_gt2(v_render_pt, v_render_gt, ipath_save, fps=50, path_audio=tmp_audio.name)
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def save_reconstruct_with_gt(self, batch_pdt_pos, batch_tgt_pos, path_save, suffix=""):
|
||||
def save_reconstruct_with_gt(self, batch_pdt_pos, batch_tgt_pos, batch_speech, path_save, suffix=""):
|
||||
# batch_pdt_pos, [B x N x nc_motion=6]
|
||||
# batch_pdt_pos, [B x N x nc_motion=6]
|
||||
# batch_speech, [B x N*sr ]
|
||||
|
||||
B, N, *_ = batch_pdt_pos.shape
|
||||
nbatch_render = 10
|
||||
@@ -570,19 +511,10 @@ class Audio2PoseModel(Base):
|
||||
|
||||
|
||||
# get audio
|
||||
jvid = self.batch_vid[i]
|
||||
jstart_frame = self.batch_interv[0][i].numpy()
|
||||
|
||||
jpath_audio = f"{self.opt.DATASET.root_raw}/{jvid}/audio/audio_trim.wav"
|
||||
jstart_time = (jstart_frame + 7) / self.opt.DATASET.fps # xxx.yyy min
|
||||
jend_time = (jstart_frame + 7 + self.nsteps) / self.opt.DATASET.fps # xxx.yyy min
|
||||
# print(f"jvid={jvid}, jnfram={jnframe}, jstart_frame={jstart_frame}, jpath_audio={jpath_audio}")
|
||||
# print(f"jstart_time={jstart_time}, jend_time={jend_time}")
|
||||
|
||||
# save animation
|
||||
# print(f"testing v_render_pt = {len(v_render_pt)}, gt shape = {len(v_render_gt)}")
|
||||
save_animation_with_gt2(v_render_pt, v_render_gt, ipath_save, fps=self.opt.DATASET.fps,\
|
||||
path_audio=jpath_audio, start_time=jstart_time, end_time=jend_time)
|
||||
write_video_with_gt(v_render_pt, v_render_gt, batch_speech[i], ipath_save, fps=50, sr=16000)
|
||||
|
||||
|
||||
#########################################
|
||||
def tensor2image(tensor):
|
||||
@@ -645,7 +577,4 @@ def define_extractor(opt, device):
|
||||
processor = Wav2Vec2FeatureExtractor.from_pretrained(path_ckpt_wav2vec)
|
||||
extractor = Wav2Vec2Model.from_pretrained(path_ckpt_wav2vec).to(device)
|
||||
|
||||
return processor, extractor
|
||||
|
||||
|
||||
|
||||
return processor, extractor
|
||||
Reference in new issue
Block a user