상세 컨텐츠

본문 제목

[논문 리뷰] Swin Transformer: Hierarchical Vision Transformer using Shifted Windows

논문 리뷰

by jii 2025. 7. 29. 01:35

본문

[2103.14030] Swin Transformer: Hierarchical Vision Transformer using Shifted Windows

 

Swin Transformer: Hierarchical Vision Transformer using Shifted Windows

This paper presents a new vision Transformer, called Swin Transformer, that capably serves as a general-purpose backbone for computer vision. Challenges in adapting Transformer from language to vision arise from differences between the two domains, such as

arxiv.org

 

1. Intro

Vision 분야에서는 CNN이, NLP에서는 Transformer가 주류였음

⇒ Transformer를 vision 분야에 적용해보자!

문제점은 언어와 이미지 도메인의 차이점에서 발생

  1. sclae이 가변↔고정
  2. 해상도 차이 : 픽셀 단위 예측을 하는 경우에는 self attention 적용하려면 O(N²) 계산량 필요

⇒ 제안 : Swin Transformer

  1. hierarchical feature map
  2. W-MSA : window 내에서만 local self attention
  3. SW-MSA : window간 정보 공유를 위해 적용, 같은 key set을 공유하기 때문에 속도 향상 

2. Related Work

CNN and variants

  • CNN은 vision 분야의 주요 backbone
  • 그러나 여기서는 vision+NLP에서의 Transformer 구조를 강조

Self-attention based backbone architectures

 

self-attention을 vision 분야에 적용

  • sliding window : 속도 느림
  • 대신 shifting window 사용

Self-attention/Transformers to complement CNNs

  • Transformer를 CNN의 complement backbone으로 사용하려는 시도는 많았음
  • 반면, 이 논문에서는 Transformer 단독 구조 사용

Transformer based vision backbones

 

최초의 Transformer 기반 vision backbone : ViT의 문제점?

  • Dense prediction(객체 탐지 등)에는 적합하지 않음
  • 고해상도 이미지에 비효율적

 

Swin Transformer의 차별점?

 

  • 계산 복잡도
    • ViT: O(N²) → 전체 패치 쌍마다 self-attention 계산
    • Swin: O(N) 
  • general purpose → 분류 뿐 아니라 dense prediction task까지 포함
  • multi-resolution feature map (hierarchical) → CNN처럼 FPN, U-Net에 연결 가능

 

3. Method

3.1. Overall Architecture

 

Swin Transformer block

  • 기존 Transformer block에서 Multi-Head Self-Attention (MSA) 모듈을 SW-MSA로 교체
  • 나머지는 동일

3.2. Shifted Window based Self-Attention

 

Self-attention in non-overlapped windows(W-MSA)

  • window 내에서만 self att
  • window 크기는 M

(1) : global self att / (2) : local self att

 

Shifted window partitioning in successive blocks(SW-MSA)

  1. regular window
  2. shifted window

1,2를 번갈아 사용하면 이전 블록에서 분리되어 있던 윈도우 사이에 연결이 생김

 

Efficient batch computation for shifted configuration ⇒ cyclic-shift

 

위에서 SW-MSA를 쓰면 window 수가 증가하게 됨 (M*M보다 작은 window 생김)

  • 해결책 : padding + masking
  • 문제점 : window 수 적을때만  +, 연산량 너무 많음

more efficient batch computation approach by cyclic-shifting 제안

  • 각 윈도우를 (⌊M/2⌋, ⌊M/2⌋)만큼 shift하여 새롭게 나눔
  • 이렇게 하면 같은 window 내에 분리 되어야 할 영역이 같이 포함되지만, 이는 masking 처리해서 해결
  • ⇒ 결국 W-MSA와 동일한 window 개수 사용하도록 (latency 낮추기 가능)

Relative position bias

 

토큰 A와 B 사이의 상대적인 거리를 기반으로 attention score에 bias (편향) 를 더해주는 것

 

  • 윈도우 안의 M×M 토큰쌍은 M²개지만, 상대 위치는 좌우 -M+1 ~ +M−1 범위
    → 예를 들어 M=3이면 가능한 상대 위치는 x축 기준 -2~2, y축 기준 -2~2
  • 즉, 상대 위치 종류는 최대 (2M−1)*(2M−1)개 (25개)
  • 이 relative bias는 학습 가능한 파라미터고, bi-cubic interpolation으로 리사이즈해서 다른 사이즈에도 사용 가능
  • 위치 정보 없거나 absolute 위치 정보 사용하는 것보다+

3.3. Architecture Variants

  • base model은 Swin-B
  • 여기서 model size, computational complexity가 0.25, 0.5, 2배 된 것 

비교

 

Swin-T (Tiny) 약 0.25× C = 96 {2, 2, 6, 2} ResNet-50 수준 (DeiT-S)
Swin-S (Small) 약 0.5× C = 96 {2, 2, 18, 2} ResNet-101 수준
Swin-B (Base) C = 128 {2, 2, 18, 2} ViT-B / DeiT-B 수준
Swin-L (Large) 약 2× C = 192 {2, 2, 18, 2} 더 높은 성능과 복잡도

 

파라미터

  • C: Stage 1의 채널 수 (이후 Stage마다 Patch Merging 거치면서 2C, 4C, 8C로 증가)
  • layer numbers = {2, 2, 18, 2}
    → 4개의 Stage마다 Swin Transformer block의 개수
  • M = 7: 윈도우 크기
  • d = 32: 각 attention head의 query/key 차원
  • α = 4: MLP의 hidden layer 확장 비율 (기본 입력 채널 수의 4배로 확장)

 

