import os
import yaml
import torch
import sys
sys.path.append(os.path.abspath('./'))
from inference.utils import *
from train import WurstCoreB
from gdf import DDPMSampler
from train import WurstCore_t2i as WurstCoreC
import numpy as np
import random
import argparse
import gradio as gr
import spaces
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument( '--height', type=int, default=2560, help='image height')
parser.add_argument('--width', type=int, default=5120, help='image width')
parser.add_argument('--seed', type=int, default=123, help='random seed')
parser.add_argument('--dtype', type=str, default='bf16', help=' if bf16 does not work, change it to float32 ')
parser.add_argument('--config_c', type=str,
default='configs/training/t2i.yaml' ,help='config file for stage c, latent generation')
parser.add_argument('--config_b', type=str,
default='configs/inference/stage_b_1b.yaml' ,help='config file for stage b, latent decoding')
parser.add_argument( '--prompt', type=str,
default='A photo-realistic image of a west highland white terrier in the garden, high quality, detail rich, 8K', help='text prompt')
parser.add_argument( '--num_image', type=int, default=1, help='how many images generated')
parser.add_argument( '--output_dir', type=str, default='figures/output_results/', help='output directory for generated image')
parser.add_argument( '--stage_a_tiled', action='store_true', help='whther or nor to use tiled decoding for stage a to save memory')
parser.add_argument( '--pretrained_path', type=str, default='models/ultrapixel_t2i.safetensors', help='pretrained path of newly added paramter of UltraPixel')
args = parser.parse_args()
return args
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("UltraPixel: Advancing Ultra-High-Resolution Image Synthesis to New Peaks ")
code
with gr.Row():
prompt = gr.Textbox(
label="Text Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
container=False
)
polish_button = gr.Button("Submit!", scale=0)
output_img = gr.Image(label="Output Image", show_label=False)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Number(
label="Random Seed",
value=123,
step=1,
minimum=0,
#maximum=MAX_SEED
)
#randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=1536,
maximum=5120,
step=32,
value=4096
)
height = gr.Slider(
label="Height",
minimum=1536,
maximum=4096,
step=32,
value=2304
)
with gr.Row():
cfg = gr.Slider(
label="CFG",
minimum=3,
maximum=10,
step=0.1,
value=4
)
timesteps = gr.Slider(
label="Timesteps",
minimum=10,
maximum=50,
step=1,
value=20
)
stage_a_tiled = gr.Checkbox(label="Stage_a_tiled", value=False)
clear_button = gr.Button("Clear!")
gr.Examples(
examples=[
"A detailed view of a blooming magnolia tree, with large, white flowers and dark green leaves, set against a clear blue sky.",
"A close-up portrait of a young woman with flawless skin, vibrant red lipstick, and wavy brown hair, wearing a vintage floral dress and standing in front of a blooming garden.",
"The image features a snow-covered mountain range with a large, snow-covered mountain in the background. The mountain is surrounded by a forest of trees, and the sky is filled with clouds. The scene is set during the winter season, with snow covering the ground and the trees.",
"Crocodile in a sweater.",
"A vibrant anime scene of a young girl with long, flowing pink hair, big sparkling blue eyes, and a school uniform, standing under a cherry blossom tree with petals falling around her. The background shows a traditional Japanese school with cherry blossoms in full bloom.",
"A playful Labrador retriever puppy with a shiny, golden coat, chasing a red ball in a spacious backyard, with green grass and a wooden fence.",
"A cozy, rustic log cabin nestled in a snow-covered forest, with smoke rising from the stone chimney, warm lights glowing from the windows, and a path of footprints leading to the front door.",
"A highly detailed, high-quality image of the Banff National Park in Canada. The turquoise waters of Lake Louise are surrounded by snow-capped mountains and dense pine forests. A wooden canoe is docked at the edge of the lake. The sky is a clear, bright blue, and the air is crisp and fresh.",
"A highly detailed, high-quality image of a Shih Tzu receiving a bath in a home bathroom. The dog is standing in a tub, covered in suds, with a slightly wet and adorable look. The background includes bathroom fixtures, towels, and a clean, tiled floor.",
],
inputs=[prompt],
outputs=[output_img],
examples_per_page=5
)
polish_button.click(get_image, inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled], outputs=output_img)
polish_button.click(clear_image, inputs=[], outputs=output_img)
if name == "main":
code
args = parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
config_file = args.config_c
with open(config_file, "r", encoding="utf-8") as file:
loaded_config = yaml.safe_load(file)
core = WurstCoreC(config_dict=loaded_config, device=device, training=False)
# SETUP STAGE B
config_file_b = args.config_b
with open(config_file_b, "r", encoding="utf-8") as file:
config_file_b = yaml.safe_load(file)
core_b = WurstCoreB(config_dict=config_file_b, device=device, training=False)
extras = core.setup_extras_pre()
models = core.setup_models(extras)
models.generator.eval().requires_grad_(False)
print("STAGE C READY")
extras_b = core_b.setup_extras_pre()
models_b = core_b.setup_models(extras_b, skip_clip=True)
models_b = WurstCoreB.Models(
**{**models_b.to_dict(), 'tokenizer': models.tokenizer, 'text_model': models.text_model}
)
models_b.generator.bfloat16().eval().requires_grad_(False)
print("STAGE B READY")
pretrained_path = args.pretrained_path
sdd = torch.load(pretrained_path, map_location='cpu')
collect_sd = {}
for k, v in sdd.items():
collect_sd[k[7:]] = v
models.train_norm.load_state_dict(collect_sd)
models.generator.eval()
models.train_norm.eval()
demo.launch(
debug=True, share=True,
)
modify it run on cpu
To modify your script to run on the CPU instead of the GPU, you'll need to make several adjustments. Running deep learning models on the CPU can significantly increase processing time and memory usage, so ensure your hardware can handle the workload. Below are the steps and the modified code with explanations:
Changes Overview:
Device Configuration:
Set the device to CPU using torch.device("cpu").
Data Type Adjustment:
Change the default dtype from 'bf16' to 'float32' since bfloat16 is primarily supported on GPUs.
Decorator Modification:
Remove or comment out the @spaces.GPU(duration=120) decorator as it is specific to GPU environments.
Model and Tensor Management:
Remove any .cuda() and .cpu() calls.
Eliminate torch.cuda.empty_cache() since it's unnecessary on the CPU.
Ensure all models and tensors are consistently moved to the CPU.
Performance Considerations:
Be aware that running on the CPU will be slower. If possible, optimize your code or consider using lighter models.
Modified Code:
python
import os
import yaml
import torch
import sys
sys.path.append(os.path.abspath('./'))
from inference.utils import *
from train import WurstCoreB
from gdf import DDPMSampler
from train import WurstCore_t2i as WurstCoreC
import numpy as np
import random
import argparse
import gradio as gr
# import spaces # Removed as it's related to GPU support
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--height', type=int, default=2560, help='image height')
parser.add_argument('--width', type=int, default=5120, help='image width')
parser.add_argument('--seed', type=int, default=123, help='random seed')
parser.add_argument('--dtype', type=str, default='float32', help='Changed default to float32 for CPU')
parser.add_argument('--config_c', type=str,
default='configs/training/t2i.yaml', help='config file for stage c, latent generation')
parser.add_argument('--config_b', type=str,
default='configs/inference/stage_b_1b.yaml', help='config file for stage b, latent decoding')
parser.add_argument('--prompt', type=str,
default='A photo-realistic image of a west highland white terrier in the garden, high quality, detail rich, 8K', help='text prompt')
parser.add_argument('--num_image', type=int, default=1, help='how many images generated')
parser.add_argument('--output_dir', type=str, default='figures/output_results/', help='output directory for generated image')
parser.add_argument('--stage_a_tiled', action='store_true', help='whether or not to use tiled decoding for stage a to save memory')
parser.add_argument('--pretrained_path', type=str, default='models/ultrapixel_t2i.safetensors', help='pretrained path of newly added parameter of UltraPixel')
args = parser.parse_args()
return args
def clear_image():
return None
def load_message(height, width, seed, prompt, args, stage_a_tiled):
args.height = height
args.width = width
args.seed = seed
args.prompt = prompt + ' rich detail, 4k, high quality'
args.stage_a_tiled = stage_a_tiled
return args
# Removed the @spaces.GPU decorator since we're switching to CPU
# @spaces.GPU(duration=120)
def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
global args
args = load_message(height, width, seed, prompt, args, stage_a_tiled)
torch.manual_seed(args.seed)
random.seed(args.seed)
np.random.seed(args.seed)
dtype = torch.float32 if args.dtype == 'float32' else torch.bfloat16 # Adjusted for CPU
captions = [args.prompt] * args.num_image
height, width = args.height, args.width
batch_size = 1
height_lr, width_lr = get_target_lr_size(height / width, std_size=32)
stage_c_latent_shape, stage_b_latent_shape = calculate_latent_sizes(height, width, batch_size=batch_size)
stage_c_latent_shape_lr, stage_b_latent_shape_lr = calculate_latent_sizes(height_lr, width_lr, batch_size=batch_size)
# Stage C Parameters
extras.sampling_configs['cfg'] = cfg
extras.sampling_configs['shift'] = 1
extras.sampling_configs['timesteps'] = timesteps
extras.sampling_configs['t_start'] = 1.0
extras.sampling_configs['sampler'] = DDPMSampler(extras.gdf)
# Stage B Parameters
extras_b.sampling_configs['cfg'] = cfg # Adjusted to use cfg from input
extras_b.sampling_configs['shift'] = 1
extras_b.sampling_configs['timesteps'] = timesteps // 2 # Example adjustment
extras_b.sampling_configs['t_start'] = 1.0
imgs = []
for _, caption in enumerate(captions):
batch = {'captions': [caption] * batch_size}
conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
with torch.no_grad():
# Ensure models are on CPU
models.generator.to('cpu')
print('STAGE C GENERATION***************************')
with torch.autocast('cpu', dtype=dtype):
sampled_c = generation_c(batch, models, extras, core, stage_c_latent_shape, stage_c_latent_shape_lr, device)
conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
conditions_b['effnet'] = sampled_c
unconditions_b['effnet'] = torch.zeros_like(sampled_c)
print('STAGE B + A DECODING***************************')
with torch.autocast('cpu', dtype=dtype):
sampled = decode_b(conditions_b, unconditions_b, models_b, stage_b_latent_shape, extras_b, device, stage_a_tiled=args.stage_a_tiled)
imgs = show_images(sampled)
return imgs[0]
#print('finished! Results ')
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("<h1><center>UltraPixel: Advancing Ultra-High-Resolution Image Synthesis to New Peaks </center></h1>")
with gr.Row():
prompt = gr.Textbox(
label="Text Prompt",
show_label=False,
max_lines=1,
placeholder="Enter your prompt",
container=False
)
polish_button = gr.Button("Submit!", scale=0)
output_img = gr.Image(label="Output Image", show_label=False)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Number(
label="Random Seed",
value=123,
step=1,
minimum=0,
#maximum=MAX_SEED
)
#randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
with gr.Row():
width = gr.Slider(
label="Width",
minimum=1536,
maximum=5120,
step=32,
value=4096
)
height = gr.Slider(
label="Height",
minimum=1536,
maximum=4096,
step=32,
value=2304
)
with gr.Row():
cfg = gr.Slider(
label="CFG",
minimum=3,
maximum=10,
step=0.1,
value=4
)
timesteps = gr.Slider(
label="Timesteps",
minimum=10,
maximum=50,
step=1,
value=20
)
stage_a_tiled = gr.Checkbox(label="Stage_a_tiled", value=False)
clear_button = gr.Button("Clear!")
gr.Examples(
examples=[
"A detailed view of a blooming magnolia tree, with large, white flowers and dark green leaves, set against a clear blue sky.",
"A close-up portrait of a young woman with flawless skin, vibrant red lipstick, and wavy brown hair, wearing a vintage floral dress and standing in front of a blooming garden.",
"The image features a snow-covered mountain range with a large, snow-covered mountain in the background. The mountain is surrounded by a forest of trees, and the sky is filled with clouds. The scene is set during the winter season, with snow covering the ground and the trees.",
"Crocodile in a sweater.",
"A vibrant anime scene of a young girl with long, flowing pink hair, big sparkling blue eyes, and a school uniform, standing under a cherry blossom tree with petals falling around her. The background shows a traditional Japanese school with cherry blossoms in full bloom.",
"A playful Labrador retriever puppy with a shiny, golden coat, chasing a red ball in a spacious backyard, with green grass and a wooden fence.",
"A cozy, rustic log cabin nestled in a snow-covered forest, with smoke rising from the stone chimney, warm lights glowing from the windows, and a path of footprints leading to the front door.",
"A highly detailed, high-quality image of the Banff National Park in Canada. The turquoise waters of Lake Louise are surrounded by snow-capped mountains and dense pine forests. A wooden canoe is docked at the edge of the lake. The sky is a clear, bright blue, and the air is crisp and fresh.",
"A highly detailed, high-quality image of a Shih Tzu receiving a bath in a home bathroom. The dog is standing in a tub, covered in suds, with a slightly wet and adorable look. The background includes bathroom fixtures, towels, and a clean, tiled floor.",
],
inputs=[prompt],
outputs=[output_img],
examples_per_page=5
)
polish_button.click(get_image, inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled], outputs=output_img)
polish_button.click(clear_image, inputs=[], outputs=output_img)
if __name__ == "__main__":
args = parse_args()
device = torch.device("cpu") # Set device to CPU
config_file = args.config_c
with open(config_file, "r", encoding="utf-8") as file:
loaded_config = yaml.safe_load(file)
core = WurstCoreC(config_dict=loaded_config, device=device, training=False)
# SETUP STAGE B
config_file_b = args.config_b
with open(config_file_b, "r", encoding="utf-8") as file:
config_file_b = yaml.safe_load(file)
core_b = WurstCoreB(config_dict=config_file_b, device=device, training=False)
extras = core.setup_extras_pre()
models = core.setup_models(extras)
models.generator.to(device) # Ensure models are on CPU
models.generator.eval().requires_grad_(False)
print("STAGE C READY")
extras_b = core_b.setup_extras_pre()
models_b = core_b.setup_models(extras_b, skip_clip=True)
models_b = WurstCoreB.Models(
**{**models_b.to_dict(), 'tokenizer': models.tokenizer, 'text_model': models.text_model}
)
models_b.generator.to(device) # Ensure models are on CPU
models_b.generator.float().eval().requires_grad_(False) # Changed from bfloat16 to float
print("STAGE B READY")
pretrained_path = args.pretrained_path
sdd = torch.load(pretrained_path, map_location='cpu') # Ensure loading on CPU
collect_sd = {}
for k, v in sdd.items():
collect_sd[k[7:]] = v
models.train_norm.load_state_dict(collect_sd)
models.generator.eval()
models.train_norm.eval()
demo.launch(
# Removed debug=True and share=True for security reasons; add them back if needed
)
Detailed Explanations of Changes:
Device Configuration:
Original:
python
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
Modified:
python
device = torch.device("cpu")
This ensures that the model and all tensors are consistently moved to the CPU, regardless of GPU availability.
Data Type Adjustment:
Original:
python
parser.add_argument('--dtype', type=str, default='bf16', help=' if bf16 does not work, change it to float32 ')
Modified:
python
parser.add_argument('--dtype', type=str, default='float32', help='Changed default to float32 for CPU')
bfloat16 is not typically supported on CPUs and is primarily used for performance optimization on GPUs. Switching to float32 ensures compatibility.
Decorator Modification:
Original:
python
@spaces.GPU(duration=120)
def get_image(...):
...
Modified:
python
# Removed the @spaces.GPU decorator since we're switching to CPU
def get_image(...):
...
The @spaces.GPU decorator is specific to GPU environments, especially when deploying on platforms like Hugging Face Spaces. Since we're targeting CPU execution, this decorator is removed.
Model and Tensor Management:
Removed .cuda() and .cpu() Calls:
All instances where models or tensors were moved to GPU or CPU have been adjusted to ensure they remain on the CPU.
python
Performance Considerations:
Running complex models on the CPU can be significantly slower compared to GPUs. Here are some tips to mitigate performance issues:
Optimize Batch Size: Reducing the number of images generated simultaneously can help manage CPU memory.
Model Optimization: Consider using optimized CPU libraries or model quantization techniques to speed up inference.
Parallel Processing: If possible, leverage multi-threading or multi-processing to utilize multiple CPU cores.
Final Notes:
Testing: After making these changes, thoroughly test the script to ensure that it functions correctly in the CPU environment.
Dependencies: Ensure all dependencies are compatible with CPU execution. Some GPU-specific libraries or optimizations might need alternative implementations.
Resource Management: Monitor CPU usage and memory consumption to avoid system slowdowns or crashes.
By following these modifications, your script should run on the CPU. However, be prepared for longer processing times and ensure that your system has adequate resources to handle the workload.