Files
hair/docs/iface2_optimization_analysis.md
2026-07-18 15:30:31 +08:00

154 lines
5.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 接口2 女性生发 — 耗时分析与优化方案
> 分析日期:2026-07-18
> 基准数据:19张图片 × 5种发际线 = 95次调用,平均 11.1s,最快 8.3s,最慢 14.0s
---
## 一、完整调用链与耗时拆解
`girl1.jpg` 单次调用(13.66s)为例,从 [hairline_grow.log](file:///home/ubuntu/hair/log/hairline_grow.log) 提取的精确时间:
```
接口2 generate_grow_results_swap()
├─ ① extract_context() ~1.8s
│ ├─ FaceLandmarker (MediaPipe) 检测 ~0.5s
│ ├─ FaceParser (SegFormer CPU) 头发分割 ~0.9s ← 瓶颈③
│ └─ 3D提升 + 平滑 ~0.4s
├─ ② generate_hairline_redraw() ~5.8s
│ ├─ detector.detect (MediaPipe) ~0.01s
│ ├─ SegFormer CPU 头发分割 ~0.9s ← 瓶颈③(重复!)
│ ├─ 遮罩计算 ~0.08s
│ ├─ _call_swap → swapHair HTTP (:8801) ~4.8s ← 瓶颈①(35%
│ │ └─ change_hair → webui SD1.5 img2img
│ └─ 接缝融合 + band_mask ~0.05s
└─ ③ _call_local_redraw → ComfyUI Flux-2 ~6.0s ← 瓶颈②(44%
├─ 上传图片 + 提交工作流 ~0.3s
├─ Flux-2 推理 (steps=4) ~4.5s
├─ 轮询 /history (sleep=1.0s) ~0.5s ← 可优化
└─ 下载结果图 ~0.7s
```
### 耗时占比
| 步骤 | 耗时 | 占比 | 可优化 |
|------|------|------|--------|
| swapHair HTTP (webui SD推理) | 4.8s | 35% | ✅ |
| ComfyUI Flux-2 推理 | 6.0s | 44% | ✅ |
| SegFormer CPU × 2次 | 1.8s | 13% | ✅ |
| 其他(MediaPipe+3D+融合) | 1.0s | 8% | — |
---
## 二、优化方案(按收益排序)
### 优化1ComfyUI 轮询间隔 1.0s → 0.2s(省 ~0.5-0.8s
**问题**[comfyui.py:148](file:///home/ubuntu/hair/hairline/comfyui.py#L148) `time.sleep(1.0)` — 每次轮询固定等1秒,Flux-2推理只需~4.5s,但轮询可能多等0.5-1s。
**改法**
```python
# comfyui.py:148
time.sleep(1.0) time.sleep(0.2)
```
**收益**:每次调用省 0.5-0.8s95次省 ~60s。
---
### 优化2:避免重复 SegFormer 推理(省 ~0.9s
**问题**`extract_context()` 在 [service.py:128](file:///home/ubuntu/hair/hairline/service.py#L128) 跑了一次 SegFormer`_grow_core()` 在 [hairline_grow.py:879](file:///home/ubuntu/hair/face_analysis/hairline_grow.py#L879) 又跑一次 SegFormer。同一张图做了两次相同的头发分割。
**改法**:让 `generate_hairline_redraw` 接受外部传入的 `parse_map`,跳过内部重复分割。
**收益**:每次调用省 ~0.9s95次省 ~85s。
---
### 优化3SegFormer 移到 GPU(省 ~0.9s,需换 torch cu128
**问题**[service.py:61](file:///home/ubuntu/hair/hairline/service.py#L61) 注释:
```
⚠️ 本 worker 是 RTX 5090(sm_120)torch 2.2.2(cu121) 只编到 sm_90
SegFormer 默认走 CPU~2.5s/张)
```
**改法**
1. 升级 torch 到 cu128`pip install torch torchvision --index-url https://download.pytorch.org/whl/cu128`
2. 设置环境变量:`SEG_DEVICE=cuda`
**收益**SegFormer CPU 0.9s → GPU 0.05s,省 ~0.85s。
---
### 优化4Flux-2 工作流 steps=4 → 2-3(省 ~1-2s
**问题**[0716add-hair-api.json](file:///home/ubuntu/hair/0716add-hair-api.json) 的 KSampler steps=4,每步约 1.1s。
**改法**:修改工作流 JSON 中 KSampler 节点的 `steps` 值。需质量验证。
**收益**steps 4→2 省 ~2.2ssteps 4→3 省 ~1.1s。
---
### 优化5swapHair webui 推理优化(省 ~1-2s
**问题**swapHair 调 change_hair:8801 → webui:57860 SD1.5 img2imgdenoising_strength=0.6。
**可能方向**
- 降低 denoising_strength0.6→0.45
- webui 启用 `--medvram``--xformers`(已启用 xformers
- 检查是否可换更快的 sampler(如 DPM++ 2M Karras
**收益**:需实测验证。
---
### 优化6base64 编解码优化(省 ~0.2s
**问题**`_call_swap` 把图片编成 base64 JSON 传输([hairline_grow.py:516-521](file:///home/ubuntu/hair/face_analysis/hairline_grow.py#L516-L521)),`_call_local_redraw` 又解一次。大图 base64 编解码耗时显著。
**改法**:用 multipart 上传代替 base64 JSON。
---
## 三、预期收益汇总
| 优化项 | 难度 | 单次省时 | 累计省时(95次) |
|--------|------|---------|---------------|
| 轮询 1s→0.2s | 极简 | 0.5s | 48s |
| 避免重复SegFormer | 中 | 0.9s | 86s |
| SegFormer→GPU | 中(需换torch) | 0.85s | 81s |
| Flux-2 steps 4→2 | 简单(需验证) | 2.2s | 209s |
| swapHair优化 | 复杂 | 1.5s | 143s |
| base64优化 | 中 | 0.2s | 19s |
### 乐观预期
| 阶段 | 平均耗时 | 95次总耗时 |
|------|---------|-----------|
| 当前 | 11.1s | ~17.6分钟 |
| 优化1-3(立即可做) | ~8.8s | ~14分钟 |
| 优化1-4(含Flux-2 | ~6.6s | ~10.5分钟 |
| 全部优化 | ~5.5s | ~8.7分钟 |
---
## 四、推荐优先级
1. **立即做**(无风险,无需换 torch):
- 优化1:轮询间隔 0.2s
- 优化2:避免重复 SegFormer
- 优化4Flux-2 steps 4→3(先验证质量)
2. **短期做**(需换 torch):
- 优化3SegFormer→GPU
3. **中期探索**
- 优化5swapHair webui 参数调优
- 优化6base64→multipart