Multimodal

LayoutLM v2 (2020) 논문 리뷰

아르카눔 2025. 4. 24. 18:32

LayoutLM v2 모델의 논문 제목은 LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding이다. (링크)


저자는 Yang Xu, Yiheng Xu, Tengchao Lv, Lei Cui, Furu Wei, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Wanxiang Che, Min Zhang, Lidong Zhou 다.

이 논문은 LayoutLM v2으로 두 번째 버젼의 논문이다.

1. Introduction

 

저자들은 스캔된 문서에 대한 분석을 Visually-rich Document Understanding (VrDU)라고 명명했다. 

 

본 논문의 요약은 다음과 같다.

 

1. Mutli-modal Transformer 모델을 이용해서 문서의 텍스트, 레이아웃, 그리고 visual information 시각적 정보를 pre-trainining 단계에서 통합한다. 이때 cross-modal interaction을 end-to-end로 단일한 프로엠워크로 학습한다. Spatial-aware self-attention 역시 트랜스포머 아키텍처에 통합되어 있다.

 

2. Masked visual-language model에 더해서 text-image alignment와 text-image matching을 새로운 pre-training 전략으로 사용하여 서로 다른 modalities의 사이에서의 alignment를 강제한다.

 

3. LayoutLMv2는 전통적인 VrDU 태스크에 대해서 SOTA를 달성했으며 동시에 문서 이미지에 대한 VQA 역시도 SOTA 성능을 달성했다.

 

 

 

2. Approach

2.1. Model Architecture

전체적인 모델의 구조는 아래 Figure 1과 같다.

 

 

 

Visual Embeddings, Text Token Embeddings, 2D Position Embeddings, 1D Position Embeddings, Segment Embeddings와 Transformer Layers, 그리고 Pre-train objectives가 있다. 

 

 

우선 Text embedding, Visual embedding, Layout embedding에 대해서 알아본다.

 

Text Embedding

 

WordPiece를 사용하여 OCR test sequence에 대해서 토큰화를 수행했으며 각각 $s_i \in \{ \left[ A \right],  \left[ B \right] \}$로 세그먼트를 할당한다.  

 

그 다음 [CLS] 토큰을 문장의 맨 앞에 배치시키고, [SEP] 토큰을 두 세그먼트위 끝에 각각 붙인다. [PAD]를 끝에 붙여서 최종 sequence length가 maximum sequecne length인 $L$와 정확히 같게 되도록 조정한다.

 

1D positional embedding을 token의 index에 따라서 덧붙인다.

 

따라서 최종 text embedding은 다음과 같다. 

 

$\textbf{t}_i$ = TokEmb($w_i$) + PosEmb1D($i$) + SegEmb($s_i$)

 

 

Visual Embedding

 

문서의 페이지의 전체 이미지의 자세한 피쳐를 활용하고자 CNN 기반의 visual encoder를 사용한다.

 

이미지를 fixed-length sequence로 바꾼 다음에 ResNeXt-FPN 아키텍처를 백본으로 하여 visual encoder로 사용한다.

 

문서 이미지 $I$를 224 x 224로 리사이즈하여 visual encoder에 넣는다. 

 

Output feature는 average-pooled되어 최종적으로 $W \times H$의 크기를 가지게 된다.

 

그 다음 flatten하여 $W \times H$ 길이의 visual embedding sequence가 된다.

 

그 다음 Linear projection layer를 활용하여 text embedding과 사이즈를 맞춘다.

 

Text embedding에 쓰였던 것과 동일한 1D positional embedding를 사용하고,

 

이미지를 위한 별도의 segment인 [C]를 사용한다.

 

따라서 최종 visual embedding은 아래와 같다.

 

$\textbf{v}_i$ = Proj(VisTokEmb$(I)_i$) + PosEmb1D($i$) + SegEmb([C])

 

ResNeXt는 이 블로그 (링크)의 글을 참조했는데 GoogLeNet과 같은 NiN 구조다. 

 

 

 

Layout Embedding

 

Spatial layout 정보를 위한 임베딩이다. 이는 axis-aligned token boung boxes에 의해 표현된다.

 

Token bbox는 OCR의 결과에 의해 도출되는데 각각 box의 너비, 높이, 그리고 코너 좌표를 지닌다.

 

모든 좌표는 normalize와 discretize를 거쳐서 [0, 1000] 범위의 정수로 변환된다.

 

Layout embedding은 x 축과 y 축을 별개로 가진다.

 

$i$ 번째 ($ 0 \leq i \leq WH + L $) text / visual token의 normalized boung box는 다음과 같이 정의된다.

 

$\text{box}_i = (x_{min}, x_{max} , y_{min} , y_{max}, width, height)$

 

$i$번째 Layout embedding은 아래와 같이 정의된다.  

 

$\textbf{l}_i$ = Concat( PosEmb2$\text{D}_\text{x}(x_{min}, x_{max} , width)$,  PosEmb2$\text{D}_\text{y}(y_{min}, y_{max} , height)$ )

 

