|
import PIL.Image
|
|
|
|
from diffusers import DiffusionPipeline
|
|
import torch
|
|
|
|
import numpy as np
|
|
|
|
from stable_diffusion_server.utils import log_time
|
|
|
|
pipe = DiffusionPipeline.from_pretrained(
|
|
"models/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
|
)
|
|
pipe.to("cuda")
|
|
|
|
refiner = DiffusionPipeline.from_pretrained(
|
|
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
|
text_encoder_2=pipe.text_encoder_2,
|
|
vae=pipe.vae,
|
|
torch_dtype=torch.float16,
|
|
use_safetensors=True,
|
|
variant="fp16",
|
|
)
|
|
refiner.to("cuda")
|
|
|
|
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
|
|
use_refiner = True
|
|
with log_time('diffuse'):
|
|
with torch.inference_mode():
|
|
image = pipe(prompt=prompt, output_type="latent" if use_refiner else "pil").images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pixels_to_delete = int(0.3 * image.shape[0])
|
|
idx_to_delete = np.ones(image.shape[0], dtype=bool, device="cuda")
|
|
idx_to_delete[:pixels_to_delete] = False
|
|
image[idx_to_delete] = [0,0,0]
|
|
|
|
|
|
|
|
|
|
|
|
image = refiner(prompt=prompt, image=image[None, :]).images[0]
|
|
|
|
|
|
|
|
|