Path: blob/main/scripts/convert_original_stable_diffusion_to_diffusers.py
1440 views
# coding=utf-81# Copyright 2023 The HuggingFace Inc. team.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14""" Conversion script for the LDM checkpoints. """1516import argparse1718from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt192021if __name__ == "__main__":22parser = argparse.ArgumentParser()2324parser.add_argument(25"--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert."26)27# !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml28parser.add_argument(29"--original_config_file",30default=None,31type=str,32help="The YAML config file corresponding to the original architecture.",33)34parser.add_argument(35"--num_in_channels",36default=None,37type=int,38help="The number of input channels. If `None` number of input channels will be automatically inferred.",39)40parser.add_argument(41"--scheduler_type",42default="pndm",43type=str,44help="Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']",45)46parser.add_argument(47"--pipeline_type",48default=None,49type=str,50help=(51"The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'"52". If `None` pipeline will be automatically inferred."53),54)55parser.add_argument(56"--image_size",57default=None,58type=int,59help=(60"The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2"61" Base. Use 768 for Stable Diffusion v2."62),63)64parser.add_argument(65"--prediction_type",66default=None,67type=str,68help=(69"The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable"70" Diffusion v2 Base. Use 'v_prediction' for Stable Diffusion v2."71),72)73parser.add_argument(74"--extract_ema",75action="store_true",76help=(77"Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights"78" or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield"79" higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning."80),81)82parser.add_argument(83"--upcast_attention",84action="store_true",85help=(86"Whether the attention computation should always be upcasted. This is necessary when running stable"87" diffusion 2.1."88),89)90parser.add_argument(91"--from_safetensors",92action="store_true",93help="If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.",94)95parser.add_argument(96"--to_safetensors",97action="store_true",98help="Whether to store pipeline in safetensors format or not.",99)100parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.")101parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)")102parser.add_argument(103"--stable_unclip",104type=str,105default=None,106required=False,107help="Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.",108)109parser.add_argument(110"--stable_unclip_prior",111type=str,112default=None,113required=False,114help="Set if this is a stable unCLIP txt2img model. Selects which prior to use. If `--stable_unclip` is set to `txt2img`, the karlo prior (https://huggingface.co/kakaobrain/karlo-v1-alpha/tree/main/prior) is selected by default.",115)116parser.add_argument(117"--clip_stats_path",118type=str,119help="Path to the clip stats file. Only required if the stable unclip model's config specifies `model.params.noise_aug_config.params.clip_stats_path`.",120required=False,121)122parser.add_argument(123"--controlnet", action="store_true", default=None, help="Set flag if this is a controlnet checkpoint."124)125args = parser.parse_args()126127pipe = download_from_original_stable_diffusion_ckpt(128checkpoint_path=args.checkpoint_path,129original_config_file=args.original_config_file,130image_size=args.image_size,131prediction_type=args.prediction_type,132model_type=args.pipeline_type,133extract_ema=args.extract_ema,134scheduler_type=args.scheduler_type,135num_in_channels=args.num_in_channels,136upcast_attention=args.upcast_attention,137from_safetensors=args.from_safetensors,138device=args.device,139stable_unclip=args.stable_unclip,140stable_unclip_prior=args.stable_unclip_prior,141clip_stats_path=args.clip_stats_path,142controlnet=args.controlnet,143)144145if args.controlnet:146# only save the controlnet model147pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)148else:149pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)150151152