Spaces:
Running
on
Zero
Running
on
Zero
File size: 13,819 Bytes
2b34e02 eae1cca 2b34e02 eae1cca 2b34e02 eae1cca 2b34e02 eae1cca 2b34e02 eae1cca 2b34e02 eae1cca 2b34e02 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
import torch
from torch import nn
from torch.nn import functional as F
class Conv2d(nn.Module):
def __init__(self, cin, cout, kernel_size, stride, padding, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conv_block = nn.Sequential(nn.Conv2d(cin, cout, kernel_size, stride, padding), nn.BatchNorm2d(cout))
self.act = nn.ReLU()
def forward(self, x):
out = self.conv_block(x)
return self.act(out)
class Conv2d_res(nn.Module):
# TensorRT does not support 'if' statement, thus we create independent Conv2d_res for residual block
def __init__(self, cin, cout, kernel_size, stride, padding, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conv_block = nn.Sequential(nn.Conv2d(cin, cout, kernel_size, stride, padding), nn.BatchNorm2d(cout))
self.act = nn.ReLU()
def forward(self, x):
out = self.conv_block(x)
out += x
return self.act(out)
class Conv2dTranspose(nn.Module):
def __init__(self, cin, cout, kernel_size, stride, padding, output_padding=0, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conv_block = nn.Sequential(
nn.ConvTranspose2d(cin, cout, kernel_size, stride, padding, output_padding),
nn.BatchNorm2d(cout),
)
self.act = nn.ReLU()
def forward(self, x):
out = self.conv_block(x)
return self.act(out)
class FETE_model(nn.Module):
def __init__(self):
super(FETE_model, self).__init__()
self.face_encoder_blocks = nn.ModuleList(
[
nn.Sequential(Conv2d(6, 16, kernel_size=7, stride=2, padding=3)), # 256,256 -> 128,128
nn.Sequential(
Conv2d(16, 32, kernel_size=3, stride=2, padding=1), # 64,64
Conv2d_res(32, 32, kernel_size=3, stride=1, padding=1),
Conv2d_res(32, 32, kernel_size=3, stride=1, padding=1),
),
nn.Sequential(
Conv2d(32, 64, kernel_size=3, stride=2, padding=1), # 32,32
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
),
nn.Sequential(
Conv2d(64, 128, kernel_size=3, stride=2, padding=1), # 16,16
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
),
nn.Sequential(
Conv2d(128, 256, kernel_size=3, stride=2, padding=1), # 8,8
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1),
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1),
),
nn.Sequential(
Conv2d(256, 512, kernel_size=3, stride=2, padding=1), # 4,4
Conv2d_res(512, 512, kernel_size=3, stride=1, padding=1),
),
nn.Sequential(
Conv2d(512, 512, kernel_size=3, stride=2, padding=0), # 1, 1
Conv2d(512, 512, kernel_size=1, stride=1, padding=0),
),
]
)
self.audio_encoder = nn.Sequential(
Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
Conv2d_res(32, 32, kernel_size=3, stride=1, padding=1),
Conv2d_res(32, 32, kernel_size=3, stride=1, padding=1),
Conv2d(32, 64, kernel_size=3, stride=(3, 1), padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d(64, 128, kernel_size=3, stride=3, padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
Conv2d(128, 256, kernel_size=3, stride=(3, 2), padding=1),
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1),
Conv2d(256, 512, kernel_size=3, stride=1, padding=0),
Conv2d(512, 512, kernel_size=1, stride=1, padding=0),
)
self.pose_encoder = nn.Sequential(
Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
Conv2d_res(32, 32, kernel_size=3, stride=1, padding=1),
Conv2d(32, 64, kernel_size=3, stride=(1, 2), padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
Conv2d(128, 256, kernel_size=3, stride=(1, 2), padding=1),
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1),
Conv2d(256, 512, kernel_size=3, stride=2, padding=0),
Conv2d(512, 512, kernel_size=1, stride=1, padding=0),
)
self.emotion_encoder = nn.Sequential(
Conv2d(1, 32, kernel_size=7, stride=1, padding=1),
Conv2d_res(32, 32, kernel_size=3, stride=1, padding=1),
Conv2d(32, 64, kernel_size=3, stride=(1, 2), padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
Conv2d(128, 256, kernel_size=3, stride=(1, 2), padding=1),
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1),
Conv2d(256, 512, kernel_size=3, stride=2, padding=0),
Conv2d(512, 512, kernel_size=1, stride=1, padding=0),
)
self.blink_encoder = nn.Sequential(
Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
Conv2d_res(32, 32, kernel_size=3, stride=1, padding=1),
Conv2d(32, 64, kernel_size=3, stride=(1, 2), padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d(64, 128, kernel_size=3, stride=(1, 2), padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
Conv2d(128, 256, kernel_size=3, stride=(1, 2), padding=1),
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1),
Conv2d(256, 512, kernel_size=1, stride=(1, 2), padding=0),
Conv2d(512, 512, kernel_size=1, stride=1, padding=0),
)
self.face_decoder_blocks = nn.ModuleList(
[
nn.Sequential(
Conv2d(2048, 512, kernel_size=1, stride=1, padding=0),
),
nn.Sequential(
Conv2dTranspose(1024, 512, kernel_size=4, stride=1, padding=0), # 4,4
Conv2d_res(512, 512, kernel_size=3, stride=1, padding=1),
),
nn.Sequential(
Conv2dTranspose(1024, 512, kernel_size=3, stride=2, padding=1, output_padding=1),
Conv2d_res(512, 512, kernel_size=3, stride=1, padding=1),
Conv2d_res(512, 512, kernel_size=3, stride=1, padding=1), # 8,8
Self_Attention(512, 512),
),
nn.Sequential(
Conv2dTranspose(768, 384, kernel_size=3, stride=2, padding=1, output_padding=1),
Conv2d_res(384, 384, kernel_size=3, stride=1, padding=1),
Conv2d_res(384, 384, kernel_size=3, stride=1, padding=1), # 16, 16
Self_Attention(384, 384),
),
nn.Sequential(
Conv2dTranspose(512, 256, kernel_size=3, stride=2, padding=1, output_padding=1),
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1),
Conv2d_res(256, 256, kernel_size=3, stride=1, padding=1), # 32, 32
Self_Attention(256, 256),
),
nn.Sequential(
Conv2dTranspose(320, 128, kernel_size=3, stride=2, padding=1, output_padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
Conv2d_res(128, 128, kernel_size=3, stride=1, padding=1),
), # 64, 64
nn.Sequential(
Conv2dTranspose(160, 64, kernel_size=3, stride=2, padding=1, output_padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
Conv2d_res(64, 64, kernel_size=3, stride=1, padding=1),
),
]
) # 128,128
# self.output_block = nn.Sequential(Conv2d(80, 32, kernel_size=3, stride=1, padding=1),
# nn.Conv2d(32, 3, kernel_size=1, stride=1, padding=0),
# nn.Sigmoid())
self.output_block = nn.Sequential(
Conv2dTranspose(80, 32, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.Conv2d(32, 3, kernel_size=1, stride=1, padding=0),
nn.Sigmoid(),
)
def forward(
self,
face_sequences,
audio_sequences,
pose_sequences,
emotion_sequences,
blink_sequences,
):
# audio_sequences = (B, T, 1, 80, 16)
B = audio_sequences.size(0)
# disabled for inference
# input_dim_size = len(face_sequences.size())
# if input_dim_size > 4:
# audio_sequences = torch.cat([audio_sequences[:, i] for i in range(audio_sequences.size(1))], dim=0)
# pose_sequences = torch.cat([pose_sequences[:, i] for i in range(pose_sequences.size(1))], dim=0)
# emotion_sequences = torch.cat([emotion_sequences[:, i] for i in range(emotion_sequences.size(1))], dim=0)
# blink_sequences = torch.cat([blink_sequences[:, i] for i in range(blink_sequences.size(1))], dim=0)
# face_sequences = torch.cat([face_sequences[:, :, i] for i in range(face_sequences.size(2))], dim=0)
# print(audio_sequences.size(), face_sequences.size(), pose_sequences.size(), emotion_sequences.size())
audio_embedding = self.audio_encoder(audio_sequences) # B, 512, 1, 1
pose_embedding = self.pose_encoder(pose_sequences) # B, 512, 1, 1
emotion_embedding = self.emotion_encoder(emotion_sequences) # B, 512, 1, 1
blink_embedding = self.blink_encoder(blink_sequences) # B, 512, 1, 1
inputs_embedding = torch.cat((audio_embedding, pose_embedding, emotion_embedding, blink_embedding), dim=1) # B, 1536, 1, 1
# print(audio_embedding.size(), pose_embedding.size(), emotion_embedding.size(), inputs_embedding.size())
feats = []
x = face_sequences
for f in self.face_encoder_blocks:
x = f(x)
# print(x.shape)
feats.append(x)
x = inputs_embedding
for f in self.face_decoder_blocks:
x = f(x)
# print(x.shape)
# try:
x = torch.cat((x, feats[-1]), dim=1)
# except Exception as e:
# print(x.size())
# print(feats[-1].size())
# raise e
feats.pop()
x = self.output_block(x)
# if input_dim_size > 4:
# x = torch.split(x, B, dim=0) # [(B, C, H, W)]
# outputs = torch.stack(x, dim=2) # (B, C, T, H, W)
# else:
outputs = x
return outputs
class Self_Attention(nn.Module):
"""
Source-Reference Attention Layer
"""
def __init__(self, in_planes_s, in_planes_r):
"""
Parameters
----------
in_planes_s: int
Number of input source feature vector channels.
in_planes_r: int
Number of input reference feature vector channels.
"""
super(Self_Attention, self).__init__()
self.query_conv = nn.Conv2d(in_channels=in_planes_s, out_channels=in_planes_s // 8, kernel_size=1)
self.key_conv = nn.Conv2d(in_channels=in_planes_r, out_channels=in_planes_r // 8, kernel_size=1)
self.value_conv = nn.Conv2d(in_channels=in_planes_r, out_channels=in_planes_r, kernel_size=1)
self.gamma = nn.Parameter(torch.zeros(1))
self.softmax = nn.Softmax(dim=-1)
def forward(self, source):
source = source.float() if isinstance(source, torch.cuda.HalfTensor) else source
reference = source
"""
Parameters
----------
source : torch.Tensor
Source feature maps (B x Cs x Ts x Hs x Ws)
reference : torch.Tensor
Reference feature maps (B x Cr x Tr x Hr x Wr )
Returns :
torch.Tensor
Source-reference attention value added to the input source features
torch.Tensor
Attention map (B x Ns x Nt) (Ns=Ts*Hs*Ws, Nr=Tr*Hr*Wr)
"""
s_batchsize, sC, sH, sW = source.size()
r_batchsize, rC, rH, rW = reference.size()
proj_query = self.query_conv(source).view(s_batchsize, -1, sH * sW).permute(0, 2, 1)
proj_key = self.key_conv(reference).view(r_batchsize, -1, rW * rH)
energy = torch.bmm(proj_query, proj_key)
attention = self.softmax(energy)
proj_value = self.value_conv(reference).view(r_batchsize, -1, rH * rW)
out = torch.bmm(proj_value, attention.permute(0, 2, 1))
out = out.view(s_batchsize, sC, sH, sW)
out = self.gamma * out + source
return out.half() if isinstance(source, torch.cuda.FloatTensor) else out
|