|
| 1 | +<!--Copyright 2023 The HuggingFace Team. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +
|
| 12 | +⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be |
| 13 | +rendered properly in your Markdown viewer. |
| 14 | +
|
| 15 | +--> |
| 16 | + |
| 17 | +# SigLIP[[siglip]] |
| 18 | + |
| 19 | +<div class="flex flex-wrap space-x-1"> |
| 20 | +<img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 21 | +<img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat"> |
| 22 | +<img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white"> |
| 23 | +</div> |
| 24 | + |
| 25 | +## 개요[[overview]] |
| 26 | + |
| 27 | +SigLIP 모델은 Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov, Lucas Beyer의 [Sigmoid Loss for Language Image Pre-Training](https://arxiv.org/abs/2303.15343) 논문에서 제안되었습니다. SigLIP은 [CLIP](clip)에서 사용된 손실 함수를 간단한 쌍별 시그모이드 손실(pairwise sigmoid loss)로 대체할 것을 제안합니다. 이는 ImageNet에서 제로샷 분류 정확도 측면에서 더 나은 성능을 보입니다. |
| 28 | + |
| 29 | +논문의 초록은 다음과 같습니다: |
| 30 | + |
| 31 | +*우리는 언어-이미지 사전 학습(Language-Image Pre-training, SigLIP)을 위한 간단한 쌍별 시그모이드 손실을 제안합니다. 소프트맥스 정규화를 사용하는 표준 대조 학습과 달리, 시그모이드 손실은 이미지-텍스트 쌍에만 작용하며 정규화를 위해 쌍별 유사성의 전역적 관점을 필요로 하지 않습니다. 시그모이드 손실은 배치 크기를 더욱 확장할 수 있게 하는 동시에 작은 배치 크기에서도 더 나은 성능을 보입니다. Locked-image Tuning과 결합하여, 단 4개의 TPUv4 칩만으로 이틀 만에 84.5%의 ImageNet 제로샷 정확도를 달성하는 SigLiT 모델을 학습했습니다. 손실 함수에서 배치 크기를 분리함으로써 예제 대 쌍의 영향과 Negative 대 Positive 비율을 연구할 수 있게 되었습니다. 마지막으로, 우리는 배치 크기를 100만 개까지 극단적으로 늘려보았고, 배치 크기 증가의 이점이 빠르게 감소하며 32k의 더 합리적인 배치 크기로도 충분하다는 것을 발견했습니다.* |
| 32 | + |
| 33 | +## 사용 팁[[usage-tips]] |
| 34 | + |
| 35 | +- SigLIP의 사용법은 [CLIP](clip)과 유사합니다. 주요 차이점은 학습 손실 함수로, 배치 내 모든 이미지와 텍스트 간의 쌍별 유사성에 대한 전역적 관점이 필요하지 않습니다. 소프트맥스 대신 로짓에 시그모이드 활성화 함수를 적용해야 합니다. |
| 36 | +- 학습은 지원되지만 `torch.distributed` 유틸리티를 사용하지 않아 배치 크기의 확장성이 제한될 수 있습니다. 그러나 단일 노드 다중 GPU 설정에서는 DDP와 FDSP가 작동합니다. |
| 37 | +- 독립형 [`SiglipTokenizer`] 또는 [`SiglipProcessor`]를 사용할 때는 모델이 그렇게 학습되었으므로 `padding="max_length"`를 전달해야 합니다. |
| 38 | +- 파이프라인과 동일한 결과를 얻으려면 "This is a photo of {label}."의 프롬프트 템플릿을 사용해야 합니다. |
| 39 | + |
| 40 | +<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/siglip_table.jpeg" |
| 41 | +alt="drawing" width="600"/> |
| 42 | + |
| 43 | +<small> CLIP과 비교한 SigLIP 평가 결과. <a href="https://arxiv.org/abs/2303.15343">원본 논문</a>에서 발췌.</small> |
| 44 | + |
| 45 | +이 모델은 [nielsr](https://huggingface.co/nielsr)가 기여했습니다. |
| 46 | +원본 코드는 [여기](https://github.com/google-research/big_vision/tree/main)에서 찾을 수 있습니다. |
| 47 | + |
| 48 | +## 사용 예시[[usage-example]] |
| 49 | + |
| 50 | +SigLIP을 사용하는 방법에는 두 가지 주요 방법이 있습니다: 모든 복잡성을 추상화하는 파이프라인 API를 사용하거나, 직접 `SiglipModel` 클래스를 사용하는 방법입니다. |
| 51 | + |
| 52 | +### 파이프라인 API[[pipeline-API]] |
| 53 | + |
| 54 | +파이프라인을 사용하면 몇 줄의 코드로 모델을 사용할 수 있습니다: |
| 55 | + |
| 56 | +```python |
| 57 | +>>> from transformers import pipeline |
| 58 | +>>> from PIL import Image |
| 59 | +>>> import requests |
| 60 | + |
| 61 | +>>> # 파이프라인 로드 |
| 62 | +>>> image_classifier = pipeline(task="zero-shot-image-classification", model="google/siglip-base-patch16-224") |
| 63 | + |
| 64 | +>>> # 이미지 로드 |
| 65 | +>>> url = 'http://images.cocodataset.org/val2017/000000039769.jpg' |
| 66 | +>>> image = Image.open(requests.get(url, stream=True).raw) |
| 67 | + |
| 68 | +>>> # 추론 |
| 69 | +>>> candidate_labels = ["2 cats", "a plane", "a remote"] |
| 70 | +>>> outputs = image_classifier(image, candidate_labels=candidate_labels) |
| 71 | +>>> outputs = [{"score": round(output["score"], 4), "label": output["label"] } for output in outputs] |
| 72 | +>>> print(outputs) |
| 73 | +[{'score': 0.1979, 'label': '2 cats'}, {'score': 0.0, 'label': 'a remote'}, {'score': 0.0, 'label': 'a plane'}] |
| 74 | +``` |
| 75 | + |
| 76 | +### 직접 모델 사용하기[[using-the-model-yourself]] |
| 77 | + |
| 78 | +전처리와 후처리를 직접 수행하려면 다음과 같이 하면 됩니다: |
| 79 | + |
| 80 | +```python |
| 81 | +>>> from PIL import Image |
| 82 | +>>> import requests |
| 83 | +>>> from transformers import AutoProcessor, AutoModel |
| 84 | +>>> import torch |
| 85 | + |
| 86 | +>>> model = AutoModel.from_pretrained("google/siglip-base-patch16-224") |
| 87 | +>>> processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224") |
| 88 | + |
| 89 | +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" |
| 90 | +>>> image = Image.open(requests.get(url, stream=True).raw) |
| 91 | + |
| 92 | +>>> candidate_labels = ["2 cats", "2 dogs"] |
| 93 | +# 파이프라인 프롬프트 템플릿을 따라 동일한 결과를 얻습니다 |
| 94 | +>>> texts = [f'This is a photo of {label}.' for label in candidate_labels] |
| 95 | +# 중요: 모델이 이렇게 학습되었으므로 `padding=max_length`를 전달합니다 |
| 96 | +>>> inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt") |
| 97 | + |
| 98 | +>>> with torch.no_grad(): |
| 99 | +... outputs = model(**inputs) |
| 100 | + |
| 101 | +>>> logits_per_image = outputs.logits_per_image |
| 102 | +>>> probs = torch.sigmoid(logits_per_image) # 시그모이드 활성화 함수를 적용한 확률입니다 |
| 103 | +>>> print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'") |
| 104 | +19.8% that image 0 is '2 cats' |
| 105 | +``` |
| 106 | + |
| 107 | +## 리소스[[resources]] |
| 108 | + |
| 109 | +SigLIP을 시작하는 데 도움이 되는 공식 Hugging Face 및 커뮤니티(🌎로 표시) 리소스 목록입니다. |
| 110 | + |
| 111 | +- [제로샷 이미지 분류 작업 가이드](../tasks/zero_shot_image_classification) |
| 112 | +- SigLIP에 대한 데모 노트북은 [여기](https://github.com/NielsRogge/Transformers-Tutorials/tree/master/SigLIP)에서 찾을 수 있습니다. 🌎 |
| 113 | + |
| 114 | +여기에 포함될 리소스를 제출하는 데 관심이 있으시면 Pull Request를 열어주시면 검토하겠습니다! 리소스는 이상적으로 기존 리소스를 복제하는 대신 새로운 것을 보여주어야 합니다. |
| 115 | + |
| 116 | + |
| 117 | +## SigLIP과 Flash Attention 2 결합하기[[combining-siglip-with-flash-attention-2]] |
| 118 | + |
| 119 | +먼저 Flash Attention 2의 최신 버전을 설치해야 합니다. |
| 120 | + |
| 121 | +```bash |
| 122 | +pip install -U flash-attn --no-build-isolation |
| 123 | +``` |
| 124 | + |
| 125 | +또한 Flash-Attention 2와 호환되는 하드웨어가 있는지 확인하세요. flash-attn 저장소의 공식 문서에서 자세히 알아보세요. 또한 모델을 반정밀도(예: `torch.float16`)로 로드해야 합니다. |
| 126 | + |
| 127 | +Flash Attention 2를 사용하여 모델을 로드하고 실행하려면 아래 코드를 참조하세요: |
| 128 | + |
| 129 | +```python |
| 130 | +>>> import torch |
| 131 | +>>> import requests |
| 132 | +>>> from PIL import Image |
| 133 | +>>> from transformers import SiglipProcessor, SiglipModel |
| 134 | +>>> device = "cuda" # 모델을 로드할 장치 |
| 135 | + |
| 136 | +>>> model = SiglipModel.from_pretrained( |
| 137 | +... "google/siglip-so400m-patch14-384", |
| 138 | +... attn_implementation="flash_attention_2", |
| 139 | +... torch_dtype=torch.float16, |
| 140 | +... device_map=device, |
| 141 | +... ) |
| 142 | +>>> processor = SiglipProcessor.from_pretrained("google/siglip-so400m-patch14-384") |
| 143 | + |
| 144 | +>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg" |
| 145 | +>>> image = Image.open(requests.get(url, stream=True).raw) |
| 146 | + |
| 147 | +>>> candidate_labels = ["2 cats", "2 dogs"] |
| 148 | +# 파이프라인 프롬프트 템플릿을 따라 동일한 결과를 얻습니다 |
| 149 | +>>> texts = [f'This is a photo of {label}.' for label in candidate_labels] |
| 150 | +# 중요: 모델이 이렇게 학습되었으므로 `padding=max_length`를 전달합니다 |
| 151 | +>>> inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt").to(device) |
| 152 | + |
| 153 | +>>> with torch.no_grad(): |
| 154 | +... with torch.autocast(device): |
| 155 | +... outputs = model(**inputs) |
| 156 | + |
| 157 | +>>> logits_per_image = outputs.logits_per_image |
| 158 | +>>> probs = torch.sigmoid(logits_per_image) # 시그모이드 활성화 함수를 적용한 확률입니다 |
| 159 | +>>> print(f"{probs[0][0]:.1%} that image 0 is '{candidate_labels[0]}'") |
| 160 | +19.8% that image 0 is '2 cats' |
| 161 | +``` |
| 162 | + |
| 163 | + |
| 164 | +## Scaled Dot Product Attention(SDPA) 사용하기[using-scaled-dot-product-attention(SDPA)]] |
| 165 | + |
| 166 | +PyTorch는 `torch.nn.functional`의 일부로 스케일된 점곱 어텐션(SDPA) 연산자를 포함합니다. 이 함수는 |
| 167 | +입력과 사용 중인 하드웨어에 따라 적용할 수 있는 여러 구현을 포함합니다. 자세한 내용은 |
| 168 | +[공식 문서](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html) |
| 169 | +또는 [GPU 추론](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention) |
| 170 | +페이지를 참조하세요. |
| 171 | + |
| 172 | +`from_pretrained()`에서 `attn_implementation="sdpa"`를 설정하여 SDPA를 명시적으로 요청할 수 있습니다. `torch>=2.1.1`이 설치되어 있는지 확인하세요. |
| 173 | + |
| 174 | +```python |
| 175 | +>>> from transformers import SiglipModel |
| 176 | + |
| 177 | +>>> model = SiglipModel.from_pretrained( |
| 178 | +... "google/siglip-so400m-patch14-384", |
| 179 | +... attn_implementation="sdpa", |
| 180 | +... torch_dtype=torch.float16, |
| 181 | +... device_map=device, |
| 182 | +... ) |
| 183 | +``` |
| 184 | + |
| 185 | +최상의 속도 향상을 위해 모델을 반정밀도(예: `torch.float16` 또는 `torch.bfloat16`)로 로드하는 것이 좋습니다. |
| 186 | + |
| 187 | + |
| 188 | +## 예상 속도 향상[[expected-speedups]] |
| 189 | + |
| 190 | +아래는 `google/siglip-so400m-patch14-384` 체크포인트를 `float16` 정밀도로 사용하는 transformers의 네이티브 구현과 Flash Attention 2 / SDPA 버전의 모델을 다양한 배치 크기로 비교한 추론 시간의 예상 속도 향상 다이어그램입니다. |
| 191 | + |
| 192 | +<div style="text-align: center"> |
| 193 | +<img src="https://i.imgur.com/cWm4rsn.png"> |
| 194 | +</div> |
| 195 | + |
| 196 | + |
| 197 | +## SiglipConfig |
| 198 | + |
| 199 | +[[autodoc]] SiglipConfig |
| 200 | + - from_text_vision_configs |
| 201 | + |
| 202 | +## SiglipTextConfig |
| 203 | + |
| 204 | +[[autodoc]] SiglipTextConfig |
| 205 | + |
| 206 | +## SiglipVisionConfig |
| 207 | + |
| 208 | +[[autodoc]] SiglipVisionConfig |
| 209 | + |
| 210 | +## SiglipTokenizer |
| 211 | + |
| 212 | +[[autodoc]] SiglipTokenizer |
| 213 | + - build_inputs_with_special_tokens |
| 214 | + - get_special_tokens_mask |
| 215 | + - create_token_type_ids_from_sequences |
| 216 | + - save_vocabulary |
| 217 | + |
| 218 | +## SiglipImageProcessor |
| 219 | + |
| 220 | +[[autodoc]] SiglipImageProcessor |
| 221 | + - preprocess |
| 222 | + |
| 223 | +## SiglipImageProcessorFast |
| 224 | + |
| 225 | +[[autodoc]] SiglipImageProcessorFast |
| 226 | + - preprocess |
| 227 | + |
| 228 | +## SiglipProcessor |
| 229 | + |
| 230 | +[[autodoc]] SiglipProcessor |
| 231 | + |
| 232 | +## SiglipModel |
| 233 | + |
| 234 | +[[autodoc]] SiglipModel |
| 235 | + - forward |
| 236 | + - get_text_features |
| 237 | + - get_image_features |
| 238 | + |
| 239 | +## SiglipTextModel |
| 240 | + |
| 241 | +[[autodoc]] SiglipTextModel |
| 242 | + - forward |
| 243 | + |
| 244 | +## SiglipVisionModel |
| 245 | + |
| 246 | +[[autodoc]] SiglipVisionModel |
| 247 | + - forward |
| 248 | + |
| 249 | + |
| 250 | +## SiglipForImageClassification |
| 251 | + |
| 252 | +[[autodoc]] SiglipForImageClassification |
| 253 | + - forward |
0 commit comments