# 解耦蒸馏 vs 集成蒸馏 消融实验设计 > 最后更新: 2026-01-11 --- ## 📋 审稿人要求 > The advantages of the decoupled learning framework—especially compared to integrated or joint distillation strategies—require clearer justification through more focused ablation studies. > > DeCLIP claims decoupling avoids optimization conflicts, but lacks: > (1) direct comparison with integrated distillation (e.g., joint content+context distillation without decoupling) in terms of mIoU/mAP and training stability --- ## 🎯 核心对比定义 ### 1. 解耦蒸馏 (Decoupled Distillation) - DeCLIP ``` ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ DeCLIP 解耦蒸馏架构 │ │ │ │ Input Image │ │ ↓ │ │ ┌─────────────────────────────────────────────────────────────────┐ │ │ │ EVA-CLIP / OpenAI-CLIP ViT │ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ │ │ Block 0 → Block 1 → ... → Block (N-2) │ │ │ │ │ │ (完整的 Attention + Residual + MLP) │ │ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ │ ↓ │ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ │ │ Block (N-1) - 最后一个 Block │ │ │ │ │ │ ┌─────────────────────────────────────────────────┐ │ │ │ │ │ │ │ LayerNorm → Q, K, V Projection │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ┌────────┐ ┌────────┐ │ │ │ │ │ │ │ │ │ Q │ │ V │ │ │ │ │ │ │ │ │ └────┬───┘ └────┬───┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ Q @ Q^T V @ V^T │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ softmax softmax │ │ │ │ │ │ │ │ ↓ ↓ │ │ │ │ │ │ │ │ ┌──────────────┐ ┌──────────────┐ │ │ │ │ │ │ │ │ │ Q Self-Sim │ │ V Self-Sim │ │ │ │ │ │ │ │ │ │ (Content) │ │ (Context) │ │ │ │ │ │ │ │ │ └──────┬───────┘ └──────┬───────┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ❌ 无残差连接 ❌ 无 MLP │ │ │ │ │ │ │ └─────────────────────────────────────────────────┘ │ │ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘ │ │ ↓ ↓ │ │ ┌──────────────────┐ ┌──────────────────┐ │ │ │ L_content Loss │ │ L_context Loss │ │ │ │ (Q features) │ │ (V self-sim) │ │ │ └──────────────────┘ └──────────────────┘ │ │ ↓ ↓ │ │ Teacher: CLIP crops Teacher: DINOv2 corr │ └─────────────────────────────────────────────────────────────────────────────────────┘ ``` **关键特点**: - 在最后一个 Block 使用 `forward_without_rcffn` 方法 - **去掉残差连接**:避免特征混合 - **去掉 MLP**:保持 Q 和 V 的纯净表示 - Q 的自相似性 → Content Loss(与 CLIP Teacher 对齐) - V 的自相似性 → Context Loss(与 DINOv2 相关性对齐) ### 2. 集成蒸馏 (Integrated Distillation) - Baseline ``` ┌─────────────────────────────────────────────────────────────────────────────────────┐ │ Integrated 集成蒸馏架构 │ │ │ │ Input Image │ │ ↓ │ │ ┌─────────────────────────────────────────────────────────────────┐ │ │ │ EVA-CLIP / OpenAI-CLIP ViT │ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ │ │ Block 0 → Block 1 → ... → Block (N-1) │ │ │ │ │ │ 完整的 Attention + Residual + MLP(所有层) │ │ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ │ ↓ │ │ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ │ │ Final Processing │ │ │ │ │ │ Head (Linear) → LayerNorm │ │ │ │ │ └─────────────────────────────────────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────┘ │ │ ↓ │ │ ┌──────────────────┐ │ │ │ Final Features │ │ │ │ (融合的特征) │ │ │ └────────┬─────────┘ │ │ │ │ │ ┌─────────────────┼─────────────────┐ │ │ ↓ ↓ ↓ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ L_content │ │ L_context │ │ L_region │ │ │ │ (ROI池化) │ │ (self-sim) │ │ (ROI sim) │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │ ↓ ↓ ↓ │ │ Teacher: CLIP Teacher: DINOv2 Teacher: DINOv2 │ │ │ │ L_total = L_content + L_context + L_region │ │ (所有 Loss 作用于同一个融合特征!) │ └─────────────────────────────────────────────────────────────────────────────────────┘ ``` **关键特点**: - 使用完整的 ViT forward(包含所有残差连接和 MLP) - 最终输出是 **融合后的特征** - Content 和 Context Loss 都作用于 **同一个融合特征** - 可能导致 **梯度冲突**:两个任务的梯度方向可能相反 --- ## 📊 实验方案 ### 实验 1:训练动态曲线(Training Dynamics) **目的**:证明解耦蒸馏训练更稳定 **记录指标**(每个 iteration): - `L_content` vs Iteration - `L_context` vs Iteration - `L_total` vs Iteration **预期结果**: - DeCLIP:曲线平稳下降,收敛快 - Integrated:曲线震荡,收敛慢,可能出现 Loss 反弹 ### 实验 2:梯度冲突分析(Gradient Conflict Analysis) **目的**:数学证明两个任务存在优化冲突 **方法**(参考 PCGrad 论文): ```python # 在 Integrated baseline 训练时: # 1. 分别计算 L_content 和 L_context 对最后 N 层的梯度 grad_content = torch.autograd.grad(L_content, params, retain_graph=True) grad_context = torch.autograd.grad(L_context, params, retain_graph=True) # 2. 计算梯度余弦相似度 cos_sim = cosine_similarity(grad_content, grad_context) # 3. 逐层统计 # cos_sim < 0: 梯度冲突 # cos_sim ≈ 0: 梯度正交 # cos_sim > 0: 梯度一致 ``` **分析范围**:逐层分析每个 ViT Block **预期结果**: - 梯度余弦相似度主要分布在 **负值区间**(冲突)或 **接近 0**(正交) - 说明两个任务的梯度方向经常相反或不相关 ### 实验 3:PCA 可视化 **目的**:直观展示解耦效果 **对比内容**: - DeCLIP 解耦蒸馏的最终输出特征 - Integrated 集成蒸馏的最终输出特征 **方法**: - 选取挑战性图片(多物体、复杂背景) - 提取特征的前 3 主成分,映射为 RGB **预期结果**: - Integrated:特征"模糊"或"混乱" - DeCLIP:边界清晰,语义准确 ### 实验 4:CKA 分析 **目的**:分析特征相似性 **对比内容**: 1. DeCLIP 的 Q 特征 vs V 特征 2. Integrated 输出特征 vs DeCLIP Q 特征 3. Integrated 输出特征 vs DeCLIP V 特征 **预期结果**: - DeCLIP 的 Q 和 V 有一定差异(证明确实解耦了) - Integrated 输出与 DeCLIP Q/V 的关系模糊 ### 实验 5:定量表格对比(暂不实现,用户自行评估) **指标**: - Detection: mAP - Segmentation: mIoU --- ## 🔧 训练配置 ### 统一配置 | 配置项 | 值 | |-------|---| | VFM Teacher | DINOv2 | | Epochs | 6 | | Learning Rate | 1e-5 | | Batch Size | 16 (total) | | Optimizer | AdamW | | Scheduler | Cosine | | Dataset | COCO | ### 模型配置 | Student Model | VFM Size | |--------------|----------| | EVA02-CLIP-B-16 | DINOv2-B | | EVA02-CLIP-L-14-336 | DINOv2-L | | OpenAI-CLIP-B-16 | DINOv2-B | | OpenAI-CLIP-L-14-336 | DINOv2-L | ### Loss 配置 **DeCLIP(解耦)**: - Content Loss 权重: 1.0 - Context Loss 权重: 0.25 - Region Loss 权重: 0.05 **Integrated(集成)**: - Content Loss 权重: 1.0 - Context Loss 权重: 1.0(直接相加,不用系数) --- ## 📁 代码组织 ``` decoupling_analysis/ ├── DESIGN.md # 本设计文档 ├── PROGRESS.md # 进度记录 ├── gradient_analysis.py # 梯度冲突分析 ├── pca_visualization.py # PCA 可视化 └── cka_analysis.py # CKA 分析 scripts/decoupling_ablation/ ├── dist_integrated_eva_vitb16_coco.sh # EVA-B/16 多卡训练 ├── dist_integrated_eva_vitL14_336_coco.sh # EVA-L/14-336 多卡训练 ├── dist_integrated_openai_vitb16_coco.sh # OpenAI-B/16 多卡训练 ├── dist_integrated_openai_vitL14_coco.sh # OpenAI-L/14 多卡训练 ├── debug_integrated_eva_vitb16_coco.sh # EVA-B/16 单卡调试 ├── debug_integrated_eva_vitL14_336_coco.sh # EVA-L/14-336 单卡调试 ├── debug_integrated_openai_vitb16_coco.sh # OpenAI-B/16 单卡调试 └── debug_integrated_openai_vitL14_coco.sh # OpenAI-L/14 单卡调试 src/training/ ├── integrated_distill.py # 集成蒸馏实现 (IntegratedDistillation 类) ├── main.py # (已修改) 添加 integrated 版本支持 └── params.py # (已修改) 添加 --version integrated 选项 ``` --- ## 📝 实现步骤 ### Step 1: 实现 Integrated Distillation ✅ - [x] 创建 `src/training/integrated_distill.py` - [x] 使用完整的 ViT forward(`mode="vanilla"`,不调用 `forward_without_rcffn`) - [x] 在最终输出特征上同时计算 Content + Context Loss ### Step 2: 实现 Gradient Analysis ✅ - [x] 创建 `gradient_analysis.py` - [x] 在训练过程中记录梯度余弦相似度 - [x] 逐层分析 ### Step 3: 实现 PCA Visualization ✅ - [x] 创建 `pca_visualization.py` - [x] 对比 DeCLIP vs Integrated 的特征 ### Step 4: 实现 CKA Analysis ✅ - [x] 创建 `cka_analysis.py` - [x] 分析 Q, V, 输出特征的相似性 ### Step 5: 创建训练脚本 ✅ - [x] EVA-CLIP B/L - [x] OpenAI CLIP B/L ### Step 6: 代码审阅与修复 ✅ (2026-01-11) - [x] 修复 `extract_roi_features_integrated` 缺少 `.transpose(-2,-1)` 的维度问题 - [x] 修复 `region_scd_loss` 中不必要的 transpose - [x] 移除未使用的导入 (`autocast`, `math`) --- ## ⚠️ 注意事项 1. **PCGrad 仅用于分析**:不使用它来解决冲突,只用来证明冲突存在 2. **Loss 直接相加**:Integrated 的 Content 和 Context Loss 直接相加,不调权重 3. **训练配置一致**:除了解耦 vs 集成的差异,其他配置完全一致 4. **评估用户自行**:训练完毕后用户自己进行 mAP/mIoU 评估