add eval.py with metrics
This commit is contained in:
1 parent
11277e478f
commit
baed1fffe3
53 files changed
+5844
-378
No files matched your search
+107
-79
@@ -6,10 +6,11 @@ import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from pathlib import Path
|
||||
|
||||
from src.models.info import flame_mouth_idx
|
||||
from src.models.info import flame_mouth_idx, flame_mouth_upper_idx, flame_mouth_lower_idx
|
||||
from src.models import init_net
|
||||
from src.models.base import Base
|
||||
from src.optims import get_optimizer, get_scheduler
|
||||
from src.losses.metrics import MeanDictMetric
|
||||
|
||||
# common
|
||||
from common.animation import save_animation_with_gt2, save_images_with_audio
|
||||
@@ -46,7 +47,6 @@ class Audio2MeshModel(Base):
|
||||
self.netR, self.flame = define_netR(device=self.device)
|
||||
# self.netG.init_reconstruct_model(self.netR)
|
||||
|
||||
# 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()}")
|
||||
@@ -59,10 +59,15 @@ class Audio2MeshModel(Base):
|
||||
# if self.opt.LOSS.filter_outlier:
|
||||
self.loss_func_wing = WingLoss(reduction="none") # return (B, )
|
||||
self.loss_func_l1 = nn.L1Loss(reduction="none")
|
||||
|
||||
if self.isTrain:
|
||||
self.names_loss = opt.LOSS.names
|
||||
|
||||
self.names_loss = opt.LOSS.names
|
||||
|
||||
# define loss weight
|
||||
self.vertices_weight = torch.ones((1, 1, self.opt.MODEL.num_vertices, 3)).to(self.device)
|
||||
self.vertices_weight[:, :, flame_mouth_idx, :] = 10
|
||||
|
||||
|
||||
if self.isTrain:
|
||||
# define optimizers
|
||||
self.optimizer_G = get_optimizer(self.netG, opt)
|
||||
self.optimizers.append(self.optimizer_G)
|
||||
@@ -70,10 +75,6 @@ class Audio2MeshModel(Base):
|
||||
self.scheduler_G = get_scheduler(self.optimizer_G, opt)
|
||||
self.schedulers.append(self.scheduler_G)
|
||||
|
||||
# define loss weight
|
||||
self.vertices_weight = torch.ones((1, 1, self.opt.MODEL.num_vertices, 3)).to(self.device)
|
||||
self.vertices_weight[:, :, flame_mouth_idx, :] = 10
|
||||
|
||||
|
||||
if self.opt.DATASET.speech_extractor == "wav2vec":
|
||||
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2Model
|
||||
@@ -172,32 +173,12 @@ class Audio2MeshModel(Base):
|
||||
self.pred = self.netG(self.cond)
|
||||
self.pred += self.template_verts
|
||||
|
||||
loss, self.loss_dict = self.eval_losses(self.pred, self.data)
|
||||
|
||||
return loss
|
||||
_, loss_dict = self.eval_losses(self.pred, self.data)
|
||||
|
||||
return loss_dict
|
||||
|
||||
|
||||
def infer_loss(self, batch_cond, batch_data):
|
||||
"""
|
||||
input parameters:
|
||||
batch_cond: B x nsteps x 16 x nfeat_speech
|
||||
batch_data: B x nsteps x nfeat_motion
|
||||
return:
|
||||
loss: B x nsteps x 3
|
||||
"""
|
||||
self.eval()
|
||||
cond = batch_cond.float().to(self.device) # (B x nsteps x 16 x nc_condit)
|
||||
data = batch_data.float().to(self.device) # (B x nsteps x nc_motion)
|
||||
|
||||
with torch.no_grad():
|
||||
samples_pt = self.netG(cond) # B x N x 16 x nc_condit
|
||||
samples_pt += self.template_verts # B x N x 5023 x 3
|
||||
loss = self.eval_losses_for_infer_loss(samples_pt, data)
|
||||
|
||||
return loss
|
||||
|
||||
|
||||
def infer(self, batch_cond, path_audio, suffix=""):
|
||||
def infer(self, batch_cond, path_audio, suffix="", save_npy=True):
|
||||
self.eval()
|
||||
self.cond = batch_cond.float().to(self.device) # (nsteps, dim_cond)
|
||||
|
||||
@@ -207,19 +188,21 @@ class Audio2MeshModel(Base):
|
||||
|
||||
print(f"testing pt shape = {samples_pt.shape}")
|
||||
|
||||
self.save_reconstruct(samples_pt, path_audio,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=suffix)
|
||||
|
||||
# !! add specific when render & output !!
|
||||
samples_pt = samples_pt - self.template_verts + self.template_verts_specific
|
||||
|
||||
# save result
|
||||
samples_pt = samples_pt.cpu().numpy()
|
||||
path_save_npy = Path(self.opt.OUTPUT_DIR).joinpath(f"{suffix}.npy")
|
||||
np.save(path_save_npy, samples_pt)
|
||||
print(f"save infer_result shape = {samples_pt.shape} -> {path_save_npy}")
|
||||
|
||||
# render & and save
|
||||
self.save_reconstruct(samples_pt, path_audio,
|
||||
path_save=Path(self.opt.OUTPUT_DIR),
|
||||
suffix=suffix)
|
||||
|
||||
if save_npy:
|
||||
# save result
|
||||
samples_pt = samples_pt.cpu().numpy()
|
||||
path_save_npy = Path(self.opt.OUTPUT_DIR).joinpath(f"{suffix}.npy")
|
||||
np.save(path_save_npy, samples_pt)
|
||||
print(f"save infer_result shape = {samples_pt.shape} -> {path_save_npy}")
|
||||
|
||||
|
||||
def testing(self, batch_input, cur_epoch=0, cur_batch=0):
|
||||
self.eval() # set eval model
|
||||
@@ -260,28 +243,6 @@ class Audio2MeshModel(Base):
|
||||
|
||||
return verts.unsqueeze(dim=0) # 1 x 1 x 5023 x 3
|
||||
|
||||
def get_template_vertices(self, mean_dict=None):
|
||||
|
||||
if mean_dict is not None:
|
||||
pass
|
||||
|
||||
else:
|
||||
batch_shape = torch.zeros(1, 100).to(self.device)
|
||||
batch_exp = torch.zeros(1, 50).to(self.device)
|
||||
batch_pos = torch.zeros(1, 6).to(self.device)
|
||||
|
||||
verts, _, _ = self.netR.get_vertices(batch_shape, batch_exp, batch_pos) # 1 x 5023 x 3
|
||||
|
||||
# np.save("./data/template.npy", verts.squeeze().cpu().numpy())
|
||||
# print(f"save template.npy = {verts.squeeze().shape}")
|
||||
# assert False
|
||||
|
||||
verts = verts.unsqueeze(dim=0) # 1 x 1 x 5023 x 3
|
||||
|
||||
print(f"template verts reshape = {verts.shape}")
|
||||
|
||||
return verts
|
||||
|
||||
|
||||
def get_vertices(self, batch_motion):
|
||||
"""
|
||||
@@ -355,9 +316,9 @@ class Audio2MeshModel(Base):
|
||||
# synthesis geo rec
|
||||
loss = 0
|
||||
loss_rec_geo = self.loss_func_wing(batch_pdt_geo, batch_tgt_geo) # B
|
||||
loss_dict["loss_rec_geo"] = loss_rec_geo.mean().detach()
|
||||
loss_dict["rec_geo"] = loss_rec_geo.mean().detach()
|
||||
loss_rec_geo = self.loss_func_l1(batch_pdt_geo, batch_tgt_geo) # B
|
||||
loss_dict["loss_rec_geo_l1"] = loss_rec_geo.mean().detach()
|
||||
loss_dict["rec_geo_l1"] = loss_rec_geo.mean().detach()
|
||||
|
||||
|
||||
loss_rec_geo2 = self.loss_func_wing(batch_pdt_geo, batch_tgt_geo, weight=self.vertices_weight) # B x nsteps x num_vertices x 3
|
||||
@@ -373,9 +334,9 @@ class Audio2MeshModel(Base):
|
||||
diff_tgt_geo = batch_tgt_geo[:, 1:, :] - batch_tgt_geo[:, :-1, :]
|
||||
|
||||
loss_vel_geo = self.loss_func_wing(diff_pdt_geo, diff_tgt_geo) # 1,
|
||||
loss_dict["loss_vel_geo"] = loss_vel_geo.mean().detach()
|
||||
loss_dict["vel_geo"] = loss_vel_geo.mean().detach()
|
||||
loss_vel_geo = self.loss_func_l1(diff_pdt_geo, diff_tgt_geo) # 1,
|
||||
loss_dict["loss_vel_geo_l1"] = loss_vel_geo.mean().detach()
|
||||
loss_dict["vel_geo_l1"] = loss_vel_geo.mean().detach()
|
||||
|
||||
loss_vel_geo2 = self.loss_func_wing(diff_pdt_geo, diff_tgt_geo, weight=self.vertices_weight) # B x nsteps x num_vertices x 3
|
||||
loss_vel_geo2 = loss_vel_geo2.sum(dim=(2, 3)) / (3 * self.opt.MODEL.num_vertices) # B x nsteps
|
||||
@@ -397,18 +358,70 @@ class Audio2MeshModel(Base):
|
||||
|
||||
# calculate again
|
||||
loss_rec_geo2 = loss_rec_geo2.mean()
|
||||
loss_dict["loss_rec_geo_w"] = loss_rec_geo2.detach()
|
||||
loss_dict["rec_geo_w"] = loss_rec_geo2.detach()
|
||||
loss += loss_rec_geo2 * self.opt.LOSS.lambda_rec_geo
|
||||
|
||||
loss_vel_geo2 = loss_vel_geo2.mean()
|
||||
loss_dict["loss_vel_geo_w"] = loss_vel_geo2.detach()
|
||||
loss_dict["vel_geo_w"] = loss_vel_geo2.detach()
|
||||
loss += loss_vel_geo2 * self.opt.LOSS.lambda_vel_geo
|
||||
|
||||
# total loss
|
||||
loss_dict['loss_all'] = loss.detach()
|
||||
loss_dict['all'] = loss.detach()
|
||||
|
||||
return loss, loss_dict
|
||||
|
||||
|
||||
def eval_losses_for_valid(self, batch_pdt_geo, batch_tgt):
|
||||
""" Computes the VAE loss function.
|
||||
pramas:
|
||||
batch_tgt, (nbatch, seqlen, dim_motion)
|
||||
batch_pdt_geo, (nbatch, seqlen, num_vertices, 3)
|
||||
"""
|
||||
|
||||
loss_dict = {}
|
||||
|
||||
# get mesh geometry
|
||||
batch_tgt_geo = self.get_vertices(batch_tgt) # B x nseq x num_vertices x 3
|
||||
|
||||
# synthesis geo rec
|
||||
loss_rec_geo = self.loss_func_wing(batch_pdt_geo, batch_tgt_geo)
|
||||
loss_dict["rec_geo"] = loss_rec_geo.mean().detach()
|
||||
|
||||
loss_rec_geo2 = self.loss_func_wing(batch_pdt_geo, batch_tgt_geo, weight=self.vertices_weight)
|
||||
loss_dict["rec_geo_w"] = loss_rec_geo2.mean().detach()
|
||||
|
||||
# synthesis geo volocity
|
||||
diff_pdt_geo = batch_pdt_geo[:, 1:, :] - batch_pdt_geo[:, :-1, :]
|
||||
diff_tgt_geo = batch_tgt_geo[:, 1:, :] - batch_tgt_geo[:, :-1, :]
|
||||
|
||||
loss_vel_geo = self.loss_func_wing(diff_pdt_geo, diff_tgt_geo)
|
||||
loss_dict["vel_geo"] = loss_vel_geo.mean().detach()
|
||||
|
||||
loss_vel_geo2 = self.loss_func_wing(diff_pdt_geo, diff_tgt_geo, weight=self.vertices_weight)
|
||||
loss_dict["vel_geo_w"] = loss_vel_geo2.mean().detach()
|
||||
|
||||
|
||||
|
||||
# closure loss
|
||||
diff_pdf_closure = batch_pdt_geo[:,:,flame_mouth_upper_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx) \
|
||||
- batch_pdt_geo[:,:,flame_mouth_lower_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx)
|
||||
|
||||
diff_tgt_closure = batch_tgt_geo[:,:,flame_mouth_upper_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx) \
|
||||
- batch_tgt_geo[:,:,flame_mouth_lower_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx)
|
||||
|
||||
loss_closure = self.loss_func_wing(diff_pdf_closure, diff_tgt_closure)
|
||||
loss_dict["closure"] = loss_closure.mean().detach()
|
||||
|
||||
if self.opt.LOSS.lambda_closure > 0:
|
||||
loss += loss_closure.mean() * self.opt.LOSS.lambda_closure
|
||||
|
||||
# total loss
|
||||
loss_dict["all"] = loss.clone().detach()
|
||||
|
||||
# return
|
||||
return loss, loss_dict
|
||||
|
||||
|
||||
def eval_losses(self, batch_pdt_geo, batch_tgt):
|
||||
""" Computes the VAE loss function.
|
||||
pramas:
|
||||
@@ -422,12 +435,11 @@ class Audio2MeshModel(Base):
|
||||
batch_tgt_geo = self.get_vertices(batch_tgt) # B x nseq x num_vertices x 3
|
||||
|
||||
# synthesis geo rec
|
||||
loss = 0
|
||||
loss_rec_geo = self.loss_func_wing(batch_pdt_geo, batch_tgt_geo)
|
||||
loss_dict["loss_rec_geo"] = loss_rec_geo.mean().detach()
|
||||
loss_dict["rec_geo"] = loss_rec_geo.mean().detach()
|
||||
|
||||
loss_rec_geo2 = self.loss_func_wing(batch_pdt_geo, batch_tgt_geo, weight=self.vertices_weight)
|
||||
loss_dict["loss_rec_geo_w"] = loss_rec_geo2.mean().detach()
|
||||
loss_dict["rec_geo_w"] = loss_rec_geo2.mean().detach()
|
||||
# loss += loss_rec_geo.mean() * self.opt.LOSS.lambda_rec_geo
|
||||
loss += loss_rec_geo2.mean() * self.opt.LOSS.lambda_rec_geo
|
||||
|
||||
@@ -436,17 +448,33 @@ class Audio2MeshModel(Base):
|
||||
diff_tgt_geo = batch_tgt_geo[:, 1:, :] - batch_tgt_geo[:, :-1, :]
|
||||
|
||||
loss_vel_geo = self.loss_func_wing(diff_pdt_geo, diff_tgt_geo)
|
||||
loss_dict["loss_vel_geo"] = loss_vel_geo.mean().detach()
|
||||
loss_dict["vel_geo"] = loss_vel_geo.mean().detach()
|
||||
|
||||
loss_vel_geo2 = self.loss_func_wing(diff_pdt_geo, diff_tgt_geo, weight=self.vertices_weight)
|
||||
loss_dict["loss_vel_geo_w"] = loss_vel_geo2.mean().detach()
|
||||
loss_dict["vel_geo_w"] = loss_vel_geo2.mean().detach()
|
||||
# loss += loss_vel_geo.mean() * self.opt.LOSS.lambda_vel_geo
|
||||
|
||||
if self.opt.LOSS.lambda_vel_geo:
|
||||
loss += loss_vel_geo2.mean() * self.opt.LOSS.lambda_vel_geo
|
||||
# total loss
|
||||
loss_dict['loss_all'] = loss.detach()
|
||||
|
||||
|
||||
# closure loss
|
||||
diff_pdf_closure = batch_pdt_geo[:,:,flame_mouth_upper_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx) \
|
||||
- batch_pdt_geo[:,:,flame_mouth_lower_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx)
|
||||
|
||||
diff_tgt_closure = batch_tgt_geo[:,:,flame_mouth_upper_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx) \
|
||||
- batch_tgt_geo[:,:,flame_mouth_lower_idx].sum(dim=[2,3]) / len(flame_mouth_upper_idx)
|
||||
|
||||
loss_closure = self.loss_func_wing(diff_pdf_closure, diff_tgt_closure)
|
||||
loss_dict["closure"] = loss_closure.mean().detach()
|
||||
|
||||
if self.opt.LOSS.lambda_closure > 0:
|
||||
loss += loss_closure.mean() * self.opt.LOSS.lambda_closure
|
||||
|
||||
# total loss
|
||||
loss_dict["all"] = loss.clone().detach()
|
||||
|
||||
# return
|
||||
return loss, loss_dict
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user