裁掉通道后,特征图的哪些尺寸会变?
查看完整题解
参考回答
直接回答
剪卷积输出通道通常让特征图的通道数 C 变小。空间高、宽由步幅、填充、卷积核等设置决定。只改通道而保留这些设置时,高和宽不变。后续卷积的输入通道数也要减少。
讲清原理
例如中间特征由 [N, 32, 28, 28] 变成 [N, 24, 28, 28],后层还可以保持原输出通道数。跟着变化的是它每个输出滤波器要读的输入切片数。加法分支和归一化也要检查。
例子与易错点
下面用两层无偏置的 1×1 卷积。将前层从 4 个输出裁到 2 个,再调整后层输入,最后和原始两层中间乘通道掩码的结果核对。
import torch
from torch import nn
torch.manual_seed(10)
first = nn.Conv2d(3, 4, 1, bias=False)
second = nn.Conv2d(4, 3, 1, bias=False)
smaller_first = nn.Conv2d(3, 2, 1, bias=False)
smaller_second = nn.Conv2d(2, 3, 1, bias=False)
keep = [0, 2]
x = torch.randn(1, 3, 5, 5)
with torch.no_grad():
smaller_first.weight.copy_(first.weight[keep])
smaller_second.weight.copy_(second.weight[:, keep])
full_features = first(x)
full_features[:, [1, 3]] = 0
compact_features = smaller_first(x)
assert compact_features.shape == (1, 2, 5, 5)
torch.testing.assert_close(second(full_features), smaller_second(compact_features))
print("中间通道从 4 变 2,空间高宽与最终输出一致")
这道题,你掌握得怎么样?
掌握程度由你主动标记,阅读和收藏不会自动改变状态。