Skip to content

Commit dd43daf

Browse files
fabxoeahnjjjun048098
authored
🌐 [i18n-KO] Translated �model_doc/clip.md to Korean (#33610)
* docs: ko: model_doc/clip.md * feat: nmt draft * fix: manual edits * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions Co-authored-by: HyeokJun SHIN <[email protected]> * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions Co-authored-by: HyeokJun SHIN <[email protected]> * fix: resolve suggestions * fix: resolve suggestions * fix: resolve suggestions * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions * fix: resolve suggestions Co-authored-by: Ahnjj_DEV <[email protected]> * fix: resolve suggestions --------- Co-authored-by: Ahnjj_DEV <[email protected]> Co-authored-by: HyeokJun SHIN <[email protected]>
1 parent acde6c7 commit dd43daf

File tree

2 files changed

+320
-2
lines changed

2 files changed

+320
-2
lines changed

docs/source/ko/_toctree.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@
675675
title: Chameleon
676676
- local: in_translation
677677
title: (번역중) Chinese-CLIP
678-
- local: in_translation
679-
title: (번역중) CLIP
678+
- local: model_doc/clip
679+
title: CLIP
680680
- local: in_translation
681681
title: (번역중) CLIPSeg
682682
- local: in_translation

docs/source/ko/model_doc/clip.md

Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
<!--Copyright 2021 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+
# CLIP[[clip]]
18+
19+
## 개요[[overview]]
20+
21+
CLIP 모델은 Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh,
22+
Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever가 제안한 [자연어 지도(supervision)를 통한 전이 가능한 시각 모델 학습](https://arxiv.org/abs/2103.00020)라는 논문에서 소개되었습니다. CLIP(Contrastive Language-Image Pre-Training)은 다양한 이미지와 텍스트 쌍으로 훈련된 신경망 입니다. GPT-2와 3의 제로샷 능력과 유사하게, 해당 작업에 직접적으로 최적화하지 않고도 주어진 이미지에 대해 가장 관련성 있는 텍스트 스니펫을 예측하도록 자연어로 지시할 수 있습니다.
23+
24+
해당 논문의 초록입니다.
25+
26+
*최신 컴퓨터 비전 시스템은 미리 정해진 고정된 객체 카테고리 집합을 예측하도록 훈련됩니다. 이러한 제한된 형태의 지도는 다른 시각적 개념을 지정하기 위해 추가적인 라벨링된 데이터가 필요하므로 그 일반성과 사용성을 제한합니다. 이미지 원시 텍스트에서 직접 학습하는 것은 훨씬 더 광범위한 지도 소스를 활용하는 아주 좋은 대안입니다. 이미지와 캡션을 맞추는 간단한 사전 학습 작업이, 인터넷에서 수집한 4억 쌍의 이미지-텍스트 데이터셋에서 SOTA 수준의 이미지 표현을 처음부터 효율적이고 확장 가능하게 학습하는 방법임을 확인할 수 있습니다. 사전 훈련 후, 자연어는 학습된 시각적 개념을 참조하거나 새로운 개념을 설명하는 데 사용되어 모델의 하위 작업으로의 제로샷 전이를 가능하게 합니다. 해당 논문에서는 OCR, 비디오 내 행동 인식, 지리적 위치 파악, 그리고 많은 종류의 세밀한 객체 분류 등 30개 이상의 다양한 기존 컴퓨터 비전 데이터셋에 대한 벤치마킹을 통해 이 접근 방식의 성능을 연구합니다. 이 모델은 대부분의 작업에 대해 의미 있게 전이되며, 종종 데이터셋별 훈련 없이도 완전 지도 학습 기준선과 경쟁력 있는 성능을 보입니다. 예를 들어, ImageNet에서 원래 ResNet-50의 정확도를 제로샷으로 일치시키는데, 이는 ResNet-50이 훈련된 128만 개의 훈련 예제를 전혀 사용할 필요가 없었습니다. 코드 및 사전 훈련된 모델 가중치는 이 https URL에서 공개합니다.*
27+
28+
이 모델은 [valhalla](https://huggingface.co/valhalla)에 의해 기여되었습니다.
29+
원본 코드는 [이곳](https://github.com/openai/CLIP)에서 확인할 수 있습니다.
30+
31+
## 사용 팁과 예시[[usage-tips-and-example]]
32+
33+
CLIP은 멀티모달 비전 밒 언어 모델입니다. 이미지-텍스트 유사도 계산과 제로샷 이미지 분류에 사용될 수 있습니다. CLIP은 ViT와 유사한 트랜스포머를 사용하여 시각적 특징을 추출하고, 인과적 언어 모델을 사용하여 텍스트 특징을 추출합니다. 그 후 텍스트와 시각적 특징 모두 동일한 차원의 잠재(latent) 공간으로 투영됩니다. 투영된 이미지와 텍스트 특징 사이의 내적이 유사도 점수로 사용됩니다.
34+
35+
트랜스포머 인코더에 이미지를 입력하기 위해, 각 이미지는 고정 크기의 겹치지 않는 패치들의 시퀀스로 분할되고, 이후 선형 임베딩됩니다. [CLS]토큰이 전체 이미지의 표현으로 추가됩니다. 저자들은 또한 절대 위치 임베딩을 추가하고, 결과로 나온 벡터 시퀀스를 표준 트랜스포머 인토더에 입력합니다. [`CLIPImageProcessor`]는 모델을 위해 이미지를 리사이즈(또는 재스캐일링)하고 정규화하는데 사용될 수 있습니다.
36+
37+
[`CLIPTokenizer`]는 텍스트를 인코딩하는데 사용됩니다. [`CLIPProcessor`][`CLIPImageProcessor`][`CLIPTokenizer`]를 하나의 인스턴스로 감싸서 텍스트를 인코딩하고 이미지를 준비하는데 모두 사용됩니다.
38+
39+
다음 예시는 [`CLIPProcessor`][`CLIPModel`]을 사용하여 이미지-텍스트 유사도 점수를 얻는 방법을 보여줍니다.
40+
41+
42+
```python
43+
>>> from PIL import Image
44+
>>> import requests
45+
46+
>>> from transformers import CLIPProcessor, CLIPModel
47+
48+
>>> model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
49+
>>> processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
50+
51+
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
52+
>>> image = Image.open(requests.get(url, stream=True).raw)
53+
54+
>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
55+
56+
>>> outputs = model(**inputs)
57+
>>> logits_per_image = outputs.logits_per_image # 이미지-텍스트 유사성 점수
58+
>>> probs = logits_per_image.softmax(dim=1) # 확률을 레이블링 하기위해서 소프트맥스를 취합니다.
59+
```
60+
61+
62+
### CLIP과 플래시 어텐션2 결합[[combining-clip-and-flash-attention-2]]
63+
64+
먼저 최신버전의 플래시 어텐션2를 설치합니다.
65+
66+
```bash
67+
pip install -U flash-attn --no-build-isolation
68+
```
69+
70+
플래시 어텐션2와 호환되는 하드웨어를 가지고 있는지 확인하세요. 이에 대한 자세한 내용은 flash-attn 리포지토리의 공식문서에서 확인할 수 있습니다. 또한 모델을 반정밀도(`torch.float16`)로 로드하는 것을 잊지 마세요.
71+
72+
<Tip warning={true}>
73+
74+
작은 배치 크기를 사용할 때, 플래시 어텐션을 사용하면 모델이 느려지는 것을 느낄 수 있습니다.아래의 [플래시 어텐션과 SDPA를 사용한 예상 속도 향상](#Expected-speedups-with-Flash-Attention-and-SDPA) 섹션을 참조하여 적절한 어텐션 구현을 선택하세요.
75+
76+
</Tip>
77+
78+
플래시 어텐션2를 사용해서 모델을 로드하고 구동하기 위해서 다음 스니펫을 참고하세요:
79+
80+
```python
81+
>>> import torch
82+
>>> import requests
83+
>>> from PIL import Image
84+
85+
>>> from transformers import CLIPProcessor, CLIPModel
86+
87+
>>> device = "cuda"
88+
>>> torch_dtype = torch.float16
89+
90+
>>> model = CLIPModel.from_pretrained(
91+
... "openai/clip-vit-base-patch32",
92+
... attn_implementation="flash_attention_2",
93+
... device_map=device,
94+
... torch_dtype=torch_dtype,
95+
... )
96+
>>> processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
97+
98+
>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
99+
>>> image = Image.open(requests.get(url, stream=True).raw)
100+
101+
>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)
102+
>>> inputs.to(device)
103+
104+
>>> with torch.no_grad():
105+
... with torch.autocast(device):
106+
... outputs = model(**inputs)
107+
108+
>>> logits_per_image = outputs.logits_per_image # 이미지-텍스트 유사성 점수
109+
>>> probs = logits_per_image.softmax(dim=1) # 확률을 레이블링 하기위해서 소프트맥스를 취합니다.
110+
>>> print(probs)
111+
tensor([[0.9946, 0.0052]], device='cuda:0', dtype=torch.float16)
112+
```
113+
114+
115+
### 스케일된 내적 어텐션 (Scaled dot-product Attention(SDPA)) 사용하기[[using-scaled-dot-product-attention-sdpa]]
116+
117+
파이토치는 `torch.nn.functional`의 일부로 네이티브 스케일된 내적 어텐션(SPDA) 연산자를 포함하고 있습니다. 이 함수는 입력과 사용 중인 하드웨어에 따라 적용될 수 있는 여러 구현을 포함합니다. 자세한 정보는 [공식문서](https://pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html)[GPU 추론](https://huggingface.co/docs/transformers/main/en/perf_infer_gpu_one#pytorch-scaled-dot-product-attention) 페이지를 참조하세요.
118+
119+
`torch>=2.1.1`에서는 구현이 가능할 때 SDPA가 기본적으로 사용되지만, `from_pretrained()` 함수에서 `attn_implementation="sdpa"`를 설정하여 SDPA를 명시적으로 사용하도록 요청할 수도 있습니다.
120+
121+
```python
122+
from transformers import CLIPModel
123+
124+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32", torch_dtype=torch.float16, attn_implementation="sdpa")
125+
```
126+
127+
최고의 속도향상을 위해서, 반정밀도로 모델을 로드하는 것을 추천합니다. (예를들면 `torch.float16` 또는 `torch.bfloat16`).
128+
129+
### 플래시 어텐션과 스케일된 내적 어텐션(SDPA)으로 인해 예상되는 속도향상[[expected-speedups-with-flash-attention-and-sdpa]]
130+
131+
로컬 벤치마크(NVIDIA A10G, PyTorch 2.3.1+cu121)에서 `float16`을 사용하여 `"openai/clip-vit-large-patch14"` 체크포인트로 추론을 수행했을 때, 다음과 같은 속도 향상을 확인 했습니다.
132+
[코드](https://gist.github.com/qubvel/ac691a54e54f9fae8144275f866a7ff8):
133+
134+
#### CLIPTextModel[[cliptextmodel]]
135+
136+
| Num text labels | Eager (s/iter) | FA2 (s/iter) | FA2 speedup | SDPA (s/iter) | SDPA speedup |
137+
|------------------:|-----------------:|---------------:|--------------:|----------------:|---------------:|
138+
| 4 | 0.009 | 0.012 | 0.737 | 0.007 | 1.269 |
139+
| 16 | 0.009 | 0.014 | 0.659 | 0.008 | 1.187 |
140+
| 32 | 0.018 | 0.021 | 0.862 | 0.016 | 1.142 |
141+
| 64 | 0.034 | 0.034 | 1.001 | 0.03 | 1.163 |
142+
| 128 | 0.063 | 0.058 | 1.09 | 0.054 | 1.174 |
143+
144+
![clip_text_model_viz_3](https://github.com/user-attachments/assets/e9826b43-4e66-4f4c-952b-af4d90bd38eb)
145+
146+
#### CLIPVisionModel[[clipvisionmodel]]
147+
148+
| Image batch size | Eager (s/iter) | FA2 (s/iter) | FA2 speedup | SDPA (s/iter) | SDPA speedup |
149+
|-------------------:|-----------------:|---------------:|--------------:|----------------:|---------------:|
150+
| 1 | 0.016 | 0.013 | 1.247 | 0.012 | 1.318 |
151+
| 4 | 0.025 | 0.021 | 1.198 | 0.021 | 1.202 |
152+
| 16 | 0.093 | 0.075 | 1.234 | 0.075 | 1.24 |
153+
| 32 | 0.181 | 0.147 | 1.237 | 0.146 | 1.241 |
154+
155+
![clip_image_model_viz_3](https://github.com/user-attachments/assets/50a36206-e3b9-4adc-ac8e-926b8b071d63)
156+
157+
#### CLIPModel[[clipmodel]]
158+
159+
| Image batch size | Num text labels | Eager (s/iter) | FA2 (s/iter) | FA2 speedup | SDPA (s/iter) | SDPA speedup |
160+
|-------------------:|------------------:|-----------------:|---------------:|--------------:|----------------:|---------------:|
161+
| 1 | 4 | 0.025 | 0.026 | 0.954 | 0.02 | 1.217 |
162+
| 1 | 16 | 0.026 | 0.028 | 0.918 | 0.02 | 1.287 |
163+
| 1 | 64 | 0.042 | 0.046 | 0.906 | 0.036 | 1.167 |
164+
| 4 | 4 | 0.028 | 0.033 | 0.849 | 0.024 | 1.189 |
165+
| 4 | 16 | 0.034 | 0.035 | 0.955 | 0.029 | 1.169 |
166+
| 4 | 64 | 0.059 | 0.055 | 1.072 | 0.05 | 1.179 |
167+
| 16 | 4 | 0.096 | 0.088 | 1.091 | 0.078 | 1.234 |
168+
| 16 | 16 | 0.102 | 0.09 | 1.129 | 0.083 | 1.224 |
169+
| 16 | 64 | 0.127 | 0.11 | 1.157 | 0.105 | 1.218 |
170+
| 32 | 4 | 0.185 | 0.159 | 1.157 | 0.149 | 1.238 |
171+
| 32 | 16 | 0.19 | 0.162 | 1.177 | 0.154 | 1.233 |
172+
| 32 | 64 | 0.216 | 0.181 | 1.19 | 0.176 | 1.228 |
173+
174+
## 자료[[resources]]
175+
176+
CLIP을 시작하는 데 도움이 되는 Hugging Face와 community 자료 목록(🌎로 표시됨) 입니다.
177+
178+
- [원격 센싱 (인공위성) 이미지와 캡션을 가지고 CLIP 미세조정하기](https://huggingface.co/blog/fine-tune-clip-rsicd):
179+
[RSICD dataset](https://github.com/201528014227051/RSICD_optimal)을 가지고 CLIP을 미세조정 하는 방법과 데이터 증강에 대한 성능 비교에 대한 블로그 포스트
180+
-[예시 스크립트](https://github.com/huggingface/transformers/tree/main/examples/pytorch/contrastive-image-text)[COCO dataset](https://cocodataset.org/#home)를 이용한 사전학습된 비전과 텍스트와 인코더를 사용해서 CLIP같은 비전-텍스트 듀얼 모델을 어떻게 학습시키는지 보여줍니다.
181+
182+
<PipelineTag pipeline="image-to-text"/>
183+
184+
- 사전학습된 CLIP모델을 이미지 캡셔닝을 위한 빔서치 추론에 어떻게 활용하는지에 관한 [노트북](https://colab.research.google.com/drive/1tuoAC5F4sC7qid56Z0ap-stR3rwdk0ZV?usp=sharing)
185+
186+
**이미지 검색**
187+
188+
- 사전학습된 CLIP모델과 MRR(Mean Reciprocal Rank) 점수 연산을 사용한 이미지 검색에 대한 [노트북](https://colab.research.google.com/drive/1bLVwVKpAndpEDHqjzxVPr_9nGrSbuOQd?usp=sharing). 🌎
189+
- 이미지 검색과 유사성 점수에 대해 보여주는 [노트북](https://colab.research.google.com/github/deep-diver/image_search_with_natural_language/blob/main/notebooks/Image_Search_CLIP.ipynb). 🌎
190+
- Multilingual CLIP를 사용해서 이미지와 텍스트를 어떻게 같은 벡터 공간에 매핑 시키는지에 대한 [노트북](https://colab.research.google.com/drive/1xO-wC_m_GNzgjIBQ4a4znvQkvDoZJvH4?usp=sharing). 🌎
191+
- [Unsplash](https://unsplash.com)[TMDB](https://www.themoviedb.org/) 데이터셋을 활용한 의미론적(semantic) 이미지 검색에서 CLIP을 구동하는 방법에 대한 [노트북](https://colab.research.google.com/github/vivien000/clip-demo/blob/master/clip.ipynb#scrollTo=uzdFhRGqiWkR). 🌎
192+
193+
**설명 가능성**
194+
195+
- 입력 토큰과 이미지 조각(segment) 사이의 유사성을 시각화 시키는 방법에 대한 [노트북](https://colab.research.google.com/github/hila-chefer/Transformer-MM-Explainability/blob/main/CLIP_explainability.ipynb). 🌎
196+
197+
여기에 포함될 자료를 제출하고 싶으시다면 PR(Pull Request)를 열어주세요. 리뷰 해드리겠습니다! 자료는 기존 자료를 복제하는 대신 새로운 내용을 담고 있어야 합니다.
198+
199+
## CLIPConfig[[transformers.CLIPConfig]]
200+
201+
[[autodoc]] CLIPConfig
202+
- from_text_vision_configs
203+
204+
## CLIPTextConfig[[transformers.CLIPTextConfig]]
205+
206+
[[autodoc]] CLIPTextConfig
207+
208+
## CLIPVisionConfig[[transformers.CLIPVisionConfig]]
209+
210+
[[autodoc]] CLIPVisionConfig
211+
212+
## CLIPTokenizer[[transformers.CLIPTokenizer]]
213+
214+
[[autodoc]] CLIPTokenizer
215+
- build_inputs_with_special_tokens
216+
- get_special_tokens_mask
217+
- create_token_type_ids_from_sequences
218+
- save_vocabulary
219+
220+
## CLIPTokenizerFast[[transformers.CLIPTokenizerFast]]
221+
222+
[[autodoc]] CLIPTokenizerFast
223+
224+
## CLIPImageProcessor[[transformers.CLIPImageProcessor]]
225+
226+
[[autodoc]] CLIPImageProcessor
227+
- preprocess
228+
229+
## CLIPFeatureExtractor[[transformers.CLIPFeatureExtractor]]
230+
231+
[[autodoc]] CLIPFeatureExtractor
232+
233+
## CLIPProcessor[[transformers.CLIPProcessor]]
234+
235+
[[autodoc]] CLIPProcessor
236+
237+
<frameworkcontent>
238+
<pt>
239+
240+
## CLIPModel[[transformers.CLIPModel]]
241+
242+
[[autodoc]] CLIPModel
243+
- forward
244+
- get_text_features
245+
- get_image_features
246+
247+
## CLIPTextModel[[transformers.CLIPTextModel]]
248+
249+
[[autodoc]] CLIPTextModel
250+
- forward
251+
252+
## CLIPTextModelWithProjection[[transformers.CLIPTextModelWithProjection]]
253+
254+
[[autodoc]] CLIPTextModelWithProjection
255+
- forward
256+
257+
## CLIPVisionModelWithProjection[[transformers.CLIPVisionModelWithProjection]]
258+
259+
[[autodoc]] CLIPVisionModelWithProjection
260+
- forward
261+
262+
## CLIPVisionModel[[transformers.CLIPVisionModel]]
263+
264+
[[autodoc]] CLIPVisionModel
265+
- forward
266+
267+
## CLIPForImageClassification[[transformers.CLIPForImageClassification]]
268+
269+
[[autodoc]] CLIPForImageClassification
270+
- forward
271+
272+
</pt>
273+
<tf>
274+
275+
## TFCLIPModel[[transformers.TFCLIPModel]]
276+
277+
[[autodoc]] TFCLIPModel
278+
- call
279+
- get_text_features
280+
- get_image_features
281+
282+
## TFCLIPTextModel[[transformers.TFCLIPTextModel]]
283+
284+
[[autodoc]] TFCLIPTextModel
285+
- call
286+
287+
## TFCLIPVisionModel[[transformers.TFCLIPVisionModel]]
288+
289+
[[autodoc]] TFCLIPVisionModel
290+
- call
291+
292+
</tf>
293+
<jax>
294+
295+
## FlaxCLIPModel[[transformers.FlaxCLIPModel]]
296+
297+
[[autodoc]] FlaxCLIPModel
298+
- __call__
299+
- get_text_features
300+
- get_image_features
301+
302+
## FlaxCLIPTextModel[[transformers.FlaxCLIPTextModel]]
303+
304+
[[autodoc]] FlaxCLIPTextModel
305+
- __call__
306+
307+
## FlaxCLIPTextModelWithProjection[[transformers.FlaxCLIPTextModelWithProjection]]
308+
309+
[[autodoc]] FlaxCLIPTextModelWithProjection
310+
- __call__
311+
312+
## FlaxCLIPVisionModel[[transformers.FlaxCLIPVisionModel]]
313+
314+
[[autodoc]] FlaxCLIPVisionModel
315+
- __call__
316+
317+
</jax>
318+
</frameworkcontent>

0 commit comments

Comments
 (0)