Scaling Up Your Kernels to 31x31_ Revisiting Large Kernel Design in CNNs

大核卷积在CNN中的应用
本文探讨了在传统CNN中使用大核卷积代替小核卷积的可能性及其优势。通过对MobileNetV2的实验表明,大核卷积能够提高模型在ImageNet分类及下游任务上的性能,尤其是在结合re-parameterizing技巧时效果更为显著。

研究背景

CNN曾是计算机视觉中常用的encoder,但最近CNN受到了Vision Transformers的极大挑战,在多个任务上(如图像分类、目标检测等)被Transformers超越。
为什么Transformer表现如此好?一些研究认为MHSA(multi-head self-attention)在vit中起着关键作用。MHSA更灵活,更少的归纳偏置,更能模拟长距离依赖。但一些研究也在质疑MHSA的必要性,它们将vit的高性能归因于适当的构建块和动态稀疏权值。
在这篇论文中,作者关注的是如何建立大感受野,在vit中,MHSA通常被设计成全局的或局部的,但具有大的内核,因此单个MHSA的每个输出都能够从一个大区域收集信息。但是大核卷积在CNN中使用并不普遍,在先进的CNN中,常用的方式是使用许多小卷积堆叠使其感受野和大卷积一样,上述观点自然引出了一个问题:如果我们在传统的cnn中使用几个大内核而不是许多小内核会怎么样?

使用大核卷积的指导准则

大深度卷积能在实践中更有效

大核卷积的计算成本很高,因为核的大小以二次形式增加了参数和浮点数。这一缺点可以通过应用深度卷积(depth-wise, DW)来克服。但DW卷积在GPU等现代并行计算设备上可能非常低效。并且不幸的是,我们发现现有的深度学习工具(如Pytorch)对大型DW卷积的支持很差,如表1所示。因此,我们尝试了几种方法来优化CUDA内核。基于fft的方法似乎是实现大卷积的合理方法。
image.png

identify shortcut在大内核中是至关重要的

为了证明这一点,我们使用MobileNet V2进行基准测试,因为它大量使用DW层,并且有两个已发布的变体(有或没有identify shortcut)。对于大卷积核的对应版本,我们只需将所有DW 3×3层替换为13×13。所有模型在ImageNet上以相同的训练配置训练100个epoch。下表显示,大型内核通过快捷方式将MobileNet V2的准确性提高了0.77%。而在没有快捷方式的情况下,大卷积核将准确率降低到只有53.98%。

