CLIP
CLIP은 이미지와 텍스트에 대한 임베딩을 생성할 수 있는 머신러닝 모델입니다. 이러한 임베딩은 제로샷 분류, 의미 기반 이미지 검색 등 여러 사용 사례에 활용될 수 있습니다. Roboflow Inference Server에서 CLIP을 사용하기 위해 제공되는 세 가지 경로는 다음과 같습니다:
embed_image: 이미지 임베딩을 계산하는 데 사용됩니다
embed_text: 텍스트 임베딩을 계산하는 데 사용됩니다
compare: 텍스트와 이미지의 임베딩을 계산한 후 비교하는 데 사용됩니다
이미지 임베딩
이미지 임베딩은 해당 이미지의 정보를 더 관리하기 쉬운 크기로 압축하는 것과 같습니다. 이미지를 임베딩할 때 수만 개의 픽셀로 구성된 이미지를 입력으로 받아 몇 백 개의 숫자(임베딩)로 정제합니다. 이러한 임베딩은 사람의 눈에 특히 의미가 있는 것은 아니지만, 다른 임베딩과 비교할 때 매우 유용할 수 있습니다.
CLIP과 Roboflow Inference Server를 사용하여 이미지 임베딩을 생성하려면:
#요청 페이로드 정의
infer_clip_payload = {
#이미지는 URL로 제공하거나 base64 인코딩된 문자열로 제공할 수 있습니다
"image": {
# "type"은 "base64"일 수도 있습니다
"type": "url",
# "value"는 이미지 데이터의 base64 인코딩 문자열일 수도 있습니다
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
},
}
# 인퍼런스 서버 URL 정의 (localhost:9001, infer.roboflow.com 등)
base_url = "https://infer.roboflow.com"
# Roboflow API Key 정의
api_key = <YOUR API KEY HERE>
res = requests.post(
f"{base_url}/clip/embed_image?api_key={api_key}",
json=infer_clip_payload,
)
embeddings = res.json()['embeddings']
print(embeddings)[[-0.4853120744228363, ... ]]여러 이미지를 한 번의 요청으로 임베딩할 수 있습니다:
#요청 페이로드 정의
infer_clip_payload = {
#이미지는 URL로 제공하거나 base64 인코딩된 문자열로 제공할 수 있습니다
"image": [
{
"type": "url",
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
},
{
"type": "url",
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
}
],
}
res = requests.post(
f"{base_url}/clip/embed_image?api_key={api_key}",
json=infer_clip_payload,
)텍스트 임베딩
CLIP은 이미지와 마찬가지로 텍스트에 대한 임베딩도 생성할 수 있습니다.
#요청 페이로드 정의
infer_clip_payload = {
"text": "the quick brown fox jumped over the lazy dog",
}
res = requests.post(
f"{base_url}/clip/embed_text?api_key={api_key}",
json=infer_clip_payload,
)
embeddings = res.json()['embeddings']
print(embeddings)[[0.56842650744228363, ... ]]여러 텍스트 블록을 하나의 요청으로 배치할 수 있습니다:
#요청 페이로드 정의
infer_clip_payload = {
"text": [
"the quick brown fox jumped over the lazy dog",
"how vexingly quick daft zebras jump"
]
}
res = requests.post(
f"{base_url}/clip/embed_text?api_key={api_key}",
json=infer_clip_payload,
)비교
CLIP의 진정한 가치는 임베딩을 비교할 때 실현됩니다. 비교는 코사인 유사도를 사용하여 계산된 두 임베딩 간의 수학적 거리입니다. 이 거리는 유사도 점수로 생각할 수 있습니다. 두 임베딩의 코사인 유사도가 1에 가깝다면, 이들은 유사하다고 볼 수 있습니다.
compare를 수행할 때 프롬프트와 하나 이상의 주제를 정의합니다. 텍스트와 이미지의 어떤 조합이든 비교할 수 있으므로 프롬프트 타입과 주제 타입도 정의해야 합니다.
#요청 페이로드 정의
infer_clip_payload = {
"prompt": {
"type": "url",
"value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
},
"prompt_type": "image",
"subject": "A very cute raccoon",
"subject_type": "text",
}
res = requests.post(
f"{base_url}/clip/compare?api_key={api_key}",
json=infer_clip_payload,
)
similarity = res.json()['similarity']
print(similarity)[0.30969720949239016]여러 프롬프트(단일 요청당 최대 8개)를 리스트로 전달할 수 있습니다:
infer_clip_payload = {
"subject": {
"type": "url",
"value": "https://i.imgur.com/Q6lDy8B.jpg",
},
"subject_type": "image",
"prompt": [
"A very cute raccoon",
"A large dog",
"A black cate",
],
"prompt_type": "text",
}[0.80559720949239016, 0.20329720949239016, 0.505559720949239016]Last updated
Was this helpful?