File size: 6,615 Bytes
002bd9b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# ์ ๋ก์ท(zero-shot) ์ด๋ฏธ์ง ๋ถ๋ฅ[[zeroshot-image-classification]]
[[open-in-colab]]
์ ๋ก์ท(zero-shot) ์ด๋ฏธ์ง ๋ถ๋ฅ๋ ํน์ ์นดํ
๊ณ ๋ฆฌ์ ์์๊ฐ ํฌํจ๋ ๋ฐ์ดํฐ๋ฅผ ํ์ต๋์ง ์์ ๋ชจ๋ธ์ ์ฌ์ฉํด ์ด๋ฏธ์ง ๋ถ๋ฅ๋ฅผ ์ํํ๋ ์์
์
๋๋ค.
์ผ๋ฐ์ ์ผ๋ก ์ด๋ฏธ์ง ๋ถ๋ฅ๋ฅผ ์ํด์๋ ๋ ์ด๋ธ์ด ๋ฌ๋ฆฐ ํน์ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ก ๋ชจ๋ธ ํ์ต์ด ํ์ํ๋ฉฐ, ์ด ๋ชจ๋ธ์ ํน์ ์ด๋ฏธ์ง์ ํน์ง์ ๋ ์ด๋ธ์ "๋งคํ"ํ๋ ๋ฐฉ๋ฒ์ ํ์ตํฉ๋๋ค.
์๋ก์ด ๋ ์ด๋ธ์ด ์๋ ๋ถ๋ฅ ์์
์ ์ด๋ฌํ ๋ชจ๋ธ์ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ์๋, ๋ชจ๋ธ์ "์ฌ๋ณด์ "ํ๊ธฐ ์ํด ๋ฏธ์ธ ์กฐ์ ์ด ํ์ํฉ๋๋ค.
์ด์ ๋์กฐ์ ์ผ๋ก, ์ ๋ก์ท ๋๋ ๊ฐ๋ฐฉํ ์ดํ(open vocabulary) ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ์ ์ผ๋ฐ์ ์ผ๋ก ๋๊ท๋ชจ ์ด๋ฏธ์ง ๋ฐ์ดํฐ์ ํด๋น ์ค๋ช
์ ๋ํด ํ์ต๋ ๋ฉํฐ๋ชจ๋ฌ(multimodal) ๋ชจ๋ธ์
๋๋ค.
์ด๋ฌํ ๋ชจ๋ธ์ ์ ๋ก์ท ์ด๋ฏธ์ง ๋ถ๋ฅ๋ฅผ ํฌํจํ ๋ง์ ๋ค์ด์คํธ๋ฆผ ์์
์ ์ฌ์ฉํ ์ ์๋ ์ ๋ ฌ๋(aligned) ๋น์ ์ธ์ด ํํ์ ํ์ตํฉ๋๋ค.
์ด๋ ์ด๋ฏธ์ง ๋ถ๋ฅ์ ๋ํ ๋ณด๋ค ์ ์ฐํ ์ ๊ทผ ๋ฐฉ์์ผ๋ก, ์ถ๊ฐ ํ์ต ๋ฐ์ดํฐ ์์ด ์๋ก์ด ๋ ์ด๋ธ์ด๋ ํ์ตํ์ง ๋ชปํ ์นดํ
๊ณ ๋ฆฌ์ ๋ํด ๋ชจ๋ธ์ ์ผ๋ฐํํ ์ ์์ต๋๋ค.
๋ํ, ์ฌ์ฉ์๊ฐ ๋์ ๊ฐ์ฒด์ ๋ํ ์์ ํ์์ ํ
์คํธ ์ค๋ช
์ผ๋ก ์ด๋ฏธ์ง๋ฅผ ๊ฒ์ํ ์ ์์ต๋๋ค.
์ด๋ฒ ๊ฐ์ด๋์์ ๋ฐฐ์ธ ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
* ์ ๋ก์ท ์ด๋ฏธ์ง ๋ถ๋ฅ ํ์ดํ๋ผ์ธ ๋ง๋ค๊ธฐ
* ์ง์ ์ ๋ก์ท ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ ์ถ๋ก ์คํํ๊ธฐ
์์ํ๊ธฐ ์ ์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ชจ๋ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์:
```bash
pip install -q transformers
```
## ์ ๋ก์ท(zero-shot) ์ด๋ฏธ์ง ๋ถ๋ฅ ํ์ดํ๋ผ์ธ[[zeroshot-image-classification-pipeline]]
[`pipeline`]์ ํ์ฉํ๋ฉด ๊ฐ์ฅ ๊ฐ๋จํ๊ฒ ์ ๋ก์ท ์ด๋ฏธ์ง ๋ถ๋ฅ๋ฅผ ์ง์ํ๋ ๋ชจ๋ธ๋ก ์ถ๋ก ํด๋ณผ ์ ์์ต๋๋ค.
[Hugging Face Hub์ ์
๋ก๋๋ ์ฒดํฌํฌ์ธํธ](https://huggingface.co/models?pipeline_tag=zero-shot-image-classification&sort=downloads)์์ ํ์ดํ๋ผ์ธ์ ์ธ์คํด์คํํฉ๋๋ค.
```python
>>> from transformers import pipeline
>>> checkpoint = "openai/clip-vit-large-patch14"
>>> detector = pipeline(model=checkpoint, task="zero-shot-image-classification")
```
๋ค์์ผ๋ก, ๋ถ๋ฅํ๊ณ ์ถ์ ์ด๋ฏธ์ง๋ฅผ ์ ํํ์ธ์.
```py
>>> from PIL import Image
>>> import requests
>>> url = "https://unsplash.com/photos/g8oS8-82DxI/download?ixid=MnwxMjA3fDB8MXx0b3BpY3x8SnBnNktpZGwtSGt8fHx8fDJ8fDE2NzgxMDYwODc&force=true&w=640"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> image
```
<div class="flex justify-center">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/owl.jpg" alt="Photo of an owl"/>
</div>
์ด๋ฏธ์ง์ ํด๋น ์ด๋ฏธ์ง์ ํ๋ณด ๋ ์ด๋ธ์ธ `candidate_labels`๋ฅผ ํ์ดํ๋ผ์ธ์ผ๋ก ์ ๋ฌํฉ๋๋ค.
์ฌ๊ธฐ์๋ ์ด๋ฏธ์ง๋ฅผ ์ง์ ์ ๋ฌํ์ง๋ง, ์ปดํจํฐ์ ์ ์ฅ๋ ์ด๋ฏธ์ง์ ๊ฒฝ๋ก๋ url๋ก ์ ๋ฌํ ์๋ ์์ต๋๋ค.
`candidate_labels`๋ ์ด ์์์ฒ๋ผ ๊ฐ๋จํ ๋จ์ด์ผ ์๋ ์๊ณ ์ข ๋ ์ค๋ช
์ ์ธ ๋จ์ด์ผ ์๋ ์์ต๋๋ค.
```py
>>> predictions = classifier(image, candidate_labels=["fox", "bear", "seagull", "owl"])
>>> predictions
[{'score': 0.9996670484542847, 'label': 'owl'},
{'score': 0.000199399160919711, 'label': 'seagull'},
{'score': 7.392891711788252e-05, 'label': 'fox'},
{'score': 5.96074532950297e-05, 'label': 'bear'}]
```
## ์ง์ ์ ๋ก์ท(zero-shot) ์ด๋ฏธ์ง ๋ถ๋ฅํ๊ธฐ[[zeroshot-image-classification-by-hand]]
์ด์ ์ ๋ก์ท ์ด๋ฏธ์ง ๋ถ๋ฅ ํ์ดํ๋ผ์ธ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์ดํด๋ณด์์ผ๋, ์คํํ๋ ๋ฐฉ๋ฒ์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
[Hugging Face Hub์ ์
๋ก๋๋ ์ฒดํฌํฌ์ธํธ](https://huggingface.co/models?pipeline_tag=zero-shot-image-classification&sort=downloads)์์ ๋ชจ๋ธ๊ณผ ํ๋ก์ธ์๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ผ๋ก ์์ํฉ๋๋ค.
์ฌ๊ธฐ์๋ ์ด์ ๊ณผ ๋์ผํ ์ฒดํฌํฌ์ธํธ๋ฅผ ์ฌ์ฉํ๊ฒ ์ต๋๋ค:
```py
>>> from transformers import AutoProcessor, AutoModelForZeroShotImageClassification
>>> model = AutoModelForZeroShotImageClassification.from_pretrained(checkpoint)
>>> processor = AutoProcessor.from_pretrained(checkpoint)
```
๋ค๋ฅธ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํด ๋ณด๊ฒ ์ต๋๋ค.
```py
>>> from PIL import Image
>>> import requests
>>> url = "https://unsplash.com/photos/xBRQfR2bqNI/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjc4Mzg4ODEx&force=true&w=640"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> image
```
<div class="flex justify-center">
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg" alt="Photo of a car"/>
</div>
ํ๋ก์ธ์๋ฅผ ์ฌ์ฉํด ๋ชจ๋ธ์ ์
๋ ฅ์ ์ค๋นํฉ๋๋ค.
ํ๋ก์ธ์๋ ๋ชจ๋ธ์ ์
๋ ฅ์ผ๋ก ์ฌ์ฉํ๊ธฐ ์ํด ์ด๋ฏธ์ง ํฌ๊ธฐ๋ฅผ ๋ณํํ๊ณ ์ ๊ทํํ๋ ์ด๋ฏธ์ง ํ๋ก์ธ์์ ํ
์คํธ ์
๋ ฅ์ ์ฒ๋ฆฌํ๋ ํ ํฌ๋์ด์ ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.
```py
>>> candidate_labels = ["tree", "car", "bike", "cat"]
>>> inputs = processor(images=image, text=candidate_labels, return_tensors="pt", padding=True)
```
๋ชจ๋ธ์ ์
๋ ฅ์ ์ ๋ฌํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ ํ์ฒ๋ฆฌํฉ๋๋ค:
```py
>>> import torch
>>> with torch.no_grad():
... outputs = model(**inputs)
>>> logits = outputs.logits_per_image[0]
>>> probs = logits.softmax(dim=-1).numpy()
>>> scores = probs.tolist()
>>> result = [
... {"score": score, "label": candidate_label}
... for score, candidate_label in sorted(zip(probs, candidate_labels), key=lambda x: -x[0])
... ]
>>> result
[{'score': 0.998572, 'label': 'car'},
{'score': 0.0010570387, 'label': 'bike'},
{'score': 0.0003393686, 'label': 'tree'},
{'score': 3.1572064e-05, 'label': 'cat'}]
``` |