one line of code at a time

Contrastive Language-Image Pre-training (CLIP) 정리 본문

AI공부

Contrastive Language-Image Pre-training (CLIP) 정리

oloc 2024. 8. 14. 02:51

https://arxiv.org/pdf/2103.00020

 

Contrastive Language-Image Pre-training (CLIP) bridges the gap between text and visual data by jointly training a model on a large-scale dataset containing images and their corresponding textual descriptions. 

 

이미지와 그것을 묘사한 텍스트 데이터를 같이 학습해서 텍스트와 이미지 데이터의 갭을 줄였다.

 

Contrastive Learning

- 비지도학습 (unsupervised learning)에서 쓰이는 기법. 많은 데이터 포인트들의 유사성과 차이를 학습시키는 방법이다. 

- main item ("anchor sample") 과 그것과 유사한 item (positive sample), 다른 item (negative sample)이 있다면, 이 constrastive learning의 목적은 모델이 anchor sample과 positive sample이 비슷하고, anchor sample과 negative sample이 다르다는 걸 알고, 비슷한 건 가깝게 다른 것은 멀게 조정하는 것이다.

 

 

CLIP 아키텍처

- 이미지 인코더와 텍스트 인코더 이렇게, 2개의 (dual) encoder 아키텍처를 쓴다. 이미지와 텍스트가 공유하는 latent space에 이미지와 텍스트 임베딩을 매핑하기 위해. 2개의 인코더를 jointly training 시킨다. 

- 이미지 인코더: The image encoder extracts salient features from the visual input. This encoder takes an ‘image as input’ and produces a high-dimensional vector representation. It typically uses a convolutional neural network (CNN) architecture, like ResNet, for extracting image features.

- 텍스트 인코더: The text encoder encodes the semantic meaning of the corresponding textual description. It takes a ‘text caption/label as input’ and produces another high-dimensional vector representation. It often uses a transformer-based architecture, like a Transformer or BERT, to process text sequences.

- Shared Embedding Space: The two encoders produce embeddings in a shared vector space. These shared embedding spaces allow CLIP to compare text and image representations and learn their underlying relationships.

→ latent space를 공유하므로 CLIP이 이미지와 텍스트 표현을 비교할 수 있고, 그것들의 관계를 학습할 수 있다.

 

CLIP Training Process

Step 1. Contrastive Pre-training

2개의 인코더를 활용해서 shared latent space에 임베딩을 매핑한다. 

 

Step 2: Create Dataset Classifiers from Label Text

각 이미지마다 여러 개의 텍스트 묘사가 생성된다. correct 묘사도 있지만, incorrect 묘사도 있다.

이것은 positive samples (matching) 뿐만 아니라 negative sample (mismatched) 쌍을 생성한다. 

 

여기서 Contrastive Loss Function이 등장하는데 바로 이 비용 함수를 통해 이미지와 텍스트 간의 incorrectly matching pair는 latent space에서 거리를 멀게 하고, correctly matching pair는 거리를 가깝게 한다. 

 

Step 3: Zero-shot Prediction

텍스트 인코더가 zero-shot classifier 역할을 한다. 새로운 이미지를 보고 CLIP은 zero-shot prediction을 할 수 있다.

 

CLIP Inference

한 개의 이미지와 가능한 caption 벡터들의 similarity score를 계산해서 가장 유사한 캡션 한 가지를 고른다.

 

Integration Between Natural Language and Image Processing

CLIP은 다음과 같은 일을 할 수 있다:

- 이미지의 캡션 생성하기

- 텍스트를 기반으로 이미지 분류하기

- 텍스트 프롬프트로 이미지 수정하기

 

Major Problems in Computer Vision and How CLIP Helps

CLIP은 visual feature와 textual description의 연관관계를 학습했다. 이는 모델이 어떻게 추론했는지를 설명하는 캡션을 생성할 수 있기에 interpretability와 신뢰성을 높일 수 있다. 

 

또한 클립은 zero-shot 분류가 가능한데 이것은 모델의 generalizability (일반화 성능)이 높다는 의미다.

 

또한 이미지에 대한 label이 없어도 분류가 가능하다. labelling 작업은 시간과 비용이 많이 소모되는 작업인데 클립을 활용하면 그런 부담을 줄일 수 있다.

 

참고

https://viso.ai/deep-learning/clip-machine-learning/

https://medium.com/one-minute-machine-learning/clip-paper-explained-easily-in-3-levels-of-detail-61959814ad13