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 191 192 193 194 195 196 197 198 199 200 201
| """ NVIDIA Cosmos Transfer: 场景变体生成
从单个重建场景生成百万级变体: - 交通行为变化 - 天气/光照变化 - 传感器配置变化 - 座舱乘员变化 """
import numpy as np from dataclasses import dataclass, field from typing import List, Dict import torch
@dataclass class CosmosConfig: """Cosmos Transfer 配置""" base_model: str = "Cosmos-3" mode: str = "transfer" variation_axes: Dict = field(default_factory=lambda: { 'weather': ['clear', 'rain', 'fog', 'snow', 'night'], 'lighting': ['dawn', 'noon', 'dusk', 'night', 'tunnel'], 'behavior': ['normal', 'aggressive', 'drowsy', 'distracted'], 'occupant': ['adult', 'child', 'infant', 'empty', 'pet'], 'traffic': ['sparse', 'normal', 'dense', 'congested'], }) n_variations: int = 1000 physics_validation: bool = True output_format: str = "OpenUSD"
def generate_cabin_variations(base_scene: dict, config: CosmosConfig) -> List[dict]: """ 从基础座舱场景生成变体 Args: base_scene: 基础场景参数 config: Cosmos 配置 Returns: variations: 变体场景列表 """ variations = [] for i in range(config.n_variations): var = { 'scene_id': f"var_{i:04d}", 'weather': np.random.choice(config.variation_axes['weather']), 'lighting': np.random.choice(config.variation_axes['lighting']), 'behavior': np.random.choice(config.variation_axes['behavior']), 'occupant': np.random.choice(config.variation_axes['occupant']), 'traffic': np.random.choice(config.variation_axes['traffic']), 'physics': { 'gravity': 9.81, 'friction': np.random.uniform(0.3, 0.8), 'temperature': np.random.uniform(-10, 40), } } variations.append(var) return variations
class CosmosReasonValidator: """ Cosmos Reason: 物理合理性验证 过滤掉物理不可能的生成场景 """ def __init__(self): self.validation_rules = { 'gravity_check': True, 'collision_check': True, 'object_permanence': True, 'causality': True, } def validate(self, generated_scene: dict) -> bool: """ 验证生成场景的物理合理性 Returns: valid: 是否通过物理验证 """ if not self.validation_rules['gravity_check']: return False return True
class CabinDataGenerator: """ 座舱合成数据生成器 目标: 为 DMS/OMS/CPD/OOP 生成训练数据 """ SCENARIOS = { 'DMS': { 'fatigue': ['yawning', 'head_nod', 'eye_closure', 'microsleep'], 'distraction': ['phone', 'eating', 'talking', 'adjusting'], 'emotion': ['angry', 'sad', 'happy', 'neutral', 'stressed'], 'impairment': ['alcohol_mild', 'alcohol_severe', 'medication'], }, 'OMS': { 'occupant': ['adult_driver', 'adult_passenger', 'child_seat', 'infant_seat', 'empty', 'pet'], 'posture': ['normal', 'leaning', 'sleeping', 'reaching', 'turned', 'slumped'], 'seatbelt': ['correct', 'incorrect', 'unfastened'], }, 'CPD': { 'infant_seat': ['rear_facing', 'forward_facing'], 'child_age': ['newborn', 'infant', 'toddler', 'child'], 'position': ['left_rear', 'right_rear', 'center_rear'], 'condition': ['sleeping', 'crying', 'moving', 'still'], }, 'OOP': { 'posture': ['forward_lean', 'side_lean', 'backward_lean', 'curled', 'kneeling', 'standing'], 'severity': ['mild', 'moderate', 'severe'], } } def __init__(self, config: CosmosConfig): self.config = config self.validator = CosmosReasonValidator() def generate_batch(self, category: str, n: int = 100) -> List[dict]: """ 生成指定类别的合成数据 Args: category: 'DMS', 'OMS', 'CPD', 'OOP' n: 生成数量 Returns: samples: 合成数据样本 """ scenarios = self.SCENARIOS.get(category, {}) samples = [] for i in range(n): sample = { 'id': f"{category}_{i:04d}", 'category': category, } for key, values in scenarios.items(): sample[key] = np.random.choice(values) sample['environment'] = { 'lighting': np.random.choice( self.config.variation_axes['lighting']), 'weather': np.random.choice( self.config.variation_axes['weather']), } sample['physics_valid'] = self.validator.validate(sample) samples.append(sample) return samples
if __name__ == "__main__": config = CosmosConfig() generator = CabinDataGenerator(config) for category in ['DMS', 'OMS', 'CPD', 'OOP']: samples = generator.generate_batch(category, n=500) valid = sum(1 for s in samples if s['physics_valid']) print(f"{category}: 生成 500, 物理 {valid} ({valid/5:.0f}%)") s = samples[0] print(f" 示例: {s.get('fatigue', s.get('posture', s.get('child_age', '')))}") base_scene = {'location': 'highway', 'time': 'noon'} variations = generate_cabin_variations(base_scene, config) print(f"\n基础场景变体: {len(variations)} 个") print(f"天气分布: {dict(zip(*np.unique([v['weather'] for v in variations], return_counts=True)))}")
|