| 6 | from model.attentionLayer import attentionLayer |
| 7 | |
| 8 | class talkNetModel(nn.Module): |
| 9 | def __init__(self): |
| 10 | super(talkNetModel, self).__init__() |
| 11 | # Visual Temporal Encoder |
| 12 | self.visualFrontend = visualFrontend() # Visual Frontend |
| 13 | # self.visualFrontend.load_state_dict(torch.load('visual_frontend.pt', map_location="cuda")) |
| 14 | # for param in self.visualFrontend.parameters(): |
| 15 | # param.requires_grad = False |
| 16 | self.visualTCN = visualTCN() # Visual Temporal Network TCN |
| 17 | self.visualConv1D = visualConv1D() # Visual Temporal Network Conv1d |
| 18 | |
| 19 | # Audio Temporal Encoder |
| 20 | self.audioEncoder = audioEncoder(layers = [3, 4, 6, 3], num_filters = [16, 32, 64, 128]) |
| 21 | |
| 22 | # Audio-visual Cross Attention |
| 23 | self.crossA2V = attentionLayer(d_model = 128, nhead = 8) |
| 24 | self.crossV2A = attentionLayer(d_model = 128, nhead = 8) |
| 25 | |
| 26 | # Audio-visual Self Attention |
| 27 | self.selfAV = attentionLayer(d_model = 256, nhead = 8) |
| 28 | |
| 29 | def forward_visual_frontend(self, x): |
| 30 | B, T, W, H = x.shape |
| 31 | x = x.view(B*T, 1, 1, W, H) |
| 32 | x = (x / 255 - 0.4161) / 0.1688 |
| 33 | x = self.visualFrontend(x) |
| 34 | x = x.view(B, T, 512) |
| 35 | x = x.transpose(1,2) |
| 36 | x = self.visualTCN(x) |
| 37 | x = self.visualConv1D(x) |
| 38 | x = x.transpose(1,2) |
| 39 | return x |
| 40 | |
| 41 | def forward_audio_frontend(self, x): |
| 42 | x = x.unsqueeze(1).transpose(2, 3) |
| 43 | x = self.audioEncoder(x) |
| 44 | return x |
| 45 | |
| 46 | def forward_cross_attention(self, x1, x2): |
| 47 | x1_c = self.crossA2V(src = x1, tar = x2) |
| 48 | x2_c = self.crossV2A(src = x2, tar = x1) |
| 49 | return x1_c, x2_c |
| 50 | |
| 51 | def forward_audio_visual_backend(self, x1, x2): |
| 52 | x = torch.cat((x1,x2), 2) |
| 53 | x = self.selfAV(src = x, tar = x) |
| 54 | x = torch.reshape(x, (-1, 256)) |
| 55 | return x |
| 56 | |
| 57 | def forward_audio_backend(self,x): |
| 58 | x = torch.reshape(x, (-1, 128)) |
| 59 | return x |
| 60 | |
| 61 | def forward_visual_backend(self,x): |
| 62 | x = torch.reshape(x, (-1, 128)) |
| 63 | return x |
| 64 | |