> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/deployment/ko/legacy/legacy-serverless/foundation-models/clip.md).

# CLIP(레거시)

Roboflow Inference Server를 사용해 CLIP 이미지 및 텍스트 임베딩을 생성하고 비교합니다.

CLIP은 이미지와 텍스트의 임베딩을 생성할 수 있는 머신러닝 모델입니다. 이러한 임베딩은 제로샷 분류, 의미 기반 이미지 검색 등 다양한 용도로 사용할 수 있습니다. Roboflow Inference Server에서 CLIP을 사용하는 경로는 다음 세 가지입니다.

* embed\_image: 이미지 임베딩 계산에 사용
* embed\_text: 텍스트 임베딩 계산에 사용
* compare: 텍스트와 이미지의 임베딩을 계산한 뒤 비교하는 데 사용

## 이미지 임베딩

이미지 임베딩은 이미지에 담긴 정보를 더 다루기 쉬운 크기로 압축하는 것과 같습니다. 이미지 임베딩을 생성할 때는 수만 개의 픽셀로 이루어진 이미지를 입력으로 받아, 이를 임베딩이라고 하는 수백 개의 숫자로 간추립니다. 이러한 임베딩은 사람의 눈으로 볼 때 특별한 의미가 있는 것은 아니지만, 다른 임베딩과 비교하면 매우 유용할 수 있습니다.

CLIP과 Roboflow Inference Server를 사용해 이미지 임베딩을 생성하려면 다음과 같이 합니다.

{% code overflow="wrap" %}

```python
#요청 페이로드 정의
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",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
```

{% endcode %}

```bash
[[-0.4853120744228363, ... ]]
```

요청 하나로 여러 이미지의 임베딩을 생성할 수 있습니다.

```python
#요청 페이로드 정의
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",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)
```

## 텍스트 임베딩

CLIP은 이미지뿐만 아니라 텍스트의 임베딩도 생성할 수 있습니다.

```python
#요청 페이로드 정의
infer_clip_payload = {
    "text": "the quick brown fox jumped over the lazy dog",
}

res = requests.post(
    f"{base_url}/clip/embed_text",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
```

```
[[0.56842650744228363, ... ]]
```

여러 텍스트 블록을 하나의 요청에 묶어 보낼 수 있습니다.

```python
#요청 페이로드 정의
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",
    headers={"Authorization": f"Bearer {api_key}"},
    json=infer_clip_payload,
)
```

## 비교

CLIP의 진정한 가치는 임베딩을 비교할 때 드러납니다. 비교는 코사인 유사도를 사용해 두 임베딩 사이의 수학적 거리를 계산합니다. 이 거리는 유사도 점수로 볼 수 있습니다. 두 임베딩의 코사인 유사도가 1에 가까우면 서로 유사합니다.

비교를 수행할 때 프롬프트와 하나 이상의 대상을 지정합니다. 텍스트와 이미지의 모든 조합을 비교할 수 있으므로 프롬프트 유형과 대상 유형도 지정해야 합니다.

```python
#요청 페이로드 정의
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",
    headers={"Authorization": f"Bearer {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]
```
