NadaGh's picture
End of training
dde5d93 verified

ํŒŒ์ผ๋“ค์„ Hub๋กœ ํ‘ธ์‹œํ•˜๊ธฐ

[[open-in-colab]]

๐Ÿค— Diffusers๋Š” ๋ชจ๋ธ, ์Šค์ผ€์ค„๋Ÿฌ ๋˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ์„ Hub์— ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋Š” [~diffusers.utils.PushToHubMixin]์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” Hub์— ๋‹น์‹ ์˜ ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ์‰ฌ์šด ๋ฐฉ๋ฒ•์ด๋ฉฐ, ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ์ž‘์—…์„ ๊ณต์œ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ์ ์œผ๋กœ [~diffusers.utils.PushToHubMixin]๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  1. Hub์— ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  2. ๋‚˜์ค‘์— ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๋ชจ๋ธ, ์Šค์ผ€์ค„๋Ÿฌ ๋˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ ํŒŒ์ผ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  3. ์ด๋Ÿฌํ•œ ํŒŒ์ผ์ด ํฌํ•จ๋œ ํด๋”๋ฅผ Hub์— ์—…๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ๋Š” [~diffusers.utils.PushToHubMixin]์„ ์‚ฌ์šฉํ•˜์—ฌ Hub์— ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

๋จผ์ € ์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ Hub ๊ณ„์ •์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

from huggingface_hub import notebook_login

notebook_login()

๋ชจ๋ธ

๋ชจ๋ธ์„ ํ—ˆ๋ธŒ์— ํ‘ธ์‹œํ•˜๋ ค๋ฉด [~diffusers.utils.PushToHubMixin.push_to_hub]๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  Hub์— ์ €์žฅํ•  ๋ชจ๋ธ์˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ id๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค:

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์— ํ‘ธ์‹œํ•  ๊ฐ€์ค‘์น˜์˜ ๋ณ€ํ˜•์„ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, fp16 ๊ฐ€์ค‘์น˜๋ฅผ ํ‘ธ์‹œํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•˜์„ธ์š”:

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

[~diffusers.utils.PushToHubMixin.push_to_hub] ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ์˜ config.json ํŒŒ์ผ์„ ์ €์žฅํ•˜๊ณ  ๊ฐ€์ค‘์น˜๋Š” safetensors ํ˜•์‹์œผ๋กœ ์ž๋™์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

์ด์ œ Hub์˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ๋ชจ๋ธ์„ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

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

์Šค์ผ€์ค„๋Ÿฌ

์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ํ—ˆ๋ธŒ์— ํ‘ธ์‹œํ•˜๋ ค๋ฉด [~diffusers.utils.PushToHubMixin.push_to_hub]๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  Hub์— ์ €์žฅํ•  ์Šค์ผ€์ค„๋Ÿฌ์˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ id๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค:

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 ํŒŒ์ผ์„ ์ง€์ •๋œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ํ—ˆ๋ธŒ์˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

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

ํŒŒ์ดํ”„๋ผ์ธ

๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํฌํ•จ๋œ ์ „์ฒด ํŒŒ์ดํ”„๋ผ์ธ์„ Hub๋กœ ํ‘ธ์‹œํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์›ํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ [StableDiffusionPipeline]์˜ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค:

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๋กœ ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค:

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์˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์„ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

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

๋น„๊ณต๊ฐœ

๋ชจ๋ธ, ์Šค์ผ€์ค„๋Ÿฌ ๋˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ ํŒŒ์ผ๋“ค์„ ๋น„๊ณต๊ฐœ๋กœ ๋‘๋ ค๋ฉด [~diffusers.utils.PushToHubMixin.push_to_hub] ํ•จ์ˆ˜์—์„œ private=True๋ฅผ ์„ค์ •ํ•˜์„ธ์š”:

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

๋น„๊ณต๊ฐœ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋Š” ๋ณธ์ธ๋งŒ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž๋Š” ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋ณต์ œํ•  ์ˆ˜ ์—†๊ณ  ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๊ฐ€ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ํ‘œ์‹œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋น„๊ณต๊ฐœ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ URL์„ ๊ฐ€์ง€๊ณ  ์žˆ๋”๋ผ๋„ 404 - Sorry, we can't find the page you are looking for๋ผ๋Š” ๋ฉ”์‹œ์ง€๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ๋น„๊ณต๊ฐœ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ ๋ชจ๋ธ์„ ๋กœ๋“œํ•˜๋ ค๋ฉด ๋กœ๊ทธ์ธ ์ƒํƒœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.