Reference-based Anomaly Image Generation via Inpainting
This project generates realistic anomaly images by transferring defect patterns from a reference anomaly image onto a normal (background) image within a user-defined inpainting mask region.
Reference Anomaly Segmentation
By analyzing the model's attention maps over the reference anomaly image, we can directly localize and segment defect regions.
- Installation
- Model Checkpoints
- Model Inference (Anomaly Generation)
- Gradio Web UI
- Attention Visualization (Anomaly Segmentation)
- Acknowledgments
- Python 3.10+
- uv (for automated setup)
python prepare_environment.pyThis will:
- Install
uvvia pip - Create a Python 3.10 virtual environment in
.venv/ - Install all dependencies from
requirement.txt
Then select the appropriate activation command based on your operating system and terminal:
source .venv/bin/activate.venv\Scripts\activate.bat.venv\Scripts\Activate.ps1After successful activation, you will see (.venv) prefix in your terminal prompt, indicating that you are now working inside the virtual environment.
No need to download them in advance. Models are automatically downloaded from HuggingFace Hub on first inference:
| Model | Source |
|---|---|
| Stable Diffusion v1.5 Inpainting | stable-diffusion-v1-5/stable-diffusion-inpainting |
| Stable Diffusion v1.5 (for ReferenceNet) | stable-diffusion-v1-5/stable-diffusion-v1-5 |
| CLIP ViT-H/14 Image Encoder | laion/CLIP-ViT-H-14-laion2B-s32B-b79K |
| Fine-tuned Checkpoint | LiXiY/ReferenceAnomaly |
python model_inference.py \
--background_image_path validation_images/background_image_2.png \
--inpainting_mask_path validation_images/inpainting_mask_2.png \
--ref_image_path validation_images/ref_image_2.png \
--save_dir visualization_results/The generated image will be saved to visualization_results/inference_image.png.
An interactive web interface is provided for easy experimentation.
python gradio_demo.pyThe server starts at http://localhost:7860. Open it in your browser to access the UI.
- Upload a normal background image via the left editor panel.
- Draw the inpainting mask on the background using the brush tool (use the slider to adjust brush size).
- Upload a reference anomaly image via the right panel.
- Click Generate — the output image and attention map will appear below.
Tip: Click any row in the "Examples" section to auto-load a complete set of inputs.
Visualize how the model attends to the reference image during generation, We can leverage this attention mechanism to perform segmentation on reference anomaly images.
python reference_anomaly_attention_map_visualizaiton.pyOutput is saved to visualization_results/reference_image_attention_map.png.
- Built on HuggingFace Diffusers
- Reference attention mechanism inspired by MagicAnimate and MimicBrush


