refiners/scripts/conversion/convert_diffusers_t2i_adapter.py

64 lines
2.3 KiB
Python
Raw Permalink Normal View History

2023-09-24 14:21:35 +00:00
import argparse
from pathlib import Path
2023-09-24 14:21:35 +00:00
import torch
from diffusers import T2IAdapter # type: ignore
from torch import nn
2023-09-24 14:21:35 +00:00
from refiners.fluxion.model_converter import ModelConverter
from refiners.foundationals.latent_diffusion.t2i_adapter import ConditionEncoder, ConditionEncoderXL
2023-09-24 14:21:35 +00:00
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert a pretrained diffusers T2I-Adapter model to refiners")
parser.add_argument(
"--from",
type=str,
dest="source_path",
required=True,
help="Path or repository name of the source model. (e.g.: 'ip-adapter_sd15.bin').",
)
parser.add_argument(
"--to",
type=str,
dest="output_path",
default=None,
help=(
"Path to save the converted model (extension will be .safetensors). If not specified, the output path will"
" be the source path with the extension changed to .safetensors."
),
)
parser.add_argument(
"--half",
action="store_true",
dest="use_half",
default=False,
help="Use this flag to save the output file as half precision (default: full precision).",
)
parser.add_argument(
"--verbose",
action="store_true",
dest="verbose",
default=False,
help="Use this flag to print verbose output during conversion.",
)
args = parser.parse_args()
if args.output_path is None:
args.output_path = f"{Path(args.source_path).name}.safetensors"
assert args.output_path is not None
sdxl = "xl" in args.source_path
target = ConditionEncoderXL() if sdxl else ConditionEncoder()
# low_cpu_mem_usage=False stops some annoying console messages us to `pip install accelerate`
source: nn.Module = T2IAdapter.from_pretrained( # type: ignore
pretrained_model_name_or_path=args.source_path,
low_cpu_mem_usage=False,
)
2023-09-24 14:21:35 +00:00
assert isinstance(source, nn.Module), "Source model is not a nn.Module"
x = torch.randn(1, 3, 1024, 1024) if sdxl else torch.randn(1, 3, 512, 512)
converter = ModelConverter(source_model=source, target_model=target, verbose=args.verbose)
if not converter.run(source_args=(x,)):
raise RuntimeError("Model conversion failed")
converter.save_to_safetensors(path=args.output_path, half=args.use_half)