File size: 4,977 Bytes
c8c00f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | # DefectFill: Realistic Defect Generation for Visual Inspection
Realistic defect image generation via fine-tuned inpainting diffusion models.
> Implementation of **DefectFill: Realistic Defect Generation with Inpainting Diffusion Model for Visual Inspection** (CVPR 2024).
---
Currently, this repository is tuned to generate **cracks in concrete** (using the MVTec AD dataset) as a proof-of-concept. The ultimate goal of this project is to apply these techniques to generate synthetic training data for **cast iron defects** (e.g., blowholes, cracks) in foundry settings.
## Visual Results
Below are generated examples showing the model's ability to fill healthy regions with realistic defect textures while preserving the surrounding structural integrity.
|||
| :---: | :---: |
|  |  |
|  |  |
|  |  |
---
## Overview
DefectFill fine-tunes a Stable Diffusion 2 inpainting model with LoRA to learn a specific defect concept from a small set of reference images. Three complementary loss terms drive training:
| Loss | Weight | Purpose |
|------|--------|---------|
| **Defect loss** `L_def` | 0.5 | Precisely captures intrinsic defect features |
| **Object loss** `L_obj` | 0.2 | Learns the semantic relationship between defect and object |
| **Attention loss** `L_attn` | 0.05 | Ensures [V*] token attends to the defect region |
After training, **Low-Fidelity Selection (LFS)** generates 8 candidates per (image, mask) pair and selects the one with the highest LPIPS score inside the masked region β the most "realistic" defect.
---
## Installation
1. Clone the repository:
```bash
git clone [https://github.com/axelsig1/defectfill.git](https://github.com/axelsig1/defectfill.git)
cd defectfill
```
2. Install the required dependencies:
```bash
pip install -r requirements.txt
```
**Requirements include:** `torch`, `diffusers`, `transformers`, `peft`, `lpips`, and `albumentations`.
---
## Data Preparation
This project follows the **MVTec AD** dataset structure. Ensure your data is organized as follows:
```
data/
βββ concrete/ # Object Class
βββ train/
β βββ defective/
β β βββ crack/ # Defect images
β βββ defective_masks/
β βββ crack/ # Corresponding binary masks
βββ test/
βββ good/ # Healthy reference images
```
## Usage
### 1. Training
To train the model on concrete cracks:
```bash
python train.py \
--data_dir ./data \
--object_class concrete \
--defect_type crack \
--output_dir ./output_concrete \
--lora_rank 8 \
--lora_alpha 16 \
--max_train_steps 2000
```
Key training details (from paper):
- **Base model**: `sd2-community/stable-diffusion-2-inpainting`
- **LoRA** on UNet attention layers + text encoder projection matrices
- **Warmup**: linear 0 β LR over first 100 steps
- **Augmentation**: random resize Γ[1.0, 1.125] + random crop
- **Random masks** M_rand: 30 boxes, sides 3β25% of image size
- **[V*] token**: the word `sks`
### 2. Inference
Generate new synthetic defects on healthy images. The script uses LPIPS to pick the best generation from a batch of candidates.
```bash
python inference.py \
--checkpoint ./output_concrete/checkpoints/checkpoint_final.pt \
--object_class concrete \
--defect_type crack \
--data_dir ./data \
--output_dir ./generated_cracks \
--total_images 6 \
--num_samples 8 \
--guidance_scale 2.0
```
---
## Method Details
### Defect Loss (Eq. 5)
```
L_def = E[ || M β (Ξ΅ β Ξ΅_ΞΈ(x_t^def, t, c^def)) ||Β² ]
```
Background image: `B_def = (1 β M) β I`
Input: `x_t^def = concat(x_t, b_def, M)`
Prompt `P_def = "A photo of sks"`
### Object Loss (Eq. 7)
```
L_obj = E[ || M' β (Ξ΅ β Ξ΅_ΞΈ(x_t^obj, t, c^obj)) ||Β² ]
M' = M + Ξ±Β·(1 β M), Ξ± = 0.3
```
Random box mask M_rand (30 boxes), `B_rand = (1 β M_rand) β I`
Input: `x_t^obj = concat(x_t, b_rand, M_rand)`
Prompt `P_obj = "A <object> with sks"`
### Attention Loss (Eq. 8)
```
L_attn = E[ || A_t^[V*] β M ||Β² ]
```
Cross-attention maps from UNet **decoder** (up_blocks) only, averaged over layers and resized to latent resolution.
### Combined Loss (Eq. 9)
```
L_ours = 0.5Β·L_def + 0.2Β·L_obj + 0.05Β·L_attn
```
---
## Citation
```bibtex
@inproceedings{song2024defectfill,
title={DefectFill: Realistic Defect Generation with Inpainting Diffusion Model for Visual Inspection},
author={Song, Jaewoo and Park, Daemin and Baek, Kanghyun and Lee, Sangyub and Choi, Jooyoung and Kim, Eunji and Yoon, Sungroh},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
year={2024}
}
```
|