diff --git a/src/enhance_pedestrian_safety/config_manager.py b/src/enhance_pedestrian_safety/config_manager.py index 0ef2d0bff3..183cb3cab3 100644 --- a/src/enhance_pedestrian_safety/config_manager.py +++ b/src/enhance_pedestrian_safety/config_manager.py @@ -36,8 +36,11 @@ def load_config(config_file=None): 'range': 100, 'points_per_second': 56000, 'rotation_frequency': 10, - 'max_points_per_frame': 100000, - 'downsample_ratio': 0.5 + 'max_points_per_frame': 50000, # 优化点6:减少最大点数 + 'downsample_ratio': 0.3, # 优化点6:增加下采样比例 + 'memory_warning_threshold': 350, # 新增:内存警告阈值(MB) + 'max_batch_memory_mb': 50, # 新增:批次最大内存 + 'v2x_save_interval': 5 # 新增:V2X格式保存间隔 } }, 'v2x': { @@ -47,7 +50,8 @@ def load_config(config_file=None): 'latency_mean': 0.05, 'latency_std': 0.01, 'packet_loss_rate': 0.01, - 'message_types': ['bsm', 'spat', 'map', 'rsm'] + 'message_types': ['bsm', 'spat', 'map', 'rsm'], + 'update_interval': 2.0 # 优化点6:V2X更新间隔 }, 'cooperative': { 'num_coop_vehicles': 2, @@ -81,11 +85,16 @@ def load_config(config_file=None): 'batch_size': 10, 'enable_compression': True, 'enable_downsampling': True, - 'max_points_per_frame': 100000 + 'max_points_per_frame': 50000, # 与sensors配置保持一致 + 'memory_warning_threshold': 350, + 'max_batch_memory_mb': 50, + 'v2x_save_interval': 5 }, 'fusion': { 'fusion_cache_size': 100 - } + }, + 'sensor_cleanup_timeout': 0.5, # 新增:传感器清理超时 + 'frame_rate_limit': 5.0 # 新增:帧率限制 }, 'output': { 'data_dir': 'cvips_dataset', @@ -150,6 +159,9 @@ def merge_args(config, args): if hasattr(args, 'enable_v2x') and args.enable_v2x: config['v2x']['enabled'] = True + # 如果通过命令行启用V2X,使用更保守的更新间隔 + if 'update_interval' not in config['v2x']: + config['v2x']['update_interval'] = 2.0 if hasattr(args, 'enable_enhancement') and args.enable_enhancement: config['enhancement']['enabled'] = True @@ -157,6 +169,9 @@ def merge_args(config, args): if hasattr(args, 'enable_lidar') and args.enable_lidar: config['sensors']['lidar_sensors'] = 1 config['output']['save_lidar'] = True + # 优化:如果启用LiDAR,使用更保守的设置 + config['sensors']['lidar_config']['max_points_per_frame'] = 50000 + config['sensors']['lidar_config']['downsample_ratio'] = 0.3 if hasattr(args, 'enable_fusion') and args.enable_fusion: config['output']['save_fusion'] = True @@ -185,6 +200,9 @@ def merge_args(config, args): if hasattr(args, 'enable_downsampling') and args.enable_downsampling: config['performance']['enable_downsampling'] = True + # 如果启用下采样,使用更保守的设置 + config['sensors']['lidar_config']['downsample_ratio'] = 0.3 + config['performance']['lidar_processing']['max_points_per_frame'] = 50000 if hasattr(args, 'output_format') and args.output_format: config['output']['output_format'] = args.output_format diff --git a/src/enhance_pedestrian_safety/lidar_processor.py b/src/enhance_pedestrian_safety/lidar_processor.py index da2590994e..654aa674b8 100644 --- a/src/enhance_pedestrian_safety/lidar_processor.py +++ b/src/enhance_pedestrian_safety/lidar_processor.py @@ -29,10 +29,15 @@ def __init__(self, output_dir, config=None): self.enable_compression = config.get('enable_compression', True) if config else True self.compression_level = config.get('compression_level', 3) if config else 3 - # 内存管理 - self.max_points_per_frame = config.get('max_points_per_frame', 100000) if config else 100000 + # 内存管理 - 优化点3:更保守的内存设置 + self.max_points_per_frame = config.get('max_points_per_frame', 50000) if config else 50000 # 减少到5万点 self.enable_downsampling = config.get('enable_downsampling', True) if config else True - self.downsample_ratio = config.get('downsample_ratio', 0.5) if config else 0.5 + self.downsample_ratio = config.get('downsample_ratio', 0.3) if config else 0.3 # 增加下采样比例 + + # 新增:内存监控和限制 + self.memory_warning_threshold = config.get('memory_warning_threshold', 300) if config else 300 # MB + self.max_batch_memory_mb = config.get('max_batch_memory_mb', 50) if config else 50 # 批次最大内存 + self.v2x_save_interval = config.get('v2x_save_interval', 5) if config else 5 # 每5帧保存一次V2X格式 self._init_calibration_files() @@ -70,34 +75,65 @@ def _init_calibration_files(self): def process_lidar_data(self, lidar_data, frame_num): with self.data_lock: - self.frame_counter = frame_num + try: + self.frame_counter = frame_num - points = self._carla_lidar_to_numpy(lidar_data) + points = self._carla_lidar_to_numpy(lidar_data) - if points is None or points.shape[0] == 0: - return None + if points is None or points.shape[0] == 0: + print(f"警告: LiDAR数据为空或无效 (帧 {frame_num})") + return None - # 下采样以减少内存使用 - if self.enable_downsampling and points.shape[0] > self.max_points_per_frame: - points = self._downsample_point_cloud(points) + # 检查内存使用情况 + if self._check_memory_usage(): + print(f"警告: 内存使用过高,跳过LiDAR处理 (帧 {frame_num})") + return None - # 添加到批处理 - self.point_cloud_batch.append((frame_num, points)) + # 下采样以减少内存使用 + original_count = points.shape[0] + if self.enable_downsampling and points.shape[0] > self.max_points_per_frame: + points = self._downsample_point_cloud(points) + print(f"LiDAR下采样: {original_count} -> {points.shape[0]} 点 (帧 {frame_num})") - # 如果达到批处理大小,保存批处理数据 - if len(self.point_cloud_batch) >= self.batch_size: - self._save_batch() + # 检查批次内存使用 + batch_memory_mb = self._estimate_batch_memory_mb() + if batch_memory_mb > self.max_batch_memory_mb: + print(f"批处理内存过高 ({batch_memory_mb:.1f}MB),强制保存当前批次") + self._save_batch() + + # 添加到批处理 + self.point_cloud_batch.append((frame_num, points)) + + # 如果达到批处理大小,保存批处理数据 + if len(self.point_cloud_batch) >= self.batch_size: + self._save_batch() + + # 保存单个文件(向后兼容) + bin_path = self._save_as_bin(points, frame_num) + npy_path = self._save_as_npy(points, frame_num) + + # 生成V2XFormer兼容格式 - 优化点:每v2x_save_interval帧保存一次 + v2xformer_path = None + if frame_num % self.v2x_save_interval == 0: + try: + v2xformer_path = self._save_as_v2xformer_format(points, frame_num) + except Exception as e: + print(f"警告: V2XFormer格式保存失败: {e}") + v2xformer_path = None - # 保存单个文件(向后兼容) - bin_path = self._save_as_bin(points, frame_num) - npy_path = self._save_as_npy(points, frame_num) + metadata = self._generate_metadata(points, bin_path, npy_path, v2xformer_path) - # 生成V2XFormer兼容格式 - v2xformer_path = self._save_as_v2xformer_format(points, frame_num) + # 定期清理内存 + if frame_num % 20 == 0: + gc.collect() - metadata = self._generate_metadata(points, bin_path, npy_path, v2xformer_path) + return metadata - return metadata + except Exception as e: + print(f"处理LiDAR数据失败: {e}") + # 强制清理内存 + gc.collect() + return None def _carla_lidar_to_numpy(self, lidar_data): """高效转换LiDAR数据""" @@ -146,6 +182,33 @@ def _downsample_point_cloud(self, points): return downsampled + def _estimate_batch_memory_mb(self): + """估计批处理内存使用""" + if not self.point_cloud_batch: + return 0 + + total_points = 0 + for _, points in self.point_cloud_batch: + total_points += points.shape[0] + + # 每个点3个float32 (12字节),加上一些额外开销 + memory_bytes = total_points * 12 * 1.2 # 20%额外开销 + return memory_bytes / (1024 * 1024) + + def _check_memory_usage(self): + """检查内存使用情况""" + try: + import psutil + process = psutil.Process() + memory_mb = process.memory_info().rss / (1024 * 1024) + + if memory_mb > self.memory_warning_threshold: + print(f"警告: 进程内存使用过高: {memory_mb:.1f}MB") + return True + return False + except: + return False + def _save_batch(self): """批量保存点云数据(提高效率)""" if not self.point_cloud_batch: @@ -153,6 +216,7 @@ def _save_batch(self): batch_data = [] for frame_num, points in self.point_cloud_batch: + # 只保存点的坐标,不保存完整对象 batch_data.append({ 'frame_num': frame_num, 'points': points.tolist(), @@ -167,15 +231,28 @@ def _save_batch(self): # 压缩保存 if self.enable_compression: - compressed_data = zlib.compress( - json.dumps(batch_data).encode('utf-8'), - level=self.compression_level - ) - with open(batch_path, 'wb') as f: - f.write(compressed_data) + try: + json_str = json.dumps(batch_data) + compressed_data = zlib.compress( + json_str.encode('utf-8'), + level=self.compression_level + ) + with open(batch_path, 'wb') as f: + f.write(compressed_data) + except Exception as e: + print(f"压缩保存批处理数据失败: {e}") + # 尝试非压缩保存 + try: + with open(batch_path, 'w') as f: + json.dump(batch_data, f) + except Exception as e2: + print(f"非压缩保存批处理数据失败: {e2}") else: - with open(batch_path, 'w') as f: - json.dump(batch_data, f) + try: + with open(batch_path, 'w') as f: + json.dump(batch_data, f) + except Exception as e: + print(f"保存批处理数据失败: {e}") # 清理批处理缓存 self.point_cloud_batch.clear() @@ -192,7 +269,11 @@ def _save_as_bin(self, points, frame_num): points_with_intensity[:, :3] = points points_with_intensity[:, 3] = 1.0 # 强度值 - points_with_intensity.tofile(bin_path) + try: + points_with_intensity.tofile(bin_path) + except Exception as e: + print(f"保存BIN文件失败: {e}") + return None return bin_path @@ -200,20 +281,29 @@ def _save_as_npy(self, points, frame_num): """保存为numpy格式""" npy_path = os.path.join(self.lidar_dir, f"lidar_{frame_num:06d}.npy") - # 使用内存映射减少内存使用 - memmap_array = np.lib.format.open_memmap( - npy_path, - mode='w+', - dtype=np.float32, - shape=points.shape - ) - memmap_array[:] = points[:] - memmap_array.flush() + try: + # 使用内存映射减少内存使用 + memmap_array = np.lib.format.open_memmap( + npy_path, + mode='w+', + dtype=np.float32, + shape=points.shape + ) + memmap_array[:] = points[:] + memmap_array.flush() + except Exception as e: + print(f"保存NPY文件失败: {e}") + # 备选保存方式 + try: + np.save(npy_path, points) + except Exception as e2: + print(f"备选保存NPY文件失败: {e2}") + return None return npy_path def _save_as_v2xformer_format(self, points, frame_num): - """保存为V2XFormer兼容格式""" + """保存为V2XFormer兼容格式 - 优化点:减少保存频率""" v2x_dir = os.path.join(self.output_dir, "v2xformer_format") os.makedirs(v2x_dir, exist_ok=True) @@ -251,61 +341,18 @@ def _save_as_v2xformer_format(self, points, frame_num): return filepath except Exception as e: - # 如果保存失败,只打印警告,不抛出异常 print(f"警告: 保存V2XFormer格式失败: {e}") return None - def process_lidar_data(self, lidar_data, frame_num): - with self.data_lock: - try: - self.frame_counter = frame_num - - points = self._carla_lidar_to_numpy(lidar_data) - - if points is None or points.shape[0] == 0: - print(f"警告: LiDAR数据为空或无效 (帧 {frame_num})") - return None - - # 下采样以减少内存使用 - if self.enable_downsampling and points.shape[0] > self.max_points_per_frame: - original_count = points.shape[0] - points = self._downsample_point_cloud(points) - print(f"LiDAR下采样: {original_count} -> {points.shape[0]} 点 (帧 {frame_num})") - - # 添加到批处理 - self.point_cloud_batch.append((frame_num, points)) - - # 如果达到批处理大小,保存批处理数据 - if len(self.point_cloud_batch) >= self.batch_size: - self._save_batch() - - # 保存单个文件(向后兼容) - bin_path = self._save_as_bin(points, frame_num) - npy_path = self._save_as_npy(points, frame_num) - - # 生成V2XFormer兼容格式(如果失败则忽略) - try: - v2xformer_path = self._save_as_v2xformer_format(points, frame_num) - except Exception as e: - v2xformer_path = None - print(f"警告: V2XFormer格式保存失败: {e}") - - metadata = self._generate_metadata(points, bin_path, npy_path, v2xformer_path) - - return metadata - - except Exception as e: - print(f"处理LiDAR数据失败: {e}") - return None def _generate_metadata(self, points, bin_path, npy_path, v2xformer_path): metadata = { 'frame_id': self.frame_counter, 'timestamp': datetime.now().isoformat(), 'point_count': int(points.shape[0]), 'file_paths': { - 'bin': os.path.basename(bin_path), - 'npy': os.path.basename(npy_path), - 'v2xformer': os.path.basename(v2xformer_path) + 'bin': os.path.basename(bin_path) if bin_path else None, + 'npy': os.path.basename(npy_path) if npy_path else None, + 'v2xformer': os.path.basename(v2xformer_path) if v2xformer_path else None }, 'statistics': { 'x_range': [float(points[:, 0].min()), float(points[:, 0].max())], @@ -318,13 +365,17 @@ def _generate_metadata(self, points, bin_path, npy_path, v2xformer_path): 'downsampled': self.enable_downsampling and points.shape[0] > self.max_points_per_frame, 'downsample_ratio': self.downsample_ratio if self.enable_downsampling else 1.0, 'compression_enabled': self.enable_compression, - 'compression_level': self.compression_level + 'compression_level': self.compression_level, + 'v2x_saved': v2xformer_path is not None } } meta_path = os.path.join(self.lidar_dir, f"lidar_meta_{self.frame_counter:06d}.json") - with open(meta_path, 'w') as f: - json.dump(metadata, f, indent=2) + try: + with open(meta_path, 'w') as f: + json.dump(metadata, f, indent=2) + except Exception as e: + print(f"保存LiDAR元数据失败: {e}") return metadata @@ -349,18 +400,21 @@ def generate_lidar_summary(self): total_size = 0 # 采样统计,避免读取所有文件 - sample_files = bin_files[:10] if len(bin_files) > 10 else bin_files + sample_files = bin_files[:5] if len(bin_files) > 5 else bin_files # 减少采样数量 for bin_file in sample_files: if os.path.exists(bin_file): - file_size = os.path.getsize(bin_file) - total_size += file_size - # 估算点数 - points_in_file = file_size // (4 * 4) # 4个float32,每个4字节 - total_points += points_in_file + try: + file_size = os.path.getsize(bin_file) + total_size += file_size + # 估算点数 + points_in_file = file_size // (4 * 4) # 4个float32,每个4字节 + total_points += points_in_file + except: + continue # 根据采样估算总数 - if sample_files: - avg_points_per_file = total_points / len(sample_files) + if sample_files and len(bin_files) > 0: + avg_points_per_file = total_points / max(len(sample_files), 1) total_points_estimated = avg_points_per_file * len(bin_files) else: total_points_estimated = 0 @@ -382,8 +436,11 @@ def generate_lidar_summary(self): } summary_path = os.path.join(self.output_dir, "metadata", "lidar_summary.json") - with open(summary_path, 'w') as f: - json.dump(summary, f, indent=2) + try: + with open(summary_path, 'w') as f: + json.dump(summary, f, indent=2) + except Exception as e: + print(f"保存LiDAR摘要失败: {e}") return summary @@ -398,7 +455,7 @@ def __init__(self, output_dir, config=None): self.calibration_data = {} self.fusion_cache = {} # 融合缓存 - self.cache_size = config.get('fusion_cache_size', 100) if config else 100 + self.cache_size = config.get('fusion_cache_size', 50) if config else 50 # 减少缓存大小 self._load_calibration() @@ -413,32 +470,15 @@ def _load_calibration(self): if file.endswith('.json'): calibration_files.append(os.path.join(root, file)) - # 并行读取校准文件 - import concurrent.futures - with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: - future_to_file = { - executor.submit(self._load_calibration_file, file): file - for file in calibration_files - } - - for future in concurrent.futures.as_completed(future_to_file): - file = future_to_file[future] - try: - sensor_name, data = future.result() - if sensor_name and data: - self.calibration_data[sensor_name] = data - except Exception as e: - print(f"加载校准文件 {file} 失败: {e}") - - def _load_calibration_file(self, filepath): - """加载单个校准文件""" - try: - with open(filepath, 'r') as f: - data = json.load(f) - sensor_name = os.path.basename(filepath).replace('.json', '') - return sensor_name, data - except: - return None, None + # 限制同时读取的文件数量 + for file in calibration_files[:10]: # 最多读取10个文件 + try: + with open(file, 'r') as f: + data = json.load(f) + sensor_name = os.path.basename(file).replace('.json', '') + self.calibration_data[sensor_name] = data + except Exception as e: + print(f"加载校准文件 {file} 失败: {e}") def create_synchronization_file(self, frame_num, sensor_data): """创建同步文件(优化版)""" @@ -521,7 +561,7 @@ def generate_fusion_report(self): # 解析文件名获取帧范围(避免读取所有文件) frame_ids = [] - for sync_file in sync_files: + for sync_file in sync_files[:20]: # 只检查前20个文件 try: # 从文件名提取帧号 filename = os.path.basename(sync_file) diff --git a/src/enhance_pedestrian_safety/main.py b/src/enhance_pedestrian_safety/main.py index 990a4d0db5..872899a477 100644 --- a/src/enhance_pedestrian_safety/main.py +++ b/src/enhance_pedestrian_safety/main.py @@ -250,12 +250,12 @@ def error(msg): @staticmethod def debug(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + timestamp = datetime.now().strftime("%Y-%m-d %H:%M:%S") print(f"[DEBUG][{timestamp}] {msg}") @staticmethod def performance(msg): - timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + timestamp = datetime.now().strftime("%Y-%m-d %H:%M:%S") print(f"[PERF][{timestamp}] {msg}") @@ -663,43 +663,34 @@ def _spawn_pedestrians(self, center_location): return spawned def cleanup(self): - Log.info("清理交通...") - - # 批量销毁 - actors_to_destroy = [] + """安全清理所有资源""" + Log.info("开始清理交通管理器...") - for vehicle in self.vehicles: - if vehicle.is_alive: - actors_to_destroy.append(vehicle) + try: + # 清理行人 + for pedestrian in self.pedestrians: + try: + if pedestrian and pedestrian.is_alive: + pedestrian.destroy() + except: + pass - for pedestrian in self.pedestrians: - if pedestrian.is_alive: - actors_to_destroy.append(pedestrian) + self.pedestrians.clear() - # 批量销毁 - batch_size = 10 - for i in range(0, len(actors_to_destroy), batch_size): - batch = actors_to_destroy[i:i + batch_size] - for actor in batch: + # 清理背景车辆 + for vehicle in self.vehicles: try: - actor.destroy() + if vehicle and vehicle.is_alive: + vehicle.destroy() except: pass - # 避免过快的销毁速度 - if i > 0 and i % 30 == 0: - time.sleep(0.1) + self.vehicles.clear() - self.vehicles.clear() - self.pedestrians.clear() + Log.info("交通管理器清理完成") - # 清理统计 - self.spawn_stats = { - 'total_attempts': 0, - 'successful_spawns': 0, - 'failed_spawns': 0, - 'total_spawn_time': 0 - } + except Exception as e: + Log.error(f"清理交通管理器失败: {e}") class SensorManager: @@ -713,6 +704,13 @@ def __init__(self, world, config, data_dir): self.last_capture_time = 0 self.last_performance_sample = 0 + # 新增:帧率控制相关变量 + self.target_fps = config['performance'].get('frame_rate_limit', 5.0) + self.min_frame_interval = 1.0 / self.target_fps if self.target_fps > 0 else 0.1 + self.last_frame_time = 0 + self.frame_skip_count = 0 + self.max_frame_skip = 2 + self.vehicle_buffer = {} self.infra_buffer = {} self.buffer_lock = threading.Lock() @@ -729,14 +727,14 @@ def __init__(self, world, config, data_dir): self.fusion_manager = None # 批处理设置 - self.batch_size = config.get('batch_size', 5) + self.batch_size = config['performance'].get('batch_size', 5) self.enable_async_processing = config.get('enable_async_processing', True) if config['sensors'].get('lidar_sensors', 0) > 0: - self.lidar_processor = LidarProcessor(data_dir, config.get('lidar_processing', {})) + self.lidar_processor = LidarProcessor(data_dir, config['performance'].get('lidar_processing', {})) if config['output'].get('save_fusion', False): - self.fusion_manager = MultiSensorFusion(data_dir, config.get('fusion', {})) + self.fusion_manager = MultiSensorFusion(data_dir, config['performance'].get('fusion', {})) # 传感器统计 self.sensor_stats = { @@ -927,9 +925,24 @@ def callback(image): current_time = time.time() frame_start_time = time.time() + # 优化点2:添加帧率控制逻辑 + # 检查是否达到最小帧间隔 + time_since_last_frame = current_time - self.last_frame_time + if time_since_last_frame < self.min_frame_interval: + # 如果帧间隔太小,跳过此帧 + self.frame_skip_count += 1 + if self.frame_skip_count > self.max_frame_skip: + # 如果连续跳过多帧,强制处理一帧 + self.frame_skip_count = 0 + else: + return # 跳过此帧 + else: + self.frame_skip_count = 0 + if current_time - self.last_capture_time >= capture_interval: self.frame_counter += 1 self.last_capture_time = current_time + self.last_frame_time = current_time # 记录帧处理时间 capture_start = time.time() filename = os.path.join(save_dir, f"{name}_{self.frame_counter:06d}.png") @@ -943,13 +956,34 @@ def callback(image): if sensor_type == 'vehicle': self.vehicle_buffer[name] = filename if len(self.vehicle_buffer) >= 4: - self.image_processor.stitch(self.vehicle_buffer, self.frame_counter, - f'vehicle_{vehicle_id}') + # 使用线程池异步处理图像拼接,避免阻塞 + if self.enable_async_processing: + import concurrent.futures + with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: + future = executor.submit( + self.image_processor.stitch, + self.vehicle_buffer.copy(), + self.frame_counter, + f'vehicle_{vehicle_id}' + ) + else: + self.image_processor.stitch(self.vehicle_buffer, self.frame_counter, + f'vehicle_{vehicle_id}') self.vehicle_buffer.clear() else: self.infra_buffer[name] = filename if len(self.infra_buffer) >= 4: - self.image_processor.stitch(self.infra_buffer, self.frame_counter, 'infrastructure') + if self.enable_async_processing: + import concurrent.futures + with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: + future = executor.submit( + self.image_processor.stitch, + self.infra_buffer.copy(), + self.frame_counter, + 'infrastructure' + ) + else: + self.image_processor.stitch(self.infra_buffer, self.frame_counter, 'infrastructure') self.infra_buffer.clear() # 定期采样性能 @@ -1023,55 +1057,55 @@ def cleanup(self): """安全清理传感器""" Log.info(f"安全清理 {len(self.sensors)} 个传感器...") - # 设置运行状态为False,防止回调函数继续执行 + # 1. 首先设置运行状态为False,防止回调函数继续执行 self.is_running = False - # 等待一小段时间让回调函数结束 - time.sleep(0.1) + # 2. 停止所有传感器的监听 + Log.info("停止所有传感器监听...") + for sensor in self.sensors: + try: + if hasattr(sensor, 'stop'): + sensor.stop() + time.sleep(0.001) # 短暂延迟 + except: + pass + + # 3. 等待一小段时间让所有传感器回调结束 + time.sleep(0.2) - # 刷新批处理数据 + # 4. 刷新批处理数据 if self.lidar_processor: try: self.lidar_processor.flush_batch() - except Exception as e: - Log.warning(f"刷新LiDAR批处理数据失败: {e}") - - # 批量销毁传感器(从后向前销毁,避免依赖问题) - if self.sensors: - Log.info(f"销毁 {len(self.sensors)} 个传感器...") - - # 首先停止所有传感器的监听 - for sensor in self.sensors: - try: - if sensor and sensor.is_alive: - sensor.stop() - except Exception as e: - Log.debug(f"停止传感器失败: {e}") - - # 等待一小段时间让传感器完全停止 - time.sleep(0.05) - - # 然后销毁传感器 - for i, sensor in enumerate(reversed(self.sensors)): - try: - if sensor and sensor.is_alive: - sensor.destroy() - except Exception as e: - Log.debug(f"销毁传感器失败: {e}") + except: + pass - # 分批销毁,避免一次性销毁太多 - if i % 5 == 0: - time.sleep(0.01) + # 5. 销毁传感器 + Log.info(f"销毁 {len(self.sensors)} 个传感器...") + for i, sensor in enumerate(self.sensors): + try: + if hasattr(sensor, 'destroy'): + sensor.destroy() + except: + pass + # 分批销毁,避免一次性销毁太多 + if i % 5 == 0: + time.sleep(0.01) self.sensors.clear() - # 清理缓存 + # 6. 清理缓存和内存 if hasattr(self.image_processor, 'image_cache'): try: self.image_processor.image_cache.clear() except: pass + # 7. 清理数据结构 + self.vehicle_buffer.clear() + self.infra_buffer.clear() + + # 8. 强制垃圾回收 gc.collect() Log.info("传感器清理完成") @@ -1251,23 +1285,58 @@ def collect_data(self): last_perception_share = time.time() last_performance_sample = time.time() last_detailed_log = time.time() + last_memory_check = time.time() + + # 内存监控变量 + memory_warning_issued = False + forced_cleanup_count = 0 try: while time.time() - self.start_time < duration and self.is_running: current_time = time.time() elapsed = current_time - self.start_time + # 内存检查 - 优化点4:定期检查内存使用 + if current_time - last_memory_check >= 2.0: # 更频繁的内存检查 + try: + import psutil + process = psutil.Process() + memory_mb = process.memory_info().rss / (1024 * 1024) + + if memory_mb > 350 and not memory_warning_issued: + Log.warning(f"内存使用较高: {memory_mb:.1f}MB,将减少数据处理") + memory_warning_issued = True + + # 如果内存过高,跳过一些非关键操作 + if self.v2x_communication: + Log.debug("内存警告:暂停部分V2X消息处理") + + elif memory_mb > 400: # 如果内存超过400MB,提前结束 + Log.error(f"内存使用过高: {memory_mb:.1f}MB,提前结束数据收集") + break + + elif memory_mb < 300: + memory_warning_issued = False + forced_cleanup_count = 0 + + except Exception as e: + Log.debug(f"内存检查失败: {e}") + + last_memory_check = current_time + # 更新车辆状态 if self.multi_vehicle_manager: self.multi_vehicle_manager.update_vehicle_states() - # V2X通信更新 - if self.v2x_communication and current_time - last_v2x_update >= 0.1: + # V2X通信更新 - 使用配置中的更新间隔 + v2x_interval = self.config['v2x'].get('update_interval', 2.0) + if self.v2x_communication and current_time - last_v2x_update >= v2x_interval: self._update_v2x_communication() last_v2x_update = current_time - # 共享感知数据 - if (self.config['cooperative'].get('enable_shared_perception', True) and + # 共享感知数据 - 仅在内存允许时进行 + if (not memory_warning_issued and + self.config['cooperative'].get('enable_shared_perception', True) and current_time - last_perception_share >= 2.0): self._share_perception_data() last_perception_share = current_time @@ -1348,6 +1417,133 @@ def collect_data(self): if self.output_format != 'standard': self._convert_to_target_format() + def _force_memory_cleanup(self): + """强制内存清理""" + Log.info("执行强制内存清理...") + + # 清理图像处理器缓存 + for sensor_manager in self.sensor_managers.values(): + if hasattr(sensor_manager, 'image_processor'): + if hasattr(sensor_manager.image_processor, 'image_cache'): + try: + old_size = len(sensor_manager.image_processor.image_cache) + sensor_manager.image_processor.image_cache.clear() + Log.debug(f"清理图像缓存: 释放了{old_size}个缓存项") + except: + pass + + # 清理LiDAR批处理 + for sensor_manager in self.sensor_managers.values(): + if hasattr(sensor_manager, 'lidar_processor'): + try: + sensor_manager.lidar_processor.flush_batch() + Log.debug("刷新LiDAR批处理数据") + except: + pass + + # 强制垃圾回收 + gc.collect() + + Log.info("强制内存清理完成") + + def cleanup(self): + """安全清理所有资源 - 完全重写的版本""" + Log.info("开始安全清理场景...") + + cleanup_start = time.time() + + try: + # 1. 设置运行状态为False + self.is_running = False + + # 2. 等待一小段时间让循环结束 + time.sleep(0.2) + + # 3. 停止V2X通信 + if self.v2x_communication: + try: + Log.info("停止V2X通信...") + self.v2x_communication.stop() + except: + pass + + # 4. 清理传感器(最重要,必须先于车辆) + Log.info(f"清理 {len(self.sensor_managers)} 个传感器管理器...") + for vehicle_id, sensor_manager in self.sensor_managers.items(): + try: + sensor_manager.cleanup() + except: + pass + + self.sensor_managers.clear() + time.sleep(0.1) # 等待传感器清理完成 + + # 5. 清理多车辆管理器 + if self.multi_vehicle_manager: + try: + Log.info("清理多车辆管理器...") + self.multi_vehicle_manager.cleanup() + except: + pass + + # 6. 清理背景交通 + if self.traffic_manager: + try: + Log.info("清理交通管理器...") + self.traffic_manager.cleanup() + except: + pass + + # 7. 清理主车(最后清理) + Log.info(f"清理 {len(self.ego_vehicles)} 个主车...") + for vehicle in self.ego_vehicles: + try: + if hasattr(vehicle, 'destroy'): + vehicle.destroy() + except: + pass + time.sleep(0.01) # 短暂延迟 + + self.ego_vehicles.clear() + + # 8. 重置世界设置 + try: + if self.world: + default_weather = carla.WeatherParameters() + self.world.set_weather(default_weather) + except: + pass + + # 9. 强制垃圾回收 + gc.collect() + + # 10. 等待确保清理完成 + time.sleep(0.3) + + # 11. 释放引用 + self.traffic_manager = None + self.multi_vehicle_manager = None + self.v2x_communication = None + self.scene_center = None + + # 最后一次强制垃圾回收 + gc.collect() + + Log.info(f"清理完成,总用时: {time.time() - cleanup_start:.2f}秒") + + except Exception as e: + Log.error(f"清理过程中发生错误: {e}") + # 紧急清理:强制释放所有引用 + try: + self.sensor_managers.clear() + self.ego_vehicles.clear() + self.traffic_manager = None + self.multi_vehicle_manager = None + self.v2x_communication = None + gc.collect() + except: + pass + def _convert_to_target_format(self): """转换为目标数据格式""" Log.info(f"转换为 {self.output_format} 格式...") @@ -1443,7 +1639,7 @@ def _update_v2x_communication(self): # 为每辆车发送基本安全消息 for vehicle in self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles: - if not vehicle.is_alive: + if not hasattr(vehicle, 'is_alive') or not vehicle.is_alive: continue try: @@ -1478,7 +1674,7 @@ def _share_perception_data(self): # 模拟车辆感知数据(简化处理,实际应从传感器获取) for vehicle in self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles: - if not vehicle.is_alive: + if not hasattr(vehicle, 'is_alive') or not vehicle.is_alive: continue # 模拟检测到的物体 @@ -1493,7 +1689,7 @@ def _simulate_object_detection(self, vehicle): # 获取车辆周围的其他车辆 for other_vehicle in self.ego_vehicles + self.multi_vehicle_manager.cooperative_vehicles: - if other_vehicle.id == vehicle.id or not other_vehicle.is_alive: + if other_vehicle.id == vehicle.id or not hasattr(other_vehicle, 'is_alive') or not other_vehicle.is_alive: continue try: @@ -1627,91 +1823,6 @@ def run_analysis(self): Log.info("运行数据分析...") DataAnalyzer.analyze_dataset(self.output_dir) - def cleanup(self): - """安全清理所有资源 - 修复的方法""" - Log.info("开始安全清理场景...") - - try: - # 1. 先停止数据收集 - self.is_running = False - - # 2. 等待一小段时间让所有传感器回调结束 - time.sleep(0.2) - - # 3. 清理传感器(这是最重要的,必须在清理车辆之前) - for sensor_manager in self.sensor_managers.values(): - try: - sensor_manager.cleanup() - except Exception as e: - Log.error(f"清理传感器管理器失败: {e}") - - self.sensor_managers.clear() - - # 4. 清理V2X通信 - if self.v2x_communication: - try: - self.v2x_communication.stop() - except Exception as e: - Log.error(f"停止V2X通信失败: {e}") - - # 5. 清理协同管理 - if self.multi_vehicle_manager: - try: - self.multi_vehicle_manager.cleanup() - except Exception as e: - Log.error(f"清理协同管理器失败: {e}") - - # 6. 清理背景交通(必须在清理主车之前) - if self.traffic_manager: - try: - self.traffic_manager.cleanup() - except Exception as e: - Log.error(f"清理交通管理器失败: {e}") - - # 7. 最后清理主车(在传感器和交通清理完之后) - for vehicle in self.ego_vehicles: - try: - if vehicle and vehicle.is_alive: - vehicle.destroy() - except Exception as e: - Log.debug(f"销毁主车失败: {e}") - - self.ego_vehicles.clear() - - # 8. 强制垃圾回收 - gc.collect() - - # 9. 重置CARLA世界(可选,但可以帮助清理) - try: - if self.world: - # 重置天气到默认 - default_weather = carla.WeatherParameters() - self.world.set_weather(default_weather) - except Exception as e: - Log.debug(f"重置世界设置失败: {e}") - - # 10. 等待一段时间确保所有清理完成 - time.sleep(0.5) - - Log.info("清理完成") - - except Exception as e: - Log.error(f"清理过程中发生错误: {e}") - import traceback - traceback.print_exc() - - # 即使出错也要尝试强制清理 - try: - # 强制删除所有引用,让Python垃圾回收器工作 - del self.sensor_managers - del self.traffic_manager - del self.multi_vehicle_manager - del self.v2x_communication - del self.ego_vehicles - gc.collect() - except: - pass - def main(): parser = argparse.ArgumentParser(description='CVIPS 性能优化数据收集系统 v12.0')