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
| import torch import torch.nn as nn import torch.nn.functional as F
class GazeDiT(nn.Module): """ GazeDiT: 注视精确扩散图像生成模型 论文: arXiv:2609.17814 核心创新: 1. 4D注视标签 → 物理眼渲染器 → 注视一致几何 2. 冻结SegFormer提取瞳孔/虹膜结构作为空间条件 3. 扩散模型在空间条件引导下生成图像 输入: 4D注视标签 + 随机噪声 输出: 注视精确的人脸/眼部图像 """ def __init__(self, gaze_dim=4, feat_dim=256): super().__init__() self.eye_renderer = PhysicalEyeRenderer() self.segformer = self._load_frozen_segformer() self.spatial_encoder = nn.Sequential( nn.Linear(feat_dim * 2, feat_dim), nn.GELU(), nn.Linear(feat_dim, feat_dim) ) self.denoising_unet = GazeConditionedUNet( condition_dim=feat_dim, gaze_dim=gaze_dim ) def _load_frozen_segformer(self): """加载冻结的SegFormer""" seg = nn.Sequential( nn.Conv2d(3, 64, 3, 1, 1), nn.ReLU(), nn.Conv2d(64, 128, 3, 2, 1), nn.ReLU(), nn.Conv2d(128, 256, 3, 2, 1), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten() ) for p in seg.parameters(): p.requires_grad = False return seg def forward(self, noise, gaze_label, timesteps): """ Args: noise: (B, 3, H, W) 噪声图像 gaze_label: (B, 4) 4D双眼注视 [left_x, left_y, right_x, right_y] timesteps: (B,) 扩散时间步 Returns: denoised: (B, 3, H, W) 去噪图像 """ eye_geometry = self.eye_renderer(gaze_label) with torch.no_grad(): spatial_feat = self.segformer(eye_geometry) spatial_cond = self.spatial_encoder( torch.cat([spatial_feat, spatial_feat], dim=-1) ) denoised = self.denoising_unet(noise, spatial_cond, gaze_label, timesteps) return denoised
class PhysicalEyeRenderer: """ 物理眼渲染器 根据注视角度生成注视一致的眼部几何 参数化模型: - 瞳孔位置随注视角度偏移 - 虹膜变形随角度变化 - 眼睑开度随角度调整 """ def __call__(self, gaze_label): """ Args: gaze_label: (B, 4) [left_yaw, left_pitch, right_yaw, right_pitch] Returns: eye_image: (B, 3, 64, 128) 双眼渲染图 """ B = gaze_label.shape[0] eye_image = torch.zeros(B, 3, 64, 128) for i in range(B): left_x = 32 + gaze_label[i, 0] * 10 left_y = 32 + gaze_label[i, 1] * 8 right_x = 32 + gaze_label[i, 2] * 10 + 64 right_y = 32 + gaze_label[i, 3] * 8 eye_image[i, :, int(left_y), int(left_x)] = 0 eye_image[i, :, int(right_y), int(right_x)] = 0 return eye_image
class GazeConditionedUNet(nn.Module): """注视条件UNet扩散模型(简化版)""" def __init__(self, condition_dim=256, gaze_dim=4): super().__init__() self.time_embed = nn.Sequential( nn.Linear(1, 64), nn.GELU(), nn.Linear(64, condition_dim) ) self.gaze_embed = nn.Sequential( nn.Linear(gaze_dim, 64), nn.GELU(), nn.Linear(64, condition_dim) ) self.cond_fuse = nn.Linear(condition_dim * 3, condition_dim) self.encoder = nn.Sequential( nn.Conv2d(3, 64, 3, 1, 1), nn.GroupNorm(8, 64), nn.SiLU(), nn.Conv2d(64, 128, 3, 2, 1), nn.GroupNorm(8, 128), nn.SiLU(), nn.Conv2d(128, 256, 3, 2, 1), nn.GroupNorm(8, 256), nn.SiLU(), ) self.film = nn.Linear(condition_dim, 256 * 2) self.decoder = nn.Sequential( nn.ConvTranspose2d(256, 128, 4, 2, 1), nn.GroupNorm(8, 128), nn.SiLU(), nn.ConvTranspose2d(128, 64, 4, 2, 1), nn.GroupNorm(8, 64), nn.SiLU(), nn.Conv2d(64, 3, 3, 1, 1) ) def forward(self, x, cond, gaze, t): feat = self.encoder(x) t_emb = self.time_embed(t.unsqueeze(-1).float()) g_emb = self.gaze_embed(gaze) combined = self.cond_fuse(torch.cat([cond, t_emb, g_emb], dim=-1)) film_params = self.film(combined) scale, shift = film_params.chunk(2, dim=-1) feat = feat * (1 + scale.unsqueeze(-1).unsqueeze(-1)) + shift.unsqueeze(-1).unsqueeze(-1) out = self.decoder(feat) return out
if __name__ == "__main__": model = GazeDiT(gaze_dim=4, feat_dim=256) noise = torch.randn(4, 3, 64, 128) gaze = torch.randn(4, 4) * 0.5 t = torch.randint(0, 1000, (4,)) out = model(noise, gaze, t) print(f"Noise input: {noise.shape}") print(f"Gaze label: {gaze.shape}") print(f"Generated image: {out.shape}") print(f"Parameters: {sum(p.numel() for p in model.parameters() if p.requires_grad):,}")
|