Diffusers 文件

將檔案推送到 Hub

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

將檔案推送到 Hub

🤗 Diffusers 提供了一個 PushToHubMixin,用於將您的模型、scheduler 或 pipeline 上傳到 Hub。這是一種將檔案儲存在 Hub 上的簡便方法,也允許您與他人分享您的工作。在底層,PushToHubMixin

  1. 在 Hub 上建立一個倉庫
  2. 儲存您的模型、scheduler 或 pipeline 檔案,以便稍後重新載入
  3. 將包含這些檔案的資料夾上傳到 Hub

本指南將向您展示如何使用 PushToHubMixin 將您的檔案上傳到 Hub。

您需要先使用您的訪問 token 登入到您的 Hub 賬戶

from huggingface_hub import notebook_login

notebook_login()

模型

要將模型推送到 Hub,請呼叫 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 的權重的 變體(variant)。例如,要推送 fp16 權重

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

push_to_hub() 函式會儲存模型的 config.json 檔案,並且權重會自動以 safetensors 格式儲存。

現在您可以從您在 Hub 上的倉庫重新載入模型

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

Scheduler

要將 scheduler 推送到 Hub,請呼叫 push_to_hub() 並指定要儲存在 Hub 上的 scheduler 的倉庫 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")

push_to_hub() 函式會將 scheduler 的 scheduler_config.json 檔案儲存到指定的倉庫中。

現在您可以從您在 Hub 上的倉庫重新載入 scheduler

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

Pipeline

您也可以將整個 pipeline 及其所有元件推送到 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,並呼叫 push_to_hub() 將 pipeline 推送到 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")

push_to_hub() 函式將每個元件儲存到倉庫中的一個子資料夾中。現在您可以從您在 Hub 上的倉庫重新載入 pipeline

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

隱私

push_to_hub() 函式中設定 private=True,以保持您的模型、scheduler 或 pipeline 檔案私有

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

私有倉庫僅對您可見,其他使用者無法克隆該倉庫,您的倉庫也不會出現在搜尋結果中。即使使用者擁有您私有倉庫的 URL,他們也會收到 404 - 抱歉,我們找不到您正在尋找的頁面 的提示。您必須 登入 才能從私有倉庫載入模型。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.