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
| import torch import torch.nn as nn import numpy as np
class FocusGate(nn.Module): """ FocusGate: 可靠性门控的先验融合框架 核心组件: 1. 散焦图(defocus map)形状统计 → 3个特征 2. 逐帧门控:判断先验是否可靠 3. 弃权机制:不可靠时先验输出0(等价于弃权) 4. 与监督模型融合 """ def __init__(self, n_priors: int = 3): super().__init__() self.n_priors = n_priors self.gate_net = nn.Sequential( nn.Linear(3, 32), nn.GELU(), nn.Linear(32, n_priors), nn.Sigmoid() ) self.priors = nn.ModuleList([ nn.Conv2d(3, 1, kernel_size=7, padding=3) for _ in range(n_priors) ]) self.midrank_norm = nn.LayerNorm(1) def compute_defocus_stats(self, image: torch.Tensor) -> torch.Tensor: """ 从图像计算散焦图的3个形状统计量 散焦图反映图像模糊程度: - 均值:整体模糊水平 - 偏度:模糊分布不对称性 - 峰度:模糊分布尖锐程度 """ laplacian = self._laplacian(image) B, C, H, W = image.shape block_size = 32 blocks = laplacian.unfold(2, block_size, block_size) \ .unfold(3, block_size, block_size) local_blur = blocks.var(dim=[-1, -2]) blur_map = local_blur.mean(dim=1) mean_val = blur_map.mean(dim=[1, 2]) std_val = blur_map.std(dim=[1, 2]) centered = blur_map - blur_map.mean(dim=[1, 2], keepdim=True) skew = (centered ** 3).mean(dim=[1, 2]) / (std_val ** 3 + 1e-8) features = torch.stack([mean_val, std_val, skew], dim=-1) return features def _laplacian(self, x: torch.Tensor) -> torch.Tensor: """拉普拉斯滤波""" kernel = torch.tensor([[0, 1, 0], [1, -4, 1], [0, 1, 0]], dtype=torch.float32).reshape(1, 1, 3, 3) kernel = kernel.to(x.device).repeat(x.size(1), 1, 1, 1) return torch.nn.functional.conv2d(x, kernel, padding=1, groups=x.size(1)) def forward(self, image: torch.Tensor, base_prediction: torch.Tensor) -> torch.Tensor: """ Args: image: (B, C, H, W) 输入帧 base_prediction: (B, T, 2) 监督模型的基础预测 Returns: fused_gaze: 门控融合后的视线预测 """ defeat_stats = self.compute_defocus_stats(image) gate_scores = self.gate_net(defeat_stats) prior_outputs = [] for i, prior in enumerate(self.priors): p = prior(image) p = self.midrank_norm(p.flatten(1)).unsqueeze(-1) prior_outputs.append(p) priors = torch.cat(prior_outputs, dim=-1) gated_priors = priors * gate_scores prior_contribution = gated_priors.sum(dim=1, keepdim=True) fused = base_prediction + prior_contribution * 0.1 return fused, gate_scores
if __name__ == "__main__": gate = FocusGate(n_priors=3) image = torch.randn(4, 3, 224, 224) base_pred = torch.randn(4, 30, 2) fused, gates = gate(image, base_pred) print(f"输入图像: {image.shape}") print(f"基础预测: {base_pred.shape}") print(f"融合输出: {fused.shape}") print(f"门控分数: {gates.shape}") print(f"\n各先验可靠性:") for i in range(3): print(f" 先验{i}: {gates[0, i].item():.3f}") print(f"\n延迟开销: 仅1%(论文报告)") print(f"效果: 16个模型-领域单元全部显著提升(shuffled AUC)")
|