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 키 정의
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에 가까우면, 두 임베딩은 유사합니다.
비교를 수행할 때 프롬프트와 하나 이상의 대상을 정의합니다. 텍스트와 이미지의 모든 조합을 비교할 수 있으므로 프롬프트 타입과 대상 타입도 정의해야 합니다.
#요청 페이로드 정의
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?