Files
audio-driven-face-animation/src/models/components/networks_audio2motion_gan.py
T
2022-06-09 16:31:36 +08:00

191 lines
7.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import torch
import torch.nn as nn
import logging
class Generator(nn.Module):
def __init__(self, dim_x, dim_c, dim_z):
super(Generator, self).__init__()
nc_input = dim_z + dim_c
# creat model
self.encoder = nn.Sequential( # (nbatch, nchannel=1024 H=16, W=1)
nn.BatchNorm2d(nc_input),
# block 1
nn.Conv2d(nc_input, 512, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 512, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 512, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 32 x 4 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 512, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 64 x 2 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 64 x 1 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 128, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 128 x 1 x 1
nn.LeakyReLU(0.02, True),
nn.Flatten(), # 128,
)
# output
self.decoder = nn.Sequential(
nn.Linear(in_features=128 + dim_z, out_features=2048), # 2048,
nn.LeakyReLU(0.02, True),
nn.Linear(in_features=2048, out_features=128), # 128,
nn.LeakyReLU(0.02, True),
nn.Linear(in_features=128, out_features=dim_x), # dim_x
)
def forward(self, c, z):
""" forward function
Args:
c, motion data, (B, nseqs, 16, dim_c=1024)
z, randon noise, (B, dim_z=32)
return:
(B x dim_x)
"""
logging.debug(f"[forward] input x shape = {c.shape}")
# remember high dimenstion
shape_dims = c.shape
if len(shape_dims) > 3:
c = c.reshape(-1, c.shape[-2], c.shape[-1]) # (BN, nwind, dim_c)
# add noise
z_expand = z.unsqueeze(dim=1).repeat([shape_dims[1], shape_dims[2], 1]) # (BN, nwind, dim_z)
c = torch.cat([c, z_expand], dim=2) # (BN, nwind, dim_z + nc_c)
# preprocessing data
c = c.unsqueeze(dim=1).transpose(1,3) # (B, 1, W, dim_x) --> (B, dim_x, W, 1)
logging.debug(f"[forward] transpose c shape = {c.shape}")
feat = self.encoder(c) # (B, 50)
logging.debug(f"[forward] encoder shape = {feat.shape}")
# add noise again
feat = torch.cat([feat, z], dim=1) # (B, 128 + dim_z)
logging.debug(f"[forward] decoder shape = {pred.shape}")
pred = self.decoder(feat) # (B, dim_x)
logging.debug(f"[forward] decoder shape = {pred.shape}")
# recover high dimension
if len(shape_dims) > 3:
pred = pred.reshape(list(shape_dims[:-2]) + list(pred.shape[1:]))
logging.debug(f"[forward] reshape shape = {pred.shape}")
return pred
class Discriminator(torch.nn.Module):
def __init__(self, dim_x, dim_c):
super(Discriminator, self).__init__()
# creat model
self.encoder = nn.Sequential( # (nbatch, nchannel=1024 H=16, W=1)
nn.BatchNorm2d(dim_c),
# block 1
nn.Conv2d(dim_c, 512, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 512, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 512, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 32 x 4 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 512, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(512, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 64 x 2 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 64 x 1 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 256, kernel_size=(3,1), stride=(1,1), padding=(1,0), bias=True), # 32 x 8 x 1
nn.LeakyReLU(0.02, True),
nn.Conv2d(256, 128, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 128 x 1 x 1
nn.LeakyReLU(0.02, True),
nn.Flatten(), # 128,
)
# output
self.decoder = nn.Sequential(
nn.Linear(in_features=128 + dim_x, out_features=2048), # 2048,
nn.LeakyReLU(0.02, True),
nn.Linear(in_features=2048, out_features=128), # 128,
nn.LeakyReLU(0.02, True),
nn.Linear(in_features=128, out_features=16), # 16
)
def forward(self, x, c):
"""
x, (B, N, dim_x=53)
c, (B, N, 16, dim_c=1024)
"""
""" forward function
Args:
x, motion data, (B, N, W, dim_c=1024)
z, randon noise, (B, dim_z=32)
return:
(B x dim_x)
"""
logging.debug(f"[forward] input x shape = {x.shape}")
# remember high dimenstion
c = c.reshape(-1, c.shape[-2], c.shape[-1]) # (BN, W, dim_c)
# preprocessing data
c = c.unsqueeze(dim=1).transpose(1,3) # (BN, 1, W, dim_x) --> (B, dim_x, W, 1)
feat = self.encoder(x) # (BN, 128)
logging.debug(f"[forward] encoder shape = {feat.shape}")
# add noise again
feat = torch.cat([feat, x], dim=1) # (BN, 128 + dim_x)
pred = self.decoder(feat) # (BN, 16)
logging.debug(f"[forward] decoder shape = {pred.shape}")
return pred