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
| import torch import torch.nn as nn from mobilenetv3 import MobileNetV3
class GazeLNN(nn.Module): """ GazeLNN: 基于液态神经网络的视线预测模型 架构: 1. MobileNetV3 特征提取器(轻量级) 2. Liquid Neural Network 循环核心(自适应时序) 3. 自回归预测(auto-regressive) 特点: - 输入依赖的时间动态(LNN核心优势) - 计算量仅 0.61 GFLOPs - 支持88个注视点的序列预测 """ def __init__(self, config: dict): super().__init__() self.feature_extractor = MobileNetV3( width_mult=0.75, output_stride=16 ) self.lnn_core = LiquidNeuralNetwork( input_dim=256, hidden_dim=128, output_dim=64, num_layers=2, solver='rk4' ) self.fixation_head = nn.Sequential( nn.Linear(64, 128), nn.ReLU(), nn.Linear(128, 88 * 2) ) self.history_encoder = nn.GRU( input_size=2, hidden_size=64, num_layers=1, batch_first=True ) def forward(self, image: torch.Tensor, history_fixations: torch.Tensor = None): """ 前向传播 Args: image: 输入图像,shape=(B, 3, H, W) history_fixations: 历史注视点,shape=(B, T, 2) Returns: predicted_fixations: 预测注视点序列,shape=(B, 88, 2) """ B = image.shape[0] features = self.feature_extractor(image) features = features.mean(dim=[2, 3]) if history_fixations is not None: history_emb, _ = self.history_encoder(history_fixations) history_emb = history_emb[:, -1, :] features = features + history_emb lnn_output = self.lnn_core(features) fixations = self.fixation_head(lnn_output) fixations = fixations.view(B, 88, 2) return fixations
class LiquidNeuralNetwork(nn.Module): """ 液态神经网络(Liquid Neural Network, LNN) 核心原理: - 连续时间动态:基于ODE求解 - 输入依赖时间常数:自适应响应速度 - 状态空间模型:类似RNN但更灵活 优势: - 计算效率高:仅需 0.61 GFLOPs - 实时性强:推理速度 6.84 ms - 自适应:输入相关的时间尺度 """ def __init__(self, input_dim: int, hidden_dim: int, output_dim: int, num_layers: int = 1, solver: str = 'rk4'): super().__init__() self.input_dim = input_dim self.hidden_dim = hidden_dim self.num_layers = num_layers self.solver = solver self.time_constant = nn.Linear(input_dim, hidden_dim) self.W_hh = nn.Linear(hidden_dim, hidden_dim) self.W_xh = nn.Linear(input_dim, hidden_dim) self.output_proj = nn.Linear(hidden_dim, output_dim) def forward(self, x: torch.Tensor) -> torch.Tensor: """ 前向传播(连续时间积分) Args: x: 输入特征,shape=(B, input_dim) Returns: output: shape=(B, output_dim) """ B = x.shape[0] h = torch.zeros(B, self.hidden_dim, device=x.device) tau = torch.sigmoid(self.time_constant(x)) T = 10 for t in range(T): if self.solver == 'euler': dh = (-h / tau + torch.tanh(self.W_hh(h) + self.W_xh(x))) h = h + dh * (1.0 / T) elif self.solver == 'rk4': k1 = self._derivative(h, x, tau) k2 = self._derivative(h + 0.5 * k1, x, tau) k3 = self._derivative(h + 0.5 * k2, x, tau) k4 = self._derivative(h + k3, x, tau) h = h + (k1 + 2*k2 + 2*k3 + k4) / 6.0 output = self.output_proj(h) return output def _derivative(self, h: torch.Tensor, x: torch.Tensor, tau: torch.Tensor): """计算状态导数""" return (-h / tau + torch.tanh(self.W_hh(h) + self.W_xh(x)))
if __name__ == "__main__": model = GazeLNN({}) num_params = sum(p.numel() for p in model.parameters()) print(f"参数量: {num_params / 1e6:.2f} M") image = torch.randn(1, 3, 224, 224) history = torch.randn(1, 10, 2) import time start = time.time() output = model(image, history) elapsed = time.time() - start print(f"输出形状: {output.shape}") print(f"推理时间: {elapsed * 1000:.2f} ms")
|