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
| import numpy as np from typing import Dict, List, Any
class ISUTest: """ ISU-Test 实现 论文复现代码 """ def __init__(self, vlm_model: str, features: Dict): self.vlm = self.load_vlm(vlm_model) self.features = features self.renderer = SceneRenderer("bmw_ix3_cabin") def run(self, budget: int = 1000) -> List[Dict]: """ 运行测试 Args: budget: 测试预算 Returns: failures: 失败案例列表 """ failures = [] population = self.initialize_population(budget // 10) for generation in range(budget // 10): fitness_list = [] for scene in population: image = self.renderer.render(scene) output = self.vlm.inference(image) fitness = self.compute_fitness(output, scene) fitness_list.append(fitness) for scene, fitness in zip(population, fitness_list): if self.oracle(fitness): failures.append({ "scene": scene, "fitness": fitness }) population = self.evolve(population, fitness_list) return failures def compute_fitness(self, output: Dict, ground_truth: Dict) -> np.ndarray: """计算适应度""" fitness = [] for key, gt_value in ground_truth.items(): pred_value = output.get(key) fitness.append(0 if pred_value == gt_value else 1) return np.array(fitness) def oracle(self, fitness: np.ndarray) -> bool: """预言机判定""" return np.mean(fitness) > 0.3
if __name__ == "__main__": features = { "belt": ["on", "off", "misuse"], "phone": ["none", "handheld", "ear"], "emotion": ["happy", "neutral", "angry"] } test = ISUTest("llava-v1.6-34b", features) failures = test.run(budget=1000) print(f"发现 {len(failures)} 个失败案例") for f in failures[:5]: print(f"Scene: {f['scene']}, Fitness: {f['fitness']}")
|