update models
Browse files- .gitattributes +0 -0
- README.md +0 -0
- app.py +4 -4
- generator/FMT.py +383 -402
- generator/generate.py +3 -7
- renderer/inference.py +7 -17
- renderer/models.py +2 -0
.gitattributes
CHANGED
|
File without changes
|
README.md
CHANGED
|
File without changes
|
app.py
CHANGED
|
@@ -82,7 +82,7 @@ class AppConfig:
|
|
| 82 |
self.renderer_path = "./checkpoints/renderer.ckpt"
|
| 83 |
self.generator_path = "./checkpoints/generator.ckpt"
|
| 84 |
self.wav2vec_model_path = "./checkpoints/wav2vec2-base-960h"
|
| 85 |
-
self.input_size =
|
| 86 |
self.input_nc = 3
|
| 87 |
self.fps = 25.0
|
| 88 |
self.rank = "cuda"
|
|
@@ -136,7 +136,7 @@ class DataProcessor:
|
|
| 136 |
else:
|
| 137 |
print("Local wav2vec model not found, downloading from 'facebook/wav2vec2-base-960h'...")
|
| 138 |
self.wav2vec_preprocessor = Wav2Vec2FeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h")
|
| 139 |
-
self.transform = transforms.Compose([transforms.Resize((
|
| 140 |
|
| 141 |
def process_img(self, img: Image.Image) -> Image.Image:
|
| 142 |
img_arr = np.array(img)
|
|
@@ -518,10 +518,10 @@ with gr.Blocks(title="IMTalker Demo") as demo:
|
|
| 518 |
)
|
| 519 |
|
| 520 |
with gr.Accordion("Settings", open=True):
|
| 521 |
-
a_crop = gr.Checkbox(label="Auto Crop Face", value=
|
| 522 |
a_seed = gr.Number(label="Seed", value=42)
|
| 523 |
a_nfe = gr.Slider(5, 50, value=10, step=1, label="Steps (NFE)")
|
| 524 |
-
a_cfg = gr.Slider(1.0, 5.0, value=
|
| 525 |
|
| 526 |
a_btn = gr.Button("Generate (Audio Driven)", variant="primary")
|
| 527 |
|
|
|
|
| 82 |
self.renderer_path = "./checkpoints/renderer.ckpt"
|
| 83 |
self.generator_path = "./checkpoints/generator.ckpt"
|
| 84 |
self.wav2vec_model_path = "./checkpoints/wav2vec2-base-960h"
|
| 85 |
+
self.input_size = 512
|
| 86 |
self.input_nc = 3
|
| 87 |
self.fps = 25.0
|
| 88 |
self.rank = "cuda"
|
|
|
|
| 136 |
else:
|
| 137 |
print("Local wav2vec model not found, downloading from 'facebook/wav2vec2-base-960h'...")
|
| 138 |
self.wav2vec_preprocessor = Wav2Vec2FeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h")
|
| 139 |
+
self.transform = transforms.Compose([transforms.Resize((512, 512)), transforms.ToTensor()])
|
| 140 |
|
| 141 |
def process_img(self, img: Image.Image) -> Image.Image:
|
| 142 |
img_arr = np.array(img)
|
|
|
|
| 518 |
)
|
| 519 |
|
| 520 |
with gr.Accordion("Settings", open=True):
|
| 521 |
+
a_crop = gr.Checkbox(label="Auto Crop Face", value=False)
|
| 522 |
a_seed = gr.Number(label="Seed", value=42)
|
| 523 |
a_nfe = gr.Slider(5, 50, value=10, step=1, label="Steps (NFE)")
|
| 524 |
+
a_cfg = gr.Slider(1.0, 5.0, value=2.0, label="CFG Scale")
|
| 525 |
|
| 526 |
a_btn = gr.Button("Generate (Audio Driven)", variant="primary")
|
| 527 |
|
generator/FMT.py
CHANGED
|
@@ -1,402 +1,383 @@
|
|
| 1 |
-
import os, math, torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
import torch.nn.functional as F
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from timm.
|
| 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 |
-
self.
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
self.
|
| 206 |
-
self.
|
| 207 |
-
|
| 208 |
-
self.
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
self.
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
)
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
self.
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
self.
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
self.
|
| 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 |
-
self,
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
x
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
x
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
t=t,
|
| 371 |
-
x=
|
| 372 |
-
a=
|
| 373 |
-
prev_x=
|
| 374 |
-
prev_a=
|
| 375 |
-
ref_x=
|
| 376 |
-
gaze=
|
| 377 |
-
prev_gaze=
|
| 378 |
-
pose=
|
| 379 |
-
prev_pose=
|
| 380 |
-
cam=
|
| 381 |
-
prev_cam=
|
| 382 |
-
train=False
|
| 383 |
-
)
|
| 384 |
-
uncond, all_cond = torch.chunk(model_output, chunks=2, dim=0)
|
| 385 |
-
return uncond + a_cfg_scale * (all_cond - uncond)
|
| 386 |
-
|
| 387 |
-
else:
|
| 388 |
-
return self.forward(
|
| 389 |
-
t=t,
|
| 390 |
-
x=x,
|
| 391 |
-
a=a,
|
| 392 |
-
prev_x=prev_x,
|
| 393 |
-
prev_a=prev_a,
|
| 394 |
-
ref_x=ref_x,
|
| 395 |
-
gaze=gaze,
|
| 396 |
-
prev_gaze=prev_gaze,
|
| 397 |
-
pose=pose,
|
| 398 |
-
prev_pose=prev_pose,
|
| 399 |
-
cam=cam,
|
| 400 |
-
prev_cam=prev_cam,
|
| 401 |
-
train=False
|
| 402 |
-
)
|
|
|
|
| 1 |
+
import os, math, torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
|
| 5 |
+
from timm.layers import use_fused_attn
|
| 6 |
+
from timm.models.vision_transformer import Mlp
|
| 7 |
+
|
| 8 |
+
# ==========================================
|
| 9 |
+
# RoPE Implementation
|
| 10 |
+
# ==========================================
|
| 11 |
+
|
| 12 |
+
class RotaryEmbedding(nn.Module):
|
| 13 |
+
def __init__(self, dim, max_position_embeddings=4096, base=10000, device=None):
|
| 14 |
+
super().__init__()
|
| 15 |
+
self.dim = dim
|
| 16 |
+
self.max_position_embeddings = max_position_embeddings
|
| 17 |
+
self.base = base
|
| 18 |
+
inv_freq = 1.0 / (self.base ** (torch.arange(0, dim, 2).float().to(device) / dim))
|
| 19 |
+
self.register_buffer("inv_freq", inv_freq)
|
| 20 |
+
self._set_cos_sin_cache(
|
| 21 |
+
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
| 25 |
+
self.max_seq_len_cached = seq_len
|
| 26 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
|
| 27 |
+
freqs = torch.outer(t, self.inv_freq)
|
| 28 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
| 29 |
+
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
| 30 |
+
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
| 31 |
+
|
| 32 |
+
def forward(self, x, seq_len=None):
|
| 33 |
+
if seq_len > self.max_seq_len_cached:
|
| 34 |
+
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
|
| 35 |
+
return (
|
| 36 |
+
self.cos_cached[:seq_len].to(dtype=x.dtype),
|
| 37 |
+
self.sin_cached[:seq_len].to(dtype=x.dtype),
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def rotate_half(x):
|
| 41 |
+
x1 = x[..., : x.shape[-1] // 2]
|
| 42 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
| 43 |
+
return torch.cat((-x2, x1), dim=-1)
|
| 44 |
+
|
| 45 |
+
def apply_rotary_pos_emb(q, k, cos, sin):
|
| 46 |
+
cos = cos.unsqueeze(0).unsqueeze(0)
|
| 47 |
+
sin = sin.unsqueeze(0).unsqueeze(0)
|
| 48 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
| 49 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
| 50 |
+
return q_embed, k_embed
|
| 51 |
+
|
| 52 |
+
# ==========================================
|
| 53 |
+
# Core Modules
|
| 54 |
+
# ==========================================
|
| 55 |
+
|
| 56 |
+
class Attention(nn.Module):
|
| 57 |
+
def __init__(
|
| 58 |
+
self,
|
| 59 |
+
dim: int,
|
| 60 |
+
num_heads: int = 8,
|
| 61 |
+
qkv_bias: bool = False,
|
| 62 |
+
qk_norm: bool = False,
|
| 63 |
+
attn_drop: float = 0.,
|
| 64 |
+
proj_drop: float = 0.,
|
| 65 |
+
norm_layer: nn.Module = nn.LayerNorm,
|
| 66 |
+
) -> None:
|
| 67 |
+
|
| 68 |
+
super().__init__()
|
| 69 |
+
assert dim % num_heads == 0, 'dim should be divisible by num_heads'
|
| 70 |
+
self.num_heads = num_heads
|
| 71 |
+
self.head_dim = dim // num_heads
|
| 72 |
+
self.scale = self.head_dim ** -0.5
|
| 73 |
+
self.fused_attn = use_fused_attn()
|
| 74 |
+
|
| 75 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
| 76 |
+
self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
|
| 77 |
+
self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
|
| 78 |
+
self.attn_drop = nn.Dropout(attn_drop)
|
| 79 |
+
self.proj = nn.Linear(dim, dim)
|
| 80 |
+
self.proj_drop = nn.Dropout(proj_drop)
|
| 81 |
+
|
| 82 |
+
def forward(self, x: torch.Tensor, rotary_pos_emb=None) -> torch.Tensor:
|
| 83 |
+
B, N, C = x.shape
|
| 84 |
+
|
| 85 |
+
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
|
| 86 |
+
q, k, v = qkv.unbind(0)
|
| 87 |
+
q, k = self.q_norm(q), self.k_norm(k)
|
| 88 |
+
|
| 89 |
+
if rotary_pos_emb is not None:
|
| 90 |
+
cos, sin = rotary_pos_emb
|
| 91 |
+
q, k = apply_rotary_pos_emb(q, k, cos, sin)
|
| 92 |
+
|
| 93 |
+
if self.fused_attn:
|
| 94 |
+
x = F.scaled_dot_product_attention(
|
| 95 |
+
q, k, v,
|
| 96 |
+
attn_mask=None,
|
| 97 |
+
dropout_p=self.attn_drop.p if self.training else 0.,
|
| 98 |
+
)
|
| 99 |
+
else:
|
| 100 |
+
q = q * self.scale
|
| 101 |
+
attn = q @ k.transpose(-2, -1)
|
| 102 |
+
attn = attn.softmax(dim=-1)
|
| 103 |
+
attn = self.attn_drop(attn)
|
| 104 |
+
x = attn @ v
|
| 105 |
+
|
| 106 |
+
x = x.transpose(1, 2).reshape(B, N, C)
|
| 107 |
+
x = self.proj(x)
|
| 108 |
+
x = self.proj_drop(x)
|
| 109 |
+
return x
|
| 110 |
+
|
| 111 |
+
class TimestepEmbedder(nn.Module):
|
| 112 |
+
def __init__(self, hidden_size, frequency_embedding_size = 256):
|
| 113 |
+
super().__init__()
|
| 114 |
+
self.mlp = nn.Sequential(
|
| 115 |
+
nn.Linear(frequency_embedding_size, hidden_size, bias=True),
|
| 116 |
+
nn.SiLU(),
|
| 117 |
+
nn.Linear(hidden_size, hidden_size, bias=True),
|
| 118 |
+
)
|
| 119 |
+
self.frequency_embedding_size = frequency_embedding_size
|
| 120 |
+
|
| 121 |
+
@staticmethod
|
| 122 |
+
def timestep_embedding(t: torch.Tensor, dim: int, max_period: int = 10000) -> torch.Tensor:
|
| 123 |
+
half = dim // 2
|
| 124 |
+
freqs = torch.exp(
|
| 125 |
+
-math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half
|
| 126 |
+
).to(device=t.device)
|
| 127 |
+
args = t[:, None].float() * freqs[None]
|
| 128 |
+
embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
|
| 129 |
+
if dim % 2:
|
| 130 |
+
embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1)
|
| 131 |
+
return embedding
|
| 132 |
+
|
| 133 |
+
def forward(self, t: torch.Tensor) -> torch.Tensor:
|
| 134 |
+
t_freq = self.timestep_embedding(t, self.frequency_embedding_size)
|
| 135 |
+
t_emb = self.mlp(t_freq)
|
| 136 |
+
return t_emb
|
| 137 |
+
|
| 138 |
+
class SequenceEmbed(nn.Module):
|
| 139 |
+
def __init__(
|
| 140 |
+
self,
|
| 141 |
+
dim_w,
|
| 142 |
+
dim_h,
|
| 143 |
+
norm_layer=None,
|
| 144 |
+
bias=True,
|
| 145 |
+
):
|
| 146 |
+
super().__init__()
|
| 147 |
+
self.proj = nn.Linear(dim_w, dim_h, bias=bias)
|
| 148 |
+
self.norm = norm_layer(dim_h) if norm_layer else nn.Identity()
|
| 149 |
+
|
| 150 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 151 |
+
return self.norm(self.proj(x))
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
class FMTBlock(nn.Module):
|
| 155 |
+
def __init__(self, hidden_size, num_heads, mlp_ratio=4.0, **block_kwargs) -> None:
|
| 156 |
+
super().__init__()
|
| 157 |
+
self.norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
|
| 158 |
+
self.attn = Attention(hidden_size, num_heads=num_heads, qkv_bias=True, **block_kwargs)
|
| 159 |
+
self.norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
|
| 160 |
+
mlp_hidden_dim = int(hidden_size * mlp_ratio)
|
| 161 |
+
approx_gelu = lambda: nn.GELU(approximate="tanh")
|
| 162 |
+
self.mlp = Mlp(in_features=hidden_size, hidden_features=mlp_hidden_dim, act_layer=approx_gelu, drop=0)
|
| 163 |
+
self.adaLN_modulation = nn.Sequential(
|
| 164 |
+
nn.SiLU(),
|
| 165 |
+
nn.Linear(hidden_size, 6 * hidden_size, bias=True)
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
def framewise_modulate(self, x, shift, scale) -> torch.Tensor:
|
| 169 |
+
return x * (1 + scale) + shift
|
| 170 |
+
|
| 171 |
+
def forward(self, x, c, rotary_pos_emb=None) -> torch.Tensor:
|
| 172 |
+
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.adaLN_modulation(c).chunk(6, dim=-1)
|
| 173 |
+
x = x + gate_msa * self.attn(self.framewise_modulate(self.norm1(x), shift_msa, scale_msa), rotary_pos_emb=rotary_pos_emb)
|
| 174 |
+
x = x + gate_mlp * self.mlp(self.framewise_modulate(self.norm2(x), shift_mlp, scale_mlp))
|
| 175 |
+
return x
|
| 176 |
+
|
| 177 |
+
class Decoder(nn.Module):
|
| 178 |
+
def __init__(self, hidden_size, dim_w):
|
| 179 |
+
super().__init__()
|
| 180 |
+
self.norm_final = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
|
| 181 |
+
self.adaLN_modulation = nn.Sequential(
|
| 182 |
+
nn.SiLU(),
|
| 183 |
+
nn.Linear(hidden_size, 2 * hidden_size, bias=True)
|
| 184 |
+
)
|
| 185 |
+
self.linear = nn.Linear(hidden_size, dim_w, bias=True)
|
| 186 |
+
|
| 187 |
+
def framewise_modulate(self, x, shift, scale) -> torch.Tensor:
|
| 188 |
+
return x * (1 + scale) + shift
|
| 189 |
+
|
| 190 |
+
def forward(self, x: torch.Tensor, c: torch.Tensor) -> torch.Tensor:
|
| 191 |
+
shift, scale = self.adaLN_modulation(c).chunk(2, dim=-1)
|
| 192 |
+
x = self.framewise_modulate(self.norm_final(x), shift, scale)
|
| 193 |
+
return self.linear(x)
|
| 194 |
+
|
| 195 |
+
# ==========================================
|
| 196 |
+
# Main Model
|
| 197 |
+
# ==========================================
|
| 198 |
+
|
| 199 |
+
class FlowMatchingTransformer(nn.Module):
|
| 200 |
+
def __init__(self, opt) -> None:
|
| 201 |
+
super().__init__()
|
| 202 |
+
self.opt = opt
|
| 203 |
+
|
| 204 |
+
self.num_frames_for_clip = int(self.opt.wav2vec_sec * self.opt.fps)
|
| 205 |
+
self.num_prev_frames = int(opt.num_prev_frames)
|
| 206 |
+
self.num_total_frames = self.num_prev_frames + self.num_frames_for_clip
|
| 207 |
+
|
| 208 |
+
self.hidden_size = opt.dim_h
|
| 209 |
+
self.mlp_ratio = opt.mlp_ratio
|
| 210 |
+
self.fmt_depth = opt.fmt_depth
|
| 211 |
+
self.num_heads = opt.num_heads
|
| 212 |
+
|
| 213 |
+
self.x_embedder = SequenceEmbed(2 * opt.dim_motion, self.hidden_size)
|
| 214 |
+
|
| 215 |
+
# RoPE Setup
|
| 216 |
+
head_dim = self.hidden_size // self.num_heads
|
| 217 |
+
self.rotary_emb = RotaryEmbedding(head_dim)
|
| 218 |
+
|
| 219 |
+
self.t_embedder = TimestepEmbedder(self.hidden_size)
|
| 220 |
+
self.c_embedder = nn.Linear(opt.dim_c, self.hidden_size)
|
| 221 |
+
|
| 222 |
+
self.blocks = nn.ModuleList([
|
| 223 |
+
FMTBlock(self.hidden_size, self.num_heads, mlp_ratio=self.mlp_ratio)
|
| 224 |
+
for _ in range(self.fmt_depth)
|
| 225 |
+
])
|
| 226 |
+
self.decoder = Decoder(self.hidden_size, self.opt.dim_motion)
|
| 227 |
+
self.initialize_weights()
|
| 228 |
+
|
| 229 |
+
def initialize_weights(self) -> None:
|
| 230 |
+
def _basic_init(module):
|
| 231 |
+
if isinstance(module, nn.Linear):
|
| 232 |
+
torch.nn.init.xavier_uniform_(module.weight)
|
| 233 |
+
if module.bias is not None:
|
| 234 |
+
nn.init.constant_(module.bias, 0)
|
| 235 |
+
|
| 236 |
+
self.apply(_basic_init)
|
| 237 |
+
|
| 238 |
+
w = self.x_embedder.proj.weight.data
|
| 239 |
+
nn.init.xavier_uniform_(w.view([w.shape[0], -1]))
|
| 240 |
+
nn.init.constant_(self.x_embedder.proj.bias, 0)
|
| 241 |
+
|
| 242 |
+
nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02)
|
| 243 |
+
nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
|
| 244 |
+
|
| 245 |
+
for block in self.blocks:
|
| 246 |
+
nn.init.constant_(block.adaLN_modulation[-1].weight, 0)
|
| 247 |
+
nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
|
| 248 |
+
|
| 249 |
+
nn.init.constant_(self.decoder.adaLN_modulation[-1].weight, 0)
|
| 250 |
+
nn.init.constant_(self.decoder.adaLN_modulation[-1].bias, 0)
|
| 251 |
+
nn.init.constant_(self.decoder.linear.weight, 0)
|
| 252 |
+
nn.init.constant_(self.decoder.linear.bias, 0)
|
| 253 |
+
|
| 254 |
+
def sequence_embedder(
|
| 255 |
+
self, sequence: torch.Tensor,
|
| 256 |
+
dropout_prob: float,
|
| 257 |
+
train: bool = False
|
| 258 |
+
) -> torch.Tensor:
|
| 259 |
+
if train:
|
| 260 |
+
batch_id_for_drop = torch.where(
|
| 261 |
+
torch.rand(sequence.shape[0], device=sequence.device) < dropout_prob
|
| 262 |
+
)
|
| 263 |
+
sequence[batch_id_for_drop] = 0
|
| 264 |
+
return sequence
|
| 265 |
+
|
| 266 |
+
def forward(
|
| 267 |
+
self,
|
| 268 |
+
t,
|
| 269 |
+
x,
|
| 270 |
+
a,
|
| 271 |
+
prev_x,
|
| 272 |
+
prev_a,
|
| 273 |
+
ref_x,
|
| 274 |
+
gaze,
|
| 275 |
+
prev_gaze,
|
| 276 |
+
pose,
|
| 277 |
+
prev_pose,
|
| 278 |
+
cam,
|
| 279 |
+
prev_cam,
|
| 280 |
+
train: bool = True,
|
| 281 |
+
**kwargs
|
| 282 |
+
) -> torch.Tensor:
|
| 283 |
+
t = self.t_embedder(t).unsqueeze(1)
|
| 284 |
+
a = self.sequence_embedder(a, dropout_prob=self.opt.audio_dropout_prob, train=train)
|
| 285 |
+
pose = self.sequence_embedder(pose, dropout_prob=self.opt.audio_dropout_prob, train=train)
|
| 286 |
+
cam = self.sequence_embedder(cam, dropout_prob=self.opt.audio_dropout_prob, train=train)
|
| 287 |
+
gaze = self.sequence_embedder(gaze, dropout_prob=self.opt.audio_dropout_prob, train=train)
|
| 288 |
+
|
| 289 |
+
if prev_x is not None:
|
| 290 |
+
prev_x = self.sequence_embedder(prev_x, dropout_prob=0.5, train=train)
|
| 291 |
+
prev_a = self.sequence_embedder(prev_a, dropout_prob=0.5, train=train)
|
| 292 |
+
prev_pose = self.sequence_embedder(prev_pose, dropout_prob=0.5, train=train)
|
| 293 |
+
prev_cam = self.sequence_embedder(prev_cam, dropout_prob=0.5, train=train)
|
| 294 |
+
prev_gaze = self.sequence_embedder(prev_gaze, dropout_prob=0.5, train=train)
|
| 295 |
+
|
| 296 |
+
x = torch.cat([prev_x, x], dim=1)
|
| 297 |
+
a = torch.cat([prev_a, a], dim=1)
|
| 298 |
+
pose = torch.cat([prev_pose, pose], dim=1)
|
| 299 |
+
cam = torch.cat([prev_cam, cam], dim=1)
|
| 300 |
+
gaze = torch.cat([prev_gaze, gaze], dim=1)
|
| 301 |
+
|
| 302 |
+
ref_x = ref_x[:, None, ...].repeat(1, x.shape[1], 1)
|
| 303 |
+
x = torch.cat([ref_x, x], dim=-1)
|
| 304 |
+
x = self.x_embedder(x)
|
| 305 |
+
|
| 306 |
+
# Calculate RoPE
|
| 307 |
+
rotary_pos_emb = self.rotary_emb(x, seq_len=x.shape[1])
|
| 308 |
+
|
| 309 |
+
c = self.c_embedder(a + pose + cam + gaze)
|
| 310 |
+
c = t + c
|
| 311 |
+
|
| 312 |
+
for block in self.blocks:
|
| 313 |
+
x = block(x, c, rotary_pos_emb=rotary_pos_emb)
|
| 314 |
+
|
| 315 |
+
return self.decoder(x, c)
|
| 316 |
+
|
| 317 |
+
@torch.no_grad()
|
| 318 |
+
def forward_with_cfg(
|
| 319 |
+
self,
|
| 320 |
+
t,
|
| 321 |
+
x,
|
| 322 |
+
a,
|
| 323 |
+
prev_x,
|
| 324 |
+
prev_a,
|
| 325 |
+
ref_x,
|
| 326 |
+
gaze,
|
| 327 |
+
prev_gaze,
|
| 328 |
+
pose,
|
| 329 |
+
prev_pose,
|
| 330 |
+
cam,
|
| 331 |
+
prev_cam,
|
| 332 |
+
a_cfg_scale: float = 1.0,
|
| 333 |
+
**kwargs
|
| 334 |
+
) -> torch.Tensor:
|
| 335 |
+
if a_cfg_scale != 1.0:
|
| 336 |
+
null_a = torch.zeros_like(a)
|
| 337 |
+
audio_cat = torch.cat([null_a, a], dim=0)
|
| 338 |
+
gaze_cat = torch.cat([gaze, gaze], dim=0)
|
| 339 |
+
pose_cat = torch.cat([pose, pose], dim=0)
|
| 340 |
+
cam_cat = torch.cat([cam, cam], dim=0)
|
| 341 |
+
|
| 342 |
+
x_cat = torch.cat([x, x], dim=0)
|
| 343 |
+
prev_x_cat = torch.cat([prev_x, prev_x], dim=0)
|
| 344 |
+
prev_a_cat = torch.cat([prev_a, prev_a], dim=0)
|
| 345 |
+
prev_gaze_cat = torch.cat([prev_gaze, prev_gaze], dim=0)
|
| 346 |
+
prev_pose_cat = torch.cat([prev_pose, prev_pose], dim=0)
|
| 347 |
+
prev_cam_cat = torch.cat([prev_cam, prev_cam], dim=0)
|
| 348 |
+
ref_x_cat = torch.cat([ref_x, ref_x], dim=0)
|
| 349 |
+
|
| 350 |
+
model_output = self.forward(
|
| 351 |
+
t=t,
|
| 352 |
+
x=x_cat,
|
| 353 |
+
a=audio_cat,
|
| 354 |
+
prev_x=prev_x_cat,
|
| 355 |
+
prev_a=prev_a_cat,
|
| 356 |
+
ref_x=ref_x_cat,
|
| 357 |
+
gaze=gaze_cat,
|
| 358 |
+
prev_gaze=prev_gaze_cat,
|
| 359 |
+
pose=pose_cat,
|
| 360 |
+
prev_pose=prev_pose_cat,
|
| 361 |
+
cam=cam_cat,
|
| 362 |
+
prev_cam=prev_cam_cat,
|
| 363 |
+
train=False
|
| 364 |
+
)
|
| 365 |
+
uncond, all_cond = torch.chunk(model_output, chunks=2, dim=0)
|
| 366 |
+
return uncond + a_cfg_scale * (all_cond - uncond)
|
| 367 |
+
|
| 368 |
+
else:
|
| 369 |
+
return self.forward(
|
| 370 |
+
t=t,
|
| 371 |
+
x=x,
|
| 372 |
+
a=a,
|
| 373 |
+
prev_x=prev_x,
|
| 374 |
+
prev_a=prev_a,
|
| 375 |
+
ref_x=ref_x,
|
| 376 |
+
gaze=gaze,
|
| 377 |
+
prev_gaze=prev_gaze,
|
| 378 |
+
pose=pose,
|
| 379 |
+
prev_pose=prev_pose,
|
| 380 |
+
cam=cam,
|
| 381 |
+
prev_cam=prev_cam,
|
| 382 |
+
train=False
|
| 383 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generator/generate.py
CHANGED
|
@@ -40,7 +40,7 @@ class DataProcessor:
|
|
| 40 |
)
|
| 41 |
|
| 42 |
self.transform = transforms.Compose([
|
| 43 |
-
transforms.Resize((
|
| 44 |
transforms.ToTensor(),
|
| 45 |
])
|
| 46 |
|
|
@@ -50,14 +50,10 @@ class DataProcessor:
|
|
| 50 |
h, w = img_arr.shape[:2]
|
| 51 |
|
| 52 |
mult = 360.0 / h
|
| 53 |
-
resized_img = cv2.resize(
|
| 54 |
-
img_arr, dsize=(0, 0), fx=mult, fy=mult,
|
| 55 |
-
interpolation=cv2.INTER_AREA if mult < 1 else cv2.INTER_CUBIC
|
| 56 |
-
)
|
| 57 |
|
| 58 |
-
bboxes = self.fa.face_detector.detect_from_image(
|
| 59 |
valid_bboxes = [
|
| 60 |
-
(int(x1
|
| 61 |
for (x1, y1, x2, y2, score) in bboxes if score > 0.95
|
| 62 |
]
|
| 63 |
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
self.transform = transforms.Compose([
|
| 43 |
+
transforms.Resize((512, 512)),
|
| 44 |
transforms.ToTensor(),
|
| 45 |
])
|
| 46 |
|
|
|
|
| 50 |
h, w = img_arr.shape[:2]
|
| 51 |
|
| 52 |
mult = 360.0 / h
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
bboxes = self.fa.face_detector.detect_from_image(img_arr)
|
| 55 |
valid_bboxes = [
|
| 56 |
+
(int(x1 ), int(y1), int(x2), int(y2), score)
|
| 57 |
for (x1, y1, x2, y2, score) in bboxes if score > 0.95
|
| 58 |
]
|
| 59 |
|
renderer/inference.py
CHANGED
|
@@ -25,7 +25,7 @@ class DataProcessor:
|
|
| 25 |
self.fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, flip_input=False)
|
| 26 |
|
| 27 |
self.transform = transforms.Compose([
|
| 28 |
-
transforms.Resize((
|
| 29 |
transforms.ToTensor(),
|
| 30 |
])
|
| 31 |
|
|
@@ -43,17 +43,13 @@ class DataProcessor:
|
|
| 43 |
|
| 44 |
# Resize for faster detection
|
| 45 |
h, w = img.shape[:2]
|
| 46 |
-
mult = 360. / h
|
| 47 |
-
resized_img = cv2.resize(
|
| 48 |
-
img, dsize=(0, 0), fx=mult, fy=mult,
|
| 49 |
-
interpolation=cv2.INTER_AREA if mult < 1. else cv2.INTER_CUBIC
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
# Filter valid faces (score > 0.95)
|
| 55 |
valid_bboxes = [
|
| 56 |
-
(int(x1
|
| 57 |
for (x1, y1, x2, y2, score) in bboxes if score > 0.95
|
| 58 |
]
|
| 59 |
|
|
@@ -65,7 +61,7 @@ class DataProcessor:
|
|
| 65 |
x1, y1, x2, y2, _ = valid_bboxes[0]
|
| 66 |
bsy, bsx = int((y2 - y1) / 2), int((x2 - x1) / 2)
|
| 67 |
my, mx = int((y1 + y2) / 2), int((x1 + x2) / 2)
|
| 68 |
-
bs = int(max(bsy, bsx) * 1.
|
| 69 |
|
| 70 |
# Pad image to allow cropping outside boundaries
|
| 71 |
img = cv2.copyMakeBorder(img, bs, bs, bs, bs, cv2.BORDER_CONSTANT, value=0)
|
|
@@ -73,11 +69,6 @@ class DataProcessor:
|
|
| 73 |
# Adjust coordinates for padding
|
| 74 |
my, mx = my + bs, mx + bs
|
| 75 |
crop_img = img[my - bs:my + bs, mx - bs:mx + bs]
|
| 76 |
-
|
| 77 |
-
crop_img = cv2.resize(
|
| 78 |
-
crop_img, (self.input_size, self.input_size),
|
| 79 |
-
interpolation=cv2.INTER_AREA if mult < 1. else cv2.INTER_CUBIC
|
| 80 |
-
)
|
| 81 |
return Image.fromarray(crop_img)
|
| 82 |
|
| 83 |
def load_image(self, path):
|
|
@@ -128,7 +119,6 @@ class Demo(nn.Module):
|
|
| 128 |
source_img = self.processor.process_img(source_img)
|
| 129 |
|
| 130 |
source_tensor = self.processor.transform(source_img).unsqueeze(0).to(self.device)
|
| 131 |
-
|
| 132 |
# 2. Encode Source Appearance & Motion
|
| 133 |
f_r, i_r = self.gen.app_encode(source_tensor)
|
| 134 |
t_r = self.gen.mot_encode(source_tensor)
|
|
@@ -216,8 +206,8 @@ if __name__ == '__main__':
|
|
| 216 |
parser.add_argument("--save_path", type=str, default="./results", help="Output directory")
|
| 217 |
|
| 218 |
# Model Params
|
| 219 |
-
parser.add_argument("--renderer_path", type=str,
|
| 220 |
-
parser.add_argument("--input_size", type=int, default=
|
| 221 |
parser.add_argument('--swin_res_threshold', type=int, default=128)
|
| 222 |
parser.add_argument('--num_heads', type=int, default=8)
|
| 223 |
parser.add_argument('--window_size', type=int, default=8)
|
|
|
|
| 25 |
self.fa = face_alignment.FaceAlignment(face_alignment.LandmarksType.TWO_D, flip_input=False)
|
| 26 |
|
| 27 |
self.transform = transforms.Compose([
|
| 28 |
+
transforms.Resize((512, 512)),
|
| 29 |
transforms.ToTensor(),
|
| 30 |
])
|
| 31 |
|
|
|
|
| 43 |
|
| 44 |
# Resize for faster detection
|
| 45 |
h, w = img.shape[:2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
|
| 48 |
+
bboxes = self.fa.face_detector.detect_from_image(img)
|
| 49 |
|
| 50 |
# Filter valid faces (score > 0.95)
|
| 51 |
valid_bboxes = [
|
| 52 |
+
(int(x1), int(y1), int(x2 ), int(y2 ), score)
|
| 53 |
for (x1, y1, x2, y2, score) in bboxes if score > 0.95
|
| 54 |
]
|
| 55 |
|
|
|
|
| 61 |
x1, y1, x2, y2, _ = valid_bboxes[0]
|
| 62 |
bsy, bsx = int((y2 - y1) / 2), int((x2 - x1) / 2)
|
| 63 |
my, mx = int((y1 + y2) / 2), int((x1 + x2) / 2)
|
| 64 |
+
bs = int(max(bsy, bsx) * 1.6)
|
| 65 |
|
| 66 |
# Pad image to allow cropping outside boundaries
|
| 67 |
img = cv2.copyMakeBorder(img, bs, bs, bs, bs, cv2.BORDER_CONSTANT, value=0)
|
|
|
|
| 69 |
# Adjust coordinates for padding
|
| 70 |
my, mx = my + bs, mx + bs
|
| 71 |
crop_img = img[my - bs:my + bs, mx - bs:mx + bs]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return Image.fromarray(crop_img)
|
| 73 |
|
| 74 |
def load_image(self, path):
|
|
|
|
| 119 |
source_img = self.processor.process_img(source_img)
|
| 120 |
|
| 121 |
source_tensor = self.processor.transform(source_img).unsqueeze(0).to(self.device)
|
|
|
|
| 122 |
# 2. Encode Source Appearance & Motion
|
| 123 |
f_r, i_r = self.gen.app_encode(source_tensor)
|
| 124 |
t_r = self.gen.mot_encode(source_tensor)
|
|
|
|
| 206 |
parser.add_argument("--save_path", type=str, default="./results", help="Output directory")
|
| 207 |
|
| 208 |
# Model Params
|
| 209 |
+
parser.add_argument("--renderer_path", type=str, default="./checkpoints/renderer.ckpt", help="Checkpoint path")
|
| 210 |
+
parser.add_argument("--input_size", type=int, default=512, help="Resolution")
|
| 211 |
parser.add_argument('--swin_res_threshold', type=int, default=128)
|
| 212 |
parser.add_argument('--num_heads', type=int, default=8)
|
| 213 |
parser.add_argument('--window_size', type=int, default=8)
|
renderer/models.py
CHANGED
|
@@ -13,6 +13,7 @@ class IdentityEncoder(nn.Module):
|
|
| 13 |
nn.BatchNorm2d(initial_channels),
|
| 14 |
nn.ReLU(inplace=True)
|
| 15 |
)
|
|
|
|
| 16 |
self.down_blocks = nn.ModuleList()
|
| 17 |
current_channels = initial_channels
|
| 18 |
for out_channels in output_channels:
|
|
@@ -27,6 +28,7 @@ class IdentityEncoder(nn.Module):
|
|
| 27 |
def forward(self, x):
|
| 28 |
features = []
|
| 29 |
x = self.initial_conv(x)
|
|
|
|
| 30 |
features.append(x)
|
| 31 |
for block in self.down_blocks:
|
| 32 |
x = block(x)
|
|
|
|
| 13 |
nn.BatchNorm2d(initial_channels),
|
| 14 |
nn.ReLU(inplace=True)
|
| 15 |
)
|
| 16 |
+
self.down_block_0 = DownConvResBlock(initial_channels, initial_channels)
|
| 17 |
self.down_blocks = nn.ModuleList()
|
| 18 |
current_channels = initial_channels
|
| 19 |
for out_channels in output_channels:
|
|
|
|
| 28 |
def forward(self, x):
|
| 29 |
features = []
|
| 30 |
x = self.initial_conv(x)
|
| 31 |
+
x = self.down_block_0(x)
|
| 32 |
features.append(x)
|
| 33 |
for block in self.down_blocks:
|
| 34 |
x = block(x)
|