> 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/roboflow/roboflow-ko/deploy/serverless/foundation-models/clip.md).

# 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",
    },
}

# inference server 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)
```

{% 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?api_key={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?api_key={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?api_key={api_key}",
    json=infer_clip_payload,
)
```

## 비교

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

compare를 수행할 때는 프롬프트와 하나 또는 여러 개의 subject를 정의합니다. 텍스트나 이미지의 어떤 조합도 비교할 수 있으므로, prompt type과 subject type도 함께 정의해야 합니다.

```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": "아주 귀여운 너구리",
    "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": [
        "아주 귀여운 너구리",
        "큰 개",
        "검은 고양이",
    ],
    "prompt_type": "text",
}
```

```
[0.80559720949239016, 0.20329720949239016, 0.505559720949239016]
```