4. Experiments

크게 3가지 vision task에 대해 실험

  1. ImageNet-1K : image classification
  2. COCO : object detection
  3. ADE20K : semantic segmentation

4.1. Image Classification on ImageNet-1K

 

방법 1 : Regular ImageNet-1K training

 

  • 데이터셋: ImageNet-1K (1.28M train, 50K val, 1000 class)
  • Optimizer: AdamW
  • Epochs: 300
  • Learning Rate:
    • 초기값 0.001
    • 20 epoch linear warm-up
    • 이후 cosine decay
  • Batch size: 1024
  • Weight decay: 0.05
  • Data augmentation: DeiT[63]에서 사용한 대부분의 증강 및 regularization 기법 사용
    • 단, repeated augmentationEMA는 사용 x
      → Swin에서는 오히려 성능이 하락
      → ViT에서는 훈련 안정화에 중요했지만, Swin에서는 불필요

 

방법 2 : ImageNet-22K pre-training(사전학습 → fine tuning)

 

Pre-training (ImageNet-22K)

  • 데이터셋: ImageNet-22K ( 14.2 million images, 22K classes )
  • Optimizer: AdamW
  • Epochs: 90
  • Learning Rate:
    • 초기값 0.001
    • 5 epoch 동안 linear warm-up
    • 이후 linear decay
  • Batch size: 4096
  • Weight decay: 0.01

Fine-tuning (ImageNet-1K)

  • Epochs: 30
  • Learning Rate: 고정값 1e-5
  • Batch size: 1024
  • Weight decay: 1e-8

방법 1 결과 

  • vs DeiT (Transformer 계열)

 

Swin-T 224×224 81.3% +1.5% ↑ vs. DeiT-S (79.8%)
Swin-B 224×224 83.3% +1.5% ↑ vs. DeiT-B (81.8%)
Swin-B 384×384 84.5% +1.4% ↑ vs. DeiT-B (83.1%)

 

  • vs ConvNet (RegNet, EfficientNet)
    • Swin Transformer가 속도-정확도 tradeoff 측면에서 +
    • RegNet, EfficientNet은 architecture search를 통해 설계된 고성능 CNN
    • 반면 Swin은 표준 Transformer에서 출발해 구조만 개선한 것으로, 더 높은 확장 가능성 있음

방법 2 결과

 

  • 사전 학습하면 1K에서 바로 학습할 때보다 성능 +
  • Swin Transformer는 기존 ViT 대비 속도, 정확도, 효율성에서 모두 +

 

4.2. Object Detection on COCO

 

비교 대상

  • ResNe(X)t (CNN 계열)
  • DeiT (Transformer 계열)
    • 백본만 변경, 나머지 설정 동일
    • Swin Transformer와 ResNe(X)t는 다양한 해상도 feature map 생성 → 다른 detection framework에 적용 가능
    • 반면 DeiT는 단일 해상도 feature map만 생성 Deconvolution layer를 사용해 계층적 feature map 생성

Comparison to ResNe(X)t

 

(a)

  • 4가지 detection 프레임워크에 대해 Swin-T vs ResNet-50
  • Box AP(객체 위치 정확도) 기준 +3.4 ~ +4.2 AP 향상

(b)

  • 모델 크기, FLOPs, latency 유사해도 Swin Transformer vs. ResNeXt101 64x4d에서 Swin이 훨씬 더 정확

(c) Improved HTC 프레임워크 사용 시

  • 성능이 높은 프레임워크에서조차 Swin이 추가 성능 향상 가능

Comparison to DeiT

 

  • Swin-T는 더 높은 성능(Box/Mask AP)을 내면서도, 모델 크기는 비슷, 추론 속도는 빠름

 

Comparison to previous state-of-the-art

 

  • Box AP: 기존 최고인 Copy-Paste보다 +2.7 AP 향상
  • Mask AP: DetectoRS보다 +2.6 AP 향상
  • 외부 데이터 없이 달성한 것!

4.3. Semantic Segmentation on ADE20K

Table 3

 

  mIoU 비교 대상 성능 차이
Swin-S 49.3 DeiT-S +5.3
Swin-S 49.3 ResNet-101 +4.4
Swin-S 49.3 ResNeSt-101 +2.4
Swin-L (22K pre-train) 53.5 SETR (prev. SOTA) +3.2

 

  • mIoU (mean Intersection-over-Union): 세그멘테이션 정확도를 평가하는 대표적인 지표. 높을수록 좋음.

4.4. Ablation Study

 

Shifted windows

  • Shifted window 방식은 인접 윈도우 간 연결성을 만들어줘서 더 넓은 문맥 정보를 반영
  • 3가지 task에서 모두 일관되게 성능 향상(파란 마킹 부분)

Relative position bias

 

  • CNN은 입력 이미지가 오른쪽으로 10픽셀 이동해도 같은 결과를 내는 걸 목표로 함 → translation invariance
  • 반면, ViT/DeiT는 position embedding으로 각 패치의 절대 위치를 넣어줘서, 이동하면 출력 바뀜 → 불변성 없음
  • 그러나 object detection이나 semantic segmentation처럼 dense prediction이 필요한 task에서는 어느정도 translation invariance 필요

 

5. Conclusion

결국 swin transformer는 patch merging으로 계층적 feature 표현을 생성하고 선형적인 계산 복잡도를 갖는다. object detection과 semantic segmentation 측면에서 성능이 우수하며 vision 분야와 NLP 분야의 통합 모델링에 도움이 될 것으로 기대된다. 

 

 

관련글 더보기

댓글 영역