File size: 6,580 Bytes
a49cc2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!--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.
-->

# νŒŒμΌλ“€μ„ Hub둜 ν‘Έμ‹œν•˜κΈ°

[[open-in-colab]]

πŸ€— DiffusersλŠ” λͺ¨λΈ, μŠ€μΌ€μ€„λŸ¬ λ˜λŠ” νŒŒμ΄ν”„λΌμΈμ„ Hub에 μ—…λ‘œλ“œν•  수 μžˆλŠ” [`~diffusers.utils.PushToHubMixin`]을 μ œκ³΅ν•©λ‹ˆλ‹€. μ΄λŠ” Hub에 λ‹Ήμ‹ μ˜ νŒŒμΌμ„ μ €μž₯ν•˜λŠ” μ‰¬μš΄ 방법이며, λ‹€λ₯Έ μ‚¬λžŒλ“€κ³Ό μž‘μ—…μ„ κ³΅μœ ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. μ‹€μ œμ μœΌλ‘œ [`~diffusers.utils.PushToHubMixin`]κ°€ λ™μž‘ν•˜λŠ” 방식은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€:

1. Hub에 리포지토리λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
2. λ‚˜μ€‘μ— λ‹€μ‹œ 뢈러올 수 μžˆλ„λ‘ λͺ¨λΈ, μŠ€μΌ€μ€„λŸ¬ λ˜λŠ” νŒŒμ΄ν”„λΌμΈ νŒŒμΌμ„ μ €μž₯ν•©λ‹ˆλ‹€.
3. μ΄λŸ¬ν•œ 파일이 ν¬ν•¨λœ 폴더λ₯Ό Hub에 μ—…λ‘œλ“œν•©λ‹ˆλ‹€.

이 κ°€μ΄λ“œλŠ” [`~diffusers.utils.PushToHubMixin`]을 μ‚¬μš©ν•˜μ—¬ Hub에 νŒŒμΌμ„ μ—…λ‘œλ“œν•˜λŠ” 방법을 λ³΄μ—¬μ€λ‹ˆλ‹€.

λ¨Όμ € μ•‘μ„ΈμŠ€ [토큰](https://huggingface.co/settings/tokens)으둜 Hub 계정에 λ‘œκ·ΈμΈν•΄μ•Ό ν•©λ‹ˆλ‹€:

```py
from huggingface_hub import notebook_login

notebook_login()
```

## λͺ¨λΈ

λͺ¨λΈμ„ ν—ˆλΈŒμ— ν‘Έμ‹œν•˜λ €λ©΄ [`~diffusers.utils.PushToHubMixin.push_to_hub`]λ₯Ό ν˜ΈμΆœν•˜κ³  Hub에 μ €μž₯ν•  λͺ¨λΈμ˜ 리포지토리 idλ₯Ό μ§€μ •ν•©λ‹ˆλ‹€:

```py
from diffusers import ControlNetModel

controlnet = ControlNetModel(
    block_out_channels=(32, 64),
    layers_per_block=2,
    in_channels=4,
    down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),
    cross_attention_dim=32,
    conditioning_embedding_out_channels=(16, 32),
)
controlnet.push_to_hub("my-controlnet-model")
```

λͺ¨λΈμ˜ 경우 Hub에 ν‘Έμ‹œν•  κ°€μ€‘μΉ˜μ˜ [*λ³€ν˜•*](loading#checkpoint-variants)을 μ§€μ •ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, `fp16` κ°€μ€‘μΉ˜λ₯Ό ν‘Έμ‹œν•˜λ €λ©΄ λ‹€μŒκ³Ό 같이 ν•˜μ„Έμš”:

```py
controlnet.push_to_hub("my-controlnet-model", variant="fp16")
```

[`~diffusers.utils.PushToHubMixin.push_to_hub`] ν•¨μˆ˜λŠ” λͺ¨λΈμ˜ `config.json` νŒŒμΌμ„ μ €μž₯ν•˜κ³  κ°€μ€‘μΉ˜λŠ” `safetensors` ν˜•μ‹μœΌλ‘œ μžλ™μœΌλ‘œ μ €μž₯λ©λ‹ˆλ‹€.

이제 Hub의 λ¦¬ν¬μ§€ν† λ¦¬μ—μ„œ λͺ¨λΈμ„ λ‹€μ‹œ 뢈러올 수 μžˆμŠ΅λ‹ˆλ‹€:

```py
model = ControlNetModel.from_pretrained("your-namespace/my-controlnet-model")
```

## μŠ€μΌ€μ€„λŸ¬

μŠ€μΌ€μ€„λŸ¬λ₯Ό ν—ˆλΈŒμ— ν‘Έμ‹œν•˜λ €λ©΄ [`~diffusers.utils.PushToHubMixin.push_to_hub`]λ₯Ό ν˜ΈμΆœν•˜κ³  Hub에 μ €μž₯ν•  μŠ€μΌ€μ€„λŸ¬μ˜ 리포지토리 idλ₯Ό μ§€μ •ν•©λ‹ˆλ‹€:

```py
from diffusers import DDIMScheduler

scheduler = DDIMScheduler(
    beta_start=0.00085,
    beta_end=0.012,
    beta_schedule="scaled_linear",
    clip_sample=False,
    set_alpha_to_one=False,
)
scheduler.push_to_hub("my-controlnet-scheduler")
```

[`~diffusers.utils.PushToHubMixin.push_to_hub`] ν•¨μˆ˜λŠ” μŠ€μΌ€μ€„λŸ¬μ˜ `scheduler_config.json` νŒŒμΌμ„ μ§€μ •λœ 리포지토리에 μ €μž₯ν•©λ‹ˆλ‹€.

이제 ν—ˆλΈŒμ˜ λ¦¬ν¬μ§€ν† λ¦¬μ—μ„œ μŠ€μΌ€μ€„λŸ¬λ₯Ό λ‹€μ‹œ 뢈러올 수 μžˆμŠ΅λ‹ˆλ‹€:

```py
scheduler = DDIMScheduler.from_pretrained("your-namepsace/my-controlnet-scheduler")
```

## νŒŒμ΄ν”„λΌμΈ

λͺ¨λ“  μ»΄ν¬λ„ŒνŠΈκ°€ ν¬ν•¨λœ 전체 νŒŒμ΄ν”„λΌμΈμ„ Hub둜 ν‘Έμ‹œν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, μ›ν•˜λŠ” νŒŒλΌλ―Έν„°λ‘œ [`StableDiffusionPipeline`]의 μ»΄ν¬λ„ŒνŠΈλ“€μ„ μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€:

```py
from diffusers import (
    UNet2DConditionModel,
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionPipeline,
)
from transformers import CLIPTextModel, CLIPTextConfig, CLIPTokenizer

unet = UNet2DConditionModel(
    block_out_channels=(32, 64),
    layers_per_block=2,
    sample_size=32,
    in_channels=4,
    out_channels=4,
    down_block_types=("DownBlock2D", "CrossAttnDownBlock2D"),
    up_block_types=("CrossAttnUpBlock2D", "UpBlock2D"),
    cross_attention_dim=32,
)

scheduler = DDIMScheduler(
    beta_start=0.00085,
    beta_end=0.012,
    beta_schedule="scaled_linear",
    clip_sample=False,
    set_alpha_to_one=False,
)

vae = AutoencoderKL(
    block_out_channels=[32, 64],
    in_channels=3,
    out_channels=3,
    down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"],
    up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"],
    latent_channels=4,
)

text_encoder_config = CLIPTextConfig(
    bos_token_id=0,
    eos_token_id=2,
    hidden_size=32,
    intermediate_size=37,
    layer_norm_eps=1e-05,
    num_attention_heads=4,
    num_hidden_layers=5,
    pad_token_id=1,
    vocab_size=1000,
)
text_encoder = CLIPTextModel(text_encoder_config)
tokenizer = CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")
```

λͺ¨λ“  μ»΄ν¬λ„ŒνŠΈλ“€μ„ [`StableDiffusionPipeline`]에 μ „λ‹¬ν•˜κ³  [`~diffusers.utils.PushToHubMixin.push_to_hub`]λ₯Ό ν˜ΈμΆœν•˜μ—¬ νŒŒμ΄ν”„λΌμΈμ„ Hub둜 ν‘Έμ‹œν•©λ‹ˆλ‹€:

```py
components = {
    "unet": unet,
    "scheduler": scheduler,
    "vae": vae,
    "text_encoder": text_encoder,
    "tokenizer": tokenizer,
    "safety_checker": None,
    "feature_extractor": None,
}

pipeline = StableDiffusionPipeline(**components)
pipeline.push_to_hub("my-pipeline")
```

[`~diffusers.utils.PushToHubMixin.push_to_hub`] ν•¨μˆ˜λŠ” 각 μ»΄ν¬λ„ŒνŠΈλ₯Ό λ¦¬ν¬μ§€ν† λ¦¬μ˜ ν•˜μœ„ 폴더에 μ €μž₯ν•©λ‹ˆλ‹€. 이제 Hub의 λ¦¬ν¬μ§€ν† λ¦¬μ—μ„œ νŒŒμ΄ν”„λΌμΈμ„ λ‹€μ‹œ 뢈러올 수 μžˆμŠ΅λ‹ˆλ‹€:

```py
pipeline = StableDiffusionPipeline.from_pretrained("your-namespace/my-pipeline")
```

## λΉ„κ³΅κ°œ

λͺ¨λΈ, μŠ€μΌ€μ€„λŸ¬ λ˜λŠ” νŒŒμ΄ν”„λΌμΈ νŒŒμΌλ“€μ„ λΉ„κ³΅κ°œλ‘œ 두렀면 [`~diffusers.utils.PushToHubMixin.push_to_hub`] ν•¨μˆ˜μ—μ„œ `private=True`λ₯Ό μ„€μ •ν•˜μ„Έμš”:

```py
controlnet.push_to_hub("my-controlnet-model-private", private=True)
```

λΉ„κ³΅κ°œ λ¦¬ν¬μ§€ν† λ¦¬λŠ” 본인만 λ³Ό 수 있으며 λ‹€λ₯Έ μ‚¬μš©μžλŠ” 리포지토리λ₯Ό λ³΅μ œν•  수 μ—†κ³  리포지토리가 검색 결과에 ν‘œμ‹œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ‚¬μš©μžκ°€ λΉ„κ³΅κ°œ λ¦¬ν¬μ§€ν† λ¦¬μ˜ URL을 κ°€μ§€κ³  μžˆλ”λΌλ„ `404 - Sorry, we can't find the page you are looking for`λΌλŠ” λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€. λΉ„κ³΅κ°œ λ¦¬ν¬μ§€ν† λ¦¬μ—μ„œ λͺ¨λΈμ„ λ‘œλ“œν•˜λ €λ©΄ [둜그인](https://huggingface.co/docs/huggingface_hub/quick-start#login) μƒνƒœμ—¬μ•Ό ν•©λ‹ˆλ‹€.