UniDG: Unified Defect Generation via Defect-Context Editing

Yuanting Fan, Jun Liu, Bin-Bin Gao, Xiaochen Chen, Yuhuan Lin, Zhewei Dai, Jiawei Zhan, Chengjie WangNeurIPS 2026

TL;DR

Training AI to spot manufacturing flaws or medical anomalies is notoriously hard because defects are rare—you simply don't have enough broken examples to learn from. UniDG solves this by treating defect synthesis as a local "copy-and-adapt" editing task: give it a single photo of a defect and a target normal image, and it realistically grafts that flaw onto the new object without any category-specific fine-tuning. Backed by a massive new 300K-sample dataset, it generates realistic synthetic anomalies that significantly boost downstream defect detectors.

What Problem Does This Paper Address?

In industrial inspection and medical imaging, normal data is cheap, while anomalies (cracks, rust, lesions) are scarce. Synthesizing fake defects is the obvious workaround, but existing generators hit three walls:

  1. Few-shot generators overfit: Models trained on a handful of scratched metal plates learn "scratched metal plate," not the transferable concept of a "scratch." Moving to wood or plastic requires retraining from scratch.
  2. General image editors miss the details: Off-the-shelf diffusion editors excel at broad semantic swaps ("turn the dog into a cat"), not subtle, mask-precise texture disruptions.
  3. No paired data: Existing datasets give you defective photos, not the exact pixel-aligned normal twin showing what that specific object looked like before it broke.
Existing defect-generation paradigms vs. UniDG

Methods

Instead of generating whole images from scratch (which introduces background artifacts), UniDG formulates the task as Defect-Context Editing built on standard Multimodal Diffusion Transformer (MM-DiT / FLUX.1) backbones and Rectified Flow matching.

Here is what is new:

  • Diptych Input Canvas: UniDG adaptively crops the defect from a reference image and places it side-by-side with the masked target image into a single two-panel canvas (a diptych). The MM-DiT processes both halves simultaneously via multimodal attention, alongside SigLIP semantic embeddings of the reference defect.
  • Defect Enhancements: During training, normal regularization forces the edited pixels to diverge from the clean counterpart, while defect attention boosts attention weights between the reference flaw and target mask.
  • Two-Stage Training:
  • Diversity-SFT (Supervised Fine-Tuning) mixes three data pairings: reconstructing a defect on the exact same image, transferring it to a different image of the same object, and transferring it across entirely different objects (e.g., putting a fabric stain onto a wooden board).
  • Consistency-RFT (Reinforcement Fine-Tuning) is an optional online alignment stage using Flow-GRPO. Two custom reward models—scoring visual realism/adherence via an MLLM and category accuracy via a classifier/segmenter—nudge the generator away from color or lighting drift.
Two-stage training pipeline: Diversity-SFT and Consistency-RFT

Datasets

  • UDG (Newly Introduced): Aggregated from 50 public datasets across industrial, natural, and medical domains, UDG contains 300K normal-abnormal-mask-caption quadruplets spanning 28 standardized defect categories (23× larger than the recent AnomVerse dataset). Because real datasets lack clean counterparts, the authors built a three-agent pipeline: an Inpainting Agent heals the defect to create a normal twin, a Captioner Agent writes structured multi-level descriptions, and a Verifier Agent filters out bad pairs.
Overview of the UDG multi-agent curation pipeline and distributions
  • MVTec-AD & VisA (Evaluation): Standard industrial anomaly benchmarks. Crucially, both are strictly excluded from UDG so UniDG is tested zero-shot on unseen objects.

Key Insights and Ideas

  • Defects are transferable visual primitives: A "scratch" or "hole" shares structural DNA across materials. Training with cross-object pairs forces the model to disentangle what a defect does to a surface from the object itself—even when overlapping defect categories are scrubbed from the training set, performance barely drops.
  • Diversity metrics can lie about quality: After applying reinforcement learning (Consistency-RFT), LPIPS diversity scores actually drop, while human preference and downstream detection accuracy go up. Why? Because RFT stops the model from hallucinating wild, off-category textures, proving that tighter adherence to the reference defect matters more than raw pixel variance.
  • SFT does the heavy lifting; RFT is the polish: Ablations reveal that data formulation and Diversity-SFT capture the vast majority of downstream detection gains, whereas RFT acts as a perceptual alignment pass for visual realism and exact category matching.

Key Results

Without ever training on MVTec-AD (using just one retrieved reference image at inference), UniDG beats category-tuned few-shot generators and FLUX-based insertion baselines trained on the exact same data:

  • Single-Class Classification & Localization: Improves within-object defect classification accuracy to 77.4% (nearly +9 points over AnomalyDiffusion's 68.4%) and hits 98.4% image-level AUROC and 77.3% pixel-level AP.
  • Unified Multi-Class Segmentation: When training a single segmenter across all 73 MVTec-AD defect classes, UniDG-RFT boosts foreground mIoU by +20.5% over AnoGen (reaching 31.87%) and PRO-P by +11.4% over AnomalyDiffusion (91.12%).
  • Human Preference: In a 30-person study, users preferred UniDG-RFT over the SFT model on reference adherence 69.2% of the time (with 26.7% ties, and only 4.1% losses).

Limitations and Future Work

  • Heavy compute footprint: The default FLUX backbone packs 12B parameters and runs 28 denoising steps, making synthesis resource-intensive (though the authors explore a 3.2B mini variant and fewer steps in the appendix).
  • Imperfect generative metrics: Standard distribution metrics like KID and IS correlate poorly with downstream utility; per-category baselines still beat UniDG on KID simply because they memorize target-domain statistics.
  • Extreme mismatches: Synthesis can still break down under severe mismatches in scale, lighting, or surface texture between the reference defect and the target scene.

Summary updated

Abstract

Generating realistic visual defects is crucial for alleviating the scarcity of abnormal samples in anomaly detection, yet existing methods often rely on category-specific few-shot adaptation and struggle to generalize across objects, defect types, and domains. This limitation stems in part from the lack of paired defect editing data and the fine-grained nature of defect appearance, where subtle variations in scale, texture, and morphology must be preserved while maintaining consistency with the target scene. To address this challenge, we introduce *UDG*, a dataset of 300K normal-abnormal-mask-caption quadruplets curated from diverse defect-related scenarios, and present **UniDG**, a unified defect generation model. UniDG formulates defect synthesis as Defect-Context Editing: it extracts reference defect context with adaptive cropping, organizes reference and target inputs in a structured diptych format, and fuses multimodal conditions through MM-DiT attention. We further develop a two-stage training strategy: ***Diversity-SFT*** learns diverse transferable defect priors from paired editing data, while optional ***Consistency-RFT*** can further improve reference adherence, local realism, and defect-category consistency. Without per-category fine-tuning or using MVTec-AD/VisA for training, UniDG outperforms prior few-shot anomaly generation and image insertion/editing baselines in both synthesis quality and downstream single- and multi-class anomaly detection/localization.
BibTeX
@inproceedings{fan2026unidg,
  author = {Yuanting Fan and Jun Liu and Bin-Bin Gao and Xiaochen Chen and Yuhuan Lin and Zhewei Dai and Jiawei Zhan and Chengjie Wang},
  title = {{UniDG: Unified Defect Generation via Defect-Context Editing}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=A3soBL3Ciu},
}
Figure