This project implements image inpainting using diffusion models, offering both RePaint's DDPM sampling and accelerated DDIM sampling. We build upon the guided-diffusion framework and integrate SAM (Segment Anything Model) for interactive mask generation, creating a powerful and user-friendly inpainting solution.
A detailed project report is included in docs/
Link to the presentation: ECE 285 Video Presentation
| Method | Steps | Time per Image | Quality |
|---|---|---|---|
| DDPM | 1000 | ~15 minutes | Highest |
| DDIM | 20-50 | <20 seconds | Excellent |
Our implementation combines three key technologies:
-
RePaint Implementation (DDPM):
- Full 1000-step diffusion for highest quality results
- Resampling strategy for complex inpainting
- Ideal for challenging cases requiring maximum quality
- Runtime: ~15 minutes per image on NVIDIA A40
-
DDIM Fast Sampling:
- Accelerated sampling with as few as 20 steps
- Excellent results in under 20 seconds on NVIDIA A40
- Perfect for quick iterations and real-time applications
-
Interactive Mask Generation:
- Integration with Meta's Segment Anything Model (SAM)
- Point-and-click object selection
- Multiple mask options with real-time preview
- Adjustable mask refinement
-
Flexible Sampling Strategy:
- DDPM (RePaint): 1000 steps, ~14 minutes, highest quality
- DDIM: 20-50 steps, <20 seconds, excellent quality
- User choice between speed and quality at runtime
-
Advanced Mask Handling:
- SAM-powered interactive selection
- Multiple mask options for best results
- Mask dilation for better boundaries
- Support for arbitrary shapes and sizes
-
User-Friendly Interface:
- Simple command-line interface
- Visual feedback for mask selection
- Progress visualization with evolution GIFs
- Easy switching between sampling methods
-
Setup:
# Clone the repository and switch to DDIM branch git clone [repository-url] cd [repository-name] git checkout ddim # Install dependencies pip install -r requirements.txt # Download required models # - SAM checkpoint (sam_vit_h_4b8939.pth) should be in models/ # - Diffusion model (256x256_diffusion_uncond.pt) should be in models/
-
Usage:
# Place your image in the data/ directory cp your_image.jpg data/ # Run the magic eraser python src/magic_eraser.py
The script will prompt you for:
- Image selection
- Number of sampling steps (T) - recommended range: 20-200
- Number of iterations (U) - recommended range: 1-10
-
Results: Results will be saved in
results/inpainting_tests/[image_name]/:mask.png: The generated maskinpainted.png: The final inpainted resultevolution.gif: Visualization of the inpainting process
-
Sampling Steps (T):
- Range: 20-200 steps
- Lower values (20-50): Faster but potentially lower quality
- Higher values (100-200): Better quality but slower
- Default recommendation: 50 steps for a good speed/quality balance
-
Iterations (U):
- Range: 1-10 iterations
- Lower values (1-3): Faster, suitable for simple masks
- Higher values (5-10): Better for complex masks
- Default recommendation: U=5 for most cases
The SAM-based mask generation offers:
- Interactive point selection
- Multiple mask options
- Adjustable mask dilation
- Option to retry if not satisfied
For advanced users who want to experiment with different parameters:
- Batch Testing:
python src/run_ddim_test.py
- Test multiple T values (20, 50, 100, 200)
- Compare results across different parameters
- Automated processing of multiple images
This animation shows a sequence of our inpainting results. For each example:
- Original image
- Region to be inpainted (highlighted in red)
- Final inpainted result
DDPM results with different U values (1000 timesteps)
DDIM results with different T values (much fewer timesteps)
This GIF shows the complete inpainting process:
- Original image
- Mask overlay showing the region to be inpainted
- Evolution of the inpainting process from noise to final result
- DDIM achieves comparable or better results in just 20 iterations (~20 seconds)
- DDPM requires 1000 iterations (>15 minutes) for similar results
- Successfully adapted RePaint's algorithm to work with DDIM sampling
- Interactive applications become feasible with DDIM's speed
This project builds upon several key works:

