Upload scripts/make_diffusers_model.py with huggingface_hub
Browse files
scripts/make_diffusers_model.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 6 |
+
sys.path.append(ROOT_DIR)
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
from toolkit.config_modules import ModelConfig
|
| 11 |
+
from toolkit.stable_diffusion_model import StableDiffusion
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
parser = argparse.ArgumentParser()
|
| 15 |
+
parser.add_argument(
|
| 16 |
+
'input_path',
|
| 17 |
+
type=str,
|
| 18 |
+
help='Path to original sdxl model'
|
| 19 |
+
)
|
| 20 |
+
parser.add_argument(
|
| 21 |
+
'output_path',
|
| 22 |
+
type=str,
|
| 23 |
+
help='output path'
|
| 24 |
+
)
|
| 25 |
+
parser.add_argument('--sdxl', action='store_true', help='is sdxl model')
|
| 26 |
+
parser.add_argument('--refiner', action='store_true', help='is refiner model')
|
| 27 |
+
parser.add_argument('--ssd', action='store_true', help='is ssd model')
|
| 28 |
+
parser.add_argument('--sd2', action='store_true', help='is sd 2 model')
|
| 29 |
+
|
| 30 |
+
args = parser.parse_args()
|
| 31 |
+
device = torch.device('cpu')
|
| 32 |
+
dtype = torch.float32
|
| 33 |
+
|
| 34 |
+
print(f"Loading model from {args.input_path}")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
diffusers_model_config = ModelConfig(
|
| 38 |
+
name_or_path=args.input_path,
|
| 39 |
+
is_xl=args.sdxl,
|
| 40 |
+
is_v2=args.sd2,
|
| 41 |
+
is_ssd=args.ssd,
|
| 42 |
+
dtype=dtype,
|
| 43 |
+
)
|
| 44 |
+
diffusers_sd = StableDiffusion(
|
| 45 |
+
model_config=diffusers_model_config,
|
| 46 |
+
device=device,
|
| 47 |
+
dtype=dtype,
|
| 48 |
+
)
|
| 49 |
+
diffusers_sd.load_model()
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
print(f"Loaded model from {args.input_path}")
|
| 53 |
+
|
| 54 |
+
diffusers_sd.pipeline.fuse_lora()
|
| 55 |
+
|
| 56 |
+
meta = OrderedDict()
|
| 57 |
+
|
| 58 |
+
diffusers_sd.save(args.output_path, meta=meta)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
print(f"Saved to {args.output_path}")
|