Commit
·
04896b2
1
Parent(s):
a67f088
add exhaustive tests
Browse files- run_local_xl.py +23 -24
- run_xl_ediffi.py +48 -0
run_local_xl.py
CHANGED
|
@@ -22,46 +22,45 @@ use_diffusers = True
|
|
| 22 |
if use_diffusers:
|
| 23 |
start_time = time.time()
|
| 24 |
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True)
|
|
|
|
| 25 |
pipe.to("cuda")
|
| 26 |
|
| 27 |
if use_refiner:
|
|
|
|
| 28 |
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
|
|
|
| 29 |
refiner.to("cuda")
|
| 30 |
# refiner.enable_sequential_cpu_offload()
|
| 31 |
else:
|
|
|
|
| 32 |
pipe = StableDiffusionXLPipeline.from_single_file("https://huggingface.co/stabilityai/stable-diffusion-xl-base-0.9/blob/main/sd_xl_base_0.9.safetensors", torch_dtype=torch.float16, use_safetensors=True)
|
|
|
|
| 33 |
pipe.to("cuda")
|
| 34 |
|
| 35 |
if use_refiner:
|
|
|
|
| 36 |
refiner = StableDiffusionXLImg2ImgPipeline.from_single_file("https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-0.9/blob/main/sd_xl_refiner_0.9.safetensors", torch_dtype=torch.float16, use_safetensors=True)
|
|
|
|
| 37 |
refiner.to("cuda")
|
| 38 |
|
| 39 |
|
| 40 |
prompt = "An astronaut riding a green horse on Mars"
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
image = pipe(prompt=prompt, num_inference_steps=steps, denoising_end=denoising_end_t2i, output_type="latent" if use_refiner else "pil").images[0]
|
| 46 |
-
# image = pipe(prompt=prompt, output_type="latent" if use_refiner else "pil").images[0]
|
| 47 |
-
|
| 48 |
-
if use_refiner:
|
| 49 |
-
denoising_start = denoising_end_t2i # denoising_start is denoising_end_t2i
|
| 50 |
-
image = refiner(prompt=prompt, num_inference_steps=steps, denoising_start=denoising_start, image=image[None, :]).images[0]
|
| 51 |
-
|
| 52 |
-
# pipe.unet.to(memory_format=torch.channels_last)
|
| 53 |
-
# pipe(prompt=prompt, num_inference_steps=2).images[0]
|
| 54 |
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 22 |
if use_diffusers:
|
| 23 |
start_time = time.time()
|
| 24 |
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True)
|
| 25 |
+
print(time.time() - start_time)
|
| 26 |
pipe.to("cuda")
|
| 27 |
|
| 28 |
if use_refiner:
|
| 29 |
+
start_time = time.time()
|
| 30 |
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
| 31 |
+
print(time.time() - start_time)
|
| 32 |
refiner.to("cuda")
|
| 33 |
# refiner.enable_sequential_cpu_offload()
|
| 34 |
else:
|
| 35 |
+
start_time = time.time()
|
| 36 |
pipe = StableDiffusionXLPipeline.from_single_file("https://huggingface.co/stabilityai/stable-diffusion-xl-base-0.9/blob/main/sd_xl_base_0.9.safetensors", torch_dtype=torch.float16, use_safetensors=True)
|
| 37 |
+
print(time.time() - start_time)
|
| 38 |
pipe.to("cuda")
|
| 39 |
|
| 40 |
if use_refiner:
|
| 41 |
+
start_time = time.time()
|
| 42 |
refiner = StableDiffusionXLImg2ImgPipeline.from_single_file("https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-0.9/blob/main/sd_xl_refiner_0.9.safetensors", torch_dtype=torch.float16, use_safetensors=True)
|
| 43 |
+
print(time.time() - start_time)
|
| 44 |
refiner.to("cuda")
|
| 45 |
|
| 46 |
|
| 47 |
prompt = "An astronaut riding a green horse on Mars"
|
| 48 |
+
steps = 30
|
| 49 |
+
seed = 0
|
| 50 |
+
seed_everything(seed)
|
| 51 |
+
image = pipe(prompt=prompt, num_inference_steps=steps, output_type="latent" if use_refiner else "pil").images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
if use_refiner:
|
| 54 |
+
image = refiner(prompt=prompt, num_inference_steps=steps - 10, image=image[None, :]).images[0]
|
| 55 |
|
| 56 |
+
file_name = f"aaa"
|
| 57 |
+
path = os.path.join(Path.home(), "images", "ediffi_sdxl", f"{file_name}.png")
|
| 58 |
+
image.save(path)
|
| 59 |
|
| 60 |
+
api.upload_file(
|
| 61 |
+
path_or_fileobj=path,
|
| 62 |
+
path_in_repo=path.split("/")[-1],
|
| 63 |
+
repo_id="patrickvonplaten/images",
|
| 64 |
+
repo_type="dataset",
|
| 65 |
+
)
|
| 66 |
+
print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
|
run_xl_ediffi.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
|
| 3 |
+
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
|
| 4 |
+
import time
|
| 5 |
+
from pytorch_lightning import seed_everything
|
| 6 |
+
import os
|
| 7 |
+
from huggingface_hub import HfApi
|
| 8 |
+
# from compel import Compel
|
| 9 |
+
import torch
|
| 10 |
+
import sys
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
import requests
|
| 13 |
+
from PIL import Image
|
| 14 |
+
from io import BytesIO
|
| 15 |
+
|
| 16 |
+
from torch.nn.functional import fractional_max_pool2d_with_indices
|
| 17 |
+
|
| 18 |
+
api = HfApi()
|
| 19 |
+
start_time = time.time()
|
| 20 |
+
|
| 21 |
+
pipe_high_noise = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True)
|
| 22 |
+
pipe_high_noise.to("cuda")
|
| 23 |
+
|
| 24 |
+
pipe_low_noise = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", text_encoder_2=pipe_high_noise.text_encoder_2, vae=pipe_high_noise.vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
| 25 |
+
pipe_low_noise.to("cuda")
|
| 26 |
+
|
| 27 |
+
prompt = "A majestic lion jumping from a big stone at night"
|
| 28 |
+
|
| 29 |
+
num_inference_steps = 40
|
| 30 |
+
high_noise_frac = 0.7
|
| 31 |
+
|
| 32 |
+
seed = 0
|
| 33 |
+
seed_everything(seed)
|
| 34 |
+
|
| 35 |
+
image = pipe_high_noise(prompt=prompt, num_inference_steps=num_inference_steps, denoising_end=high_noise_frac, output_type="latent").images
|
| 36 |
+
image = pipe_low_noise(prompt=prompt, num_inference_steps=num_inference_steps, denoising_start=high_noise_frac, image=image).images[0]
|
| 37 |
+
|
| 38 |
+
file_name = f"aaa"
|
| 39 |
+
path = os.path.join(Path.home(), "images", "ediffi_sdxl", f"{file_name}.png")
|
| 40 |
+
image.save(path)
|
| 41 |
+
|
| 42 |
+
api.upload_file(
|
| 43 |
+
path_or_fileobj=path,
|
| 44 |
+
path_in_repo=path.split("/")[-1],
|
| 45 |
+
repo_id="patrickvonplaten/images",
|
| 46 |
+
repo_type="dataset",
|
| 47 |
+
)
|
| 48 |
+
print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
|