|
|
|
from diffusers import AutoPipelineForInpainting, AutoPipelineForImage2Image |
|
from diffusers.utils import load_image |
|
import torch |
|
from pathlib import Path |
|
import os |
|
from huggingface_hub import HfApi |
|
|
|
torch.backends.cuda.matmul.allow_tf32 = True |
|
torch_device = "cuda" if torch.cuda.is_available() else "cpu" |
|
api = HfApi() |
|
|
|
pipe = AutoPipelineForInpainting.from_pretrained("runwayml/stable-diffusion-inpainting", torch_dtype=torch.float16, variant="fp16", use_safetensors=True) |
|
|
|
pipe = pipe.to(torch_device) |
|
pipe.enable_xformers_memory_efficient_attention() |
|
|
|
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" |
|
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" |
|
|
|
image = load_image(img_url) |
|
mask_image = load_image(mask_url) |
|
|
|
prompt = "dslr photography of an empty bench, high quality" |
|
generator = torch.Generator(device="cuda").manual_seed(0) |
|
|
|
image = pipe( |
|
prompt=prompt, |
|
image=image, |
|
mask_image=mask_image, |
|
guidance_scale=8.0, |
|
num_inference_steps=20, |
|
generator=generator, |
|
).images[0] |
|
|
|
|
|
image = image.resize((1024, 1024)) |
|
|
|
pipe = AutoPipelineForInpainting.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True) |
|
pipe.to("cuda") |
|
|
|
pipe.enable_xformers_memory_efficient_attention() |
|
|
|
image = pipe( |
|
prompt=prompt, |
|
image=image, |
|
mask_image=mask_image, |
|
guidance_scale=8.0, |
|
num_inference_steps=100, |
|
strength=0.2, |
|
generator=generator, |
|
).images[0] |
|
|
|
pipe = AutoPipelineForImage2Image.from_pipe(pipe) |
|
pipe.enable_xformers_memory_efficient_attention() |
|
|
|
image = pipe( |
|
prompt=prompt, |
|
image=image, |
|
guidance_scale=8.0, |
|
num_inference_steps=100, |
|
strength=0.2, |
|
generator=generator, |
|
).images[0] |
|
|
|
file_name = f"aaa" |
|
path = os.path.join(Path.home(), "images", "ediffi_sdxl", f"{file_name}.png") |
|
image.save(path) |
|
|
|
api.upload_file( |
|
path_or_fileobj=path, |
|
path_in_repo=path.split("/")[-1], |
|
repo_id="patrickvonplaten/images", |
|
repo_type="dataset", |
|
) |
|
print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png") |
|
|