Commit
·
a3ecce8
1
Parent(s):
6421583
up
Browse files- run_local_xl_inpaint.py +65 -0
run_local_xl_inpaint.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
|
3 |
+
from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, StableDiffusionXLInpaintPipeline, AutoencoderKL
|
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 |
+
api = HfApi()
|
17 |
+
start_time = time.time()
|
18 |
+
|
19 |
+
use_refiner = bool(int(sys.argv[1]))
|
20 |
+
use_diffusers = True
|
21 |
+
|
22 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16, force_upcast=True)
|
23 |
+
pipe = StableDiffusionXLInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True)
|
24 |
+
print(time.time() - start_time)
|
25 |
+
pipe.to("cuda")
|
26 |
+
|
27 |
+
def download_image(url):
|
28 |
+
response = requests.get(url)
|
29 |
+
return Image.open(BytesIO(response.content)).convert("RGB")
|
30 |
+
|
31 |
+
|
32 |
+
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
|
33 |
+
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
|
34 |
+
|
35 |
+
init_image = download_image(img_url).resize((1024, 1024))
|
36 |
+
mask_image = download_image(mask_url).resize((1024, 1024))
|
37 |
+
|
38 |
+
if use_refiner:
|
39 |
+
start_time = time.time()
|
40 |
+
refiner = StableDiffusionXLInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
41 |
+
refiner.to("cuda")
|
42 |
+
# refiner.enable_sequential_cpu_offload()
|
43 |
+
|
44 |
+
prompt = "A majestic tiger sitting on a bench"
|
45 |
+
steps = 100
|
46 |
+
seed = 3
|
47 |
+
seed_everything(seed)
|
48 |
+
start_time = time.time()
|
49 |
+
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image, num_inference_steps=steps, denoising_end=0.8, strength=0.80, output_type="latent").images
|
50 |
+
print(time.time() - start_time)
|
51 |
+
|
52 |
+
if use_refiner:
|
53 |
+
image = refiner(prompt=prompt, image=image, mask_image=mask_image, num_inference_steps=steps, denoising_start=0.8).images[0]
|
54 |
+
|
55 |
+
file_name = f"aaa_1"
|
56 |
+
path = os.path.join(Path.home(), "images", "ediffi_sdxl", f"{file_name}.png")
|
57 |
+
image.save(path)
|
58 |
+
|
59 |
+
api.upload_file(
|
60 |
+
path_or_fileobj=path,
|
61 |
+
path_in_repo=path.split("/")[-1],
|
62 |
+
repo_id="patrickvonplaten/images",
|
63 |
+
repo_type="dataset",
|
64 |
+
)
|
65 |
+
print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
|