4. CNN fundamentals for inspection

In a rule-based inspection pipeline, an engineer writes explicit thresholds: gradient magnitude above 30, blob area between 12 and 400 pixels, elongation below 0.6. A convolutional neural network (CNN) replaces that hand-crafted logic with learned filters. For surface inspection — scratches, pits, dents, stains, orange-peel texture, edge chips — this matters because defect appearance is dominated by local texture statistics rather than by a single measurable geometric primitive. A CNN learns which local patterns separate "acceptable brushed metal" from "faint longitudinal scratch," and it does so hierarchically: edges first, then texture motifs, then defect-scale structures.

Convolution as a Learned Matched Filter

A convolution layer slides a small kernel K of size k×k (typically 3×3 or 5×5) across the input feature map and computes a dot product at every position:

y[i, j] = Σ_m Σ_n x[i+m, j+n] · K[m, n] + b

Three hyperparameters define the output geometry. Padding p controls whether the spatial size shrinks; stride s controls the sampling step; dilation d inserts gaps in the kernel. The output size for a square input of side W is:

W_out = floor((W + 2p − d·(k − 1) − 1) / s) + 1

For a 512×512 grayscale surface image with k=3, p=1, s=1, d=1, the output stays 512×512 — the standard "same" convolution used in inspection backbones so that fine defect detail is not lost early. A 3×3 kernel with p=1 has a receptive field of exactly 3×3 at layer 1, but the effective receptive field grows as layers stack.

What the kernel actually learns is instructive. In the first layer of a network trained on rolled-steel surface data, filters typically converge to oriented edge detectors (horizontal, vertical, ±45°), Gabor-like texture probes, and center-surround blobs. These are precisely the primitives a human inspector's visual system uses when scanning a surface under grazing illumination. The difference is that the CNN learns dozens or hundreds of them simultaneously and weights them by their discriminative value for the specific defect classes in the training set.

Pooling, Stride, and Translation Tolerance

Pooling reduces spatial resolution and introduces local translation invariance. Max pooling with a 2×2 window and stride 2 halves each dimension, keeping the strongest activation in each window. For inspection this is a double-edged tool:

  • Benefit: a scratch shifted by 1–2 pixels still produces the same pooled response, so the classifier is robust to minor registration error between the camera and the part.
  • Risk: a 2-pixel-wide hairline crack can vanish after two or three pooling stages. If the defect is smaller than the accumulated stride, it is gone before the classifier sees it.

This is why high-resolution surface inspection often uses strided convolutions instead of max pooling in the early stages, or keeps the first two stages at full resolution and only downsamples after the defect scale has been captured. A practical rule: the smallest defect dimension in pixels should be at least 4–8× the total downsampling factor of the network before the classification head.

Receptive Field: Why Depth Matters

The receptive field (RF) of a neuron is the region of the input image that can influence its activation. For a stack of L layers each with kernel k and stride 1, the RF grows linearly:

RF = 1 + L · (k − 1)

With 3×3 kernels, ten layers give RF = 1 + 10·2 = 21 pixels. With stride-2 layers interleaved, the growth is multiplicative and the RF expands much faster. This is the core design tension in inspection CNNs:

Defect typeTypical pixel extentRequired RFImplication
Hairline scratch1–3 px wide, 50–500 px longSmall in width, large in lengthAnisotropic kernels or elongated pooling
Pit / pinhole3–10 px diameter~15–30 px3–4 conv stages suffice
Dent / depression30–120 px~150–300 pxDeep backbone or dilated convs
Texture anomaly (orange peel)Global statisticalWhole patchGlobal pooling or large-RF stage

A network with a 21-pixel RF cannot "see" a 100-pixel dent as a single object; it only sees local edge fragments. If your defect class requires global shape judgment, either deepen the network, use dilated convolutions (which expand RF without adding parameters), or add a global average pooling branch that aggregates texture statistics across the entire patch.

Feature Extraction in Practice: A Worked Inspection Example

Consider a brushed aluminum surface inspected for three defect classes: longitudinal scratch, circular pit, and stain. Input is 512×512 grayscale, 8-bit, captured under dark-field illumination so that scratches appear bright against a dark background.

A minimal but effective backbone for this task:

Input: 512×512×1
Conv 3×3, 16 filters, stride 1, pad 1  → 512×512×16
MaxPool 2×2, stride 2                  → 256×256×16
Conv 3×3, 32 filters, stride 1, pad 1  → 256×256×32
MaxPool 2×2, stride 2                  → 128×128×32
Conv 3×3, 64 filters, stride 1, pad 1  → 128×128×64
Global Average Pooling                 → 64
FC 64→3, softmax                       → class probabilities

