tools / benchmark /sd_inpaint.py
patrickvonplaten's picture
test all
7f2323b
raw
history blame
1.69 kB
#!/usr/bin/env python3
from diffusers import StableDiffusionInpaintPipeline
import requests
import torch
from PIL import Image
from io import BytesIO
import os
from pathlib import Path
from huggingface_hub import HfApi
api = HfApi()
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
def download_image(url):
response = requests.get(url)
return Image.open(BytesIO(response.content)).convert("RGB")
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"
init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))
path = "runwayml/stable-diffusion-inpainting"
run_compile = True # Set True / False
pipe = StableDiffusionInpaintPipeline.from_pretrained(path, torch_dtype=torch.float16)
pipe = pipe.to("cuda:0")
torch.manual_seed(33)
prompt = "A cute dog"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image, force_unmasked_unchanged=True).images[0]
# image = pipe(prompt=prompt, image=init_image, mask_image=mask_image, force_unmasked_unchanged=False).images[0]
file_name = "aa"
path = os.path.join(Path.home(), "images", 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")