File size: 22,915 Bytes
17c6d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<!--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.

-->

# IDEFICSλ₯Ό μ΄μš©ν•œ 이미지 μž‘μ—…[[image-tasks-with-idefics]]

[[open-in-colab]]

κ°œλ³„ μž‘μ—…μ€ νŠΉν™”λœ λͺ¨λΈμ„ λ―Έμ„Έ μ‘°μ •ν•˜μ—¬ μ²˜λ¦¬ν•  수 μžˆμ§€λ§Œ, 졜근 λ“±μž₯ν•˜μ—¬ 인기λ₯Ό μ–»κ³  μžˆλŠ” 방식은 λŒ€κ·œλͺ¨ λͺ¨λΈμ„ λ―Έμ„Έ μ‘°μ • 없이 λ‹€μ–‘ν•œ μž‘μ—…μ— μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, λŒ€κ·œλͺ¨ μ–Έμ–΄ λͺ¨λΈμ€ μš”μ•½, λ²ˆμ—­, λΆ„λ₯˜ λ“±κ³Ό 같은 μžμ—°μ–΄μ²˜λ¦¬ (NLP) μž‘μ—…μ„ μ²˜λ¦¬ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이 μ ‘κ·Ό 방식은 ν…μŠ€νŠΈμ™€ 같은 단일 λͺ¨λ‹¬λ¦¬ν‹°μ— κ΅­ν•œλ˜μ§€ μ•ŠμœΌλ©°, 이 κ°€μ΄λ“œμ—μ„œλŠ” IDEFICSλΌλŠ” λŒ€κ·œλͺ¨ λ©€ν‹°λͺ¨λ‹¬ λͺ¨λΈμ„ μ‚¬μš©ν•˜μ—¬ 이미지-ν…μŠ€νŠΈ μž‘μ—…μ„ λ‹€λ£¨λŠ” 방법을 μ„€λͺ…ν•©λ‹ˆλ‹€.

