From 409802d5d71fdbd1d4b7f726e79f565526afafa0 Mon Sep 17 00:00:00 2001 From: jingrow Date: Sun, 7 Dec 2025 03:36:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4rmbg=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/rmbg/service.py | 76 -------------------------------------------- 1 file changed, 76 deletions(-) diff --git a/apps/rmbg/service.py b/apps/rmbg/service.py index 43549f4..4e69f68 100644 --- a/apps/rmbg/service.py +++ b/apps/rmbg/service.py @@ -82,7 +82,6 @@ class RmbgService: self.model = self.model.to(self.device) if torch.cuda.is_available(): self.model = self.model.half() # 转换为半精度 - logger.info("模型已使用半精度(FP16)加载,显存占用减少约50%") except Exception as e: # 如果半精度加载失败,降级到全精度 logger.warning(f"半精度加载失败,使用全精度: {str(e)}") @@ -99,7 +98,6 @@ class RmbgService: # 设置显存分配器,减少碎片化 os.environ.setdefault('PYTORCH_CUDA_ALLOC_CONF', 'expandable_segments:True') torch.cuda.empty_cache() - logger.info(f"模型加载完成,当前显存占用: {torch.cuda.memory_allocated() / 1024**3:.2f} GB") def _process_image_sync(self, image): """同步处理图像,移除背景(单张)""" @@ -242,12 +240,9 @@ class RmbgService: self.queue_running = True self.queue_task = asyncio.create_task(self._queue_processor()) - logger.info("队列批处理机制已启动") async def _queue_processor(self): """后台队列批处理任务(核心逻辑)""" - logger.info("队列批处理任务开始运行") - while self.queue_running: try: # 收集一批请求 @@ -451,13 +446,6 @@ class RmbgService: batch_size = settings.batch_size loop = asyncio.get_event_loop() - # 性能统计变量 - download_time = 0.0 - gpu_inference_time = 0.0 - save_time = 0.0 - batch_count = 0 - batch_sizes = [] - stats_printed = False # 流水线队列:收集已下载的图片 download_queue = asyncio.Queue() @@ -465,51 +453,6 @@ class RmbgService: download_done_count = 0 download_error_count = 0 - def print_stats(): - """输出性能统计信息""" - nonlocal stats_printed - if stats_printed: - return - stats_printed = True - - total_time = time.time() - batch_start_time - other_time = total_time - download_time - gpu_inference_time - save_time - - logger.info("=" * 60) - logger.info("📊 批处理性能统计(流水线模式)") - logger.info("=" * 60) - logger.info(f"图片总数: {total}") - logger.info(f"成功数量: {success_count}") - logger.info(f"失败数量: {error_count}") - logger.info(f"批处理次数: {batch_count}") - logger.info(f"每批图片数: {batch_sizes}") - logger.info("-" * 60) - logger.info("⏱️ 各阶段耗时:") - - if total_time > 0: - download_pct = (download_time / total_time) * 100 - gpu_pct = (gpu_inference_time / total_time) * 100 - save_pct = (save_time / total_time) * 100 - other_pct = (other_time / total_time) * 100 - - logger.info(f" 1. 下载图片: {download_time:.3f}s ({download_pct:.1f}%)") - logger.info(f" 2. GPU推理: {gpu_inference_time:.3f}s ({gpu_pct:.1f}%)") - logger.info(f" 3. 保存图片: {save_time:.3f}s ({save_pct:.1f}%)") - logger.info(f" 4. 其他开销: {other_time:.3f}s ({other_pct:.1f}%)") - else: - logger.info(f" 1. 下载图片: {download_time:.3f}s") - logger.info(f" 2. GPU推理: {gpu_inference_time:.3f}s") - logger.info(f" 3. 保存图片: {save_time:.3f}s") - logger.info(f" 4. 其他开销: {other_time:.3f}s") - - logger.info("-" * 60) - logger.info(f"📈 总耗时: {total_time:.3f}s") - if total > 0: - avg_per_image = (total_time / total) * 1000 - logger.info(f"📈 平均每张: {avg_per_image:.1f}ms") - if batch_count > 0: - avg_batch_time = gpu_inference_time / batch_count - logger.info(f"📈 每批平均耗时: {avg_batch_time:.3f}s") async def download_image_async(index, url): """异步下载图片并放入队列""" @@ -543,7 +486,6 @@ class RmbgService: download_complete.set() # 启动所有下载任务(并行下载) - download_start_time = time.time() download_tasks = [ asyncio.create_task(download_image_async(i, url)) for i, url in enumerate(urls, 1) @@ -558,7 +500,6 @@ class RmbgService: async def process_pending_batch(force=False): """处理待处理的批次""" nonlocal pending_batch, completed_order, success_count, error_count - nonlocal gpu_inference_time, save_time, batch_count, batch_sizes if not pending_batch: return @@ -605,11 +546,7 @@ class RmbgService: # 尝试一次性处理所有图片 images_with_info = [(img, size, idx) for img, size, idx, _ in valid_items] - gpu_start_time = time.time() batch_results = await self.process_batch_images(images_with_info) - gpu_inference_time += time.time() - gpu_start_time - batch_count += 1 - batch_sizes.append(len(images_with_info)) # 并行保存 save_tasks = [] @@ -623,9 +560,7 @@ class RmbgService: ) save_tasks.append((index, save_task)) - save_start_time = time.time() save_results = await asyncio.gather(*[task for _, task in save_tasks], return_exceptions=True) - save_time += time.time() - save_start_time for (index, _), image_url in zip(save_tasks, save_results): if isinstance(image_url, Exception): @@ -668,11 +603,7 @@ class RmbgService: images_with_info = [(img, size, idx) for img, size, idx, _ in batch_items] - gpu_start_time = time.time() batch_results = await self.process_batch_images(images_with_info) - gpu_inference_time += time.time() - gpu_start_time - batch_count += 1 - batch_sizes.append(len(images_with_info)) # 并行保存 save_tasks = [] @@ -686,9 +617,7 @@ class RmbgService: ) save_tasks.append((index, save_task)) - save_start_time = time.time() save_results = await asyncio.gather(*[task for _, task in save_tasks], return_exceptions=True) - save_time += time.time() - save_start_time for (index, _), image_url in zip(save_tasks, save_results): if isinstance(image_url, Exception): @@ -850,15 +779,11 @@ class RmbgService: # 等待所有下载任务完成 await asyncio.gather(*download_tasks, return_exceptions=True) - download_time = time.time() - download_start_time # 确保所有结果都已处理 if pending_batch: async for result in process_pending_batch(force=True): yield result - - # 输出性能统计信息 - print_stats() def is_valid_url(self, url): """验证URL是否有效""" @@ -900,7 +825,6 @@ class RmbgService: await self.queue_task except asyncio.CancelledError: pass - logger.info("队列批处理机制已停止") # 处理队列中剩余的请求 remaining_items = []