This commit is contained in:
liangsen committed 2022-07-14 19:50:48 +08:00
1 parent ff84e9d0b0
commit 2ecd45d991
4 files changed
+130 -32

No files matched your search

-4
View File
@@ -1,7 +1,4 @@
import os
from django import conf
import yaml
import numpy as np
from easydict import EasyDict as edict
@@ -19,7 +16,6 @@ config.isTrain = True
config.preprocess = ''
config.verbose = True
config.FLAME = edict()
config.FLAME.flame_model_path = "./data/mesh/generic_model.pkl"
config.FLAME.flame_lmk_embedding_path = "./data/mesh/landmark_embedding.npy"
+19 -8
View File
@@ -46,6 +46,8 @@ class FaceFormerModel(Base):
self.mean_dict = np.load(path_deca_mean, allow_pickle=True).item()
self.logger.info(f"load mean deca coeff from > {path_deca_mean}, \nkeys = {self.mean_dict.keys()}")
self.temp_coeff = self.get_template_coeff(self.mean_dict) # (1, 1, 53)
# get template
self.temp_verts, self.temp_ldmk2d, self.temp_ldmk3d \
= self.get_template_vertices() # verts: (1 x 1 x 5023 x 3), ldmk: (1 x 1 x 68 x 3)
@@ -65,7 +67,6 @@ class FaceFormerModel(Base):
pickle.dump(template_mesh, f)
self.loss_func_l1 = nn.L1Loss(reduction="mean")
self.loss_func_wing = WingLoss(reduction="mean")
self.names_loss = opt.LOSS.names
@@ -83,6 +84,18 @@ class FaceFormerModel(Base):
self.scheduler_G = get_scheduler(self.optimizer_G, opt)
self.schedulers.append(self.scheduler_G)
def get_template_coeff(self, mean_dict):
data_exp = mean_dict["exp"].reshape(1, 1, 50)
data_pos = mean_dict["pose"].reshape(1, 1, 6)[:, :, 3:]
template_motion = np.concatenate([data_exp, data_pos], axis=2) # (1, 1, 53)
template_motion = torch.from_numpy(template_motion).float().to(self.device)
self.logger.info(f"template_motion = {template_motion.shape}")
return template_motion
@torch.no_grad()
def set_input(self, batch_input):
@@ -136,7 +149,7 @@ class FaceFormerModel(Base):
debug(self.opt, f"[set_input] ldmk2d = {self.ldmk2d.shape}, dtype={self.ldmk2d.dtype}, device={self.ldmk2d.device}", verbose=False)
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()
@@ -155,11 +168,10 @@ class FaceFormerModel(Base):
B, N, *_ = self.cond.size()
batch_x = self.cond # (B, num_x, 16, 1024)
batch_y = self.data # (B, num_y, 53)
template_y = self.template_data # (B, 53)
# one_hot = self.one_hot # (B, M)
batch_x = self.cond # (B, num_x, 16, 1024)
batch_y = self.data # (B, num_y, 53)
template_y = self.temp_coeff.repeat(B, 1,1) # (B, 1, 53)
# one_hot = self.one_hot # (B, M)
# forward
pred = self.netG(batch_x, batch_y, template_y) # (B, N, 53)
@@ -267,7 +279,6 @@ class FaceFormerModel(Base):
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}")
self.save_reconstruct_with_gt(batch_pdt_geo, self.vert, self.raw_speech,
path_save=Path(self.opt.OUTPUT_DIR),
suffix=f'sampled_{idx_epoch:05d}_{idx_batch:03d}')
+22 -20
View File
@@ -13,13 +13,15 @@ class FaceFormer(nn.Module):
self.dim_motion = opt.MODEL.dim_motion # 53
self.dim_speech = opt.MODEL.dim_speech # 29
self.dim_hidden = opt.MODEL.dim_hidden # 256
self.period = 1*50 # 1s
self.max_seq_len = 20 * 50 # 20s
# object header
self.subject_head = nn.Linear(opt.num_train_subjects, opt.feature_dim, bias=False)
# self.subject_head = nn.Linear(opt.num_train_subjects, self.dim_hidden, bias=False)
# audio header
# self.speech_head = nn.Linear(768, opt.feature_dim)
# self.speech_head = nn.Linear(768, self.dim_hidden)
self.speech_head = nn.Sequential( # (nbatch, nchannel=29, H=16, W=1)
nn.BatchNorm2d(self.dim_speech),
@@ -44,23 +46,23 @@ class FaceFormer(nn.Module):
nn.Linear(in_features=128, out_features=1024), # 128,
nn.Tanh(),
nn.Linear(in_features=1024, out_features=opt.feature_dim) # 50,
nn.Linear(in_features=1024, out_features=self.dim_hidden) # 50,
)
# batch_y
self.motion_head = nn.Linear(opt.vertice_dim, opt.feature_dim)
self.motion_tail = nn.Linear(opt.feature_dim, opt.vertice_dim)
self.motion_head = nn.Linear(self.dim_motion, self.dim_hidden)
self.motion_tail = nn.Linear(self.dim_hidden, self.dim_motion)
# positional encoding
self.PPE = PeriodicPositionalEncoding(opt.feature_dim, period = opt.period)
self.PPE = PeriodicPositionalEncoding(self.dim_hidden, period = self.period)
# Transformer
_decoder_layer = nn.TransformerDecoderLayer(d_model=opt.feature_dim, nhead=4, dim_feedforward=2*opt.feature_dim, batch_first=True)
_decoder_layer = nn.TransformerDecoderLayer(d_model=self.dim_hidden, nhead=4, dim_feedforward=2*self.dim_hidden, batch_first=True)
self.decoder = nn.TransformerDecoder(_decoder_layer, num_layers=1)
# init temporal bias
self.biased_mask = self.generate_biased_mask(n_head = 4, max_seq_len = 600, period=opt.period)
self.biased_mask = self.generate_biased_mask(n_head = 4, max_seq_len = self.max_seq_len, period=self.period)
# init weigt
nn.init.constant_(self.motion_tail.weight, 0)
@@ -70,7 +72,7 @@ class FaceFormer(nn.Module):
def generate_mask(self, T, S):
mask = torch.ones(T, S) # T x S
for i in range(T):
mask[i, i] = 0 # 60fps audio --> 60fps frame: 1:1
mask[i, i] = 0 # 50fps audio --> 50fps frame: 1:1
# convert to True or false
mask = (mask==1).to(device=self.device)
@@ -118,13 +120,13 @@ class FaceFormer(nn.Module):
def forward(self, batch_x, batch_y, template_y, one_hot, teacher_forcing=False):
def forward(self, batch_x, batch_y, template_y, teacher_forcing=False):
""" forward function
Args:
batch_x: (B, num_x, dim_condit=1024]
batch_y: (B, num_y, dim_motion=3V]
template_y: (B, dim_motion=3V]
onehot: (B, M subjects]
template_y: (B, 1, dim_motion]
# onehot: (B, M subjects]
return:
pred: [B x num_y x dim_motion]
@@ -147,20 +149,20 @@ class FaceFormer(nn.Module):
x_feat = self.speech_head(x) # (B * num_x, dim_feat)
x_feat = x_feat.reshape(B, num_x, -1) # (B, num_x, dim_feat)
# subject embedding
embedding = self.subject_head(one_hot) # (B x dim_feat)
embedding = embedding.unsqueeze(1) # (B x 1 x dim_feat)
# # subject embedding
# embedding = self.subject_head(one_hot) # (B x dim_feat)
# embedding = embedding.unsqueeze(1) # (B x 1 x dim_feat)
# teacher forcing
if teacher_forcing:
embedding_style = embedding # (B, 1, dim_feat)
# embedding_style = embedding # (B, 1, dim_feat)
# GT right shift && add template
y = torch.cat([template_y, batch_y[:, :-1]]) # (B, num_y, dim_motion)
y = y - template_y # - mean
feat_y = self.motion_head(y) # (B x num_y x dim_feat)
feat_y += embedding_style # add style encoding
# feat_y += embedding_style # add style encoding
feat_y = self.PPE(feat_y) # (B x num_y x dim_feat)
# prepare mask
@@ -178,7 +180,7 @@ class FaceFormer(nn.Module):
inum_y = i+1
ifeat_y = self.motion_head(pred[:, :inum_y, :]) # (B, inum_y, dim_feat)
ifeat_y += embedding # add style encoding
# ifeat_y += embedding # add style encoding
ifeat_y = self.PPE(ifeat_y) # (B, inum_y, dim_feat)