After three conv stages and two pooling stages, the RF is approximately 1 + 2·(3−1) + 2·(3−1) + 2·(3−1) = 13 pixels at the deepest conv layer, but the global average pooling aggregates across the entire 128×128 map, giving the classifier access to whole-image texture statistics. This is a deliberate design choice: the scratch and pit are detected by local filters, while the stain is detected by the global texture signature.

During training on a dataset of 2,000 labeled patches (1,200 OK, 400 scratch, 250 pit, 150 stain), the first-layer filters after 30 epochs typically show:

  • 4–6 oriented edge detectors at 0°, 45°, 90°, 135° — these respond to scratch edges.
  • 2–3 blob detectors with center-surround structure — these respond to pits.
  • 3–4 low-frequency texture filters — these respond to stain regions.

You can verify this by visualizing the 16 first-layer kernels as 3×3 grayscale patches. If all 16 look like noise, the learning rate is too high or the input normalization is wrong. If all 16 look like the same edge detector, the initialization is degenerate or the dataset lacks diversity.

Practical check: Before trusting a CNN inspection model, run a receptive field audit. Take a known defect, occlude it with a black square, and confirm that the model's confidence drops. If it does not, the defect is outside the effective RF or the model is relying on background context rather than the defect itself.

Texture vs. Object Defects: Two Feature Regimes

Surface inspection defects fall into two regimes, and CNNs handle them differently:

Object-like defects (scratches, pits, chips) have a distinct local shape. They are detected by mid-level filters that respond to edges, corners, and blobs. The classifier decision is driven by the presence of a specific activation pattern in a localized region. Data augmentation should include small translations and rotations, because the defect can appear anywhere and at any orientation.

Texture-like defects (stains, discoloration, orange peel, micro-cracking) have no single shape. They are defined by a statistical deviation from the nominal texture. Here, global average pooling or a second-order pooling layer (which captures feature covariance) is more effective than a deep stack of local filters. Data augmentation should include illumination changes and slight blur, because the defect is a distribution shift rather than a geometric object.

A single backbone can serve both regimes if it has both a local branch (deep conv stack with small RF) and a global branch (global pooling or a large-RF dilated block). The two feature vectors are concatenated before the final classifier. This is the architectural pattern behind most modern surface inspection networks.

CNN Feature Extraction Pipeline for Surface Defect Inspection Input Patch 512×512×1 dark-field grayscale Conv 3×3 16 filters stride 1, pad 1 512×512×16 MaxPool 2×2 stride 2 256×256×16 RF ≈ 4 px Conv 3×3 32 filters 256×256×32 RF ≈ 8 px MaxPool 2×2 stride 2 128×128×32 RF ≈ 10 px Conv 3×3 64 filters 128×128×64 RF ≈ 13 px Global Avg Pool → 64-dim vector whole-image texture statistics FC 64→3 softmax OK / scratch / pit / stain Learned First-Layer Filters (16 kernels, 3×3) 0° edge 45° edge 90° edge 135° edge blob blob texture texture texture Oriented edge detectors → scratch edges Blob detectors → pits / pinholes Low-freq texture → stains Receptive field grows from 3×3 (layer 1) to ≈13×13 (layer 3); global pooling captures whole-image texture statistics for stain detection.

What to Watch During Training

Three diagnostic signals tell you whether the CNN is learning the right features for inspection:

  1. First-layer kernel visualization. After 10–20 epochs, kernels should show structured patterns (edges, blobs, texture probes). Random noise indicates the learning rate is too high or the input is not normalized. All-identical kernels indicate dead ReLUs or a collapsed initialization.
  2. Activation maps on known defects. Feed a scratch image and inspect the feature maps after each conv stage. The activation should be spatially localized along the scratch. If the activation is diffuse or appears in the background, the model is learning context rather than the defect.
  3. Occlusion sensitivity. Slide a black square across the input and record the change in the target class probability. The region of maximum sensitivity should coincide with the defect. If it does not, the model is exploiting a spurious correlation in the training set — for example, a lighting gradient that happens to co-occur with defects.
Common failure mode: A model achieves 99% validation accuracy but fails on the production line. The cause is usually that the training set contains the same surface region in both training and validation splits, or that the defect is always in the same image location. Always split by part or production batch, not by random patch, and verify that the defect location varies across the dataset.

The core takeaway: convolution learns local matched filters, pooling trades spatial precision for translation tolerance, receptive field determines what scale of defect the network can "see," and the choice between local and global feature aggregation should follow the defect's physical nature — object-like or texture-like. Get these four fundamentals right, and the rest of the inspection pipeline — data collection, augmentation, thresholding, and deployment — has a solid foundation to build on.