% 31×31大卷积核感受野热力图可视化(蓝底版本) % 基于《Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs》 % 使用蓝色系热力图 clear; close all; clc; fprintf('31×31大卷积核感受野热力图可视化(蓝底版本)\n'); fprintf('基于《Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs》\n\n'); % 创建输入图像 inputSize = [224, 224, 3]; % 标准ImageNet尺寸 inputImg = createTestImage(inputSize); % 创建蓝色系颜色映射 blueColormap = createBlueColormap(256); % 创建图形窗口 figure('Position', [100, 100, 1200, 800], 'Name', '31×31大卷积核感受野热力图可视化(蓝底版本)', 'Color', [0.9, 0.9, 0.95]); % 显示输入图像 subplot(2, 3, 1); imshow(inputImg); title('输入图像', 'FontSize', 12, 'FontWeight', 'bold', 'Color', [0.2, 0.2, 0.5]); % 生成并显示31×31卷积核的感受野热力图 subplot(2, 3, 2); rf_31x31 = [31, 31]; % 31×31卷积核的感受野 heatmap_31x31 = generate31x31ReceptiveFieldHeatmap(inputSize(1), inputSize(2), rf_31x31); imagesc(heatmap_31x31); colormap(blueColormap); colorbar; axis image; title('31×31卷积核感受野热力图', 'FontSize', 12, 'FontWeight', 'bold', 'Color', [0.2, 0.2, 0.5]); hold on; plot(inputSize(2)/2, inputSize(1)/2, 'w+', 'MarkerSize', 10, 'LineWidth', 2); hold off; % 生成并显示3×3卷积核的感受野热力图作为对比 subplot(2, 3, 3); rf_3x3 = [3, 3]; % 3×3卷积核的感受野 heatmap_3x3 = generate3x3ReceptiveFieldHeatmap(inputSize(1), inputSize(2), rf_3x3); imagesc(heatmap_3x3); colormap(blueColormap); colorbar; axis image; title('3×3卷积核感受野热力图', 'FontSize', 12, 'FontWeight', 'bold', 'Color', [0.2, 0.2, 0.5]); hold on; plot(inputSize(2)/2, inputSize(1)/2, 'w+', 'MarkerSize', 10, 'LineWidth', 2); hold off; % 显示两种卷积核感受野的对比 subplot(2, 3, 4); compareReceptiveFields(inputSize, rf_31x31, rf_3x3); title('31×31 vs 3×3感受野对比', 'FontSize', 12, 'FontWeight', 'bold', 'Color', [0.2, 0.2, 0.5]); % 显示31×31卷积核的权重分布 subplot(2, 3, 5); visualize31x31KernelWeights(blueColormap); title('31×31卷积核权重分布', 'FontSize', 12, 'FontWeight', 'bold', 'Color', [0.2, 0.2, 0.5]); % 显示不同位置像素的感受野影响 subplot(2, 3, 6); visualizeMultipleReceptiveFields(inputSize); title('不同位置像素的感受野影响', 'FontSize', 12, 'FontWeight', 'bold', 'Color', [0.2, 0.2, 0.5]); % 添加标题 sgtitle('31×31大卷积核感受野热力图可视化 (基于《Scaling Up Your Kernels to 31x31》)', 'FontSize', 16, 'FontWeight', 'bold', 'Color', [0.1, 0.1, 0.4]); % 添加说明文本 annotation('textbox', [0.3, 0.02, 0.4, 0.05], 'String', ... '颜色表示感受野影响强度 (浅蓝=高, 深蓝=低) | 白色十字表示中心点', ... 'FontSize', 10, 'EdgeColor', 'none', 'HorizontalAlignment', 'center', ... 'BackgroundColor', [0.8, 0.8, 0.95], 'Color', [0.2, 0.2, 0.5]); % 创建蓝色系颜色映射函数 function cmap = createBlueColormap(n) % 创建从深蓝到浅蓝的颜色映射 cmap = zeros(n, 3); for i = 1:n intensity = (i-1)/(n-1); % 0到1的线性渐变 cmap(i, 1) = 0.2 * intensity; % R cmap(i, 2) = 0.4 * intensity + 0.2; % G cmap(i, 3) = 0.8 * intensity + 0.2; % B end end % 31×31卷积核感受野热力图生成函数 function heatmap = generate31x31ReceptiveFieldHeatmap(height, width, rf) % 生成31×31卷积核的感受野热力图 % 基于论文中的思想,大卷积核具有更广泛、更均匀的感受野 % 创建网格 [X, Y] = meshgrid(1:width, 1:height); % 计算中心点 centerX = width / 2; centerY = height / 2; % 计算每个点到中心的距离 distance = sqrt((X - centerX).^2 + (Y - centerY).^2); % 31×31卷积核的感受野特性 % 大卷积核具有更广泛、更均匀的影响 sigma = min(rf) / 2.5; % 较大的标准差,表示更广泛的影响 exponent = 1.2; % 较低的指数,表示更平缓的衰减 % 生成高斯分布热力图 heatmap = exp(-(distance.^exponent) / (2 * sigma^exponent)); % 添加轻微的边缘效应,模拟大卷积核的边缘响应 edgeEffect = 0.1 * sin(2*pi*X/width) .* sin(2*pi*Y/height); heatmap = heatmap + edgeEffect; % 归一化到[0,1]范围 heatmap = heatmap - min(heatmap(:)); if max(heatmap(:)) > 0 heatmap = heatmap / max(heatmap(:)); end end % 3×3卷积核感受野热力图生成函数 function heatmap = generate3x3ReceptiveFieldHeatmap(height, width, rf) % 生成3×3卷积核的感受野热力图 % 小卷积核具有更集中、更局部的影响 % 创建网格 [X, Y] = meshgrid(1:width, 1:height); % 计算中心点 centerX = width / 2; centerY = height / 2; % 计算每个点到中心的距离 distance = sqrt((X - centerX).^2 + (Y - centerY).^2); % 3×3卷积核的感受野特性 % 小卷积核具有更集中、更局部的影响 sigma = min(rf) / 1.5; % 较小的标准差,表示更集中的影响 exponent = 2.5; % 较高的指数,表示更陡峭的衰减 % 生成高斯分布热力图 heatmap = exp(-(distance.^exponent) / (2 * sigma^exponent)); % 归一化到[0,1]范围 heatmap = heatmap - min(heatmap(:)); if max(heatmap(:)) > 0 heatmap = heatmap / max(heatmap(:)); end end % 感受野对比函数 function compareReceptiveFields(inputSize, rf_31x31, rf_3x3) % 比较31×31和3×3卷积核的感受野 % 生成31×31卷积核的感受野轮廓 heatmap_31x31 = generate31x31ReceptiveFieldHeatmap(inputSize(1), inputSize(2), rf_31x31); % 生成3×3卷积核的感受野轮廓 heatmap_3x3 = generate3x3ReceptiveFieldHeatmap(inputSize(1), inputSize(2), rf_3x3); % 创建图像 imagesc(ones(inputSize(1), inputSize(2)) * 0.7); % 浅蓝色背景 colormap(gray); hold on; % 绘制31×31卷积核的感受野轮廓 contour(heatmap_31x31, [0.5, 0.5], 'r-', 'LineWidth', 3); % 绘制3×3卷积核的感受野轮廓 contour(heatmap_3x3, [0.5, 0.5], 'b-', 'LineWidth', 3); % 添加中心点标记 plot(inputSize(2)/2, inputSize(1)/2, 'w+', 'MarkerSize', 10, 'LineWidth', 2); % 添加图例 text(inputSize(2)*0.7, inputSize(1)*0.15, '31×31感受野', 'Color', 'r', 'FontSize', 12, 'FontWeight', 'bold'); text(inputSize(2)*0.7, inputSize(1)*0.1, '3×3感受野', 'Color', 'b', 'FontSize', 12, 'FontWeight', 'bold'); axis image; hold off; end % 31×31卷积核权重可视化函数 function visualize31x31KernelWeights(blueColormap) % 可视化31×31卷积核的权重分布 % 基于论文中的思想,大卷积核的权重通常具有中心-周围结构 % 创建31×31网格 [X, Y] = meshgrid(1:31, 1:31); % 计算中心点 centerX = 15.5; centerY = 15.5; % 计算每个点到中心的距离 distance = sqrt((X - centerX).^2 + (Y - centerY).^2); % 生成中心-周围权重分布 % 基于论文中的观察,大卷积核通常具有高斯分布的权重 sigma = 8; % 控制权重分布的范围 weights = exp(-(distance.^2) / (2 * sigma^2)); % 添加一些随机变化,模拟实际学习到的权重 randomVariation = 0.1 * randn(31); weights = weights + randomVariation; % 可视化权重 imagesc(weights); colormap(blueColormap); colorbar; axis image; title('权重值'); % 添加中心点标记 hold on; plot(15.5, 15.5, 'w+', 'MarkerSize', 10, 'LineWidth', 2); hold off; end % 多位置感受野可视化函数 function visualizeMultipleReceptiveFields(inputSize) % 可视化不同位置像素的感受野影响 % 创建图像 imagesc(ones(inputSize(1), inputSize(2)) * 0.7); % 浅蓝色背景 colormap(gray); hold on; % 定义多个位置 positions = [ inputSize(2)/2, inputSize(1)/2; % 中心 inputSize(2)/4, inputSize(1)/4; % 左上 3*inputSize(2)/4, inputSize(1)/4; % 右上 inputSize(2)/4, 3*inputSize(1)/4; % 左下 3*inputSize(2)/4, 3*inputSize(1)/4 % 右下 ]; colors = ['r', 'g', 'b', 'c', 'm']; % 为每个位置生成并绘制感受野轮廓 for i = 1:size(positions, 1) % 生成以该位置为中心的感受野热力图 [X, Y] = meshgrid(1:inputSize(2), 1:inputSize(1)); distance = sqrt((X - positions(i, 1)).^2 + (Y - positions(i, 2)).^2); % 31×31卷积核的感受野特性 sigma = 15; % 较大的标准差,表示广泛的影响 heatmap = exp(-(distance.^2) / (2 * sigma^2)); % 绘制轮廓 contour(heatmap, [0.5, 0.5], [colors(i) '-'], 'LineWidth', 2); % 标记位置 plot(positions(i, 1), positions(i, 2), [colors(i) 'o'], 'MarkerSize', 8, 'LineWidth', 2); end axis image; hold off; end % 测试图像创建函数 function img = createTestImage(inputSize) % 创建测试图像,特别设计以展示31×31大卷积核的优势 % 创建空白图像 img = zeros(inputSize(1), inputSize(2), 3); % 添加大范围渐变背景 [X, Y] = meshgrid(1:inputSize(2), 1:inputSize(1)); background = sin(0.02*X) .* cos(0.02*Y); img(:,:,1) = 0.5 + 0.3 * background; % 红色通道 img(:,:,2) = 0.5 + 0.2 * background; % 绿色通道 img(:,:,3) = 0.5 + 0.1 * background; % 蓝色通道 % 添加长距离结构 - 水平线 img(50, 20:200, :) = 1; img(150, 50:220, :) = 1; % 添加长距离结构 - 垂直线 img(30:180, 80, :) = 1; img(70:200, 180, :) = 1; % 添加大尺寸形状 center = [inputSize(1)/2, inputSize(2)/2]; radius = 40; for x = 1:inputSize(2) for y = 1:inputSize(1) distance = sqrt((x - center(2))^2 + (y - center(1))^2); if distance <= radius && distance >= radius-5 img(y, x, :) = 1; % 白色圆环 end end end % 添加一些局部细节 img(100:102, 100:102, :) = 0; % 小黑块 img(180:182, 180:182, :) = 0; % 小黑块 img = im2single(img); end
最新发布
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值