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
| import torch import torch.nn as nn
class DriverWM(nn.Module): """Driver-WM: 驾驶员状态世界模型""" def __init__(self, vlm_encoder, latent_dim: int = 256, num_future_steps: int = 10): super().__init__() self.encoder = vlm_encoder for param in self.encoder.parameters(): param.requires_grad = False self.dynamics = nn.Sequential( nn.Linear(latent_dim + 64, 512), nn.ReLU(), nn.Linear(512, latent_dim) ) self.predictor = nn.Sequential( nn.Linear(latent_dim, 128), nn.ReLU(), nn.Linear(128, 5) ) def forward(self, images, traffic_condition): """ 前向传播 Args: images: 图像序列 (B, T, C, H, W) traffic_condition: 交通条件编码 (B, 64) Returns: predictions: 未来状态预测 (B, num_future_steps, 5) """ B, T = images.shape[:2] current_latent = self.encoder(images[:, -1]) predictions = [] latent = current_latent for step in range(self.num_future_steps): latent = self.dynamics( torch.cat([latent, traffic_condition], dim=-1) ) state = self.predictor(latent) predictions.append(state) return torch.stack(predictions, dim=1)
|