【SLAM】Swin-Transformer论文精读

本文精读Swin-Transformer论文,聚焦其作为通用视觉骨干的核心设计:通过分层结构、非重叠窗口自注意力(W-MSA)、移位窗口自注意力(SW-MSA)和相对位置偏置,实现线性计算复杂度与多尺度特征输出,适配分类、检测与分割任务。相比ViT,Swin在ImageNet、COCO和ADE20K上显著提升,且可即插即用于FPN/U-Net等下游架构。

一句话定位
把 Vision Transformer 改成「像 CNN 一样的多尺度金字塔」:注意力只在小窗口里算(算力随图像线性涨),再层间移位窗口让信息能跨窗传——于是既能分类,也能当检测/分割的通用骨干。

本页概念卡

  • 骨干:Swin Transformer · ViT
  • 核心模块:W-MSA / SW-MSA · Patch Merging · 相对位置偏置
  • 相关用法:Swin-BEV · LAW Swin3D 断点

1. 背景概念(给新人)

站在 计算机视觉骨干网络(backbone) 这条线上:给下游分类 / 检测 / 分割抽特征。

术语白话
ViT图切成 patch 当「词」,全局注意力;分类强,高分辨率/密集任务贵
Self-Attention(自注意力)每个位置问「该听谁」:用 Query/Key/Value 加权汇总别人信息
Backbone(骨干)特征提取器;后面再接检测头、分割头等
分层 / 金字塔特征越深分辨率越低、语义越强(类似 ResNet 的 1/4→1/32)
FLOPs / 延迟理论算量 vs 真机跑得快不快(滑动窗注意力常「理论还行、实际很慢」)

为什么 ViT 不够当「通用骨干」?

  1. 尺度:视觉物体有大有小;ViT 全程单分辨率
  2. 分辨率:像素远多于文字 token;全局注意力对 token 数是二次复杂度,大图扛不住

2. 论文在干什么

  • 问题:让 Transformer 像 CNN 一样,成为检测、分割也能直接插拔的通用视觉骨干
  • 设定:
    • 分类:ImageNet-1K / 22K→1K
    • 检测:COCO(Cascade Mask R-CNN、HTC++ 等)
    • 分割:ADE20K + UperNet
  • 贡献概览:分层表示 + 窗口注意力(线性复杂度)+ 移位窗口跨窗连接 + 相对位置偏置

3. 核心创新点

  1. 分层 Transformer(Hierarchical)
    从小 patch 起步,用 Patch Merging 逐步合并,得到与 ResNet 同分辨率的多尺度特征图 → 可直接接 FPN / U-Net。

  2. 非重叠窗口自注意力(W-MSA)
    注意力限制在固定大小窗口(默认 M=7M=7M=7)内 → 复杂度对图像尺寸近似线性,而不是二次。

  3. 移位窗口(SW-MSA)
    相邻层窗口错开半窗,跨过上一层窗边界 → 在几乎不增加硬件延迟的前提下补上跨窗建模(比滑动窗注意力更「真机友好」)。

  4. 相对位置偏置
    注意力分数加可学习相对偏置 BBB,消融上优于无偏置 / 纯绝对位置。

4.1 总体数据流

  1. Patch Split + Linear Embedding
    RGB 切 4×44\times44×4 patch(每块 4×4×3=484\times4\times3=484×4×3=48 维)→ 线性投到通道 CCC(Swin-T:C=96C=96C=96)→ Stage 1 分辨率 H4×W4\frac{H}{4}\times\frac{W}{4}4H​×4W​。

  2. 四个 Stage
    每个 Stage:若干 Swin block;Stage 之间 Patch Merging(2×2\times2× 下采样,通道 ×2\times2×2)。
    输出分辨率:H4,H8,H16,H32\frac{H}{4},\frac{H}{8},\frac{H}{16},\frac{H}{32}4H​,8H​,16H​,32H​(与典型 CNN 骨干一致)。

  3. Swin Transformer Block
    LN → (S)W-MSA → 残差 → LN → MLP(GELU) → 残差。
    连续两块交替使用 W-MSA 与 SW-MSA(Eq.3)。

4.2 变体规模

变体CCC每 Stage 层数体量直觉
Swin-T962,2,6,2≈ ResNet-50 量级
Swin-S962,2,18,2≈ ResNet-101
Swin-B1282,2,18,2≈ ViT-B
Swin-L1922,2,18,2更大,常配 ImageNet-22K

