IT 정리용 블로그!

[논문] Generic Attention-model Explainability for Interpreting Bi-Model and Encoder-Decoder Transformers 설명,정리 본문

Computer Vision

[논문] Generic Attention-model Explainability for Interpreting Bi-Model and Encoder-Decoder Transformers 설명,정리

집가고시퍼 2021. 7. 20. 19:57
  • Introduction

    기존의 computer vision model들은 주로 고정된 수의 label에 task specific하게 훈련됐다. 하지만 transformer을 사용한 image-text model을 사용하고 각 modality를 enocde하면, 추가 훈련 없이 수많은 downstream task를 수행할 수 있다.
    첫 방법은 텍스트를 transformer로, image를 resnet이나 transformer로 encode한다. 둘째 방법은 quantize된 image representation을 text token에 concate하고 transformer model을 사용한다. 이 외에도 텍스트와 이미지를 combine하는 다른 방법들이 있겠지만, 두 input과 prediction을 mapping해 두 modality들 간의 interaction을 파악하는 것이 핵심이다. 모든 존재하는 transformer explainability metohd들은 self attention에 많이 의존하고, multi-modal transformer에서 그런 것 처럼 다른 형태의 attention에 adaptation을 제공하지 않는다.
    Self attention에 국한되지 않는 또다른 종류의 transformer model은 transformer encoder-decoder다. 이는 single domain에서 input을 받고, 다른 domain에서 output을 만든다.
    본 논문에서는 모든 transformer 구조에 적용 가능한 방법을 제시하고, 가장 흔히 사용되는 세 개의 transformer 구조에서의 효과를 보인다(pure self-attention, self attention combined with co-attention, encoder decoder attention).


  • Related Work

    1.  Explainability in Computer Vision
    Computer vision 알고리즘을 해석하다 보면 각 이미지 위치에서의 relevancy를 표현하는 heatmap을 볼 수 있다. 이건 모든 가능한 label에 대해 class dependent할 수도 있고, input과 model에만 의존하는 class agnostic한 것일 수 있다. 본 논문에서 제안하는 방법은 class dependent하다.

    2.  Explainability for Transformers
    Transformer을 설명하려면 주로 attention map을 사용한다. 하지만 이러면 중간 attention score와 transformer의 다른 component를 무시하게 된다. 각 attention head에서의 연산은 query, key, value를 mix한다. 하지만 이는 소위 attention이라 불리는 query와 key들의 내적만으로는 완전히 파악할 수 없다.

    3.  Transformers in computer vision
    Transformer 기술은 image captioning과 text-based image retrieval과 같은 bi-modal task에서 널리 사용되고 있다. 우리는 self attention에만 의존하는 network와 co-attention module또한 사용하는 network를 구분할 수 있다. 본 논문에서는 두 방법 모두를 다룬다.

  • Method
    본 논문에서는 model의 attention layer을 사용해 network에서의 input modality들 간의 상호작용을 나타내는 relevancy map을 만들려 한다. 이를 위해, image와 text의 상호작용과 generative model을 위한 attention module(encoder-decoder attention)에 집중한다. 하지만 본 논문의 방법은 모든 transformer 기반 구조에 적용 가능하며, 두 개 이상의 modality에서 generalize할 수 있다.

    t,i를 각각 text와 image input token의 수라 하자. (t,i)는 두 domain에서의 variable을 나타낸다 하자. Multi modal attention network는 input token들 간 네 종류의 interaction이 가능하다. \(A^{tt}\), \(A^{ii}\)는 self attention interaction을 나타내고, \(A^{ti}\), \(A^{it}\)는 multi-modal interaction을 나타낸다. \(A^{ti}\)는 각 text 토큰에 대한 image token들의 영향을 나타낸다. 각 attention interaction에 대해, relevancy map을 만든다. \(R^{tt}\), \(R^{ii}\)는 self attention에 대해, \(R^{ti}\), \(R^{it}\)는 bi-modal attention에 대해 만든다. relevancy map은 attention layer들에서 forward pass해서 얻을 수 있고, 각 layer는 다음 항목들에서 설명할 update rule들을 사용해 aggregated relevance matrices에 기여한다.

    Attention operation 전에, 각 token은 독립적이다. 따라서 self attention matrix는 identity matrix로 초기화된다. Bi-modal interaction에 대해, attention layer 전에서는 , 각 modality는 분리되어 있고, 다른 modality에서 온 context를 포함하지 않는다. 따라서 relevancy map은 0으로 초기화된다. $$ R^{ii} = I^{i\times i},  R^{tt} = I^{t\times t}\ \ \ \ (1) $$$$ R^{it} = 0^{i\times t},  R^{ti} = I^{t\times i}\ \ \ \ (2) $$
    Attention layer들은 token들을 contextualize하기 때문에, 본 논문은 token embedding의 mixture에 영향을 받은 relevancy map을 수정한다. Attention은 $$ A = softmax(\frac{Q\cdot K^{T}}{\sqrt{d_{h}}})\ \ \ \ (3)$$$$ O = A\cdot V\ \ \ \ (4) $$이다. \(O\in R^{h\times s\times d_{h}}\)는 attention module의 output이고, h는 head의 수, \(d_{h}\)는 embedding dimension, \(s,q\in {i,t}\)는 domain과 각 domain에서의 token의 수를 나타낸다. 예를 들어 attention은 s query token들과 q key token들 사이에서 일어난다. \(Q\in R^{h\times s\times d_{h}}\)는 query matrix, \(K,V\in R^{h\times q\times d_{h}}\)는 key와 value matrix를 나타낸다.


    Fig 1에서, self attention layer의 경우, s=q이고 Q,K,V는 attention unit에 들어가는 input의 projection이다. 반면, co-attention에서는 Q는 input의 projection이고, K,V는 다른 modality에서의 context input의 projection이다. \(A\in R^{h\times s\times q}\)는 attention map으로, s, q에서의 각 쌍들 간의 connection을 직관적으로 보여준다. Fig 1에서 처럼, attention module은 residual connection뒤에 따라오므로, 각 layer의 contribution을 aggregated relevancies에 더함으로써 relevancies를 축적해나간다.

    본 논문에서는 각 attention layer에서 attention map A를 사용해 relevancy map을 update한다. 이 map은 h개의 head로 구성되어 있으므로 head들 간의  평균을 내기 위해 gradient를 사용한다. 각 attention head들의 importance와 relevance가 다르므로, 단순히 head 결과들을 simple average하는 것은 왜곡된 relevancy map을 만든다. 본 논문에서의 최종적인 attention map은 \( \bar{A}\in R^{s\times q}\)로, 다음과 같다. $$ \bar{A}= E_{h}((\bigtriangledown A\odot A)^{+})\ \ \ \ (5)$$\(\odot \)는 Hadamard product, \(\bigtriangledown A:= \frac{\partial y_{t}}{\partial A}\) (\(y_{t}\)는 visualize하고 싶은 class t의 output), \(E_{h}\)는 head의 dimension들 간의 mean을 의미한다.

    \( \bar{A}\in R^{s\times s}\)을 만족하는 self attention layer에는 다음과 같이 affected aggregated relevancy score을 update한다. $$ R^{ss} = R^{ss} + \bar{A}\cdot R^{ss}\ \ \ \ (6) $$$$ R^{sq} = R^{sq} + \bar{A}\cdot R^{sq}\ \ \ \ (7) $$(6)에서 token들은 이미 전 attention layer에서 aggregated self attention matrix \(R^{ss}\)에 의해 mat mul이
    되어 contextualize되어있다. (7)에서, 전 bi-modal attention layer가 context를 q에서 s로 끼워넣었으므로, self attention이 s에서 token을 섞을때, s에서 온 각 token들에 context q를 섞는다. 전 layer의 context가 섞인 것은 \(R^{sq}\)로 쓰인다. 따라서 self attention process에서 added context를 계산한다.

    Bi-modal attention이 가해진 \( \bar{A}\in R^{s\times q}\)의 경우, relevancy accumulator의 update rule은 self attention matrices \( R^{xx}, \left \{ s,q \right \}\)의 normalization을 포함한다. \(R^{xx} = I^{x\times x}\)로 초기화했고, (6)에서 각 layer에서 relevancy matrices를 축적하므로, aggregated self attention matrix \(R^{xx}\)를 두 부분으로 생각할 수 있다. 하나는 initialize에서 온 identity matrix고, 다른 하나는 layer들에서의 self attention의 aggregation에 의해 생긴 \(\hat{R}^{xx} = R^{xx} - I^{xx}\)이다. (5)에서 head들 간 average를 구하려 gradient를 사용하므로, \(\hat{R}^{xx}\)의 값은 점차 줄어든다.

    우리는 각 토큰이 자신에 영향을 끼친다는 사실과 self attention mechanism에 의해 contextualization이 일어난다는 사실 모두를 고려하고자 한다. 이를 위해, \(\hat{R}^{xx}\)에서의 각 row를 normalize해 합이 1이 되도록 한다. \(\hat{R}^{xx}\)에서의 i번째 row는 i번째 token의 self attention 값을 나타내고, \(I^{xx}\)가 각 token 자기 자신에 대한 값을 1로 만들어 준다.$$ \hat{S}_{m,n}^{xx} = \sum_{k=1}^{|x|}(\hat{R}_{m,k}^{xx})\ \ \ \ (8) $$$$ \bar{R}^{xx} = \hat{R^{xx}}/\hat{S^{xx}} + I^{xx}\ \ \ \ (9) $$(9)에서 /는 element 단위에서의 matrix devision을 의미한다. 결국 \(\hat{R^{xx}}\)의 row에 있는 각 element를 해당 row의 sum으로 나눈다.

    다음으로, bi-modal attention unit들에 대해 aggregation rule을 적용한다. $$ R^{sq} = R^{sq} + (\bar{R}^{ss})^{T}\cdot \bar{A}\cdot \bar{R}^{qq}\ \ \ \ (10) $$$$ R^{ss} = R^{ss} +\bar{A}\cdot \bar{R}^{qs}\ \ \ \ (11) $$(10)은 각 modality의 token들이 전 atention layer에서 이미 normalized aggregated self attention matrices \(\bar{R}^{ss},\bar{R}^{qq}\)를 matmul하여 contextualize된 것을 보인다.
    (11)에서는 앞선 bi-modal attention layer가 두 modality들의 embedding을 integrate하므로, s를 q와 contextualize할 때, q 또한 s의 정보를 포함한다(\(R^{qs}\)를 통해). 위 규칙들은 modality \(s\in {i,t}\)에서 온 input과 modality \(q\in{i,t}\)에서 온 context에 관해 설명되었다. 이 규칙들은 image와 text라는 두 개의 modality에도 적용된다.

    최종 classify를 하기 위해, 일반적으로 transformer based model들은 CLS token을 사용한다. CLS token은 input token에 더해져 모든 input token들의 general representation을 나타낸다. Classification task를 위한 각 토큰의 relevancies를 얻기 위해, 해당하는 relevancy map에서의 CLS token의 row를 고려한다. 예를 들어, CLS token이 text modality에서 첫 token이라 가정했을 때, text token당 relevancy를 얻기 위해서는 \(R^{tt}\)의 첫 row를 고려해야 한다. 그리고 image token relevancies를 얻기 위해서는, \(R^{ti}\)의 첫 row(CLS token과 각 image token간의 상관관계를 설명한다)를 고려해야 한다.


    본 논문에서는 제안된 아이디어를 세 가지 transformer based network를 통해 검증한다. Fig 2-(a)는 multi-modal transformer로, 두 modality들이 concate되지만 SEP token으로 분리된다. 이 모델은 (6)과 같이 modality들을 contextualize하기 위해 self attention만 사용한다. 따라서 relevancy map \(R^{(t+i,t+i)}\)을 만들고, 이는 modality들간 뿐 아니라 각 modality들 스스로의 connection을 나타낸다. Classification에 관련된 token을 visualize하려면, \(R^{(t+i,t+i)}\)의 row를 살펴야 한다.이 row \(R_{cls}^{(t+i)}\)는 image토큰 당과 text토큰 당 relevancy score을 나타낸다.

    Fig 2-(b)는 Multi-modal attention network로 각 modality를 다른 modality와 contextualize하는 co-attention module을 만들어준다. 이런 network는 각 modality를 위해 위에서 설명된 모든 propagation rule을 필요로 한다. Classification을 위한 relevancy를 만들기 위해, 우리는 단순히 CLS token부분의 설명을 따른다. Fig 2-(b)에서의 CLS token은 text modality의 첫 token이기 때문이다.

    Fig 2-(c)는 Generative model로 input에 self attention을 사용하는 encoder와 decoder가 있다. Decoder는 두 개의 input을 가지는데, 하나는 바뀌지 않는 encoded data고, 다른 하나는 decoder의 domain에서 오는 input이다. 디코더는 디코더 도메인의 토큰에 self attention을 사용한 뒤, co attention layer을 이용해 인코더의 output과 contextualize한다. e를 encoder token, d를 decoder token이라 할 때, relevance update rule은 다음과 같다. \(R^{dd},R^{ee}\)는 각각 self attention interaction을 나타내고, \(R^{de}\)는 인코더, 디코더 토큰 간의 bi-modal interaction을 나타낸다. encoder는 contextualize되지 않았으므로, \(R^{ed}\)는 존재하지 않는다. \(R^{ee}\)는 (6)을 따른다. 디코더의 self attention을 위해서는 (6)과 (7)을 사용한다. 인코더에서의 bi-modal attention을 위해서는 (10)을 사용한다. \(R^{ed} = R^{qs}\)에 해당하는 relevancy map은 없기 때문에 (11)은 사용하지 않는다. Relevancies를 얻기 위해, \(R^{de}\)를 고려한다. 본 논문에서는 object detection으로 실험을 하는데, d에서 온 token은 query로, input image에서의 object를 나타낸다. 발견된 object j와 각 image region의 relevancy를 얻기 위해서는 \(R^{de}\)의 j번째 row를 봐야 한다(\(R_{j}^{de}\)). \(R_{j}^{de}\)는 각 인코더 토큰 별로 relevancy score를 가지고 있으며, 해당 경우에는 image region을 나타낸다.


  • Baselines

    본 논문에서의 attention map baseline은 raw attention과 rollout이다. Raw attention은 attention map의 마지막 layer만을 relevancy map으로 고려한다(\(R^{tt} = A^{tt}\). \(A^{tt}\)는 마지막 text self attention map을 나타낸다. Rollout은 모든 self attention layer에 따라온다. rollout baseline은 self attention만 단독으로 사용하기 때문에, raw attention과 구분하기 위해 \(R^{sq}, s,q \in \left \{t,i\right \}\)를 사용한다.$$ R^{sq} = (R^{ss})^{T}\cdot \bar{A}\cdot R^{qq}\ \ \ \ (12) $$ \(R^{ss}, R^{qq}\)는 rollout에서 계산한 self attention relevancies고, \(\bar{A}\in R^{s\times q}\)는 마지막 bi-attention map이다.
    Gradient baseline을 위해서 Grad-CAM adaptation을 사용한다. 마지막 attention layer을 테스트하고, attention map의 head에서 Grad-CAM을 수행한다.
    마지막으로 head들 간의 average를 구하기 위해 마지막 attention layer에서 LRP relevancy를 사용해 partial LRP을 relevancy map baseline에 포함한다. 또한, transformer attribution method도 포함한다. Head들 간에 average하기 위해 (5)를 모든 attention layer에 적용한다. $$ \bar{A} = E_{h}((\bigtriangledown A\odot R^{A})^{+})\ \ \ \ (13) $$(5)와 유일하게 다른 점은 (5)에서는 raw attention map을 사용했지만, 위 방법은 A의 LRP relevancy value(\(R^{A}\))를 사용했다는 것이다. 또한, 여기에 모든 self attention layer에 (6)을 적용한다. Non self attention layer들에 대해, 위 방법은 마지막 attention map을 가지고 (13)을 이용해 head들을 average한다.


  • Experiments

    Fig 2-(a)에는 VisualBERT, Fig 2-(b)에는 LXMERT를 사용한다. 본 논문에서는 두 모델에 대해 각 modality에 대해 각각 positive and negative perturbation test를 진행하여 이 방법들로 만들어진 relevancy matrices를 평가한다. 이들에게 Visiual Question Answering(VQA) task를 사용해 input modalities와 이들 간의 connection을 파악하도록 한다.

    Perturbation test는 다음과 같이 진행된다. 1.  pre-trained model을 사용해 VQA dataset에서 10,000개의 랜덤하게 고른 샘플들에 대해 relevancy map을 추출한다.  2.  주어진 modality에서 토큰을 점차 줄여나가 network의 mean top 1 accuracy를 측정한다. Positive perturbation에서는 token들은 높은 relevance에서 낮은 relevance 순으로 제거되고, negative에서는 반대다. Positive perturbation에서는 성능이 가파르게 떨어지는 것을 확인할 수 있으며, 이는 제거된 토큰에 classification score에 중요한 역할을 했음을 알 수 있다. Negative perturbation에서는 제거된 토큰이 classification에 큰 연관이 없기에 accuracy를 유지할 것이다. 두 경우에서, area under the curve(AUC)를 사용해 모델의 성능 감소를 평가한다.
    모든 perturbation test에서, accuracy는 0%까지 내려가지 않는다. 이는 각 modality에서 100%의 토큰을 제거하더라도, 다른 modality에서의 input이 온전히 남아있기 때문에 model은 합리적인 답을 내리기에 하나의 modality에 의존할 수 있기 때문이다.


    Fig 3에서는 LXMERT에 대해 perturbation test를 진행한 결과, 본 논문에서 제시한 방법이 negative perturbation에서는 token들을 제거하더라도 성능이 가장 적게 떨어지고(Fig 3-(a), Fig 3-(c)), positive perturbation에서는 가장 크게 떨어지는 것을 확인할 수 있다(Fig 3-(b), Fig 3-(d)).


    Fig 4에서는 본 논문의 방법에의 적용을 확인할 수 있다. Self attention map인 \(R^{tt}\)를 visualize한 결과이다. 이미지의 relevancy는 각 지역을 reletavie relevancy로 곱함으로써 얻을 수 있다. 텍스트도 더 중요한 부분은 빨간색으로 나타나는데, 본 논문에 제시된 방법이 더 집중적으로 파악함을 알 수 있다. 텍스트의 경우 큰 차이가 나지는 않지만, 이미지의 경우 관련있는 part에 더 집중한 것을 확인할 수 있다.

    마지막으로 세 번째 모델은 DETR(Fig 2-(c))이다. Pretrained image DETR model을 사용하는데, ImageNet으로 pretrain된 ResNet50과, MSCOCO dataset으로 object detection train됐다고 한다.


Comments