共计 1979 个字符,预计需要花费 5 分钟才能阅读完成。
背景痛点:为什么需要门控融合网络
传统去雾方法如暗通道先验 (Dark Channel Prior, DCP) 在均匀雾霾场景下表现良好,但在实际应用中存在三个关键问题:
- 物理假设失效:当场景包含大面积白色物体或天空区域时,暗通道先验的统计规律不再成立
- 计算复杂度高:基于优化的方法需要迭代求解,难以满足实时性要求
- 参数固定:手工设计的参数无法自适应不同浓度的雾霾分布
深度学习方案虽然性能优越,但主流模型如 All-in-One Dehazing Network 存在参数量大、计算成本高的问题。门控融合网络 (Gated Fusion Network) 通过结构创新,在保持精度的同时显著降低了计算开销。
技术解析:门控机制如何工作
双分支架构设计

网络包含两个并行的子网络:
- 基础去雾分支(Base Dehazing Branch):采用编码器 - 解码器结构,负责提取全局特征
- 门控分支(Gate Branch):使用轻量级结构生成空间注意力图
门控权重计算
门控值 $G(x)$ 通过 sigmoid 函数实现 0 - 1 之间的软选择:
G(x) = \sigma(W_g * x + b_g)
最终输出为两分支的加权融合:
Output = G(x) \cdot F_{base}(x) + (1-G(x)) \cdot F_{gate}(x)
参数量对比
| 模型 | 参数量(M) | FLOPs(G) |
|---|---|---|
| U-Net | 31.0 | 252.3 |
| GFN(本文) | 4.8 | 36.7 |
PyTorch 实现详解
模型核心代码
class GatedConvBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
# 使用深度可分离卷积减少计算量
self.depthwise = nn.Conv2d(in_channels, in_channels, kernel_size=3,
padding=1, groups=in_channels)
self.pointwise = nn.Conv2d(in_channels, out_channels, kernel_size=1)
self.gate = nn.Sequential(nn.Conv2d(in_channels, 1, kernel_size=3, padding=1),
nn.Sigmoid())
def forward(self, x):
base_feat = self.pointwise(self.depthwise(x))
gate_map = self.gate(x) # [B,1,H,W]
return base_feat * gate_map
多尺度损失设计
class MultiScaleLoss(nn.Module):
def __init__(self):
super().__init__()
self.l1_loss = nn.L1Loss()
def forward(self, outputs, targets):
# outputs 包含 [full_res, 1/2_res, 1/4_res] 三个尺度
total_loss = 0
for pred in outputs:
# 对预测结果下采样到对应尺度
scaled_target = F.interpolate(targets, size=pred.shape[2:])
total_loss += self.l1_loss(pred, scaled_target)
return total_loss
实战建议
数据处理技巧
- 对 RESIDE 数据集建议使用以下预处理:
- 随机裁剪 512×512 patches
- 概率性水平翻转
-
归一化到 [-1,1] 范围
-
学习率策略:
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[50,80], gamma=0.1) -
量化部署方案:
- 使用 QAT(Quantization-Aware Training)
- 对门控值采用 8bit 对称量化
- 添加量化噪声模拟部署环境
避坑指南
- 门控饱和问题:
- 在 sigmoid 前添加 BatchNorm 层
-
初始化 gate 分支最后一层卷积的 bias 为 -2
-
分辨率适配:
# 确保输入尺寸是 32 的倍数 pad_h = (32 - H % 32) % 32 pad_w = (32 - W % 32) % 32 x = F.pad(x, (0, pad_w, 0, pad_h), mode='reflect') -
特征可视化:
# 可视化门控图 plt.imshow(gate_map[0,0].cpu().detach().numpy(), cmap='viridis')
开放性问题
当前架构针对单幅图像设计,如何扩展到视频去雾场景?可能的改进方向:
- 引入 3D 卷积处理时序信息
- 利用光流对齐相邻帧
- 设计时域一致性损失函数
通过本教程,读者应当能够完整复现 CVPR 2018 的门控融合网络,并理解其设计精髓。该框架也可迁移到其他图像增强任务中,如去雨、低光增强等。
正文完
发表至: 未分类
近一天内
