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 DiffusionPipeline
from diffusers.utils import load_image
# switch to "mps" for apple devices
pipe = DiffusionPipeline.from_pretrained("esalahterus/refo", dtype=torch.bfloat16, device_map="cuda")
prompt = "Turn this cat into a dog"
input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png")
image = pipe(image=input_image, prompt=prompt).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