您當前的位置:首頁 > 書法

Transformer實現Video Instance Segmentation

作者:由 Archimist.Song 發表于 書法時間:2021-01-10

End-to-End Video Instance Segmentation with Transformers

這篇應該是第一篇將Transformer用於影片例項分割的paper。Transformer是一個非常有潛力的結構,可以統一不同模態的資料,統一不同的視覺任務。

Transformer可以用於建模long-range依賴,非常適用於影片相關的任務,對時序資訊建模。

Transformer的核心self-attention,可以不斷的學習,更新,基於幀與幀之間的相似性。

本篇論文的VisTR(Video Instance Segmentation TRansformer)對一個序列並行解碼,預測。首先我們宏觀的來看一下整個網路的pipeline:

Transformer實現Video Instance Segmentation

第一階段,將若干幀一起輸入pipeline,經過CNN提取特徵後,將多幀按照時間順序concat在一起,得到clip-level sequence。

第二階段,transformer將上述clip-level sequence作為輸入,按順序輸出一些列object預測結果。

object預測按照輸入圖片得順序輸出。如上圖,同樣得形狀表示屬於同一幀得object。同樣得顏色表示這些object屬於同一類。

為了解決如何保持輸出序列得有序性以及合理得mask。

VisTR架構

整個架構由四部分組成,backbone,transformer,instance sequence matching,instance sequence segmentation。輸入一個影片序列,輸出該影片序列對應的每幀的mask。

Transformer實現Video Instance Segmentation

Overall architecture

Backbone

backbone負責提取一個序列幀的特徵。提取每一幀的特徵並將所有的特徵圖concat一起,得到

\mathbb{R}^{T×C×H×W}

Transformer Encoder

Encoder利用

self-attention

,建模影片內每一個畫素之間的相似性。首先使用1×1的卷積將輸入的特徵圖tensor降維到

\mathbb{R}^{T×d×H×W}

。為了使得T輸入的tensor適用於Transformer的結構,將spatial和temporal維度flatten到同一維,得到

\mathbb{T}^{d×(T×H×W)}

,其為一個二維的tensor。

d相當於transformer在處理語言時句子的長度,T×H×W相當關於embedded vector的長度

。將該tensor輸入到

multi-head self-attention module

fully connected feed forward network

Temporal and spatial positional encoding

Vision下的transformer同樣需要position encoding。Transformer的結果是與輸入序列順序無關的(permutation-invariant),而例項分割任務需要精確的位置資訊。論文采用原始transformer的位置編碼公式,得到包含temporal和spatial三個維度的編碼資訊。計算三個維度的position encoding,然後concat在一起。

\mathrm{PE}(\mathrm{pos}, i)=\left\{\begin{array}{ll} \sin \left(\operatorname{pos} \cdot \omega_{k}\right), & \text { for } i=2 k \\ \cos \left(\operatorname{pos} \cdot \omega_{k}\right), & \text { for } i=2 k+1 \end{array}\right.

其中,

\omega_{k}=1 / 10000^{2 k / \frac{d}{3}}

,d要能被3整除。該部分編碼類似於DETR中的position encoding,無非是這裡使用的是三維,而DETR使用的是二維。

Transformer Decoder

同樣借用DETR中的思想,預設每一幀有固定n個的input embedding用於抽取instance feature,T幀總共會query n×T個instance。這些instance queries是

透過模型學習

得到的。Decoder的輸入為預設的instance queries和encoder的輸出。這樣,預測的結果按照原始影片幀序列的順序輸出,輸出為n×T個instance向量,即學習到的instance queries。

這裡預設的instance queries可以理解為Faster-RCNN中預設的anchors,只是queries是可學習的。instance queries的每一個通道,學習到對應instance在所有訓練資料中的分佈。

Instance Sequence Matching

ViTR的decoder最後得到固定大小的N個prediction序列。Instance Sequence Matching模組是為了解決如何在不同圖片的prediction序列中對於相同的instance保持相對位置不變,找到每幀中instance對應的GT從而監督訓練。簡單來說就是對預測到的instance找到對應的Ground Truth從而訓練模型。

decoder輸出的固定個數的prediction序列是無序的,每一幀包含n個instance sequence。本論文和DETR相同,利用匈牙利演算法進行匹配。ViTR採用了和DETR類似的方法,雖然是例項分割,但需要用到目標檢測中的bounding box方便組合最佳化計算。透過FFN,即全連線計算出歸一化的bounding box中心,寬和高。透過softmax計算出該bounding box的label。最後我們得到n×T個bounding box。利用上述得到label機率分佈和bounding box匹配instance sequence和gournd truth。

\hat{\sigma}=\underset{\sigma \in S_{n}}{\arg \min } \sum_{i}^{n} \mathcal{L}{\operatorname{match}}\left(y{i}, \hat{y}{\sigma(i)}\right)

$

其中,

\mathcal{L}{\operatorname{match}}\left(y_{i}, \hat{y}{\sigma(i)}\right)

為pair-wise的代價,

y{i}

為groudn truth,

\hat{y}{\sigma(i)}

為預測的sequence其中一種排列組合。簡單來說,就是找

\mathcal{L}{\operatorname{match}}

最小的一個prediction的排列組合

用於計算損失。如何找到的方法就是用匈牙利演算法,該方法更為高效。該代價是為了尋找最佳的組後,並不是訓練用的loss。

最後計算匈牙利演算法的loss,同時考慮label的機率分佈以及bounding box的位置。Loss基本遵循DETR的設計,使用L1 loss和IOU loss。下式為訓練用的loss。由label,bounding box,instance sequence三者的loss組成。

\begin{aligned} \mathcal{L}{\text {Hung }}(y, \hat{y}) &=\sum{i=1}^{N}\left[\left(-\log \hat{p}{\hat{\sigma}(i)}\left(c{i}\right)\right)+\mathcal{L}{\text {box }}\left(b{i}, \hat{b}{\hat{\sigma}}(i)\right)+\mathcal{L}{\text {mask }}\left(m_{i}, \hat{m}_{\hat{\sigma}}(i)\right)\right] \end{aligned}

Instance Sequence Segmentation

該模組的任務是計算每一個instance在對應幀中的mask。

對於每一幀,將

decoder輸出的instance preidiction

和對

應幀的encoder得到的feature

輸入到一個self-attention模組中,得到初始的attention map。從這個操作可以看出,將encoder和decoder的輸出做一個self-attention,可以把encoder看作一個feature map,decoder看作一個attention map,attention map是透過上面的Instance Sequence Matching Module 匹配後訓練得到的。feature map和attention map對應通道相乘即為該幀上該通道對應的instance的mask結果。

之後self-attention的結果會和backbone提取的特徵和encode的輸出按幀的順序融合。最後輸出每一幀中instance的mask結果。

網路的最後,基於不同幀中同一個instance可以相互增強的思想,使用3D卷積輸出最後的instance segmentation 結果。

Expriments

使用YouTube-VIS資料集測試,該資料集影片最大幀數為36,所以設定T為36。不需要後處理將多個處理結果拼接在一起。預設每一幀至多10個instance,故instance query的長度為360。

Transformer實現Video Instance Segmentation