Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swin Transformer (ICCV'21 Best Paper) を完璧に理解する資料

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for yu4u yu4u
December 12, 2021

Swin Transformer (ICCV'21 Best Paper) を完璧に理解する資料

第9回全日本コンピュータビジョン勉強会「ICCV2021論文読み会」の発表資料です
https://kantocv.connpass.com/event/228283/
ICCV'21 Best PaperであるSwin Transformerを完全に理解するためにふんだんに余談を盛り込んだ資料となります

Avatar for yu4u

yu4u

December 12, 2021
Tweet

More Decks by yu4u

Other Decks in Technology

Transcript

  1. Yusuke Uchida (@yu4u) 株式会社 Mobility Technologies Swin Transformer: Hierarchical Vision

    Transformer Using Shifted Windows 本資料はDeNA+MoTでの 輪講資料に加筆したものです
  2. 2 ▪ 本家 ▪ https://github.com/microsoft/Swin- Transformer/blob/main/models/swin_transformer.py ▪ timm版(ほぼ本家のporting) ▪ https://github.com/rwightman/pytorch-image-

    models/blob/master/timm/models/swin_transformer.py ▪ バックボーンとして使うならこちら ▪ https://github.com/SwinTransformer/Swin-Transformer-Object- Detection/blob/master/mmdet/models/backbones/swin_transformer.py 本家実装が参考になるので合わせて見ましょう
  3. 5 ▪ TransformerはNLPでデファクトバックボーンとなった ▪ TransformerをVisionにおけるCNNのように 汎用的なバックボーンとすることはできないか? → Swin Transformer! ▪

    NLPとVisionのドメインの違いに対応する拡張を提案 ▪ スケールの問題 ▪ NLPではword tokenが処理の最小単位、 画像はmulti-scaleの処理が重要なタスクも存在(e.g. detection) →パッチマージによる階層的な特徴マップの生成 ▪ 解像度の問題 ▪ パッチ単位よりも細かい解像度の処理が求められるタスクも存在 →Shift Windowによる計算量削減、高解像度特徴マップ実現 概要
  4. 10 ▪ Patch Partition ▪ ViTと同じく画像を固定サイズのパッチに分割 ▪ デフォルトだと 4x4 のパッチ

    →RGB画像だと 4x4x3 次元のtokenができる ▪ Linear Embedding ▪ パッチ (token) をC次元に変換 ▪ 実際は上記2つをkernel_size=stride=パッチサイズの conv2dで行っている ▪ デフォルトではその後 Layer Normalization Patch Partition & Linear Embedding
  5. 11 ▪ 近傍 2x2 のC次元パッチを統合 ▪ concat → 4C次元 ▪

    Layer Normalization ▪ Linear → 2C次元 Patch Merging (B, HW, C) にしてるのでpixel_unshuffle 使いづらい︖
  6. 12 ▪ Transformerのencoder layerとほぼ同じ ▪ 差分は Shifted Window-based Multi-head Self-attention

    Swin Transformer Block Two Successive Swin Transformer Blocks ココがポイント
  7. 13 ▪ Transformerのencoder layerとほぼ同じ ▪ 差分は Shifted Window-based Multi-head Self-attention

    Swin Transformer Block Two Successive Swin Transformer Blocks ココがポイント Pre-norm Post-norm
  8. 14 ▪ Learning Deep Transformer Models for Machine Translation, ACL’19.

    ▪ On Layer Normalization in the Transformer Architecture, ICML’20. Post-norm vs. Pre-norm ResNetのpost-act, pre-actを 思い出しますね︖
  9. 15 ▪ Transformerのencoder layerとほぼ同じ ▪ 差分は Shifted Window-based Multi-head Self-attention

    Swin Transformer Block Two Successive Swin Transformer Blocks ココがポイント
  10. 16 ▪ 特徴マップをサイズがMxMのwindowに区切り window内でのみself-attentionを求める ▪ hxw個のパッチが存在する特徴マップにおいて、 (hw)x(hw)の計算量が、M2xM2 x (h/M)x(w/M) =

    M2hwに削減 ▪ M=7 (入力サイズ224の場合) ▪ C2(stride=4, 56x56のfeature map)だと、8x8個window Window-based Multi-head Self-attention (W-MSA) per window window数 パッチ数の2乗
  11. 20 ▪ Self-attention自体は単なる集合のencoder ▪ Positional encodingにより系列データであることを教えている ▪ SwinではRelative Position Biasを利用

    ▪ Relativeにすることで、translation invarianceを表現 Relative Position Bias Window内の相対的な位置関係によって attention強度を調整(learnable)
  12. 22 ▪ On Position Embeddings in BERT, ICLR’21 ▪ https://openreview.net/forum?id=onxoVA9FxMw

    ▪ https://twitter.com/akivajp/status/1442241252204814336 ▪ Rethinking and Improving Relative Position Encoding for Vision Transformer, ICCV’21. thanks to @sasaki_ts ▪ CSWin Transformer: A General Vision Transformer Backbone with Cross-Shaped Windows, arXiv’21. thanks to @Ocha_Cocoa Positional Encoding(余談)
  13. 23 img_size (int | tuple(int)): Input image size. Default 224

    patch_size (int | tuple(int)): Patch size. Default: 4 in_chans (int): Number of input image channels. Default: 3 num_classes (int): Number of classes for classification head. Default: 1000 embed_dim (int): Patch embedding dimension. Default: 96 depths (tuple(int)): Depth of each Swin Transformer layer. [2, 2, 6, 2] num_heads (tuple(int)): Number of attention heads in different layers. [3, 6, 12, 24] window_size (int): Window size. Default: 7 mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4 qkv_bias (bool): If True, add a learnable bias to query, key, value. Default: True qk_scale (float): Override default qk scale of head_dim ** -0.5 if set. Default: None drop_rate (float): Dropout rate. Default: 0 attn_drop_rate (float): Attention dropout rate. Default: 0 drop_path_rate (float): Stochastic depth rate. Default: 0.1 norm_layer (nn.Module): Normalization layer. Default: nn.LayerNorm. ape (bool): If True, add absolute position embedding to the patch embedding. Default: False patch_norm (bool): If True, add normalization after patch embedding. Default: True use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False パラメータとか Stochastic depthをガッツリ使っている 次元の増加に合わせhead数増加
  14. 24 ▪ クラス分類学習時stochastic depthのdrop確率 T: 0.2, S: 0.3, B: 0.5

    ▪ Detection, segmentationだと全て0.2 Model Configuration
  15. 29 ▪ チャネルを2等分して、縦横のstripeでのself-attention 関連手法:CSWin Transformer X. Dong, et al., "CSWin

    Transformer: A General Vision Transformer Backbone with Cross-Shaped Windows," in arXiv:2107.00652.
  16. 30 🤔 関連手法:Pyramid Vision Transformer W. Wang, et al., "Pyramid

    Vision Transformer: A Versatile Backbone for Dense Prediction without Convolutions," in Proc. of ICCV, 2021. https://github.com/whai362/PVT
  17. 31 関連手法:Pyramid Vision Transformer W. Wang, et al., "Pyramid Vision

    Transformer: A Versatile Backbone for Dense Prediction without Convolutions," in Proc. of ICCV, 2021.
  18. 32 関連手法:Pyramid Vision Transformer W. Wang, et al., "Pyramid Vision

    Transformer: A Versatile Backbone for Dense Prediction without Convolutions," in Proc. of ICCV, 2021. 複数パッチを統合してflatten, liner, norm linerとnormの順番が逆なだけでPatch Mergingと同じ
  19. 33 関連手法:Pyramid Vision Transformer W. Wang, et al., "Pyramid Vision

    Transformer: A Versatile Backbone for Dense Prediction without Convolutions," in Proc. of ICCV, 2021. Position Embeddingは 普通の学習するやつ
  20. 34 関連手法:Pyramid Vision Transformer W. Wang, et al., "Pyramid Vision

    Transformer: A Versatile Backbone for Dense Prediction without Convolutions," in Proc. of ICCV, 2021. Spatial-Reduction Attention (SRA) がポイント
  21. 35 ▪ K, V(辞書側)のみ空間サイズを縮小 ▪ 実装としてはConv2D -> LayerNorm ▪ Qはそのままなので

    出力サイズは変わらない ▪ 削減率は8, 4, 2, 1 とstrideに合わせる Spatial-Reduction Attention (SRA)
  22. 38 ▪ Token mixerよりもTransformerの一般的な構造自体が重要 ▪ Token mixer = self-attention, MLP

    ▪ Token mixerが単なるpoolingのPoolFormerを提案 関連手法: MetaFormer W. Yu, et al., "MetaFormer is Actually What You Need for Vision," in arXiv:2111.11418. Conv3x3 stride=2 Ave pool3x3