← Latest papers
🤖 AI

MAGIC: Few-Shot Mask-Guided Anomaly Inpainting with Prompt Perturbation, Spatially Adaptive Guidance, and Context Awareness

The paper proposes MAGIC, a few-shot anomaly generation framework that leverages Gaussian prompt perturbation, spatially adaptive guidance, and context-aware mask alignment to produce high-fidelity, diverse anomalies for robust industrial quality control.

Original authors: JaeHyuck Choi, MinJun Kim, Je Hyeong Hong

Published 2026-04-30
📖 4 min read☕ Coffee break read

Original authors: JaeHyuck Choi, MinJun Kim, Je Hyeong Hong

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you are a quality control inspector at a factory. Your job is to spot defects on products like screws, pills, or circuit boards. The problem? You have thousands of pictures of perfect products, but almost no pictures of broken ones. You can't train your AI to spot a broken screw if it has never seen one.

To solve this, scientists usually try to "fake" broken images using computers. But existing methods have two big problems:

  1. The "Global" approach: It tries to invent a broken product from scratch. It often gets the defect right but accidentally ruins the rest of the product (like turning a perfect screw into a melted blob).
  2. The "Mask" approach: It takes a perfect product and paints a defect onto a specific spot. But it's too rigid. It only learns to make one specific type of scratch, and if you tell it to paint a scratch in the wrong place (like on the head of a screw when it should be on the side), it creates a weird, unrealistic image.

Enter MAGIC (Mask-Guided Anomaly Inpainting with Prompt Perturbation, Spatially Adaptive Guidance, and Context Awareness). Think of MAGIC as a master forger who can create realistic, diverse "broken" products without messing up the good parts.

Here is how MAGIC works, using three simple tricks:

1. The "Creative Stretch" (Gaussian Prompt Perturbation)

Imagine you are teaching an artist to draw a "scratch." If you only show them one photo of a scratch, they will memorize it exactly and draw the same scratch every time. That's boring and not helpful.

MAGIC uses a technique called Gaussian Prompt Perturbation. Think of this as giving the artist a slightly blurry, fuzzy instruction instead of a sharp, rigid one.

  • How it works: Instead of saying "Draw a scratch exactly like this," the system adds a little bit of "noise" or fuzziness to the instruction during both the learning phase and the drawing phase.
  • The Result: The artist learns the concept of a scratch rather than just copying one photo. This allows them to draw thousands of different scratches (long, short, deep, shallow) that all look real, rather than just repeating the same one.

2. The "Smart Brush" (Spatially Adaptive Guidance)

When you paint a defect onto a perfect product, you want the defect to look wild and varied, but you want the rest of the product to stay perfectly normal.

  • The Problem: Standard AI tools use the same "strictness" for the whole image. If they are too strict, the defect looks boring. If they are too loose, the background gets ruined.
  • The MAGIC Solution: They use a Spatially Adaptive Guidance brush.
    • On the defect area: The brush is "loose." It encourages the AI to be creative and try many different textures and patterns for the scratch.
    • On the background: The brush is "tight." It forces the AI to keep the background exactly as it was, ensuring the perfect parts of the screw or pill don't get distorted.
  • The Result: You get a highly varied, realistic defect that sits perfectly on a pristine background.

3. The "Context Detective" (Context-Aware Mask Alignment)

Sometimes, the user gives the AI a mask (a stencil) in the wrong place. For example, they might try to put a scratch on the very tip of a screw, but scratches usually happen on the head. If the AI just paints there, it looks fake.

MAGIC has a Context-Aware Mask Alignment module. Think of this as a detective who knows how objects work.

  • How it works: Before painting, the system looks at the object. It says, "Hey, you want to put a scratch here, but that's the smooth tip of the screw. Scratches usually happen on the head."
  • The Action: It automatically slides the stencil (the mask) to a semantically correct spot (the screw head) before painting.
  • The Result: The defect appears in a logical, realistic location, making the fake image indistinguishable from a real broken product.

The Big Picture

By combining these three tricks, MAGIC creates a massive library of "fake" broken products that are:

  • Diverse: They look different every time (not just copies).
  • Realistic: The defects look genuine, and the background stays perfect.
  • Logical: The defects appear in the right places.

The paper shows that when factories use these "fake" images to train their AI inspectors, the inspectors become much better at spotting real defects in the real world. MAGIC essentially teaches the AI what "broken" looks like by showing it a million different, realistic ways a product can break, without ever needing a real broken product to start with.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →