add soft/noisey label for GAN loss

This commit is contained in:
sen.liang committed 2022-06-10 15:03:02 +08:00
1 parent 5a6725072a
commit 6fe3a2c3cd
6 files changed
+32 -14

No files matched your search

@@ -48,7 +48,7 @@ model:
path_mean_dict: ${path_data}/mean_deca_coeff_Actor6_processing2.npy path_mean_dict: ${path_data}/mean_deca_coeff_Actor6_processing2.npy
pretrain: True pretrain: True
pretrain_path: ${path_cwd}/logs/experiments/runs/audio2motion/2022-06-09_17-19-57/checkpoints/epoch_009-v1.ckpt pretrain_path: ${path_cwd}/logs/experiments/runs/audio2motion/2022-06-09_21-35-54/checkpoints/epoch_014.ckpt
opt_optim: opt_optim:
lr_G: 0.0005 # 0.00125 lr_G: 0.0005 # 0.00125
@@ -49,12 +49,13 @@ model:
path_mean_dict: ${path_data}/mean_deca_coeff_Actor7_processing.npy path_mean_dict: ${path_data}/mean_deca_coeff_Actor7_processing.npy
pretrain: True pretrain: True
pretrain_path: ${path_cwd}/logs/experiments/runs/audio2motion/2022-06-09_17-19-57/checkpoints/epoch_009-v1.ckpt pretrain_path: ${path_cwd}/logs/experiments/runs/audio2motion/2022-06-09_17-19-57/checkpoints/epoch_040.ckpt
opt_optim: opt_optim:
lr_G: 0.0005 # 0.00125 lr_G: 0.0005 # 0.00125
lr_D: 0.0005 # 0.00125 lr_D: 0.0005 # 0.00125
opt_loss: opt_loss:
lambda_rec_exp: 0.5 lambda_rec_exp: 0.5
lambda_rec_pos: 1 lambda_rec_pos: 1
@@ -15,7 +15,7 @@ if __name__ == "__main__":
sub_folder = "processing2" # processing, smile, body sub_folder = "processing2" # processing, smile, body
path_data = Path(f"/ssdata1/lsen/DATA/{actor}/{sub_folder}") path_data = Path(f"/ssdata1/lsen/DATA/{actor}/{sub_folder}")
path_save = Path(f'data/{actor}/{actor}-raw-{sub_folder}-2h') path_save = Path(f'data/{actor}/{actor}-raw-{sub_folder}-10min')
path_save.mkdir(parents=True, exist_ok=True) path_save.mkdir(parents=True, exist_ok=True)
# set logging # set logging
@@ -118,6 +118,9 @@ if __name__ == "__main__":
# #################################### # ####################################
if vid in vid_train: if vid in vid_train:
# if durations_train / 3600 > 6: break
if durations_train / 60 > 10: continue
durations_train += nframes/fps durations_train += nframes/fps
list_train.append(vid) list_train.append(vid)
+23 -10
View File
@@ -1,27 +1,40 @@
import torch import torch
import torch.nn.functional as F import torch.nn.functional as F
import logging
def get_label(pred, target_is_real, soft_and_noisy=False):
if target_is_real:
if not soft_and_noisy:
return torch.ones_like(pred) # all 1
else:
# 0~1 uniform [0,1) , NOTE: not randn_like, which is not [0,1)
# [0,1) --> [0,0.5) --> [0.5, 1.0)
return torch.rand_like(pred) * 0.5 + 0.5
else:
if not soft_and_noisy:
return torch.zeros_like(pred) # all 0
else:
# 0~1 uniform [0,1) , NOTE: not randn_like, which is not [0,1)
# [0,1) --> [0,0.3)
return torch.rand_like(pred) * 0.3
def adversarial_loss(pred, target_is_real=True, loss_mode="vanilla"): def adversarial_loss(pred, target_is_real=True, loss_mode="vanilla"):
target = get_label(pred, target_is_real, soft_and_noisy=True)
# logging.info(f"[validation] target ={target.shape}, target_is_real={target_is_real}")
if loss_mode == "vanilla": if loss_mode == "vanilla":
if target_is_real:
target = torch.ones_like(pred)
else:
target = torch.zeros_like(pred)
return F.binary_cross_entropy_with_logits(pred, target) return F.binary_cross_entropy_with_logits(pred, target)
elif loss_mode == "lsgan": elif loss_mode == "lsgan":
if target_is_real:
target = torch.ones_like(pred)
else:
target = torch.zeros_like(pred)
return F.mse_loss(pred, target) return F.mse_loss(pred, target)
elif loss_mode == "hinge": elif loss_mode == "hinge":
if target_is_real: if target_is_real:
loss = torch.maximum(1-pred, torch.ones_like(pred)).mean() loss = torch.maximum(1-pred, target).mean()
else: else:
loss = torch.maximum(1+pred, torch.zeros_like(pred)).mean() loss = torch.maximum(1+pred, target).mean()
return loss return loss
else: else:
@@ -49,9 +49,11 @@ class Generator(nn.Module):
self.decoder = nn.Sequential( self.decoder = nn.Sequential(
nn.Linear(in_features=128 + dim_z, out_features=2048), # 2048, nn.Linear(in_features=128 + dim_z, out_features=2048), # 2048,
nn.LeakyReLU(0.02, True), nn.LeakyReLU(0.02, True),
nn.Dropout(0.3),
nn.Linear(in_features=2048, out_features=128), # 128, nn.Linear(in_features=2048, out_features=128), # 128,
nn.LeakyReLU(0.02, True), nn.LeakyReLU(0.02, True),
nn.Dropout(0.3),
nn.Linear(in_features=128, out_features=dim_x), # dim_x nn.Linear(in_features=128, out_features=dim_x), # dim_x
) )
-1
View File
@@ -373,7 +373,6 @@ class Audio2MotionGANModel(LightningModule):
batch_z = torch.randn(B, N, self.opt.dim_z).type_as(batch_c) # (B, dim_latent) batch_z = torch.randn(B, N, self.opt.dim_z).type_as(batch_c) # (B, dim_latent)
batch_pdt = self.netG(batch_z, batch_c) #(B, N, nc_motion) batch_pdt = self.netG(batch_z, batch_c) #(B, N, nc_motion)
################################################## ##################################################
######### Discriminator ######### ######### Discriminator #########
# GT label # GT label