training ok
This commit is contained in:
1 parent
2ecd45d991
commit
2cba31e6bc
3 files changed
+102
-78
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
GPUS: '1'
|
GPUS: '0'
|
||||||
WORKERS: 8
|
WORKERS: 8
|
||||||
OUTPUT_DIR: 'experiments/audio2motion'
|
OUTPUT_DIR: 'experiments/audio2motion'
|
||||||
|
|
||||||
@@ -6,10 +6,10 @@ DEBUG: False
|
|||||||
verbose: False
|
verbose: False
|
||||||
|
|
||||||
FREQ:
|
FREQ:
|
||||||
epoch_save: 5
|
epoch_save: 1
|
||||||
epoch_valid: 1
|
epoch_valid: 1
|
||||||
batch_print: 20
|
batch_print: 10
|
||||||
epoch_test: 5
|
epoch_test: 1
|
||||||
|
|
||||||
MODEL:
|
MODEL:
|
||||||
name: 'faceformer'
|
name: 'faceformer'
|
||||||
@@ -50,7 +50,7 @@ TEST:
|
|||||||
|
|
||||||
TRAIN:
|
TRAIN:
|
||||||
lr: 0.0001 #0.0001
|
lr: 0.0001 #0.0001
|
||||||
batch_size: 128
|
batch_size: 16
|
||||||
shuffle: True
|
shuffle: True
|
||||||
|
|
||||||
apply_testing: True
|
apply_testing: True
|
||||||
|
|||||||
@@ -169,15 +169,16 @@ class FaceFormerModel(Base):
|
|||||||
B, N, *_ = self.cond.size()
|
B, N, *_ = self.cond.size()
|
||||||
|
|
||||||
batch_x = self.cond # (B, num_x, 16, 1024)
|
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)
|
template_y = self.temp_coeff.repeat(B, 1,1) # (B, 1, 53)
|
||||||
# one_hot = self.one_hot # (B, M)
|
batch_y = self.data - template_y # (B, num_y, 53)
|
||||||
|
|
||||||
# forward
|
# forward
|
||||||
pred = self.netG(batch_x, batch_y, template_y) # (B, N, 53)
|
pred = self.netG(batch_x, batch_y, template_y) # (B, N, 53)
|
||||||
|
pred += template_y
|
||||||
|
|
||||||
|
|
||||||
# loss
|
# loss
|
||||||
loss, loss_dict = self.eval_losses(pred, batch_y)
|
loss, loss_dict = self.eval_losses(pred, self.data)
|
||||||
|
|
||||||
# backward
|
# backward
|
||||||
self.optimizer_G.zero_grad()
|
self.optimizer_G.zero_grad()
|
||||||
@@ -195,13 +196,25 @@ class FaceFormerModel(Base):
|
|||||||
self.eval() # set eval model
|
self.eval() # set eval model
|
||||||
self.set_input(batch_input)
|
self.set_input(batch_input)
|
||||||
|
|
||||||
with torch.no_grad():
|
|
||||||
pred = self.netG(self.cond)
|
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
B, N, *_ = self.cond.size()
|
||||||
|
|
||||||
|
batch_x = self.cond # (B, num_x, 16, 1024)
|
||||||
|
template_y = self.temp_coeff.repeat(B, 1,1) # (B, 1, 53)
|
||||||
|
batch_y = self.data - template_y # (B, num_y, 53)
|
||||||
|
|
||||||
|
# forward
|
||||||
|
pred = self.netG(batch_x, batch_y, template_y) # (B, N, 53)
|
||||||
|
pred += template_y
|
||||||
|
|
||||||
|
# loss
|
||||||
loss, loss_dict = self.eval_losses(pred, self.data)
|
loss, loss_dict = self.eval_losses(pred, self.data)
|
||||||
|
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
@torch.no_grad()
|
||||||
def infer_with_gt(self, batch_cond, batch_data, batch_speech, suffix="", save_npy=True, eval_loss=True):
|
def infer_with_gt(self, batch_cond, batch_data, batch_speech, suffix="", save_npy=True, eval_loss=True):
|
||||||
self.eval()
|
self.eval()
|
||||||
@@ -269,12 +282,22 @@ class FaceFormerModel(Base):
|
|||||||
self.logger.info(f"save infer_result shape = {batch_pdt.shape} -> {path_save_npy}")
|
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):
|
def testing(self, batch_input, idx_epoch=0, idx_batch=0):
|
||||||
self.eval() # set eval model
|
self.eval() # set eval model
|
||||||
self.set_input(batch_input)
|
self.set_input(batch_input)
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
batch_pdt = self.netG(self.cond) # B x N x dim_motion
|
|
||||||
|
B, N, *_ = self.cond.size()
|
||||||
|
|
||||||
|
batch_x = self.cond # (B, num_x, 16, 1024)
|
||||||
|
template_y = self.temp_coeff.repeat(B, 1,1) # (B, 1, 53)
|
||||||
|
batch_y = self.data - template_y # (B, num_y, 53)
|
||||||
|
|
||||||
|
# forward
|
||||||
|
batch_pdt = self.netG(batch_x, batch_y, template_y) # (B, N, 53)
|
||||||
|
batch_pdt += template_y
|
||||||
|
|
||||||
batch_pdt_geo, *_ = self.get_vertices(batch_pdt) # B x N x 5023 x 3
|
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.logger.info(f"testing batch_pdt shape = {batch_pdt.shape}, batch_pdt_geo = {batch_pdt_geo.shape}")
|
||||||
|
|||||||
+68
-67
@@ -64,61 +64,6 @@ class FaceFormer(nn.Module):
|
|||||||
# init temporal bias
|
# init temporal bias
|
||||||
self.biased_mask = self.generate_biased_mask(n_head = 4, max_seq_len = self.max_seq_len, period=self.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)
|
|
||||||
nn.init.constant_(self.motion_tail.bias, 0)
|
|
||||||
|
|
||||||
|
|
||||||
def generate_mask(self, T, S):
|
|
||||||
mask = torch.ones(T, S) # T x S
|
|
||||||
for i in range(T):
|
|
||||||
mask[i, i] = 0 # 50fps audio --> 50fps frame: 1:1
|
|
||||||
|
|
||||||
# convert to True or false
|
|
||||||
mask = (mask==1).to(device=self.device)
|
|
||||||
|
|
||||||
return mask
|
|
||||||
|
|
||||||
|
|
||||||
def generate_biased_mask(self, n_head, max_seq_len, period):
|
|
||||||
"""
|
|
||||||
# Temporal Bias, inspired by ALiBi: https://github.com/ofirpress/attention_with_linear_biases
|
|
||||||
"""
|
|
||||||
def get_slopes(n):
|
|
||||||
def get_slopes_power_of_2(n):
|
|
||||||
start = (2**(-2**-(math.log2(n)-3)))
|
|
||||||
ratio = start
|
|
||||||
return [start*ratio**i for i in range(n)]
|
|
||||||
|
|
||||||
if math.log2(n).is_integer():
|
|
||||||
return get_slopes_power_of_2(n)
|
|
||||||
else:
|
|
||||||
closest_power_of_2 = 2**math.floor(math.log2(n))
|
|
||||||
return get_slopes_power_of_2(closest_power_of_2) + get_slopes(2*closest_power_of_2)[0::2][:n-closest_power_of_2]
|
|
||||||
|
|
||||||
slopes = torch.Tensor(get_slopes(n_head))
|
|
||||||
print(f"slops = {slopes}")
|
|
||||||
|
|
||||||
bias = torch.arange(start=0, end=max_seq_len, step=period).unsqueeze(1).repeat(1,period).view(-1)//(period)
|
|
||||||
bias = - torch.flip(bias,dims=[0])
|
|
||||||
|
|
||||||
alibi = torch.zeros(max_seq_len, max_seq_len)
|
|
||||||
for i in range(max_seq_len):
|
|
||||||
alibi[i, :i+1] = bias[-(i+1):]
|
|
||||||
|
|
||||||
alibi = slopes.unsqueeze(1).unsqueeze(1) * alibi.unsqueeze(0)
|
|
||||||
|
|
||||||
mask = torch.ones(max_seq_len, max_seq_len)
|
|
||||||
mask = (torch.triu(mask) == 1).transpose(0, 1)
|
|
||||||
|
|
||||||
mask = mask.float()\
|
|
||||||
.masked_fill(mask == 0, float('-inf'))\
|
|
||||||
.masked_fill(mask == 1, float(0.0))
|
|
||||||
mask = mask.unsqueeze(0) + alibi
|
|
||||||
|
|
||||||
return mask
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def forward(self, batch_x, batch_y, template_y, teacher_forcing=False):
|
def forward(self, batch_x, batch_y, template_y, teacher_forcing=False):
|
||||||
""" forward function
|
""" forward function
|
||||||
@@ -162,40 +107,96 @@ class FaceFormer(nn.Module):
|
|||||||
y = y - template_y # - mean
|
y = y - template_y # - mean
|
||||||
|
|
||||||
feat_y = self.motion_head(y) # (B x num_y x dim_feat)
|
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)
|
feat_y = self.PPE(feat_y) # (B x num_y x dim_feat)
|
||||||
|
|
||||||
# prepare mask
|
# prepare mask
|
||||||
tgt_mask = self.biased_mask[:, :num_y, :num_y].clone().to(device=self.device) # biased causal mask
|
tgt_mask = self.biased_mask[:, :num_y, :num_y].clone().to(device=batch_y.device) # biased causal mask
|
||||||
memory_mask = self.generate_mask(num_y, x_feat.shape[1]).to(device=self.device) # biassd memory mask
|
memory_mask = self.generate_mask(num_y, x_feat.shape[1]).to(device=batch_y.device) # biassd memory mask
|
||||||
|
|
||||||
# decoder
|
# decoder
|
||||||
pred = self.decoder(feat_y, x_feat, tgt_mask=tgt_mask, memory_mask=memory_mask)
|
pred = self.decoder(feat_y, x_feat, tgt_mask=tgt_mask, memory_mask=memory_mask)
|
||||||
pred = self.motion_tail(pred) # (B x num_y x dim_motion)
|
pred = self.motion_tail(pred) # (B x num_y x dim_motion)
|
||||||
|
|
||||||
# auto-regression
|
# auto-regression
|
||||||
else:
|
else:
|
||||||
pred = torch.zeros_like(batch_y, requires_grad=True) # (B, num_y, dim_motion)
|
vertice_emb = torch.zeros((B, 1, self.dim_motion)).float().to(batch_y.device)
|
||||||
for i in range(num_y):
|
for i in range(num_y):
|
||||||
|
|
||||||
inum_y = i+1
|
inum_y = i+1
|
||||||
|
debug(self.opt, f"[forward] inum_y = {inum_y}", verbose=False)
|
||||||
|
|
||||||
ifeat_y = self.motion_head(pred[:, :inum_y, :]) # (B, inum_y, dim_feat)
|
|
||||||
# ifeat_y += embedding # add style encoding
|
ifeat_y = self.motion_head(vertice_emb) # (B, inum_y, dim_feat)
|
||||||
|
# ifeat_y += embedding # add style encoding
|
||||||
ifeat_y = self.PPE(ifeat_y) # (B, inum_y, dim_feat)
|
ifeat_y = self.PPE(ifeat_y) # (B, inum_y, dim_feat)
|
||||||
|
debug(self.opt, f"\t [forward] ifeat_y = {ifeat_y.shape}", verbose=False)
|
||||||
|
|
||||||
|
|
||||||
tgt_mask = self.biased_mask[:, :inum_y, :inum_y].clone().to(device=self.device) # biased causal mask
|
tgt_mask = self.biased_mask[:, :inum_y, :inum_y].repeat(B, 1, 1).to(device=batch_y.device) # biased causal mask
|
||||||
memory_mask = self.generate_mask(inum_y, num_x).to(device=self.device) # biassd memory mask
|
memory_mask = self.generate_mask(inum_y, num_x).to(device=batch_y.device) # biassd memory mask
|
||||||
|
debug(self.opt, f"\t [forward] tgt_mask = {tgt_mask.shape}, memory_mask = {memory_mask.shape}", verbose=False)
|
||||||
|
|
||||||
|
|
||||||
# decoder
|
# decoder
|
||||||
ipred = self.decoder(ifeat_y, x_feat, tgt_mask=tgt_mask, memory_mask=memory_mask)
|
ipred = self.decoder(ifeat_y, x_feat, tgt_mask=tgt_mask, memory_mask=memory_mask)
|
||||||
ipred = self.motion_tail(pred) # (B x inum_y x dim_motion)
|
ipred = self.motion_tail(ipred) # (B x inum_y x dim_motion)
|
||||||
|
debug(self.opt, f"\t [forward] ipred = {ipred.shape}", verbose=False)
|
||||||
|
|
||||||
# update
|
# update
|
||||||
pred[:, i, :] = ipred[:, -1:, :]
|
vertice_emb = torch.cat((vertice_emb, ipred[:, -1:, :]), 1) # (B, inum_y+1, dim_motion)
|
||||||
|
|
||||||
|
return ipred # NOTE: not return pred, but last ipred
|
||||||
|
|
||||||
|
|
||||||
return pred
|
def generate_mask(self, T, S):
|
||||||
|
mask = torch.ones(T, S) # T x S
|
||||||
|
for i in range(T):
|
||||||
|
mask[i, i] = 0 # 50fps audio --> 50fps frame: 1:1
|
||||||
|
|
||||||
|
# convert to True or false
|
||||||
|
mask = (mask==1)
|
||||||
|
return mask
|
||||||
|
|
||||||
|
|
||||||
|
def generate_biased_mask(self, n_head, max_seq_len, period):
|
||||||
|
"""
|
||||||
|
# Temporal Bias, inspired by ALiBi: https://github.com/ofirpress/attention_with_linear_biases
|
||||||
|
"""
|
||||||
|
def get_slopes(n):
|
||||||
|
def get_slopes_power_of_2(n):
|
||||||
|
start = (2**(-2**-(math.log2(n)-3)))
|
||||||
|
ratio = start
|
||||||
|
return [start*ratio**i for i in range(n)]
|
||||||
|
|
||||||
|
if math.log2(n).is_integer():
|
||||||
|
return get_slopes_power_of_2(n)
|
||||||
|
else:
|
||||||
|
closest_power_of_2 = 2**math.floor(math.log2(n))
|
||||||
|
return get_slopes_power_of_2(closest_power_of_2) + get_slopes(2*closest_power_of_2)[0::2][:n-closest_power_of_2]
|
||||||
|
|
||||||
|
slopes = torch.Tensor(get_slopes(n_head)) #(4,)
|
||||||
|
print(f"slops = {slopes}")
|
||||||
|
|
||||||
|
bias = torch.arange(start=0, end=max_seq_len, step=period).unsqueeze(1).repeat(1,period).view(-1)//(period)
|
||||||
|
bias = - torch.flip(bias,dims=[0])
|
||||||
|
|
||||||
|
alibi = torch.zeros(max_seq_len, max_seq_len)
|
||||||
|
for i in range(max_seq_len):
|
||||||
|
alibi[i, :i+1] = bias[-(i+1):]
|
||||||
|
|
||||||
|
alibi = slopes.unsqueeze(1).unsqueeze(1) * alibi.unsqueeze(0) # (4, 1, 1) * (1, N, N) --> (4, N, N)
|
||||||
|
|
||||||
|
mask = torch.ones(max_seq_len, max_seq_len)
|
||||||
|
mask = (torch.triu(mask) == 1).transpose(0, 1)
|
||||||
|
|
||||||
|
mask = mask.float()\
|
||||||
|
.masked_fill(mask == 0, float('-inf'))\
|
||||||
|
.masked_fill(mask == 1, float(0.0))
|
||||||
|
mask = mask.unsqueeze(0) + alibi
|
||||||
|
|
||||||
|
return mask
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Periodic Positional Encoding
|
# Periodic Positional Encoding
|
||||||
|
|||||||
Reference in new issue
Block a user