Outpainting with Diffusion Models
Module: Generative AI | Difficulty: Advanced
Outpainting Problem
Extend image beyond boundaries while maintaining coherence.
Boundary Conditioning
Use the boundary region as conditioning.
Autoregressive Outpainting
- Generate left/right/top/bottom strip
- Use generated strip as new boundary
- Repeat
Coherence via Attention
def outpaint(model, image, n_steps=4, direction='right'):
result = image.clone()
for _ in range(n_steps):
h, w = result.shape[2:]
if direction == 'right':
canvas = torch.zeros(1, 3, h, w*2)
canvas[:, :, :, :w] = result
mask = torch.zeros(1, 1, h, w*2)
mask[:, :, :, :w] = 1
# Inpaint the blank region
result = inpaint_step(model, canvas, mask, result, t=500)
return result
Research Insight: Outpainting works best when the image has strong structure (e.g., landscapes, architecture). Random textures (e.g., grass, clouds) are easier than structured content (e.g., faces, text).