diff --git a/experiments/audio2motion/train_audio2motion_CN_actor6_20220715.yaml b/experiments/audio2motion/train_audio2motion_CN_actor6_20220715.yaml new file mode 100644 index 0000000..7345114 --- /dev/null +++ b/experiments/audio2motion/train_audio2motion_CN_actor6_20220715.yaml @@ -0,0 +1,89 @@ +GPUS: '1' +WORKERS: 8 +OUTPUT_DIR: 'experiments/audio2motion' + +DEBUG: False +verbose: False + +FREQ: + epoch_save: 5 + epoch_valid: 1 + batch_print: 20 + epoch_test: 5 + +MODEL: + name: 'faceformer' + name_netG: "faceformer" + + dim_motion: 53 # 68*2 + dim_speech: 1024 + num_vertices: 5023 + dim_hidden: 128 + + pretrain: False + path_checkpoint: "experiments/audio2motion/train_audio2motion_CN_20220525/20220602-11H29M18S" + load_epoch: 15 + + + +DATASET: + fps: 50 + sr: 16000 + name_train: "motion_live" + name_valid: "motion_eval" + flip: False + shifting_speed: True + + root: './data/Actor6/Actor6-raw-bg-30m-processing2-2h' + path_mean_dict: './data/Actor6/mean_deca_coeff_Actor6_processing2.npy' + + filter: 'savgol' + speech_extractor: 'wav2vec' + + nwind_second_train: 3 + nwind_second_valid: 3 + nwind_second_test: 15 + +TEST: + batch_size: 2 + + +TRAIN: + lr: 0.0001 #0.0001 + batch_size: 128 + shuffle: True + + apply_testing: True + apply_validation: True + apply_lr_scheduler: True + apply_update_parameters: True + + optimizer: 'Adam' + momentum: 0.9 + beta1: 0.5 + beta2: 0.999 + weight_decay: 0.0001 + + nesterov: True + lr_policy: 'plateau' + + # lr_step_gamma: 0.5 + # lr_policy: 'multistep' + # lr_milestones: [10, 50, 100] + + resume: False + reset_lr: True + resume_suffix: "20220524-21H54M38S" + epoch_begin: 0 # 5000 # 2300 # 2600 + epoch_end: 300 + + +LOSS: + names: ["rec_exp","rec_pos", "rec_geo", "vel_exp","vel_pos", "vel_geo", "all"] + lambda_rec_exp: 0.5 + lambda_rec_pos: 1 + lambda_rec_geo: 100 + lambda_vel_exp: 10 + lambda_vel_pos: 10 + lambda_vel_geo: 100 + diff --git a/src/config.py b/src/config.py index 37beccc..ced4e38 100644 --- a/src/config.py +++ b/src/config.py @@ -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" diff --git a/src/models/model_faceformer.py b/src/models/model_faceformer.py index c8ac2b3..ac17d91 100644 --- a/src/models/model_faceformer.py +++ b/src/models/model_faceformer.py @@ -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}') diff --git a/src/nn/faceformer.py b/src/nn/faceformer.py index 1dcce2d..3dccc01 100644 --- a/src/nn/faceformer.py +++ b/src/nn/faceformer.py @@ -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)