File size: 5,466 Bytes
20bf0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!--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.
-->

# Unconditional 이미지 생성

unconditional 이미지 생성은 text-to-image λ˜λŠ” image-to-image λͺ¨λΈκ³Ό 달리 ν…μŠ€νŠΈλ‚˜ 이미지에 λŒ€ν•œ 쑰건이 없이 ν•™μŠ΅ 데이터 뢄포와 μœ μ‚¬ν•œ μ΄λ―Έμ§€λ§Œμ„ μƒμ„±ν•©λ‹ˆλ‹€.

<iframe
	src="https://stevhliu-ddpm-butterflies-128.hf.space"
	frameborder="0"
	width="850"
	height="550"
></iframe>


이 κ°€μ΄λ“œμ—μ„œλŠ” 기쑴에 μ‘΄μž¬ν•˜λ˜ 데이터셋과 μžμ‹ λ§Œμ˜ μ»€μŠ€ν…€ 데이터셋에 λŒ€ν•΄ unconditional image generation λͺ¨λΈμ„ ν›ˆλ ¨ν•˜λŠ” 방법을 μ„€λͺ…ν•©λ‹ˆλ‹€. ν›ˆλ ¨ μ„ΈλΆ€ 사항에 λŒ€ν•΄ 더 μžμ„Ένžˆ μ•Œκ³  μ‹Άλ‹€λ©΄ unconditional image generation을 μœ„ν•œ λͺ¨λ“  ν•™μŠ΅ 슀크립트λ₯Ό [μ—¬κΈ°](https://github.com/huggingface/diffusers/tree/main/examples/unconditional_image_generation)μ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

슀크립트λ₯Ό μ‹€ν–‰ν•˜κΈ° μ „, λ¨Όμ € μ˜μ‘΄μ„± λΌμ΄λΈŒλŸ¬λ¦¬λ“€μ„ μ„€μΉ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.

```bash
pip install diffusers[training] accelerate datasets
```

κ·Έ λ‹€μŒ πŸ€— [Accelerate](https://github.com/huggingface/accelerate/) ν™˜κ²½μ„ μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.

```bash
accelerate config
```

λ³„λ„μ˜ μ„€μ • 없이 κΈ°λ³Έ μ„€μ •μœΌλ‘œ πŸ€— [Accelerate](https://github.com/huggingface/accelerate/) ν™˜κ²½μ„ μ΄ˆκΈ°ν™”ν•΄λ΄…μ‹œλ‹€.

```bash
accelerate config default
```

λ…ΈνŠΈλΆκ³Ό 같은 λŒ€ν™”ν˜• μ‰˜μ„ μ§€μ›ν•˜μ§€ μ•ŠλŠ” ν™˜κ²½μ˜ 경우, λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•΄λ³Ό μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

```py
from accelerate.utils import write_basic_config

write_basic_config()
```

## λͺ¨λΈμ„ ν—ˆλΈŒμ— μ—…λ‘œλ“œν•˜κΈ°

ν•™μŠ΅ μŠ€ν¬λ¦½νŠΈμ— λ‹€μŒ 인자λ₯Ό μΆ”κ°€ν•˜μ—¬ ν—ˆλΈŒμ— λͺ¨λΈμ„ μ—…λ‘œλ“œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

```bash
--push_to_hub
```

## 체크포인트 μ €μž₯ν•˜κ³  뢈러였기

ν›ˆλ ¨ 쀑 λ¬Έμ œκ°€ λ°œμƒν•  경우λ₯Ό λŒ€λΉ„ν•˜μ—¬ 체크포인트λ₯Ό μ •κΈ°μ μœΌλ‘œ μ €μž₯ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. 체크포인트λ₯Ό μ €μž₯ν•˜λ €λ©΄ ν•™μŠ΅ μŠ€ν¬λ¦½νŠΈμ— λ‹€μŒ 인자λ₯Ό μ „λ‹¬ν•©λ‹ˆλ‹€:

```bash
--checkpointing_steps=500
```

전체 ν›ˆλ ¨ μƒνƒœλŠ” 500μŠ€ν…λ§ˆλ‹€ `output_dir`의 ν•˜μœ„ 폴더에 μ €μž₯되며, ν•™μŠ΅ μŠ€ν¬λ¦½νŠΈμ— `--resume_from_checkpoint` 인자λ₯Ό μ „λ‹¬ν•¨μœΌλ‘œμ¨ 체크포인트λ₯Ό 뢈러였고 ν›ˆλ ¨μ„ μž¬κ°œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

```bash
--resume_from_checkpoint="checkpoint-1500"
```

## νŒŒμΈνŠœλ‹

이제 ν•™μŠ΅ 슀크립트λ₯Ό μ‹œμž‘ν•  μ€€λΉ„κ°€ λ˜μ—ˆμŠ΅λ‹ˆλ‹€! `--dataset_name` μΈμžμ— νŒŒμΈνŠœλ‹ν•  데이터셋 이름을 μ§€μ •ν•œ λ‹€μŒ, `--output_dir` μΈμžμ— μ§€μ •λœ 경둜둜 μ €μž₯ν•©λ‹ˆλ‹€. 본인만의 데이터셋λ₯Ό μ‚¬μš©ν•˜λ €λ©΄, [ν•™μŠ΅μš© 데이터셋 λ§Œλ“€κΈ°](create_dataset) κ°€μ΄λ“œλ₯Ό μ°Έμ‘°ν•˜μ„Έμš”.

ν•™μŠ΅ μŠ€ν¬λ¦½νŠΈλŠ” `diffusion_pytorch_model.bin` νŒŒμΌμ„ μƒμ„±ν•˜κ³ , 그것을 λ‹Ήμ‹ μ˜ 리포지토리에 μ €μž₯ν•©λ‹ˆλ‹€.

<Tip>

πŸ’‘ 전체 ν•™μŠ΅μ€ V100 GPU 4개λ₯Ό μ‚¬μš©ν•  경우, 2μ‹œκ°„μ΄ μ†Œμš”λ©λ‹ˆλ‹€.

</Tip>

예λ₯Ό λ“€μ–΄, [Oxford Flowers](https://huggingface.co/datasets/huggan/flowers-102-categories) 데이터셋을 μ‚¬μš©ν•΄ νŒŒμΈνŠœλ‹ν•  경우:

```bash
accelerate launch train_unconditional.py \
  --dataset_name="huggan/flowers-102-categories" \
  --resolution=64 \
  --output_dir="ddpm-ema-flowers-64" \
  --train_batch_size=16 \
  --num_epochs=100 \
  --gradient_accumulation_steps=1 \
  --learning_rate=1e-4 \
  --lr_warmup_steps=500 \
  --mixed_precision=no \
  --push_to_hub
```

<div class="flex justify-center">
    <img src="https://user-images.githubusercontent.com/26864830/180248660-a0b143d0-b89a-42c5-8656-2ebf6ece7e52.png"/>
</div>
[Naruto](https://huggingface.co/datasets/lambdalabs/naruto-blip-captions) 데이터셋을 μ‚¬μš©ν•  경우:

```bash
accelerate launch train_unconditional.py \
  --dataset_name="lambdalabs/naruto-blip-captions" \
  --resolution=64 \
  --output_dir="ddpm-ema-naruto-64" \
  --train_batch_size=16 \
  --num_epochs=100 \
  --gradient_accumulation_steps=1 \
  --learning_rate=1e-4 \
  --lr_warmup_steps=500 \
  --mixed_precision=no \
  --push_to_hub
```

<div class="flex justify-center">
    <img src="https://user-images.githubusercontent.com/26864830/180248200-928953b4-db38-48db-b0c6-8b740fe6786f.png"/>
</div>

### μ—¬λŸ¬κ°œμ˜ GPU둜 ν›ˆλ ¨ν•˜κΈ°

`accelerate`을 μ‚¬μš©ν•˜λ©΄ μ›ν™œν•œ 닀쀑 GPU ν›ˆλ ¨μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. `accelerate`을 μ‚¬μš©ν•˜μ—¬ λΆ„μ‚° ν›ˆλ ¨μ„ μ‹€ν–‰ν•˜λ €λ©΄ [μ—¬κΈ°](https://huggingface.co/docs/accelerate/basic_tutorials/launch) 지침을 λ”°λ₯΄μ„Έμš”. λ‹€μŒμ€ λͺ…λ Ήμ–΄ μ˜ˆμ œμž…λ‹ˆλ‹€.

```bash
accelerate launch --mixed_precision="fp16" --multi_gpu train_unconditional.py \
  --dataset_name="lambdalabs/naruto-blip-captions" \
  --resolution=64 --center_crop --random_flip \
  --output_dir="ddpm-ema-naruto-64" \
  --train_batch_size=16 \
  --num_epochs=100 \
  --gradient_accumulation_steps=1 \
  --use_ema \
  --learning_rate=1e-4 \
  --lr_warmup_steps=500 \
  --mixed_precision="fp16" \
  --logger="wandb" \
  --push_to_hub
```