Image-to-Image Translation with Diffusion
Module: Generative AI | Difficulty: Advanced
SDEdit (Img2Img)
- Add noise to input image:
- Denoise from to
Strength Parameter
- Low strength: preserve input structure
- High strength: creative variation
DDIM Inversion
Invert a real image to noise, then denoise with different conditioning.
def img2img(model, image, text, strength=0.75, steps=50):
scheduler.set_timesteps(steps)
init_timestep = int(steps * strength)
t_start = steps - init_timestep
noise = torch.randn_like(image)
t = scheduler.timesteps[t_start]
latent = scheduler.add_noise(vae.encode(image), noise, t)
for t in scheduler.timesteps[t_start:]:
latent = scheduler.step(model, latent, t, text)
return vae.decode(latent)
Research Insight: SDEdit's quality depends heavily on the noise level. Too little noise preserves artifacts; too much noise loses the input structure. The optimal strength is typically 0.5-0.8 for style transfer.