update audduio2pose v1.0
This commit is contained in:
1 parent
c2797657f8
commit
3a15011f4d
28 files changed
+1817
-483
No files matched your search
@@ -2,9 +2,6 @@
|
||||
import numpy as np
|
||||
from collections import OrderedDict
|
||||
|
||||
import tempfile
|
||||
from scipy.io import wavfile
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from pathlib import Path
|
||||
@@ -19,7 +16,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 save_animation_with_gt2, write_video, write_video_with_gt
|
||||
from common.debug import debug
|
||||
from common.losses import WingLoss
|
||||
|
||||
@@ -101,7 +98,7 @@ class Audio2MeshModel(Base):
|
||||
|
||||
|
||||
def set_input(self, batch_input):
|
||||
|
||||
""" """
|
||||
# motion data
|
||||
data = batch_input["data"].float().to(self.device) # (B, nsteps, nc_motion)
|
||||
|
||||
@@ -170,7 +167,7 @@ class Audio2MeshModel(Base):
|
||||
return pred
|
||||
|
||||
|
||||
def training(self, batch_input):
|
||||
def training(self, batch_input, idx_epoch=0, idx_batch=0):
|
||||
self.train() # set train model
|
||||
self.set_input(batch_input)
|
||||
|
||||
@@ -195,7 +192,7 @@ class Audio2MeshModel(Base):
|
||||
return loss_dict
|
||||
|
||||
|
||||
def validation(self, batch_input):
|
||||
def validation(self, batch_input, idx_epoch=0, idx_batch=0):
|
||||
self.eval() # set eval model
|
||||
self.set_input(batch_input)
|
||||
|
||||
@@ -224,11 +221,18 @@ class Audio2MeshModel(Base):
|
||||
# !! add specific when render & output !!
|
||||
batch_pdt_geo = batch_pdt_geo - self.temp_verts + self.temp_verts_specific
|
||||
|
||||
# render & and save
|
||||
self.save_reconstruct(batch_pdt_geo, audio_raw,
|
||||
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_gt2(batch_pdt_geo, vert, audio_raw,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=suffix)
|
||||
suffix=suffix,vis_landmarks=False)
|
||||
|
||||
|
||||
if save_npy:
|
||||
# save result
|
||||
batch_pdt_geo = batch_pdt_geo.cpu().numpy()
|
||||
@@ -237,7 +241,7 @@ class Audio2MeshModel(Base):
|
||||
print(f"save infer_result shape = {batch_pdt_geo.shape} -> {path_save_npy}")
|
||||
|
||||
|
||||
def infer(self, batch_cond, path_audio, suffix="", save_npy=True):
|
||||
def infer(self, batch_cond, raw_speech, suffix="", save_npy=True):
|
||||
self.eval()
|
||||
self.cond = batch_cond.float().to(self.device) # (nsteps, dim_cond)
|
||||
|
||||
@@ -251,10 +255,9 @@ 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, path_audio,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=suffix)
|
||||
|
||||
self.save_reconstruct(batch_pdt_geo, raw_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()
|
||||
@@ -263,7 +266,7 @@ class Audio2MeshModel(Base):
|
||||
print(f"save infer_result shape = {batch_pdt_geo.shape} -> {path_save_npy}")
|
||||
|
||||
|
||||
def testing(self, batch_input, cur_epoch=0, cur_batch=0):
|
||||
def testing(self, batch_input, idx_epoch=0, idx_batch=0):
|
||||
self.eval() # set eval model
|
||||
self.set_input(batch_input)
|
||||
|
||||
@@ -277,9 +280,9 @@ class Audio2MeshModel(Base):
|
||||
print(f"testing pt shape = {batch_pdt_geo.shape}, gt shape = {self.vert.shape}")
|
||||
self.save_reconstruct_with_gt(batch_pdt_geo, self.vert,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=f'sampled_{cur_epoch:05d}_{cur_batch:03d}')
|
||||
suffix=f'sampled_{idx_epoch:05d}_{idx_batch:03d}')
|
||||
|
||||
|
||||
|
||||
def get_template_vertices(self, mean_dict=None):
|
||||
|
||||
batch_exp = torch.zeros(1, 50).to(self.device)
|
||||
@@ -299,9 +302,6 @@ class Audio2MeshModel(Base):
|
||||
ldmk3d = ldmk3d.reshape(1, 1, -1, 3) # 1 x 1 x 68 x 3
|
||||
|
||||
print(f"template verts reshape = {verts.shape}")
|
||||
# np.save("./data/template.npy", verts.squeeze().cpu().numpy())
|
||||
# print(f"save template.npy = {verts.squeeze().shape}")
|
||||
# assert False
|
||||
|
||||
return verts, ldmk2d, ldmk3d
|
||||
|
||||
@@ -402,8 +402,7 @@ class Audio2MeshModel(Base):
|
||||
|
||||
return loss, loss_dict
|
||||
|
||||
|
||||
def save_reconstruct(self, batch_pdt_geo, path_audio, path_save, suffix=""):
|
||||
def save_reconstruct(self, batch_pdt_geo, raw_speech, path_save, suffix="", vis_landmarks=False):
|
||||
# batch_pdt_geo, [B x N x dim_motion]
|
||||
|
||||
B, N, *_ = batch_pdt_geo.shape
|
||||
@@ -412,7 +411,6 @@ class Audio2MeshModel(Base):
|
||||
# for each batch
|
||||
for i in range(B):
|
||||
igeo_pt = batch_pdt_geo[i, ...]
|
||||
ipath_save = path_save.joinpath(f"{suffix}_{i:02d}.mp4")
|
||||
|
||||
v_render_pt = []
|
||||
for k in range(0, N, nbatch_render):
|
||||
@@ -427,16 +425,24 @@ class Audio2MeshModel(Base):
|
||||
|
||||
# render
|
||||
render_pt = self.render_geometry(kgeo_pt, cam, self.device) # (nbatch_real, 3, 512, 512)
|
||||
render_pt = render_pt.cpu()
|
||||
# print(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, ...]))
|
||||
|
||||
# save animation
|
||||
# print(f"testing v_render_pt = {len(v_render_pt)}")
|
||||
save_images_with_audio(v_render_pt, path_audio, ipath_save)
|
||||
# write video
|
||||
ipath_save = path_save.joinpath(f"{suffix}_{i:02d}_pt.mp4")
|
||||
write_video(v_render_pt, raw_speech, ipath_save, fps=50)
|
||||
self.logger.info(f"save video to -> {str(ipath_save)}")
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
@@ -459,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=""):
|
||||
def save_reconstruct_with_gt2(self, batch_pdt_geo, batch_tgt_geo, raw_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]
|
||||
|
||||
@@ -467,7 +473,7 @@ class Audio2MeshModel(Base):
|
||||
|
||||
igeo_pt = batch_pdt_geo[0, ...] # [N x 5023 x 3]
|
||||
igeo_gt = batch_tgt_geo[0, ...] # [N x 5023 x 3]
|
||||
ipath_save = path_save.joinpath(f"{suffix}_{0:02d}.mp4")
|
||||
ipath_save = path_save.joinpath(f"{suffix}_{0:02d}_pt_gt.mp4")
|
||||
|
||||
v_render_pt = []
|
||||
v_render_gt = []
|
||||
@@ -488,18 +494,22 @@ class Audio2MeshModel(Base):
|
||||
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}")
|
||||
|
||||
# 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:];
|
||||
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))
|
||||
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):
|
||||
@@ -507,13 +517,9 @@ class Audio2MeshModel(Base):
|
||||
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
|
||||
write_video_with_gt(v_render_pt, v_render_gt, raw_speech, ipath_save, fps=50, sr=16000)
|
||||
# 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()
|
||||
@@ -589,6 +595,9 @@ class Audio2MeshModel(Base):
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
#########################################
|
||||
def tensor2image(tensor):
|
||||
# image = tensor.detach().cpu().numpy()
|
||||
|
||||
Reference in new issue
Block a user