您當前的位置:首頁 > 詩詞

大白話Pyramid Vision Transformer

作者:由 whai362 發表于 詩詞時間:2021-02-26

PVTv2:(1)在物體檢測上,同樣大小的模型比Swin好1~2個點;(2)解決了大解析度輸入下計算複雜度大的問題。

PVTv1

論文地址:

https://

arxiv。org/abs/2102。1212

2

原始碼:

https://

github。com/whai362/PVT

(歡迎star)

TL;DR

這個工作把金字塔結構引入到Transformer

[1]

中,使得它可以像ResNet

[2]

那樣無縫接入到各種下游任務中(如:物體檢測,語義分割),同時也取得了非常不錯的效果。希望這些嘗試能夠促進更多下游任務的進一步發展,將NLP領域中Transformer的火把傳遞到CV的各個任務上。歡迎各位看官試用~

我們主要做了以下幾個微小的工作:

1. 分析ViT

[3]

遺留的問題

大家都知道,在ViT中,作者為影象分類提出了一個純Transformer的模型,邁出了非常重要的一步。相信大家看到ViT後的第一個想法就是:要是能替代掉愷明大佬的ResNet用在下游任務中豈不是美滋滋。

但是,ViT的結構是下面這樣的,它和原版Transformer一樣是柱狀結構的 。這就意味著,1)它全程只能輸出16-stride或者32-stride的feature map;2)一旦輸入影象的解析度稍微大點,佔用視訊記憶體就會很高甚至視訊記憶體溢位。

大白話Pyramid Vision Transformer

圖來自ViT的論文[3]。

大白話Pyramid Vision Transformer

圖來自網路,侵刪

2. 引入金字塔結構

計算機視覺中CNN backbone經過多年的發展,沉澱了一些通用的設計模式。

最為典型的就是金字塔結構。

簡單的概括就是:

1)feature map的解析度隨著網路加深,逐漸減小;

2)feature map的channel數隨著網路加深,逐漸增大。

幾乎所有的密集預測(dense prediction)演算法都是圍繞著特徵金字塔設計的,比如SSD

[4]

,Faster R-CNN

[5]

, RetinaNet

[6]

這個結構怎麼才能引入到Transformer裡面呢?

大白話Pyramid Vision Transformer

圖來自網路,侵刪

試過一堆胡裡花哨的做法之後,我們最終還是發現:簡單地堆疊多個獨立的Transformer encoder效果是最好的(奧卡姆剃刀定律,yes)。然後我們就得到了PVT,如下圖所示。在每個Stage中透過Patch Embedding來逐漸降低輸入的解析度。

大白話Pyramid Vision Transformer

其中,除了金字塔結構以外。為了可以以更小的代價處理高解析度(4-stride或8-stride)的feature map,我們對Multi-Head Attention也做了一些調整。

大白話Pyramid Vision Transformer

為了在保證feature map解析度和全域性感受野的同時降低計算量,我們把key(K)和value(V)的長和寬分別縮小到以前的1/R_i。透過這種方法,我們就可以以一個較小的代價處理4-stride,和8-stride的feature map了。

3. 應用到檢測分割上

接下來,我們可以把PVT接入到檢測和分割模型上試試水了。PVT替換ResNet非常容易,以大家常用的mmdetection

[7]

為例,放置好模型的程式碼檔案之後,只需要修改模型的config檔案就可以了。

大白話Pyramid Vision Transformer

從上面的圖中可以看到,PVT在RetinaNet上的效果還是非常不錯的。在和ResNet50相同的引數量下,PVT-S+RetinaNet在COCO val2017上的AP可以到40+。

另外我們還基於PVT+DETR

[8]

和Trans2Seg

[9]

構建了純transformer的檢測和分割網路,效果也不錯。具體參考論文Section 5。4。

最後分享一下我個人的一些不成熟的看法吧。

1. 為什麼PVT在同樣引數量下比CNN效果好?

我認為有兩點1)全域性感受野和2)動態權重。

其實本質上,Multi-Head Attention(MHA)和Conv有一些相通的地方。MHA可以大致看作是一個具備全域性感受野的,且結果是按照attention weight加權平均的卷積。因此Transformer的特徵表達能力會更強。

關於MHA和Conv之間的聯絡,更多有意思的見解可以拜讀下代季峰大佬的

An Empirical Study of Spatial Attention Mechanisms in Deep Networks

[10]

2. 後續可擴充套件的思路

1)效率更高的Attention:隨著輸入圖片的增大,PVT消耗資源的增長率要比ResNet要高,所以PVT更適合處理中等輸入解析度的圖片(具體見PVT的Ablation Study)。所以找到一種效率更高的Attention方案是很重要的。

2)Position Embedding:PVT的position embedding是和ViT一樣,都是隨機的引數,然後硬學的。而且在改變輸入影象的解析度的時候,position embedding還需要透過插值來調整大小。所以我覺得這也是可以改進的地方,找到一種更適合2D影象的方法。

3)金字塔結構:PVT只是一種較簡單的金字塔式Tranformer。中間是透過Patch Embedding連線的,或許有更優美的方案。

最後的最後,PVT在分類/檢測/分割或其他一些領域上的應用(code/config/model)都會在

https://

github。com/whai362/PVT

上釋出,大家可以star一下,方便後面檢視。

參考

^

Vaswani A, Shazeer N, Parmar N, et al。 Attention is all you need[C]//Proceedings of the 31st International Conference on Neural Information Processing Systems。 2017: 6000-6010。

^

He K, Zhang X, Ren S, et al。 Deep residual learning for image recognition[C]//Proceedings of the IEEE conference on computer vision and pattern recognition。 2016: 770-778。

^

Dosovitskiy A, Beyer L, Kolesnikov A, et al。 An image is worth 16x16 words: Transformers for image recognition at scale[J]。 arXiv preprint arXiv:2010。11929, 2020。

^

Liu W, Anguelov D, Erhan D, et al。 Ssd: Single shot multibox detector[C]//European conference on computer vision。 Springer, Cham, 2016: 21-37。

^

Ren S, He K, Girshick R, et al。 Faster R-CNN: towards real-time object detection with region proposal networks[C]//Proceedings of the 28th International Conference on Neural Information Processing Systems-Volume 1。 2015: 91-99。

^

Lin T Y, Goyal P, Girshick R, et al。 Focal loss for dense object detection[C]//Proceedings of the IEEE international conference on computer vision。 2017: 2980-2988。

^

Chen K, Wang J, Pang J, et al。 MMDetection: Open mmlab detection toolbox and benchmark[J]。 arXiv preprint arXiv:1906。07155, 2019。

^

Carion N, Massa F, Synnaeve G, et al。 End-to-end object detection with transformers[C]//European Conference on Computer Vision。 Springer, Cham, 2020: 213-229。

^

Xie E, Wang W, Wang W, et al。 Segmenting transparent object in the wild with transformer[J]。 arXiv preprint arXiv:2101。08461, 2021。

^

Zhu X, Cheng D, Zhang Z, et al。 An empirical study of spatial attention mechanisms in deep networks[C]//Proceedings of the IEEE/CVF International Conference on Computer Vision。 2019: 6688-6697。

標簽: PVT  ET  al  Transformer  ViT