Bbox를 계산할 때 visual tokens는 evenly divided girds로 다루어진다.

 

비어있는 bbox bo$\text{x}_\text{PAD} = (0, 0, 0, 0, 0, 0)$은 [CLS], [SEP], [PAD]의 스페셜 토큰에 붙는다.

 

 

Multi-modal Encoder with Spatial-Aware Self-Attention Mechanism

 

Encoder는 visual embeddings $\{ \textbf{v}_{0}, ..., \textbf{v}_{WH - 1} \}$과 text embeddings  $\{ \textbf{t}_{0}, ..., \textbf{t}_{L - 1} \}$을 concatanate하여 하나의 sequence를 구성한다.

 

그리고 Layout embeddings를 더해서 spatial information을 결합한다.

 

$ \textbf{x}_{i}^{(0)} = X_i + \textbf{l}_i $ 가 된다.  

 

이때, $X$ = $\{ \textbf{v}_{0}, ...,\textbf{v}_{WH - 1}, \textbf{t}_{0}, ..., \textbf{t}_{L - 1} \}$다. 

 

 

추가적으로 spatial information을 explicitly하게 넣어주기 위해서 attention score를 계산할 때 약간 변형했다.

 

$\alpha_{ij}$가 Q과 K를 통해 계산된 attention score다.  

 

이때 $i$ 번째 bbox의 left cornor 좌표를 $(x_i, y_i)$라고 하자.  

 

둘의 차이의 1D and 2D relative position biases $\textbf{b}^{\text{1D}}, \textbf{b}^{\text{2D}_\text{x}}, \textbf{b}^{\text{2D}_\text{y}}$를 정의하고 이를 활용한다.

 

Biases는 head 마다 다르지만 모든 encoder layers에 대해서는 sharing parmateres다.  

 

이를 이용해 보정한 spatial-aware attention score는 아래와 같이 정의된다.

 

$\alpha_{ij}' = \alpha_{ij} + \textbf{b}_{j - i}^{\text{1D}}  +  \textbf{b}_{x_j - x_i}^{\text{2D}_\text{x}}  +  \textbf{b}_{y_j - y_i}^{\text{2D}_\text{y}}  $

 

 

최종적인 projected value vectors와 normalized spatial-aware attention scores의 계산으로 도출된 output vectors는 다음과 같다.

 

$\textbf{h}_i = \sum_j \frac{\text{exp} ( \alpha_{ij}'  ) }{ \sum_k \text{exp} ( \alpha_{ik}'  ) } \textbf{x}_j \textbf{W}^{V}$

 

 

2.2. Pre-training Tasks

Masked Visual-Language Modeling

 

Vanilla LayoutLM (LayoutLM v1)처럼 MVLM을 수행한다.  

Masked tokens에 따라서 원본 이미지의 해당 부분 역시 마스킹을 수행한다.

 

Text-Image Alignment (TIA)

 

Text-Image Alignment (TIA)을 제안한다. 

Some token lines가 랜덤하게 선택되고 이 이미지 영역은 문서의 이미지로 덮여진다.

Masking과 구별하기 위해 operation covering이라고 불리는 위 작업을 수행한다.

[Covered]와 [Not Covered]로 구분되는데 이를 통해서 binary cross entropy loss를 계산한다.

이는 이미지의 해상도가 제한적일 때 문서의 특정 sign이나 bar가 텍스트 영역에 의해서 가려지는걸 고려하기 위해 제안되었다.

 

Text-Image Matching

 

[CLS]의 output representation을 classifier 분류기에 보내서 해당 이미지와 텍스트가 같은 문서의 페이지에서 왔는지 아닌지를 판변한다.

 

Positive pairs과 negative pairs가 존재하게 되는데, negatives를 구축하기 위해서 페이지의 이미지를 다른 문서로 대체하거나 삭제했다. 

 

모델의 치팅을 방지하기 위해서 negative samples에 대해서 masking과 covering operation을 수행했다.

 

모든 negative samples의 TIA target labels는 [Covered]로 설정되었다. 

 

 

 

3. Experiments

IIT-CDIP Test Collection을 pre-trian 데이터로 사용했다.

FUNSD, CORD, SROIE, Kleister-NDA, RVL-CDIP, DocVQA를 다운스트림 태스크로 설정했다.

데이터의 수에 대한 정보는 다음의 Table 1에 나와있다.

 

 

Transformer 구조는 12-layer, 12-head, encoders이며, hidden size는 768이다.  

위는 Base 모델이고 Large 모델은 헤드가 16개이며 hidden size는 1024다.  

Visual 백본은 ResNeXt101-FPN이다.

 

 

 

실험 결과 LayoutLMv2가 성능이 좋다는걸 확인할 수 있다.

 

 

 

 

 

 

References:

https://imlim0813.tistory.com/40