add streaming inference support

This commit is contained in:
sen.liang committed 2022-06-17 19:03:40 +08:00
1 parent e0c6694f52
commit dd5b0646be
13 files changed
+775 -808

No files matched your search

+27 -28
View File
@@ -192,7 +192,7 @@ class Audio2MeshModel(Base):
return loss_dict
@torch.no_grad()
def infer_with_gt(self, batch_cond, batch_data, batch_speech, suffix="", save_npy=True):
"""
batch batch = [B, nsegment]
@@ -204,42 +204,41 @@ class Audio2MeshModel(Base):
cond = batch_cond.float().to(self.device) # (nsteps, dim_cond)
data = batch_data.float().to(self.device) # (B, nframe, 53)
vert, ldmk2d, ldmk3d = self.get_vertices(data)
vert = vert - self.temp_verts + self.temp_verts_specific # add template
batch_tgt_geo, *_ = self.get_vertices(data)
batch_tgt_geo = batch_tgt_geo - self.temp_verts + self.temp_verts_specific # add template
# add a dimensions
# if batch_speech.dims == 1:
batch_speech = batch_speech[np.newaxis, :] # (B, nsegmt)
print(f"batch_speech shape = {batch_speech.shape}") # (1, nsegmt)
with torch.no_grad():
batch_pdt_geo = self.netG(cond) # B x N x dim_motion
batch_pdt_geo += self.temp_verts # B x N x 5023 x 3
print(f"testing pt shape = {batch_pdt_geo.shape}") # 1, 3000, 5023, 3
batch_pdt_geo = self.netG(cond) # B x N x dim_motion
batch_pdt_geo += self.temp_verts # B x N x 5023 x 3
print(f"testing pt shape = {batch_pdt_geo.shape}") # 1, 3000, 5023, 3
# !! add specific when render & output !!
batch_pdt_geo = batch_pdt_geo - self.temp_verts + self.temp_verts_specific
# !! add specific when render & output !!
batch_pdt_geo = batch_pdt_geo - self.temp_verts + self.temp_verts_specific
# render & and save
# render & and save
self.save_reconstruct(batch_pdt_geo, batch_speech,
path_save=Path(self.opt.OUTPUT_DIR),
suffix=suffix, vis_landmarks=False)
# render & and save
print(f"testing pt shape = {batch_pdt_geo.shape}, gt shape = {batch_tgt_geo.shape}")
self.save_reconstruct_with_gt(batch_pdt_geo, batch_tgt_geo, batch_speech,
path_save=Path(self.opt.OUTPUT_DIR),
suffix=suffix, vis_landmarks=False)
if save_npy:
# save result
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_pdt_geo)
print(f"save infer_result shape = {batch_pdt_geo.shape} -> {path_save_npy}")
self.save_reconstruct(batch_pdt_geo, batch_speech,
path_save=Path(self.opt.OUTPUT_DIR),
suffix=suffix, vis_landmarks=False)
# render & and save
print(f"testing pt shape = {batch_pdt_geo.shape}, gt shape = {vert.shape}")
self.save_reconstruct_with_gt(batch_pdt_geo, vert, batch_speech,
path_save=Path(self.opt.OUTPUT_DIR),
suffix=suffix, vis_landmarks=False)
if save_npy:
# save result
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_pdt_geo)
print(f"save infer_result shape = {batch_pdt_geo.shape} -> {path_save_npy}")
def infer(self, batch_cond, batch_speech, suffix="", save_npy=True):
self.eval()
+122 -25
View File
@@ -37,12 +37,14 @@ class Audio2MotionModel(Base):
# define FLAME & render
self.netF = define_FLAME(opt, self.device)
self.netR = define_render(opt, self.device)
self.processor, self.extractor = define_extractor(opt, self.device)
if self.isTrain:
self.processor, self.extractor = define_extractor(opt, self.device)
# path_deca_mean = Path("./data/DECA_obama_mean.npy")
path_deca_mean = self.opt.DATASET.path_mean_dict
self.mean_dict = np.load(path_deca_mean, allow_pickle=True).item()
print(f"load mean deca coeff from > {path_deca_mean}, \nkeys = {self.mean_dict.keys()}")
self.logger.info(f"load mean deca coeff from > {path_deca_mean}, \nkeys = {self.mean_dict.keys()}")
# get template
self.temp_verts, self.temp_ldmk2d, self.temp_ldmk3d \
@@ -135,6 +137,18 @@ class Audio2MotionModel(Base):
debug(self.opt, f"[set_input] ldmk3d = {self.ldmk3d.shape}, dtype={self.ldmk3d.dtype}, device={self.ldmk3d.device}", verbose=False)
@torch.no_grad()
def forward(self, batch_cond):
self.eval()
cond = batch_cond.float().to(self.device) # B x N x 16 x nc_speech
batch_pdt = self.netG(cond) # B x N x dim_motion
# self.logger.info(f"testing batch_pdt shape = {batch_pdt.shape}, batch_pdt_geo = {batch_pdt_geo.shape}")
return batch_pdt
def training(self, batch_input, idx_epoch=0, idx_batch=0):
self.train() # set train model
self.set_input(batch_input)
@@ -168,31 +182,71 @@ class Audio2MotionModel(Base):
return loss_dict
@torch.no_grad()
def infer_with_gt(self, batch_cond, batch_data, batch_speech, suffix="", save_npy=True, eval_loss=True):
self.eval()
cond = batch_cond.float().to(self.device) # B x N x 16 x nc_condit
data = batch_data.float().to(self.device) # B x N x nc_motion
batch_pdt = self.netG(cond) # B x N x nc_motion
self.logger.info(f"testing pt shape = {batch_pdt.shape}")
batch_pdt_geo, *_ = self.get_vertices(batch_pdt) # B x N x 5023 x 3
batch_tgt_geo, *_ = self.get_vertices(data) # B x N x 5023 x 3
self.logger.info(f"testing batch_pdt shape = {batch_pdt.shape}, batch_pdt_geo = {batch_pdt_geo.shape}, batch_tgt_geo = {batch_tgt_geo.shape}")
if save_npy:
# save result
path_save_npy = Path(self.opt.OUTPUT_DIR).joinpath(f"{suffix}_coeff.npy")
np.save(path_save_npy, batch_pdt.cpu().numpy())
self.logger.info(f"save infer_result shape = {batch_pdt.shape} -> {path_save_npy}")
if eval_loss:
loss, loss_dict = self.eval_losses(batch_pdt, data)
self.logger.info(loss_dict)
# l1 loss
diff = torch.abs(batch_pdt - data).mean()
self.logger.info(f"l1 coeff rec loss = {diff:0.6f}")
# render & and save
self.save_reconstruct(batch_pdt_geo, batch_speech, path_save=Path(self.opt.OUTPUT_DIR),
suffix=suffix, vis_landmarks=False)
# save with GT
self.save_reconstruct_with_gt(batch_pdt_geo, batch_tgt_geo, batch_speech,
path_save=Path(self.opt.OUTPUT_DIR),
suffix=suffix, vis_landmarks=False)
@torch.no_grad()
def infer(self, batch_cond, batch_speech, suffix="", save_npy=True):
self.eval()
self.cond = batch_cond.float().to(self.device) # (nsteps, dim_cond)
cond = batch_cond.float().to(self.device) # (nsteps, dim_cond)
with torch.no_grad():
batch_pdt = self.netG(self.cond) # B x N x dim_motion
print(f"testing pt shape = {batch_pdt.shape}")
batch_pdt = self.netG(cond) # B x N x dim_motion
self.logger.info(f"testing pt shape = {batch_pdt.shape}")
# to geo
batch_pdt_geo, *_ = self.get_vertices(batch_pdt) # B x N x 5023 x 3
print(f"testing batch_pdt shape = {batch_pdt.shape}, batch_pdt_geo = {batch_pdt_geo.shape}")
# to geo
batch_pdt_geo, *_ = self.get_vertices(batch_pdt) # B x N x 5023 x 3
self.logger.info(f"testing batch_pdt shape = {batch_pdt.shape}, batch_pdt_geo = {batch_pdt_geo.shape}")
# render & and save
self.save_reconstruct(batch_pdt_geo, batch_speech, path_save=Path(self.opt.OUTPUT_DIR),
suffix=suffix, vis_landmarks=False)
# render & and save
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_pdt_geo = batch_pdt_geo.cpu().numpy()
batch_pdt = batch_pdt.cpu().numpy()
path_save_npy = Path(self.opt.OUTPUT_DIR).joinpath(f"{suffix}_coeff.npy")
np.save(path_save_npy, batch_pdt)
print(f"save infer_result shape = {batch_pdt.shape} -> {path_save_npy}")
if save_npy:
# save result
# batch_pdt_geo = batch_pdt_geo.cpu().numpy()
batch_pdt = batch_pdt.cpu().numpy()
path_save_npy = Path(self.opt.OUTPUT_DIR).joinpath(f"{suffix}_coeff.npy")
np.save(path_save_npy, batch_pdt)
self.logger.info(f"save infer_result shape = {batch_pdt.shape} -> {path_save_npy}")
def testing(self, batch_input, idx_epoch=0, idx_batch=0):
@@ -203,7 +257,7 @@ class Audio2MotionModel(Base):
batch_pdt = self.netG(self.cond) # B x N x dim_motion
batch_pdt_geo, *_ = self.get_vertices(batch_pdt) # B x N x 5023 x 3
print(f"testing batch_pdt shape = {batch_pdt.shape}, batch_pdt_geo = {batch_pdt_geo.shape}")
self.logger.info(f"testing batch_pdt shape = {batch_pdt.shape}, batch_pdt_geo = {batch_pdt_geo.shape}")
self.save_reconstruct_with_gt(batch_pdt_geo, self.vert, self.raw_speech,
@@ -229,7 +283,7 @@ class Audio2MotionModel(Base):
ldmk2d = ldmk2d.reshape(1, 1, -1, 3) # 1 x 1 x 68 x 3
ldmk3d = ldmk3d.reshape(1, 1, -1, 3) # 1 x 1 x 68 x 3
print(f"template verts reshape = {verts.shape}")
self.logger.info(f"template verts reshape = {verts.shape}")
return verts, ldmk2d, ldmk3d
@@ -346,11 +400,11 @@ class Audio2MotionModel(Base):
# cam
cam = torch.tensor([5, 0, 0], device=self.device) # (1, 3)
cam = cam.repeat(nbatch_real, 1) # (nbatch_real, 3)
# print(f"cam shape = {cam.shape}")
# self.logger.info(f"cam shape = {cam.shape}")
# render
render_pt = self.render_geometry(kgeo_pt, cam, self.device) # (nbatch_real, 3, 512, 512)
# print(f"render_pt ={render_pt.shape}, render_gt={render_gt.shape}")
# self.logger.info(f"render_pt ={render_pt.shape}, render_gt={render_gt.shape}")
if vis_landmarks:
kldmk3d_pt = self.netF.seletec_3d68(kgeo_pt) # [nbatch_real x 68 x 3]
@@ -370,6 +424,48 @@ class Audio2MotionModel(Base):
self.logger.info(f"save video to -> {str(ipath_save)}")
@torch.no_grad()
def render_geometry_batch(self, pred_mesh, vis_landmarks=True):
""" render geometry from audio2mesh prediction
pred_mesh: shape = N x 5023 x 3
return:
batch_faces: shape = N x 512 x 512 x 3
"""
N, *_ = pred_mesh.shape
v_render_pt = []
nbatch_render = 10
for k in range(0, N, nbatch_render):
kgeo_pt = pred_mesh[k:k+nbatch_render, ...] # [nbatch_real x 5023 x 3]
nbatch_real = kgeo_pt.size(0)
# cam
cam = torch.tensor([5, 0, 0], device=self.device) # (1, 3)
cam = cam.repeat(nbatch_real, 1) # (nbatch_real, 3)
# self.logger.info(f"cam shape = {cam.shape}")
# render
render_pt = self.render_geometry(kgeo_pt, cam, self.device) # (nbatch_real, 3, 512, 512)
# self.logger.info(f"render_pt ={render_pt.shape}, render_gt={render_gt.shape}")
if vis_landmarks:
kldmk3d_pt = self.netF.seletec_3d68(kgeo_pt) # [nbatch_real x 68 x 3]
kldmk3d_pt = util.batch_orth_proj(kldmk3d_pt, cam);
kldmk3d_pt[:,:,1:] = -kldmk3d_pt[:,:,1:];
render_pt = util.tensor_vis_landmarks(render_pt, kldmk3d_pt, isScale=True, color = (255, 0, 0))
else:
render_pt = render_pt.cpu()
# append each frame
for m in range(nbatch_real):
v_render_pt.append(tensor2image(render_pt[m, ...]))
return v_render_pt
@torch.no_grad()
def render_geometry(self, verts, cam=None, device=None):
"""
@@ -418,12 +514,12 @@ class Audio2MotionModel(Base):
# cam
cam = torch.tensor([5, 0, 0], device=self.device).repeat(nbatch_real, 1) # (nbatch_real, 3)
# print(f"cam shape = {cam.shape}")
# self.logger.info(f"cam shape = {cam.shape}")
# render
render_pt = self.render_geometry(kgeo_pt, cam, self.device) # (nbatch_real, 3, 512, 512)
render_gt = self.render_geometry(kgeo_gt, cam, self.device) # (nbatch_real, 3, 512, 512)
# print(f"render_pt ={render_pt.shape}, render_gt={render_gt.shape}")
# self.logger.info(f"render_pt ={render_pt.shape}, render_gt={render_gt.shape}")
if vis_landmarks:
# landmarks 3d
@@ -510,6 +606,7 @@ def define_render(opt, device):
return model
def define_extractor(opt, device):
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2Model
print("load Wav2Vec2Model ...")