import torch import torch.nn as nn from common.debug import debug class Audio2MotionVOCA3(nn.Module): def __init__(self, opt): super(Audio2MotionVOCA3, self).__init__() self.opt = opt self.dim_motion = opt.MODEL.dim_motion # 53 self.dim_speech = opt.MODEL.dim_speech # 29 self.dim_hidden = opt.MODEL.dim_hidden # 256 # encoder self.encoder = nn.Sequential( # (nbatch, nchannel=29, H=16, W=1) nn.BatchNorm2d(self.dim_speech), # block 1 nn.Conv2d(self.dim_speech, 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=(2,1), padding=(1,0), bias=True), # 32 x 4 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=(2,1), padding=(1,0), bias=True), # 64 x 1 x 1 nn.LeakyReLU(0.02, True), nn.Conv2d(256, 128, 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=128, out_features=1024), # 128, nn.Tanh(), nn.Linear(in_features=1024, out_features=128) # 50, ) # output self.decoder = nn.Sequential( nn.Linear(in_features=128, out_features=self.dim_motion), ) 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_motion] """ (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) pred = self.decoder(feat) # (B * nseq, dim_motion) debug(self.opt, f"[forward] decoder shape = {pred.shape}", verbose=False) pred = pred.reshape(nbatch, nseqlen, -1) # (B, nseq, dim_motion) debug(self.opt, f"[forward] reshape shape = {pred.shape}", verbose=False) return pred