File size: 3,341 Bytes
43b7e92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!--Copyright 2024 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.
-->

# Deterministic(결정적) 생성을 ν†΅ν•œ 이미지 ν’ˆμ§ˆ κ°œμ„ 

μƒμ„±λœ μ΄λ―Έμ§€μ˜ ν’ˆμ§ˆμ„ κ°œμ„ ν•˜λŠ” 일반적인 방법은 *결정적 batch(배치) 생성*을 μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이 방법은 이미지 batch(배치)λ₯Ό μƒμ„±ν•˜κ³  두 번째 μΆ”λ‘  λΌμš΄λ“œμ—μ„œ 더 μžμ„Έν•œ ν”„λ‘¬ν”„νŠΈμ™€ ν•¨κ»˜ κ°œμ„ ν•  이미지 ν•˜λ‚˜λ₯Ό μ„ νƒν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 핡심은 일괄 이미지 생성을 μœ„ν•΄ νŒŒμ΄ν”„λΌμΈμ— [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html#generator) λͺ©λ‘μ„ μ „λ‹¬ν•˜κ³ , 각 `Generator`λ₯Ό μ‹œλ“œμ— μ—°κ²°ν•˜μ—¬ 이미지에 μž¬μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ [`runwayml/stable-diffusion-v1-5`](runwayml/stable-diffusion-v1-5)λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€μŒ ν”„λ‘¬ν”„νŠΈμ˜ μ—¬λŸ¬ 버전을 생성해 λ΄…μ‹œλ‹€.

```py
prompt = "Labrador in the style of Vermeer"
```

(κ°€λŠ₯ν•˜λ‹€λ©΄) νŒŒμ΄ν”„λΌμΈμ„ [`DiffusionPipeline.from_pretrained`]둜 μΈμŠ€ν„΄μŠ€ν™”ν•˜μ—¬ GPU에 λ°°μΉ˜ν•©λ‹ˆλ‹€.

```python
>>> from diffusers import DiffusionPipeline

>>> pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
>>> pipe = pipe.to("cuda")
```

이제 λ„€ 개의 μ„œλ‘œ λ‹€λ₯Έ `Generator`λ₯Ό μ •μ˜ν•˜κ³  각 `Generator`에 μ‹œλ“œ(`0` ~ `3`)λ₯Ό ν• λ‹Ήν•˜μ—¬ λ‚˜μ€‘μ— νŠΉμ • 이미지에 λŒ€ν•΄ `Generator`λ₯Ό μž¬μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

```python
>>> import torch

>>> generator = [torch.Generator(device="cuda").manual_seed(i) for i in range(4)]
```

이미지λ₯Ό μƒμ„±ν•˜κ³  μ‚΄νŽ΄λ΄…λ‹ˆλ‹€.

```python
>>> images = pipe(prompt, generator=generator, num_images_per_prompt=4).images
>>> images
```

![img](https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds.jpg)

이 μ˜ˆμ œμ—μ„œλŠ” 첫 번째 이미지λ₯Ό κ°œμ„ ν–ˆμ§€λ§Œ μ‹€μ œλ‘œλŠ” μ›ν•˜λŠ” λͺ¨λ“  이미지λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€(심지어 두 개의 눈이 μžˆλŠ” 이미지도!). 첫 번째 μ΄λ―Έμ§€μ—μ„œλŠ” μ‹œλ“œκ°€ '0'인 '생성기'λ₯Ό μ‚¬μš©ν–ˆκΈ° λ•Œλ¬Έμ— 두 번째 μΆ”λ‘  λΌμš΄λ“œμ—μ„œλŠ” 이 '생성기'λ₯Ό μž¬μ‚¬μš©ν•  κ²ƒμž…λ‹ˆλ‹€. μ΄λ―Έμ§€μ˜ ν’ˆμ§ˆμ„ κ°œμ„ ν•˜λ €λ©΄ ν”„λ‘¬ν”„νŠΈμ— λͺ‡ 가지 ν…μŠ€νŠΈλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€:

```python
prompt = [prompt + t for t in [", highly realistic", ", artsy", ", trending", ", colorful"]]
generator = [torch.Generator(device="cuda").manual_seed(0) for i in range(4)]
```

μ‹œλ“œκ°€ `0`인 μ œλ„ˆλ ˆμ΄ν„° 4개λ₯Ό μƒμ„±ν•˜κ³ , 이전 λΌμš΄λ“œμ˜ 첫 번째 μ΄λ―Έμ§€μ²˜λŸΌ λ³΄μ΄λŠ” λ‹€λ₯Έ 이미지 batch(배치)λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€!

```python
>>> images = pipe(prompt, generator=generator).images
>>> images
```

![img](https://huggingface.co/datasets/diffusers/diffusers-images-docs/resolve/main/reusabe_seeds_2.jpg)