Spaces:
Running
on
Zero
Running
on
Zero
roubaofeipi
commited on
Commit
•
b7323ae
1
Parent(s):
9171659
Delete inference/t2i_demo.py
Browse files- inference/t2i_demo.py +0 -191
inference/t2i_demo.py
DELETED
@@ -1,191 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import yaml
|
3 |
-
import torch
|
4 |
-
import sys
|
5 |
-
sys.path.append(os.path.abspath('./'))
|
6 |
-
from inference.utils import *
|
7 |
-
from train import WurstCoreB
|
8 |
-
from gdf import DDPMSampler
|
9 |
-
from train import WurstCore_t2i as WurstCoreC
|
10 |
-
from core.utils import load_or_fail
|
11 |
-
import numpy as np
|
12 |
-
import random
|
13 |
-
import argparse
|
14 |
-
import gradio as gr
|
15 |
-
|
16 |
-
|
17 |
-
def parse_args():
|
18 |
-
parser = argparse.ArgumentParser()
|
19 |
-
parser.add_argument( '--height', type=int, default=2560, help='image height')
|
20 |
-
parser.add_argument('--width', type=int, default=5120, help='image width')
|
21 |
-
parser.add_argument('--seed', type=int, default=123, help='random seed')
|
22 |
-
parser.add_argument('--dtype', type=str, default='bf16', help=' if bf16 does not work, change it to float32 ')
|
23 |
-
parser.add_argument('--config_c', type=str,
|
24 |
-
default='configs/training/t2i.yaml' ,help='config file for stage c, latent generation')
|
25 |
-
parser.add_argument('--config_b', type=str,
|
26 |
-
default='configs/inference/stage_b_1b.yaml' ,help='config file for stage b, latent decoding')
|
27 |
-
parser.add_argument( '--prompt', type=str,
|
28 |
-
default='A photo-realistic image of a west highland white terrier in the garden, high quality, detail rich, 8K', help='text prompt')
|
29 |
-
parser.add_argument( '--num_image', type=int, default=1, help='how many images generated')
|
30 |
-
parser.add_argument( '--output_dir', type=str, default='figures/output_results/', help='output directory for generated image')
|
31 |
-
parser.add_argument( '--stage_a_tiled', action='store_true', help='whther or nor to use tiled decoding for stage a to save memory')
|
32 |
-
parser.add_argument( '--pretrained_path', type=str, default='models/ultrapixel_t2i.safetensors', help='pretrained path of newly added paramter of UltraPixel')
|
33 |
-
args = parser.parse_args()
|
34 |
-
return args
|
35 |
-
|
36 |
-
def clear_image():
|
37 |
-
return None
|
38 |
-
def load_message(height, width, seed, prompt, args, stage_a_tiled):
|
39 |
-
args.height = height
|
40 |
-
args.width = width
|
41 |
-
args.seed = seed
|
42 |
-
args.prompt = prompt + ' rich detail, 4k, high quality'
|
43 |
-
args.stage_a_tiled = stage_a_tiled
|
44 |
-
return args
|
45 |
-
def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
|
46 |
-
global args
|
47 |
-
args = load_message(height, width, seed, prompt, args, stage_a_tiled)
|
48 |
-
torch.manual_seed(args.seed)
|
49 |
-
random.seed(args.seed)
|
50 |
-
np.random.seed(args.seed)
|
51 |
-
dtype = torch.bfloat16 if args.dtype == 'bf16' else torch.float
|
52 |
-
|
53 |
-
captions = [args.prompt] * args.num_image
|
54 |
-
height, width = args.height, args.width
|
55 |
-
batch_size=1
|
56 |
-
height_lr, width_lr = get_target_lr_size(height / width, std_size=32)
|
57 |
-
stage_c_latent_shape, stage_b_latent_shape = calculate_latent_sizes(height, width, batch_size=batch_size)
|
58 |
-
stage_c_latent_shape_lr, stage_b_latent_shape_lr = calculate_latent_sizes(height_lr, width_lr, batch_size=batch_size)
|
59 |
-
|
60 |
-
# Stage C Parameters
|
61 |
-
extras.sampling_configs['cfg'] = 4
|
62 |
-
extras.sampling_configs['shift'] = 1
|
63 |
-
extras.sampling_configs['timesteps'] = 20
|
64 |
-
extras.sampling_configs['t_start'] = 1.0
|
65 |
-
extras.sampling_configs['sampler'] = DDPMSampler(extras.gdf)
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
# Stage B Parameters
|
70 |
-
extras_b.sampling_configs['cfg'] = 1.1
|
71 |
-
extras_b.sampling_configs['shift'] = 1
|
72 |
-
extras_b.sampling_configs['timesteps'] = 10
|
73 |
-
extras_b.sampling_configs['t_start'] = 1.0
|
74 |
-
|
75 |
-
for _, caption in enumerate(captions):
|
76 |
-
|
77 |
-
|
78 |
-
batch = {'captions': [caption] * batch_size}
|
79 |
-
#conditions = core.get_conditions(batch, models, extras, is_eval=True, is_unconditional=False, eval_image_embeds=False)
|
80 |
-
#unconditions = core.get_conditions(batch, models, extras, is_eval=True, is_unconditional=True, eval_image_embeds=False)
|
81 |
-
|
82 |
-
conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
|
83 |
-
unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
|
84 |
-
|
85 |
-
|
86 |
-
with torch.no_grad():
|
87 |
-
|
88 |
-
|
89 |
-
models.generator.cuda()
|
90 |
-
print('STAGE C GENERATION***************************')
|
91 |
-
with torch.cuda.amp.autocast(dtype=dtype):
|
92 |
-
sampled_c = generation_c(batch, models, extras, core, stage_c_latent_shape, stage_c_latent_shape_lr, device)
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
models.generator.cpu()
|
97 |
-
torch.cuda.empty_cache()
|
98 |
-
|
99 |
-
conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
|
100 |
-
unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
|
101 |
-
conditions_b['effnet'] = sampled_c
|
102 |
-
unconditions_b['effnet'] = torch.zeros_like(sampled_c)
|
103 |
-
print('STAGE B + A DECODING***************************')
|
104 |
-
|
105 |
-
with torch.cuda.amp.autocast(dtype=dtype):
|
106 |
-
sampled = decode_b(conditions_b, unconditions_b, models_b, stage_b_latent_shape, extras_b, device, stage_a_tiled=args.stage_a_tiled)
|
107 |
-
|
108 |
-
torch.cuda.empty_cache()
|
109 |
-
imgs = show_images(sampled)
|
110 |
-
#for idx, img in enumerate(imgs):
|
111 |
-
#print(os.path.join(save_dir, args.prompt[:20]+'_' + str(cnt).zfill(5) + '.jpg'), idx)
|
112 |
-
#img.save(os.path.join(save_dir, args.prompt[:20]+'_' + str(cnt).zfill(5) + '.jpg'))
|
113 |
-
|
114 |
-
return imgs[0]
|
115 |
-
#print('finished! Results ')
|
116 |
-
|
117 |
-
|
118 |
-
with gr.Blocks() as demo:
|
119 |
-
with gr.Column():
|
120 |
-
with gr.Row():
|
121 |
-
with gr.Column():
|
122 |
-
height = gr.Slider(value=2304, step=32, minimum=1536, maximum=4096, label='Height')
|
123 |
-
width = gr.Slider(value=4096, step=32, minimum=1536, maximum=5120, label='Width')
|
124 |
-
seed = gr.Number(value=123, step=1, label='Random Seed')
|
125 |
-
prompt = gr.Textbox(value='', max_lines=4, label='Text Prompt')
|
126 |
-
cfg = gr.Slider(value=4, step=0.1, minimum=3, maximum=10, label='CFG')
|
127 |
-
timesteps = gr.Slider(value=20, step=1, minimum=10, maximum=50, label='Timesteps')
|
128 |
-
stage_a_tiled = gr.Checkbox(value=False, label='Stage_a_tiled')
|
129 |
-
with gr.Row():
|
130 |
-
clear_button = gr.Button("Clear!")
|
131 |
-
polish_button = gr.Button("Submit!")
|
132 |
-
with gr.Column():
|
133 |
-
output_img = gr.Image(label='Output Image', sources=None)
|
134 |
-
with gr.Column():
|
135 |
-
prompt2 = gr.Textbox(
|
136 |
-
value='''
|
137 |
-
1. a happy cat
|
138 |
-
2. a happy girl
|
139 |
-
''', label='Text prompt examples'
|
140 |
-
)
|
141 |
-
|
142 |
-
polish_button.click(get_image, inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled], outputs=output_img)
|
143 |
-
polish_button.click(clear_image, inputs=[], outputs=output_img)
|
144 |
-
|
145 |
-
if __name__ == "__main__":
|
146 |
-
|
147 |
-
args = parse_args()
|
148 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
149 |
-
|
150 |
-
config_file = args.config_c
|
151 |
-
with open(config_file, "r", encoding="utf-8") as file:
|
152 |
-
loaded_config = yaml.safe_load(file)
|
153 |
-
|
154 |
-
core = WurstCoreC(config_dict=loaded_config, device=device, training=False)
|
155 |
-
|
156 |
-
# SETUP STAGE B
|
157 |
-
config_file_b = args.config_b
|
158 |
-
with open(config_file_b, "r", encoding="utf-8") as file:
|
159 |
-
config_file_b = yaml.safe_load(file)
|
160 |
-
|
161 |
-
core_b = WurstCoreB(config_dict=config_file_b, device=device, training=False)
|
162 |
-
|
163 |
-
extras = core.setup_extras_pre()
|
164 |
-
models = core.setup_models(extras)
|
165 |
-
models.generator.eval().requires_grad_(False)
|
166 |
-
print("STAGE C READY")
|
167 |
-
|
168 |
-
extras_b = core_b.setup_extras_pre()
|
169 |
-
models_b = core_b.setup_models(extras_b, skip_clip=True)
|
170 |
-
models_b = WurstCoreB.Models(
|
171 |
-
**{**models_b.to_dict(), 'tokenizer': models.tokenizer, 'text_model': models.text_model}
|
172 |
-
)
|
173 |
-
models_b.generator.bfloat16().eval().requires_grad_(False)
|
174 |
-
print("STAGE B READY")
|
175 |
-
|
176 |
-
pretrained_path = args.pretrained_path
|
177 |
-
sdd = torch.load(pretrained_path, map_location='cpu')
|
178 |
-
collect_sd = {}
|
179 |
-
for k, v in sdd.items():
|
180 |
-
collect_sd[k[7:]] = v
|
181 |
-
|
182 |
-
models.train_norm.load_state_dict(collect_sd)
|
183 |
-
models.generator.eval()
|
184 |
-
models.train_norm.eval()
|
185 |
-
|
186 |
-
|
187 |
-
demo.launch(
|
188 |
-
debug=True, share=True,
|
189 |
-
#server_name='10.160.211.26', server_port=7867
|
190 |
-
|
191 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|