Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/scripts/convert_original_stable_diffusion_to_diffusers.py
1440 views
1
# coding=utf-8
2
# 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 at
7
#
8
# http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# 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 and
14
# limitations under the License.
15
""" Conversion script for the LDM checkpoints. """
16
17
import argparse
18
19
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_from_original_stable_diffusion_ckpt
20
21
22
if __name__ == "__main__":
23
parser = argparse.ArgumentParser()
24
25
parser.add_argument(
26
"--checkpoint_path", default=None, type=str, required=True, help="Path to the checkpoint to convert."
27
)
28
# !wget https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml
29
parser.add_argument(
30
"--original_config_file",
31
default=None,
32
type=str,
33
help="The YAML config file corresponding to the original architecture.",
34
)
35
parser.add_argument(
36
"--num_in_channels",
37
default=None,
38
type=int,
39
help="The number of input channels. If `None` number of input channels will be automatically inferred.",
40
)
41
parser.add_argument(
42
"--scheduler_type",
43
default="pndm",
44
type=str,
45
help="Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']",
46
)
47
parser.add_argument(
48
"--pipeline_type",
49
default=None,
50
type=str,
51
help=(
52
"The pipeline type. One of 'FrozenOpenCLIPEmbedder', 'FrozenCLIPEmbedder', 'PaintByExample'"
53
". If `None` pipeline will be automatically inferred."
54
),
55
)
56
parser.add_argument(
57
"--image_size",
58
default=None,
59
type=int,
60
help=(
61
"The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2"
62
" Base. Use 768 for Stable Diffusion v2."
63
),
64
)
65
parser.add_argument(
66
"--prediction_type",
67
default=None,
68
type=str,
69
help=(
70
"The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable"
71
" Diffusion v2 Base. Use 'v_prediction' for Stable Diffusion v2."
72
),
73
)
74
parser.add_argument(
75
"--extract_ema",
76
action="store_true",
77
help=(
78
"Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights"
79
" or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield"
80
" higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning."
81
),
82
)
83
parser.add_argument(
84
"--upcast_attention",
85
action="store_true",
86
help=(
87
"Whether the attention computation should always be upcasted. This is necessary when running stable"
88
" diffusion 2.1."
89
),
90
)
91
parser.add_argument(
92
"--from_safetensors",
93
action="store_true",
94
help="If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.",
95
)
96
parser.add_argument(
97
"--to_safetensors",
98
action="store_true",
99
help="Whether to store pipeline in safetensors format or not.",
100
)
101
parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.")
102
parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)")
103
parser.add_argument(
104
"--stable_unclip",
105
type=str,
106
default=None,
107
required=False,
108
help="Set if this is a stable unCLIP model. One of 'txt2img' or 'img2img'.",
109
)
110
parser.add_argument(
111
"--stable_unclip_prior",
112
type=str,
113
default=None,
114
required=False,
115
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.",
116
)
117
parser.add_argument(
118
"--clip_stats_path",
119
type=str,
120
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`.",
121
required=False,
122
)
123
parser.add_argument(
124
"--controlnet", action="store_true", default=None, help="Set flag if this is a controlnet checkpoint."
125
)
126
args = parser.parse_args()
127
128
pipe = download_from_original_stable_diffusion_ckpt(
129
checkpoint_path=args.checkpoint_path,
130
original_config_file=args.original_config_file,
131
image_size=args.image_size,
132
prediction_type=args.prediction_type,
133
model_type=args.pipeline_type,
134
extract_ema=args.extract_ema,
135
scheduler_type=args.scheduler_type,
136
num_in_channels=args.num_in_channels,
137
upcast_attention=args.upcast_attention,
138
from_safetensors=args.from_safetensors,
139
device=args.device,
140
stable_unclip=args.stable_unclip,
141
stable_unclip_prior=args.stable_unclip_prior,
142
clip_stats_path=args.clip_stats_path,
143
controlnet=args.controlnet,
144
)
145
146
if args.controlnet:
147
# only save the controlnet model
148
pipe.controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
149
else:
150
pipe.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
151
152