Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
shivamshrirao
GitHub Repository: shivamshrirao/diffusers
Path: blob/main/scripts/convert_original_controlnet_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 stable diffusion checkpoints which _only_ contain a contrlnet. """
16
17
import argparse
18
19
from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_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
parser.add_argument(
29
"--original_config_file",
30
type=str,
31
required=True,
32
help="The YAML config file corresponding to the original architecture.",
33
)
34
parser.add_argument(
35
"--num_in_channels",
36
default=None,
37
type=int,
38
help="The number of input channels. If `None` number of input channels will be automatically inferred.",
39
)
40
parser.add_argument(
41
"--image_size",
42
default=512,
43
type=int,
44
help=(
45
"The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2"
46
" Base. Use 768 for Stable Diffusion v2."
47
),
48
)
49
parser.add_argument(
50
"--extract_ema",
51
action="store_true",
52
help=(
53
"Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights"
54
" or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield"
55
" higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning."
56
),
57
)
58
parser.add_argument(
59
"--upcast_attention",
60
action="store_true",
61
help=(
62
"Whether the attention computation should always be upcasted. This is necessary when running stable"
63
" diffusion 2.1."
64
),
65
)
66
parser.add_argument(
67
"--from_safetensors",
68
action="store_true",
69
help="If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.",
70
)
71
parser.add_argument(
72
"--to_safetensors",
73
action="store_true",
74
help="Whether to store pipeline in safetensors format or not.",
75
)
76
parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.")
77
parser.add_argument("--device", type=str, help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)")
78
args = parser.parse_args()
79
80
controlnet = download_controlnet_from_original_ckpt(
81
checkpoint_path=args.checkpoint_path,
82
original_config_file=args.original_config_file,
83
image_size=args.image_size,
84
extract_ema=args.extract_ema,
85
num_in_channels=args.num_in_channels,
86
upcast_attention=args.upcast_attention,
87
from_safetensors=args.from_safetensors,
88
device=args.device,
89
)
90
91
controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)
92
93