# Hugging Face에서 OpenCLIP 사용

[OpenCLIP](https://github.com/mlfoundations/open_clip)는 OpenAI의 CLIP의 오픈소스 구현입니다.

## Hub에서 OpenCLIP 탐색하기

[모델 페이지](https://huggingface.co/models?library=open_clip&sort=trending) 왼쪽에서 필터링하여 OpenCLIP 모델을 찾을 수 있습니다.

Hub에 호스팅된 OpenCLIP 모델에는 모델에 대한 유용한 정보가 포함된 모델 카드가 있습니다. OpenCLIP Hugging Face Hub 통합 덕분에 몇 줄의 코드로 OpenCLIP 모델을 로드할 수 있습니다. 또한 [Inference Endpoints](https://huggingface.co/inference-endpoints)를 사용하여 이러한 모델을 배포할 수도 있습니다.

## 설치

시작하려면 [OpenCLIP 설치 가이드](https://github.com/mlfoundations/open_clip#usage)를 따를 수 있습니다. 또한 다음 pip 한 줄 설치 명령을 사용할 수도 있습니다:

```
$ pip install open_clip_torch
```

## 기존 모델 사용

모든 OpenCLIP 모델은 Hub에서 쉽게 로드할 수 있습니다:

```py
import open_clip

model, preprocess = open_clip.create_model_from_pretrained('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
tokenizer = open_clip.get_tokenizer('hf-hub:laion/CLIP-ViT-g-14-laion2B-s12B-b42K')
```

로드가 완료되면 이미지를 인코딩하여 [제로샷 이미지 분류](https://huggingface.co/tasks/zero-shot-image-classification)를 수행할 수 있습니다:

```py
import torch
from PIL import Image
import requests

url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
image = Image.open(requests.get(url, stream=True).raw)
image = preprocess(image).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])

with torch.no_grad(), torch.cuda.amp.autocast():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)
    image_features /= image_features.norm(dim=-1, keepdim=True)
    text_features /= text_features.norm(dim=-1, keepdim=True)

    text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)

print("Label probs:", text_probs) 
```

각 가능한 클래스의 확률을 출력합니다:

```text
Label probs: tensor([[0.0020, 0.0034, 0.9946]])
```

특정 OpenCLIP 모델을 로드하려면 모델 카드에서 `Use in OpenCLIP`를 클릭하면 작동하는 스니펫이 제공됩니다!

## 추가 리소스

* OpenCLIP [저장소](https://github.com/mlfoundations/open_clip)
* OpenCLIP [문서](https://github.com/mlfoundations/open_clip/tree/main/docs)
* Hub의 OpenCLIP [모델](https://huggingface.co/models?library=open_clip&sort=trending)

