File size: 5,074 Bytes
a385e25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from pytorch_lightning import seed_everything
from scripts.demo.streamlit_helpers import *
from scripts.util.detection.nsfw_and_watermark_dectection import DeepFloydDataFiltering

import argparse
import tqdm

if __name__ == "__main__":
    
    parser = argparse.ArgumentParser()
    parser.add_argument('--model_version', type=str, default='2.1', 
                        choices=['2.1', '2.1-768', 'xl'])
    parser.add_argument("--num_samples", type=int, default=4)
    parser.add_argument("--seed", type=int, default=42)
    parser.add_argument("--prompt", type=str, default="a corgi is sitting on a couch")
    parser.add_argument("--prompt_listpath", type=str, default="", help="path to a txt file with a list of prompts")
    parser.add_argument("--negative_prompt", type=str, default="ugly, low quality")
    parser.add_argument('--save_path', type=str, default='outputs/demo/txt2img/')
    args = parser.parse_args()

    seed_everything(args.seed)
    save_path = args.save_path

    version_map = {
        '2.1': 'sd-2.1',
        '2.1-768': 'sd-2.1-768',
        'xl': 'SD-XL base',
    }

    SD_XL_BASE_RATIOS = {
        "0.5": (704, 1408),
        "0.52": (704, 1344),
        "0.57": (768, 1344),
        "0.6": (768, 1280),
        "0.68": (832, 1216),
        "0.72": (832, 1152),
        "0.78": (896, 1152),
        "0.82": (896, 1088),
        "0.88": (960, 1088),
        "0.94": (960, 1024),
        "1.0": (1024, 1024),
        "1.07": (1024, 960),
        "1.13": (1088, 960),
        "1.21": (1088, 896),
        "1.29": (1152, 896),
        "1.38": (1152, 832),
        "1.46": (1216, 832),
        "1.67": (1280, 768),
        "1.75": (1344, 768),
        "1.91": (1344, 704),
        "2.0": (1408, 704),
        "2.09": (1472, 704),
        "2.4": (1536, 640),
        "2.5": (1600, 640),
        "2.89": (1664, 576),
        "3.0": (1728, 576),
    }

    VERSION2SPECS = {
        "SD-XL base": {
            "H": 1024,
            "W": 1024,
            "C": 4,
            "f": 8,
            "is_legacy": False,
            "config": "configs/inference/sd_xl_base.yaml",
            "ckpt": "checkpoints/sd_xl_base_0.9.safetensors",
            "is_guided": True,
        },
        "sd-2.1": {
            "H": 512,
            "W": 512,
            "C": 4,
            "f": 8,
            "is_legacy": True,
            "config": "configs/inference/sd_2_1.yaml",
            "ckpt": "checkpoints/v2-1_512-ema-pruned.safetensors",
            "is_guided": True,
        },
        "sd-2.1-768": {
            "H": 768,
            "W": 768,
            "C": 4,
            "f": 8,
            "is_legacy": True,
            "config": "configs/inference/sd_2_1_768.yaml",
            "ckpt": "checkpoints/v2-1_768-ema-pruned.safetensors",
        },
        "SDXL-Refiner": {
            "H": 1024,
            "W": 1024,
            "C": 4,
            "f": 8,
            "is_legacy": True,
            "config": "configs/inference/sd_xl_refiner.yaml",
            "ckpt": "checkpoints/sd_xl_refiner_0.9.safetensors",
            "is_guided": True,
        },
    }

    version = args.model_version
    version = version_map[version]
    version_dict = VERSION2SPECS[version]

    # initialize model
    state = init_st(version_dict)
    if state["msg"]:
        st.info(state["msg"])
    model = state["model"]

    if version == "SD-XL base":
        ratio = '1.0'
        W, H = SD_XL_BASE_RATIOS[ratio]
    else:
        W, H = version_dict['W'], version_dict['H']

    C = version_dict["C"]
    F = version_dict["f"]

    if args.prompt_listpath:
        with open(args.prompt_listpath, 'r') as f:
            prompts = f.readlines()
        prompts = [p.strip() for p in prompts]
    else:
        prompts = [args.prompt]
    negative_prompt = args.negative_prompt
    init_dict = {
        "orig_width": W,
        "orig_height": H,
        "target_width": W,
        "target_height": H,
    }

    for prompt in tqdm.tqdm(prompts):
        print('Current Prompt: >>>>> {} <<<<<'.format(prompt))
        value_dict = init_embedder_options(
            get_unique_embedder_keys_from_conditioner(state["model"].conditioner),
            init_dict,
            prompt=prompt,
            negative_prompt=negative_prompt,
        )
        _, _, sampler = init_sampling(
            use_identity_guider=not version_dict["is_guided"]
        )

        num_samples = args.num_samples

        is_legacy=False
        return_latents = False
        filter=None
        with torch.no_grad():
            samples = do_sample(
                state["model"],
                sampler,
                value_dict,
                num_samples,
                H,
                W,
                C,
                F,
                force_uc_zero_embeddings=["txt"] if not is_legacy else [],
                return_latents=return_latents,
                filter=filter,
            )

        if samples is not None:
            perform_save_locally(save_path, samples)
            print("Saved samples to {}. Enjoy.".format(save_path))