CLIP
Contrastive Language-Image Pre-training
OpenAI's text-image connection model. Used for image search and classification.
Contrastive Language-Image Pre-training
OpenAI's text-image connection model. Used for image search and classification.
CLIP (Contrastive Language-Image Pre-training) is a multimodal AI model announced by OpenAI in January 2021, a neural network trained to understand text and images in the same embedding space. Trained on 400 million image-text pairs, it can classify and search images using natural language descriptions alone.
The core idea of CLIP is contrastive learning. It is trained so that correct image-text pairs are placed close together in the embedding space, while incorrect pairs are placed far apart. This made it possible to train on large-scale data collected from the web without separate labeling.
Previous image classification models worked only on predefined categories, such as the 1,000 classes of ImageNet. However, CLIP is capable of zero-shot classification, so it can recognize new categories not seen during training using natural language descriptions alone.
In practice, CLIP is used as a core component of image search engines, content moderation, and image generation models such as DALL-E. It is widely used in particular to build product search, similar image recommendation, and image-based question answering systems.
# CLIP을 사용한 이미지-텍스트 유사도 계산
import torch
from PIL import Image
from transformers import CLIPProcessor, CLIPModel
# 모델 및 프로세서 로드
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
# 이미지 로드 (URL 또는 로컬 파일)
image = Image.open("sample_image.jpg")
# 비교할 텍스트 후보들
texts = ["고양이 사진", "강아지 사진", "자동차 사진", "음식 사진"]
# 입력 전처리
inputs = processor(text=texts, images=image, return_tensors="pt", padding=True)
# 임베딩 추출 및 유사도 계산
with torch.no_grad():
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)
# 결과 출력
for text, prob in zip(texts, probs[0]):
print(f"{text}: {prob.item():.2%}")
"To improve the product search feature, we should adopt an approach based on CLIP that calculates the embedding similarity between text queries and product images. This enables semantic search rather than the existing keyword matching."
"CLIP's contrastive learning approach calculates cosine similarity for all image-text pairs within a batch, and trains to increase the similarity of the diagonal elements (correct pairs) while decreasing the rest (incorrect pairs)."
"If we compute CLIP embeddings every time without caching them, the latency is too high. It would be better to extract the product image embeddings in advance and store them in a vector DB, and compute only the text embedding in real time at query time."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
CLIP distinguishes general concepts well, but for fine-grained classification in specific domains (e.g., distinguishing particular breeds, medical imaging), its accuracy may be lower than that of fine-tuned specialized models.
Because it is based on Vision Transformer, the inference cost is considerable. When processing images at large scale, consider batch processing, GPU optimization, or using a lightweight version such as ViT-B/32.
Using a prompt template of the form "a photo of a [object]" greatly improves performance. Sentence-style prompts are more effective than simple nouns.