CoolFace
Apppublic

tsi-org/tango

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
convert_original_stable_diffusion_to_diffusers.py157 linesDownload Raw Back to scripts
1# coding=utf-82# Copyright 2023 The HuggingFace Inc. team.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15""" Conversion script for the LDM checkpoints. """16 17import argparse18 19import torch20 21from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt22 23 24if __name__ == "__main__":25    parser = argparse.ArgumentParser()26 27    parser.add_argument(28        "--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert."29    )30    # !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml31    parser.add_argument(32        "--original_config_file",33        default=None,34        type=str,35        help="The YAML config file corresponding to the original architecture.",36    )37    parser.add_argument(38        "--num_in_channels",39        default=None,40        type=int,41        help="The number of input channels. If `None` number of input channels will be automatically inferred.",42    )43    parser.add_argument(44        "--scheduler_type",45        default="pndm",46        type=str,47        help="Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']",48    )49    parser.add_argument(50        "--pipeline_type",51        default=None,52        type=str,53        help=(54            "The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'"55            ". If `None` pipeline will be automatically inferred."56        ),57    )58    parser.add_argument(59        "--image_size",60        default=None,61        type=int,62        help=(63            "The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2"64            " Base. Use 768 for Stable Diffusion v2."65        ),66    )67    parser.add_argument(68        "--prediction_type",69        default=None,70        type=str,71        help=(72            "The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable"73            " Diffusion v2 Base. Use 'v_prediction' for Stable Diffusion v2."74        ),75    )76    parser.add_argument(77        "--extract_ema",78        action="store_true",79        help=(80            "Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights"81            " or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield"82            " higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning."83        ),84    )85    parser.add_argument(86        "--upcast_attention",87        action="store_true",88        help=(89            "Whether the attention computation should always be upcasted. This is necessary when running stable"90            " diffusion 2.1."91        ),92    )93    parser.add_argument(94        "--from_safetensors",95        action="store_true",96        help="If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.",97    )98    parser.add_argument(99        "--to_safetensors",100        action="store_true",101        help="Whether to store pipeline in safetensors format or not.",102    )103    parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.")104    parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)")105    parser.add_argument(106        "--stable_unclip",107        type=str,108        default=None,109        required=False,110        help="Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.",111    )112    parser.add_argument(113        "--stable_unclip_prior",114        type=str,115        default=None,116        required=False,117        help="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.",118    )119    parser.add_argument(120        "--clip_stats_path",121        type=str,122        help="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`.",123        required=False,124    )125    parser.add_argument(126        "--controlnet", action="store_true", default=None, help="Set flag if this is a controlnet checkpoint."127    )128    parser.add_argument("--half", action="store_true", help="Save weights in half precision.")129    args = parser.parse_args()130 131    pipe = download_from_original_stable_diffusion_ckpt(132        checkpoint_path=args.checkpoint_path,133        original_config_file=args.original_config_file,134        image_size=args.image_size,135        prediction_type=args.prediction_type,136        model_type=args.pipeline_type,137        extract_ema=args.extract_ema,138        scheduler_type=args.scheduler_type,139        num_in_channels=args.num_in_channels,140        upcast_attention=args.upcast_attention,141        from_safetensors=args.from_safetensors,142        device=args.device,143        stable_unclip=args.stable_unclip,144        stable_unclip_prior=args.stable_unclip_prior,145        clip_stats_path=args.clip_stats_path,146        controlnet=args.controlnet,147    )148 149    if args.half:150        pipe.to(torch_dtype=torch.float16)151 152    if args.controlnet:153        # only save the controlnet model154        pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)155    else:156        pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)157