Using Autoencoders for Industrial Defect Detection

This article explains how to train a simple fully‑connected autoencoder on defect‑free images, use reconstruction error to highlight anomalies in industrial parts, and convert the error into a single metric that cleanly separates good from defective components.

AI Algorithm Path
AI Algorithm Path
AI Algorithm Path
Using Autoencoders for Industrial Defect Detection

Problem

Defect detection on production lines suffers from severe class imbalance: most images are of good parts, defective samples are scarce. Traditional computer‑vision methods require quantifiable defects, which is difficult under this imbalance.

Methodology

Train an autoencoder only on defect‑free images so it learns the distribution of normal parts. When a defective image is passed through the trained model, reconstruction errors appear, providing a decision boundary between normal and anomalous parts.

Dataset

Public Kaggle dataset of good and defective brake‑caliper castings.

https://www.kaggle.com/datasets/ravirajsinh45/real-life-industrial-dataset-of-casting-product

Autoencoder implementation

stacked_encoder = Sequential([
    layers.Input(img.shape),  # img.shape is 300 x 300 pixels
    layers.Flatten(),
    layers.Dense(100, activation="relu"),
    layers.Dense(30, activation="relu"),
])
stacked_decoder = Sequential([
    layers.Dense(100, activation="relu"),
    layers.Dense(img.shape[0] * img.shape[1] * img.shape[2], activation="sigmoid"),
    layers.Reshape([img.shape[0], img.shape[1], img.shape[2]]),
])
stacked_ae = Sequential([stacked_encoder, stacked_decoder])
stacked_ae.compile(loss="mse", optimizer="nadam")
stacked_ae.summary()

Training

history = stacked_ae.fit(X_good, X_good, epochs=200)

X_good contains only defect‑free images and is split into training and validation sets.

Reconstruction comparison

Reconstructed images for good and defective parts show visual differences. Difference images are obtained by subtracting the reconstruction from the original and clipping to [0, 1].

Feature extraction

def get_diff_sums(imgs, autoenc):
    Y = autoenc.predict(imgs)          # reconstructions
    D = Y - imgs                       # differences
    sums = []
    for diff in D:
        sums.append(np.abs(np.std(diff)))  # absolute standard deviation per image
    return np.array(sums)

good = get_diff_sums(X_good, stacked_ae)
bad  = get_diff_sums(X_bad,  stacked_ae)

The absolute standard deviation of the difference image yields a single positive scalar per sample. Plotting these values shows a clear separation between good and defective parts.

Result

The separation indicates that a simple downstream classifier can be built on the scalar metric derived from reconstruction error.

Conclusion

Autoencoders effectively detect image‑based defects when the dataset is dominated by normal samples. Training on normal images and using the absolute standard deviation of reconstruction error provides a robust, positive‑valued feature that separates good from defective parts, enabling subsequent classification.

computer visionPythonanomaly detectionKerasautoencoderdefect detection
AI Algorithm Path
Written by

AI Algorithm Path

A public account focused on deep learning, computer vision, and autonomous driving perception algorithms, covering visual CV, neural networks, pattern recognition, related hardware and software configurations, and open-source projects.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.