默认:窗 M=7M=7M=7,每 head 维度 d=32d=32d=32,MLP 扩展 α=4\alpha=4α=4。

4.3 移位窗的高效批计算

移位后窗数会变多、边缘窗变小。论文用 cyclic-shift(向左上循环平移) 保持批次数与常规分区相同,再用 mask 禁止拼在同一批里、但空间上不相邻的 token 互相关注。

5. 整体框架(Mermaid)

RGB 图像

Patch Embed 4x4

Stage1 W/SW-MSA

Patch Merging

Stage2

Patch Merging

Stage3

Patch Merging

Stage4

多尺度特征金字塔

分类头

检测 / FPN

分割 / UperNet

连续两个 Swin Block

LN+W-MSA

LN+MLP

LN+SW-MSA

LN+MLP

规则窗口分区

移位半窗

上图:左支是「整网金字塔」;右支是「层间如何交替窗口」。缩写见本页概念卡。

主结果(论文报告)

任务关键数字
ImageNet-1K(22K 预训练 Swin-L@384)top-1 87.3%
COCO test-dev(Swin-L HTC++*)box AP 58.7 / mask AP 51.1(相对此前 SOTA 约 +2.7 / +2.6)
ADE20K valmIoU 53.5(相对 SETR 约 +3.2)

同复杂度下,分类上显著优于 DeiT;检测上换 backbone 时,Swin-T 相对 R-50 在多种检测器上稳定涨点。

消融(Swin-T,Table 4 要点)

  • 去掉移位(全程规则窗):分类/检测/分割都掉;移位带来约 +1.1% ImageNet、+2.8 mIoU ADE20K 量级收益(论文叙述)
  • 相对位置偏置:优于无偏置与绝对位置;再叠绝对位置略降
  • 移位窗 vs 滑动窗(Table 5/6):精度接近,移位窗延迟更低

局限(诚实)

  • 仍是「局部窗」归纳偏置:极远依赖需多层移位才能传开
  • 大模型依赖 ImageNet-22K 等大数据才冲到最高点
  • 本文是 2D 图像骨干;视频/BEV 需 Video Swin / 工程改写

7. 要点总结

  • Swin = 分层 + 窗口注意力 + 层间移位 + 相对位置偏置
  • 复杂度:全局 MSA 二次;W-MSA 在 MMM 固定时对图像近似线性
  • 输出分辨率对齐 CNN → 可当检测/分割即插即用骨干
  • 「移位」改分组;「相对偏置」改打分——别混
  • 读 LAW/Occ 时看到 Swin block,先想:窗内注意力 + 多尺度特征

8. 开放问题

  • Swin-V2 对位置编码 / 大规模训练改了哪些,和本篇默认设定差多少?
  • Video Swin 的时间窗与 LAW 单帧编码如何精确对应到权重加载?
  • 在人形/机器人高分辨率相机上,window size 与下采样策略怎么折中实时性?

相关

  • https://github.com/microsoft/Swin-Transformer
内容概要:本文围绕MongoDB在通信行业的深度应用,系统讲解了可查询加密、时间序列集合、窗口函数与性能诊断四大核心技术。通过详实的代码案例,展示了如何利用可查询加密实现敏感数据合规存储与查询,使用时序集合高效处理5G网络KPI等高频写入场景,借助窗口函数进行经营指标的趋势分析,并结合explain()与查询分析器完成性能调优。文章还涵盖分片一致性检查、索引优化等运维实践,全面覆盖通信系统对安全、性能与扩展性的严苛要求。; 适合人群:具备一定数据库基础,从事通信行业研发、架构或运维工作1-3年的技术人员,尤其是参与5G网络监控、客户数据管理或大数据平台建设的相关人员。; 使用场景及目标:① 实现用户手机号、身份证等敏感信息的加密存储与等值查询,满足安全合规要求;② 构建高吞吐、低延迟的5G基站KPI时序数据存储与分析系统;③ 在数据库层直接完成滑动平均、累计求和等经营分析,减少应用层计算负担;④ 诊断慢查询、验证索引有效性,保障系统高性能运行。; 阅读建议:此资源强调理论与实战结合,建议读者在本地或测试环境部署MongoDB 7.0+版本,动手复现文中代码,重点关注schemaMap配置、时序集合参数选择与窗口函数语法细节,并结合explain执行计划理解底层优化机制,从而真正掌握通信级数据架构的设计精髓。
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值