[IDEFICS](../model_doc/idefics)λŠ” [Flamingo](https://huggingface.co/papers/2204.14198)λ₯Ό 기반으둜 ν•˜λŠ” μ˜€ν”ˆ μ•‘μ„ΈμŠ€ λΉ„μ „ 및 μ–Έμ–΄ λͺ¨λΈλ‘œ, DeepMindμ—μ„œ 처음 κ°œλ°œν•œ μ΅œμ‹  μ‹œκ° μ–Έμ–΄ λͺ¨λΈμž…λ‹ˆλ‹€. 이 λͺ¨λΈμ€ μž„μ˜μ˜ 이미지 및 ν…μŠ€νŠΈ μž…λ ₯ μ‹œν€€μŠ€λ₯Ό λ°›μ•„ 일관성 μžˆλŠ” ν…μŠ€νŠΈλ₯Ό 좜λ ₯으둜 μƒμ„±ν•©λ‹ˆλ‹€. 이미지에 λŒ€ν•œ μ§ˆλ¬Έμ— λ‹΅λ³€ν•˜κ³ , μ‹œκ°μ μΈ λ‚΄μš©μ„ μ„€λͺ…ν•˜λ©°, μ—¬λŸ¬ 이미지에 κΈ°λ°˜ν•œ 이야기λ₯Ό μƒμ„±ν•˜λŠ” λ“± λ‹€μ–‘ν•œ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€. IDEFICSλŠ” [800μ–΅ νŒŒλΌλ―Έν„°](https://huggingface.co/HuggingFaceM4/idefics-80b)와 [90μ–΅ νŒŒλΌλ―Έν„°](https://huggingface.co/HuggingFaceM4/idefics-9b) 두 κ°€μ§€ 버전을 μ œκ³΅ν•˜λ©°, 두 버전 λͺ¨λ‘ πŸ€— Hubμ—μ„œ μ΄μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 각 λ²„μ „μ—λŠ” λŒ€ν™”ν˜• μ‚¬μš© 사둀에 맞게 λ―Έμ„Έ μ‘°μ •λœ 버전도 μžˆμŠ΅λ‹ˆλ‹€.

이 λͺ¨λΈμ€ 맀우 λ‹€μž¬λ‹€λŠ₯ν•˜λ©° κ΄‘λ²”μœ„ν•œ 이미지 및 λ©€ν‹°λͺ¨λ‹¬ μž‘μ—…μ— μ‚¬μš©λ  수 μžˆμŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ λŒ€κ·œλͺ¨ λͺ¨λΈμ΄κΈ° λ•Œλ¬Έμ— μƒλ‹Ήν•œ μ»΄ν“¨νŒ… μžμ›κ³Ό 인프라가 ν•„μš”ν•©λ‹ˆλ‹€. 각 κ°œλ³„ μž‘μ—…μ— νŠΉν™”λœ λͺ¨λΈμ„ λ―Έμ„Έ μ‘°μ •ν•˜λŠ” 것보닀 λͺ¨λΈμ„ κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜λŠ” 것이 더 μ ν•©ν•œμ§€λŠ” μ‚¬μš©μžκ°€ νŒλ‹¨ν•΄μ•Ό ν•©λ‹ˆλ‹€.

이 κ°€μ΄λ“œμ—μ„œλŠ” λ‹€μŒμ„ 배우게 λ©λ‹ˆλ‹€:
- [IDEFICS λ‘œλ“œν•˜κΈ°](#loading-the-model) 및 [μ–‘μžν™”λœ λ²„μ „μ˜ λͺ¨λΈ λ‘œλ“œν•˜κΈ°](#quantized-model)
- IDEFICSλ₯Ό μ‚¬μš©ν•˜μ—¬:
  - [이미지 캑셔닝](#image-captioning)
  - [ν”„λ‘¬ν”„νŠΈ 이미지 캑셔닝](#prompted-image-captioning)
  - [퓨샷 ν”„λ‘¬ν”„νŠΈ](#few-shot-prompting)
  - [μ‹œκ°μ  질의 응닡](#visual-question-answering)
  - [이미지 λΆ„λ₯˜](#image-classification)
  - [이미지 기반 ν…μŠ€νŠΈ 생성](#image-guided-text-generation)
- [배치 λͺ¨λ“œμ—μ„œ μΆ”λ‘  μ‹€ν–‰](#running-inference-in-batch-mode)
- [λŒ€ν™”ν˜• μ‚¬μš©μ„ μœ„ν•œ IDEFICS 인슀트럭트 μ‹€ν–‰](#idefics-instruct-for-conversational-use)

μ‹œμž‘ν•˜κΈ° 전에 ν•„μš”ν•œ λͺ¨λ“  λΌμ΄λΈŒλŸ¬λ¦¬κ°€ μ„€μΉ˜λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•˜μ„Έμš”.

```bash
pip install -q bitsandbytes sentencepiece accelerate transformers
```

<Tip>
λ‹€μŒ 예제λ₯Ό λΉ„μ–‘μžν™”λœ λ²„μ „μ˜ λͺ¨λΈ 체크포인트둜 μ‹€ν–‰ν•˜λ €λ©΄ μ΅œμ†Œ 20GB의 GPU λ©”λͺ¨λ¦¬κ°€ ν•„μš”ν•©λ‹ˆλ‹€.
</Tip>

## λͺ¨λΈ λ‘œλ“œ[[loading-the-model]]

λͺ¨λΈμ„ 90μ–΅ νŒŒλΌλ―Έν„° λ²„μ „μ˜ 체크포인트둜 λ‘œλ“œν•΄ λ΄…μ‹œλ‹€:

```py
>>> checkpoint = "HuggingFaceM4/idefics-9b"
```

λ‹€λ₯Έ Transformers λͺ¨λΈκ³Ό λ§ˆμ°¬κ°€μ§€λ‘œ, μ²΄ν¬ν¬μΈνŠΈμ—μ„œ ν”„λ‘œμ„Έμ„œμ™€ λͺ¨λΈ 자체λ₯Ό λ‘œλ“œν•΄μ•Ό ν•©λ‹ˆλ‹€.
IDEFICS ν”„λ‘œμ„Έμ„œλŠ” [`LlamaTokenizer`]와 IDEFICS 이미지 ν”„λ‘œμ„Έμ„œλ₯Ό ν•˜λ‚˜μ˜ ν”„λ‘œμ„Έμ„œλ‘œ κ°μ‹Έμ„œ ν…μŠ€νŠΈμ™€ 이미지 μž…λ ₯을 λͺ¨λΈμ— 맞게 μ€€λΉ„ν•©λ‹ˆλ‹€.

```py
>>> import torch

>>> from transformers import IdeficsForVisionText2Text, AutoProcessor

>>> processor = AutoProcessor.from_pretrained(checkpoint)

>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16, device_map="auto")
```

`device_map`을 `"auto"`둜 μ„€μ •ν•˜λ©΄ μ‚¬μš© 쀑인 μž₯치λ₯Ό κ³ λ €ν•˜μ—¬ λͺ¨λΈ κ°€μ€‘μΉ˜λ₯Ό κ°€μž₯ μ΅œμ ν™”λœ λ°©μ‹μœΌλ‘œ λ‘œλ“œν•˜κ³  μ €μž₯ν•˜λŠ” 방법을 μžλ™μœΌλ‘œ κ²°μ •ν•©λ‹ˆλ‹€.

### μ–‘μžν™”λœ λͺ¨λΈ[[quantized-model]]

κ³ μš©λŸ‰ GPU μ‚¬μš©μ΄ μ–΄λ €μš΄ 경우, λͺ¨λΈμ˜ μ–‘μžν™”λœ 버전을 λ‘œλ“œν•  수 μžˆμŠ΅λ‹ˆλ‹€. λͺ¨λΈκ³Ό ν”„λ‘œμ„Έμ„œλ₯Ό 4λΉ„νŠΈ μ •λ°€λ„λ‘œ λ‘œλ“œν•˜κΈ° μœ„ν•΄μ„œ, `from_pretrained` λ©”μ†Œλ“œμ— `BitsAndBytesConfig`λ₯Ό μ „λ‹¬ν•˜λ©΄ λͺ¨λΈμ΄ λ‘œλ“œλ˜λŠ” λ™μ•ˆ μ‹€μ‹œκ°„μœΌλ‘œ μ••μΆ•λ©λ‹ˆλ‹€.

```py
>>> import torch
>>> from transformers import IdeficsForVisionText2Text, AutoProcessor, BitsAndBytesConfig

>>> quantization_config = BitsAndBytesConfig(
...     load_in_4bit=True,
...     bnb_4bit_compute_dtype=torch.float16,
... )

>>> processor = AutoProcessor.from_pretrained(checkpoint)

>>> model = IdeficsForVisionText2Text.from_pretrained(
...     checkpoint,
...     quantization_config=quantization_config,
...     device_map="auto"
... )
```

이제 λͺ¨λΈμ„ μ œμ•ˆλœ 방법 쀑 ν•˜λ‚˜λ‘œ λ‘œλ“œν–ˆμœΌλ‹ˆ, IDEFICSλ₯Ό μ‚¬μš©ν•  수 μžˆλŠ” μž‘μ—…λ“€μ„ νƒκ΅¬ν•΄λ΄…μ‹œλ‹€.

## 이미지 캑셔닝[[image-captioning]]
이미지 캑셔닝은 μ£Όμ–΄μ§„ 이미지에 λŒ€ν•œ μΊ‘μ…˜μ„ μ˜ˆμΈ‘ν•˜λŠ” μž‘μ—…μž…λ‹ˆλ‹€. 일반적인 μ‘μš© λΆ„μ•ΌλŠ” μ‹œκ° μž₯애인이 λ‹€μ–‘ν•œ 상황을 탐색할 수 μžˆλ„λ‘ λ•λŠ” κ²ƒμž…λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, μ˜¨λΌμΈμ—μ„œ 이미지 μ½˜ν…μΈ λ₯Ό νƒμƒ‰ν•˜λŠ” 데 도움을 쀄 수 μžˆμŠ΅λ‹ˆλ‹€.

μž‘μ—…μ„ μ„€λͺ…ν•˜κΈ° μœ„ν•΄ μΊ‘μ…˜μ„ 달 이미지 μ˜ˆμ‹œλ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€. μ˜ˆμ‹œ:

<div class="flex justify-center">
     <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-im-captioning.jpg" alt="Image of a puppy in a flower bed"/>
</div>

사진 제곡: [Hendo Wang](https://unsplash.com/@hendoo).

IDEFICSλŠ” ν…μŠ€νŠΈ 및 이미지 ν”„λ‘¬ν”„νŠΈλ₯Ό λͺ¨λ‘ μˆ˜μš©ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 이미지λ₯Ό μΊ‘μ…˜ν•˜κΈ° μœ„ν•΄ λͺ¨λΈμ— ν…μŠ€νŠΈ ν”„λ‘¬ν”„νŠΈλ₯Ό μ œκ³΅ν•  ν•„μš”λŠ” μ—†μŠ΅λ‹ˆλ‹€. μ „μ²˜λ¦¬λœ μž…λ ₯ μ΄λ―Έμ§€λ§Œ μ œκ³΅ν•˜λ©΄ λ©λ‹ˆλ‹€. ν…μŠ€νŠΈ ν”„λ‘¬ν”„νŠΈ 없이 λͺ¨λΈμ€ BOS(μ‹œν€€μŠ€ μ‹œμž‘) 토큰뢀터 ν…μŠ€νŠΈ 생성을 μ‹œμž‘ν•˜μ—¬ μΊ‘μ…˜μ„ λ§Œλ“­λ‹ˆλ‹€.

λͺ¨λΈμ— 이미지 μž…λ ₯μœΌλ‘œλŠ” 이미지 객체(`PIL.Image`) λ˜λŠ” 이미지λ₯Ό κ°€μ Έμ˜¬ 수 μžˆλŠ” URL을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

```py
>>> prompt = [
...     "https://images.unsplash.com/photo-1583160247711-2191776b4b91?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3542&q=80",
... ]

>>> inputs = processor(prompt, return_tensors="pt").to("cuda")
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> print(generated_text[0])
A puppy in a flower bed
```

<Tip>

`max_new_tokens`의 크기λ₯Ό μ¦κ°€μ‹œν‚¬ λ•Œ λ°œμƒν•  수 μžˆλŠ” 였λ₯˜λ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ `generate` 호좜 μ‹œ `bad_words_ids`λ₯Ό ν¬ν•¨ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. λͺ¨λΈλ‘œλΆ€ν„° μƒμ„±λœ 이미지가 없을 λ•Œ μƒˆλ‘œμš΄ `<image>` λ˜λŠ” `<fake_token_around_image>` 토큰을 μƒμ„±ν•˜λ €κ³  ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
이 κ°€μ΄λ“œμ—μ„œμ²˜λŸΌ `bad_words_ids`λ₯Ό ν•¨μˆ˜ 호좜 μ‹œμ— λ§€κ°œλ³€μˆ˜λ‘œ μ„€μ •ν•˜κ±°λ‚˜, [ν…μŠ€νŠΈ 생성 μ „λž΅](../generation_strategies) κ°€μ΄λ“œμ— μ„€λͺ…λœ λŒ€λ‘œ `GenerationConfig`에 μ €μž₯ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.
</Tip>

## ν”„λ‘¬ν”„νŠΈ 이미지 캑셔닝[[prompted-image-captioning]]

ν…μŠ€νŠΈ ν”„λ‘¬ν”„νŠΈλ₯Ό μ΄μš©ν•˜μ—¬ 이미지 캑셔닝을 ν™•μž₯ν•  수 있으며, λͺ¨λΈμ€ μ£Όμ–΄μ§„ 이미지λ₯Ό λ°”νƒ•μœΌλ‘œ ν…μŠ€νŠΈλ₯Ό 계속 μƒμ„±ν•©λ‹ˆλ‹€. λ‹€μŒ 이미지λ₯Ό μ˜ˆμ‹œλ‘œ λ“€μ–΄λ³΄κ² μŠ΅λ‹ˆλ‹€:

<div class="flex justify-center">
     <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-prompted-im-captioning.jpg" alt="Image of the Eiffel Tower at night"/>
</div>

사진 제곡: [Denys Nevozhai](https://unsplash.com/@dnevozhai).

ν…μŠ€νŠΈ 및 이미지 ν”„λ‘¬ν”„νŠΈλŠ” μ μ ˆν•œ μž…λ ₯을 μƒμ„±ν•˜κΈ° μœ„ν•΄ λͺ¨λΈμ˜ ν”„λ‘œμ„Έμ„œμ— ν•˜λ‚˜μ˜ λͺ©λ‘μœΌλ‘œ 전달될 수 μžˆμŠ΅λ‹ˆλ‹€.

```py
>>> prompt = [
...     "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80",
...     "This is an image of ",
... ]

>>> inputs = processor(prompt, return_tensors="pt").to("cuda")
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> print(generated_text[0])
This is an image of the Eiffel Tower in Paris, France.
```

## 퓨샷 ν”„λ‘¬ν”„νŠΈ[[few-shot-prompting]]

IDEFICSλŠ” ν›Œλ₯­ν•œ μ œλ‘œμƒ· κ²°κ³Όλ₯Ό λ³΄μ—¬μ£Όμ§€λ§Œ, μž‘μ—…μ— νŠΉμ • ν˜•μ‹μ˜ μΊ‘μ…˜μ΄ ν•„μš”ν•˜κ±°λ‚˜ μž‘μ—…μ˜ λ³΅μž‘μ„±μ„ λ†’μ΄λŠ” λ‹€λ₯Έ μ œν•œ μ‚¬ν•­μ΄λ‚˜ μš”κ΅¬ 사항이 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€. 이럴 λ•Œ 퓨샷 ν”„λ‘¬ν”„νŠΈλ₯Ό μ‚¬μš©ν•˜μ—¬ λ§₯락 λ‚΄ ν•™μŠ΅(In-Context Learning)을 κ°€λŠ₯ν•˜κ²Œ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
ν”„λ‘¬ν”„νŠΈμ— μ˜ˆμ‹œλ₯Ό μ œκ³΅ν•¨μœΌλ‘œμ¨ λͺ¨λΈμ΄ μ£Όμ–΄μ§„ μ˜ˆμ‹œμ˜ ν˜•μ‹μ„ λͺ¨λ°©ν•œ κ²°κ³Όλ₯Ό μƒμ„±ν•˜λ„λ‘ μœ λ„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ΄μ „μ˜ μ—νŽ νƒ‘ 이미지λ₯Ό λͺ¨λΈμ— μ˜ˆμ‹œλ‘œ μ‚¬μš©ν•˜κ³ , λͺ¨λΈμ—κ²Œ μ΄λ―Έμ§€μ˜ 객체λ₯Ό ν•™μŠ΅ν•˜λŠ” 것 외에도 ν₯미둜운 정보λ₯Ό μ–»κ³  μ‹Άλ‹€λŠ” 것을 λ³΄μ—¬μ£ΌλŠ” ν”„λ‘¬ν”„νŠΈλ₯Ό μž‘μ„±ν•΄ λ΄…μ‹œλ‹€.
그런 λ‹€μŒ 자유의 여신상 이미지에 λŒ€ν•΄ λ™μΌν•œ 응닡 ν˜•μ‹μ„ 얻을 수 μžˆλŠ”μ§€ 확인해 λ΄…μ‹œλ‹€:

<div class="flex justify-center">
     <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-few-shot.jpg" alt="Image of the Statue of Liberty"/>
</div>

사진 제곡: [Juan Mayobre](https://unsplash.com/@jmayobres).
  
```py
>>> prompt = ["User:",
...            "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80",
...            "Describe this image.\nAssistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building.\n",
...            "User:",
...            "https://images.unsplash.com/photo-1524099163253-32b7f0256868?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3387&q=80",
...            "Describe this image.\nAssistant:"
...            ]

>>> inputs = processor(prompt, return_tensors="pt").to("cuda")
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, max_new_tokens=30, bad_words_ids=bad_words_ids)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> print(generated_text[0])
User: Describe this image.
Assistant: An image of the Eiffel Tower at night. Fun fact: the Eiffel Tower is the same height as an 81-storey building. 
User: Describe this image.
Assistant: An image of the Statue of Liberty. Fun fact: the Statue of Liberty is 151 feet tall.
```

단 ν•˜λ‚˜μ˜ μ˜ˆμ‹œλ§ŒμœΌλ‘œλ„(즉, 1-shot) λͺ¨λΈμ΄ μž‘μ—… μˆ˜ν–‰ 방법을 ν•™μŠ΅ν–ˆλ‹€λŠ” 점이 μ£Όλͺ©ν•  λ§Œν•©λ‹ˆλ‹€. 더 λ³΅μž‘ν•œ μž‘μ—…μ˜ 경우, 더 λ§Žμ€ μ˜ˆμ‹œ(예: 3-shot, 5-shot λ“±)λ₯Ό μ‚¬μš©ν•˜μ—¬ μ‹€ν—˜ν•΄ λ³΄λŠ” 것도 쒋은 λ°©λ²•μž…λ‹ˆλ‹€. 

## μ‹œκ°μ  질의 응닡[[visual-question-answering]]

μ‹œκ°μ  질의 응닡(VQA)은 이미지λ₯Ό 기반으둜 κ°œλ°©ν˜• μ§ˆλ¬Έμ— λ‹΅ν•˜λŠ” μž‘μ—…μž…λ‹ˆλ‹€. 이미지 캑셔닝과 λ§ˆμ°¬κ°€μ§€λ‘œ μ ‘κ·Όμ„± μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ‚¬μš©ν•  수 μžˆμ§€λ§Œ, ꡐ윑(μ‹œκ° μžλ£Œμ— λŒ€ν•œ μΆ”λ‘ ), 고객 μ„œλΉ„μŠ€(이미지λ₯Ό 기반으둜 ν•œ μ œν’ˆ 질문), 이미지 검색 λ“±μ—μ„œλ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

이 μž‘μ—…μ„ μœ„ν•΄ μƒˆλ‘œμš΄ 이미지λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€:

<div class="flex justify-center">
     <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-vqa.jpg" alt="Image of a couple having a picnic"/>
</div>

사진 제곡: [Jarritos Mexican Soda](https://unsplash.com/@jarritos).

μ μ ˆν•œ μ§€μ‹œλ¬Έμ„ μ‚¬μš©ν•˜λ©΄ 이미지 μΊ‘μ…”λ‹μ—μ„œ μ‹œκ°μ  질의 μ‘λ‹΅μœΌλ‘œ λͺ¨λΈμ„ μœ λ„ν•  수 μžˆμŠ΅λ‹ˆλ‹€:

```py
>>> prompt = [
...     "Instruction: Provide an answer to the question. Use the image to answer.\n",
...     "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80",
...     "Question: Where are these people and what's the weather like? Answer:"
... ]

>>> inputs = processor(prompt, return_tensors="pt").to("cuda")
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, max_new_tokens=20, bad_words_ids=bad_words_ids)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> print(generated_text[0])
Instruction: Provide an answer to the question. Use the image to answer.
 Question: Where are these people and what's the weather like? Answer: They're in a park in New York City, and it's a beautiful day.
```

## 이미지 λΆ„λ₯˜[[image-classification]]

IDEFICSλŠ” νŠΉμ • μΉ΄ν…Œκ³ λ¦¬μ˜ 라벨이 ν¬ν•¨λœ λ°μ΄ν„°λ‘œ λͺ…μ‹œμ μœΌλ‘œ ν•™μŠ΅λ˜μ§€ μ•Šμ•„λ„ 이미지λ₯Ό λ‹€μ–‘ν•œ μΉ΄ν…Œκ³ λ¦¬λ‘œ λΆ„λ₯˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μΉ΄ν…Œκ³ λ¦¬ λͺ©λ‘μ΄ μ£Όμ–΄μ§€λ©΄, λͺ¨λΈμ€ 이미지와 ν…μŠ€νŠΈ 이해 λŠ₯λ ₯을 μ‚¬μš©ν•˜μ—¬ 이미지가 속할 κ°€λŠ₯성이 높은 μΉ΄ν…Œκ³ λ¦¬λ₯Ό μΆ”λ‘ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

여기에 야채 κ°€νŒλŒ€ 이미지가 μžˆμŠ΅λ‹ˆλ‹€.

<div class="flex justify-center">
     <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-classification.jpg" alt="Image of a vegetable stand"/>
</div>

사진 제곡: [Peter Wendt](https://unsplash.com/@peterwendt).

μš°λ¦¬λŠ” λͺ¨λΈμ—κ²Œ μš°λ¦¬κ°€ κ°€μ§„ μΉ΄ν…Œκ³ λ¦¬ 쀑 ν•˜λ‚˜λ‘œ 이미지λ₯Ό λΆ„λ₯˜ν•˜λ„둝 μ§€μ‹œν•  수 μžˆμŠ΅λ‹ˆλ‹€:

```py
>>> categories = ['animals','vegetables', 'city landscape', 'cars', 'office']
>>> prompt = [f"Instruction: Classify the following image into a single category from the following list: {categories}.\n",
...     "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80",    
...     "Category: "
... ]

>>> inputs = processor(prompt, return_tensors="pt").to("cuda")
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, max_new_tokens=6, bad_words_ids=bad_words_ids)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> print(generated_text[0])
Instruction: Classify the following image into a single category from the following list: ['animals', 'vegetables', 'city landscape', 'cars', 'office'].
Category: Vegetables
```  

μœ„ μ˜ˆμ œμ—μ„œλŠ” λͺ¨λΈμ—κ²Œ 이미지λ₯Ό 단일 μΉ΄ν…Œκ³ λ¦¬λ‘œ λΆ„λ₯˜ν•˜λ„둝 μ§€μ‹œν–ˆμ§€λ§Œ, μˆœμœ„ λΆ„λ₯˜λ₯Ό ν•˜λ„λ‘ λͺ¨λΈμ— ν”„λ‘¬ν”„νŠΈλ₯Ό μ œκ³΅ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

## 이미지 기반 ν…μŠ€νŠΈ 생성[[image-guided-text-generation]]

이미지λ₯Ό ν™œμš©ν•œ ν…μŠ€νŠΈ 생성 κΈ°μˆ μ„ μ‚¬μš©ν•˜λ©΄ λ”μš± 창의적인 μž‘μ—…μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. 이 κΈ°μˆ μ€ 이미지λ₯Ό λ°”νƒ•μœΌλ‘œ ν…μŠ€νŠΈλ₯Ό λ§Œλ“€μ–΄λ‚΄λ©°, μ œν’ˆ μ„€λͺ…, κ΄‘κ³  문ꡬ, μž₯λ©΄ λ¬˜μ‚¬ λ“± λ‹€μ–‘ν•œ μš©λ„λ‘œ ν™œμš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

κ°„λ‹¨ν•œ 예둜, λΉ¨κ°„ λ¬Έ 이미지λ₯Ό IDEFICS에 μž…λ ₯ν•˜μ—¬ 이야기λ₯Ό λ§Œλ“€μ–΄λ³΄κ² μŠ΅λ‹ˆλ‹€:

<div class="flex justify-center">
     <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-story-generation.jpg" alt="Image of a red door with a pumpkin on the steps"/>
</div>

사진 제곡: [Craig Tidball](https://unsplash.com/@devonshiremedia).
  
```py
>>> prompt = ["Instruction: Use the image to write a story. \n",
...     "https://images.unsplash.com/photo-1517086822157-2b0358e7684a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=2203&q=80",
...     "Story: \n"]

>>> inputs = processor(prompt, return_tensors="pt").to("cuda")
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, num_beams=2, max_new_tokens=200, bad_words_ids=bad_words_ids)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> print(generated_text[0]) 
Instruction: Use the image to write a story. 
 Story: 
Once upon a time, there was a little girl who lived in a house with a red door.  She loved her red door.  It was the prettiest door in the whole world.

One day, the little girl was playing in her yard when she noticed a man standing on her doorstep.  He was wearing a long black coat and a top hat.

The little girl ran inside and told her mother about the man.

Her mother said, β€œDon’t worry, honey.  He’s just a friendly ghost.”

The little girl wasn’t sure if she believed her mother, but she went outside anyway.

When she got to the door, the man was gone.

The next day, the little girl was playing in her yard again when she noticed the man standing on her doorstep.

He was wearing a long black coat and a top hat.

The little girl ran
```

IDEFICSκ°€ λ¬Έ μ•žμ— μžˆλŠ” ν˜Έλ°•μ„ 보고 μœ λ Ήμ— λŒ€ν•œ μœΌμŠ€μŠ€ν•œ ν• λ‘œμœˆ 이야기λ₯Ό λ§Œλ“  것 κ°™μŠ΅λ‹ˆλ‹€.

<Tip>

이처럼 κΈ΄ ν…μŠ€νŠΈλ₯Ό 생성할 λ•ŒλŠ” ν…μŠ€νŠΈ 생성 μ „λž΅μ„ μ‘°μ •ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ μƒμ„±λœ 결과물의 ν’ˆμ§ˆμ„ 크게 ν–₯μƒμ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ [ν…μŠ€νŠΈ 생성 μ „λž΅](../generation_strategies)을 μ°Έμ‘°ν•˜μ„Έμš”.
</Tip>

## 배치 λͺ¨λ“œμ—μ„œ μΆ”λ‘  μ‹€ν–‰[[running-inference-in-batch-mode]]

μ•žμ„  λͺ¨λ“  μ„Ήμ…˜μ—μ„œλŠ” 단일 μ˜ˆμ‹œμ— λŒ€ν•΄ IDEFICSλ₯Ό μ„€λͺ…ν–ˆμŠ΅λ‹ˆλ‹€. 이와 맀우 μœ μ‚¬ν•œ λ°©μ‹μœΌλ‘œ, ν”„λ‘¬ν”„νŠΈ λͺ©λ‘μ„ μ „λ‹¬ν•˜μ—¬ μ—¬λŸ¬ μ˜ˆμ‹œμ— λŒ€ν•œ 좔둠을 μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€:

```py
>>> prompts = [
...     [   "https://images.unsplash.com/photo-1543349689-9a4d426bee8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3501&q=80",
...         "This is an image of ",
...     ],
...     [   "https://images.unsplash.com/photo-1623944889288-cd147dbb517c?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80",
...         "This is an image of ",
...     ],
...     [   "https://images.unsplash.com/photo-1471193945509-9ad0617afabf?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=3540&q=80",
...         "This is an image of ",
...     ],
... ]

>>> inputs = processor(prompts, return_tensors="pt").to("cuda")
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, max_new_tokens=10, bad_words_ids=bad_words_ids)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> for i,t in enumerate(generated_text):
...     print(f"{i}:\n{t}\n") 
0:
This is an image of the Eiffel Tower in Paris, France.

1:
This is an image of a couple on a picnic blanket.

2:
This is an image of a vegetable stand.
```

## λŒ€ν™”ν˜• μ‚¬μš©μ„ μœ„ν•œ IDEFICS 인슀트럭트 μ‹€ν–‰[[idefics-instruct-for-conversational-use]]

λŒ€ν™”ν˜• μ‚¬μš© 사둀λ₯Ό μœ„ν•΄, πŸ€— Hubμ—μ„œ λͺ…λ Ήμ–΄ μˆ˜ν–‰μ— μ΅œμ ν™”λœ λ²„μ „μ˜ λͺ¨λΈμ„ 찾을 수 μžˆμŠ΅λ‹ˆλ‹€. μ΄κ³³μ—λŠ” `HuggingFaceM4/idefics-80b-instruct`와 `HuggingFaceM4/idefics-9b-instruct`κ°€ μžˆμŠ΅λ‹ˆλ‹€.

이 μ²΄ν¬ν¬μΈνŠΈλŠ” 지도 ν•™μŠ΅ 및 λͺ…λ Ήμ–΄ λ―Έμ„Έ μ‘°μ • λ°μ΄ν„°μ…‹μ˜ ν˜Όν•©μœΌλ‘œ 각각의 κΈ°λ³Έ λͺ¨λΈμ„ λ―Έμ„Έ μ‘°μ •ν•œ κ²°κ³Όμž…λ‹ˆλ‹€. 이λ₯Ό 톡해 λͺ¨λΈμ˜ ν•˜μœ„ μž‘μ—… μ„±λŠ₯을 ν–₯μƒμ‹œν‚€λŠ” λ™μ‹œμ— λŒ€ν™”ν˜• ν™˜κ²½μ—μ„œ λͺ¨λΈμ„ 더 μ‚¬μš©ν•˜κΈ° μ‰½κ²Œ ν•©λ‹ˆλ‹€.

λŒ€ν™”ν˜• μ‚¬μš©μ„ μœ„ν•œ μ‚¬μš©λ²• 및 ν”„λ‘¬ν”„νŠΈλŠ” κΈ°λ³Έ λͺ¨λΈμ„ μ‚¬μš©ν•˜λŠ” 것과 맀우 μœ μ‚¬ν•©λ‹ˆλ‹€.

```py
>>> import torch
>>> from transformers import IdeficsForVisionText2Text, AutoProcessor

>>> device = "cuda" if torch.cuda.is_available() else "cpu"

>>> checkpoint = "HuggingFaceM4/idefics-9b-instruct"
>>> model = IdeficsForVisionText2Text.from_pretrained(checkpoint, torch_dtype=torch.bfloat16).to(device)
>>> processor = AutoProcessor.from_pretrained(checkpoint)

>>> prompts = [
...     [
...         "User: What is in this image?",
...         "https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG",
...         "<end_of_utterance>",

...         "\nAssistant: This picture depicts Idefix, the dog of Obelix in Asterix and Obelix. Idefix is running on the ground.<end_of_utterance>",

...         "\nUser:",
...         "https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052",
...         "And who is that?<end_of_utterance>",

...         "\nAssistant:",
...     ],
... ]

>>> # --batched mode
>>> inputs = processor(prompts, add_end_of_utterance_token=False, return_tensors="pt").to(device)
>>> # --single sample mode
>>> # inputs = processor(prompts[0], return_tensors="pt").to(device)

>>> # args 생성
>>> exit_condition = processor.tokenizer("<end_of_utterance>", add_special_tokens=False).input_ids
>>> bad_words_ids = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids

>>> generated_ids = model.generate(**inputs, eos_token_id=exit_condition, bad_words_ids=bad_words_ids, max_length=100)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
>>> for i, t in enumerate(generated_text):
...     print(f"{i}:\n{t}\n")
```