import torch import torch.nn as nn from common.debug import debug class Audio2Mesh(nn.Module): def __init__(self, opt): super(Audio2Mesh, self).__init__() self.opt = opt self.num_vertices = opt.MODEL.num_vertices self.dim_coef = opt.MODEL.dim_motion # 53 self.dim_mesh = 3 * self.num_vertices # 3 * 5023 self.dim_cond = opt.MODEL.dim_speech # 1024 # encoder self.encoder = nn.Sequential( # (nbatch, nchannel=1024, H=16, W=1) nn.BatchNorm2d(self.dim_cond), # block 1 nn.Conv2d(self.dim_cond, 32, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 32 x 8 x 1 nn.LeakyReLU(0.02, True), nn.Conv2d(32, 32, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 32 x 4 x 1 nn.LeakyReLU(0.02, True), nn.Conv2d(32, 64, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 64 x 2 x 1 nn.LeakyReLU(0.02, True), nn.Conv2d(64, 64, kernel_size=(3,1), stride=(2,1), padding=(1,0), bias=True), # 64 x 1 x 1 nn.LeakyReLU(0.02, True), nn.Flatten(), # 64, nn.Linear(in_features=64, out_features=128), # 128, nn.Tanh(), nn.Linear(in_features=128, out_features=50) # 50, ) # output FLAME mesh self.decoder_mesh = nn.Linear(in_features=50, out_features=self.dim_mesh) def forward(self, x): """ forward function Args: x: torch.Tensor, [nbatch x seqlen x nwind x dim_condit] return: pred: torch.Tensor, [nbatch x seqlen x dim_coef] """ (nbatch, nseqlen, nwind, nfeat) = x.shape # (B, nseq, 16, 29) debug(self.opt, f"[forward] input x shape = {x.shape}", verbose=False) # preprocessing data x = x.reshape(nbatch * nseqlen, 1, nwind, nfeat) # (B * nseq, 1, 16, 29) x = torch.transpose(x, 1, 3) # (B * nseq, 29, 16, 1) debug(self.opt, f"[forward] transpose x shape = {x.shape}", verbose=False) feat = self.encoder(x) # (B * nseq, 50) debug(self.opt, f"[forward] encoder shape = {feat.shape}", verbose=False) # predict FLAME mesh pred_vert = self.decoder_mesh(feat) # (B * nseq, dim_mesh) debug(self.opt, f"[forward] decoder mesh shape = {pred_vert.shape}", verbose=False) pred_vert = pred_vert.reshape(nbatch, nseqlen, self.num_vertices, 3) # (B, nseq, num_vertices, 3) debug(self.opt, f"[forward] reshape shape = {pred_vert.shape}", verbose=False) return pred_vert