ReFo
Collection
Background Editing model • 1 item • Updated
How to use esalahterus/refo with Diffusers:
pip install -U diffusers transformers accelerate
import torch
from diffusers import AutoPipelineForInpainting
from diffusers.utils import load_image
# switch to "mps" for apple devices
pipe = AutoPipelineForInpainting.from_pretrained("esalahterus/refo", dtype=torch.float16, device_map="cuda")
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).resize((1024, 1024))
mask_image = load_image(mask_url).resize((1024, 1024))
prompt = "a tiger sitting on a park bench"
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, # steps between 15 and 30 work well for us
strength=0.99, # make sure to use `strength` below 1.0
generator=generator,
).images[0]Fine-tuned checkpoint of diffusers/stable-diffusion-xl-1.0-inpainting-0.1 for background editing — replacing a photo's background via text-guided inpainting while preserving the original foreground subject.
Merged checkpoint: a LoRA trained on synthetic background-replacement pairs has been fused directly into the UNet weights. The model is ready to use as-is, no separate adapter loading required.
diffusers/stable-diffusion-xl-1.0-inpainting-0.1import torch
from PIL import Image
from diffusers import StableDiffusionXLInpaintPipeline
pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
"esalahterus/refo", torch_dtype=torch.bfloat16
).to("cuda")
source_image = Image.open("path/to/your_image.jpg").convert("RGB")
mask_image = Image.open("path/to/your_mask.png").convert("L") # white = area to edit, black = area to keep
result = pipe(
prompt="a high quality photo background, a quiet beach at sunset, photorealistic, detailed, no people, no text",
negative_prompt="low quality, blurry foreground, distorted subject, watermark, text",
image=source_image,
mask_image=mask_image,
num_inference_steps=30,
guidance_scale=7.5,
strength=1.0, # important: use exactly 1.0 — values like 0.99 only blend lightly instead of fully regenerating the masked area
generator=torch.Generator(device="cuda").manual_seed(0), # optional, for reproducible results
).images[0]
result.save("output.png")
No mask image? You can auto-generate a foreground mask with rembg:
from rembg import remove, new_session
session = new_session("u2net")
fg_mask = remove(source_image, session=session, only_mask=True).convert("L")
mask_image = Image.eval(fg_mask, lambda x: 255 - x) # invert so white = background
strength must be set to exactly 1.0 for the mask region to be fully regenerated; lower values (e.g. 0.99) result in only a light blend and largely ignore the prompt.Follows the license of the base model diffusers/stable-diffusion-xl-1.0-inpainting-0.1.
Base model
stabilityai/stable-diffusion-xl-base-1.0