System designHow-to

Small defect misses in anomaly detection: use image tiling

Anomaly detection misses small defects because the whole frame is resized to 256. Enable tiling: tile_size 256, stride 128 keeps defects at native pixels.

LumaSpec Vision9 min read Share on LinkedIn

On this page 7 sections
  1. 01Why the defect disappears at image_size 256
  2. 02What tiling changes
  3. 03Set the tiling options
  4. 04Worked example: a 12 px scratch on a 1024 × 1024 frame at image_size 256
  5. 05When tiling is not the fix
  6. 06Checks before you retrain
  7. 07Questions

Your model scores the whole frame after resizing it to image_size, and a defect that measures 12 px on the raw image arrives as 3 px. At that size it falls inside one position of the feature map, its evidence is averaged with the background, and the adaptive threshold reads it as noise. The fix is already in the anomalib config: the tiling block. Cut each frame into 256 × 256 tiles with a stride of 128 px and the model scores the defect at native resolution. This guide is for anyone running an unsupervised segmentation task on a smart camera or vision controller; padim on a resnet18 backbone is the case worked here. You will compute whether your defect survives the resize, set the tiling options, and budget the extra inference time before you retrain.

Why the defect disappears at image_size 256

The data pipeline resizes every image to image_size before the backbone sees it. The scale is image_size divided by frame width in pixels, and area shrinks by the square of that ratio:

s = image_size / frame_width_px defect_px_scaled = defect_px_raw × s cells = defect_px_scaled / layer_stride

A 1024 px frame at image_size 256 gives s = 256 / 1024 = 0.25: the defect's width drops to a quarter and its area to 1/16. With center_crop unset (null in the config), nothing crops it further.

The backbone decides the rest. resnet18 downsamples the input ×4 at layer1, ×8 at layer2 and ×16 at layer3, so a 256 px input yields feature maps of 64 × 64, 32 × 32 and 16 × 16 positions. padim scores each position of those maps against the statistics it learned from the normal images. A defect spanning fewer than two positions at layer1 hands the model a single mixed vector, mostly background — that is the miss you are seeing.

Commit to this rule: compute cells at layer1, the finest scored layer (stride 4). Two or more, the defect can survive the resize; fewer than two, tile.

What tiling changes

Tiling cuts the raw image into tile_size squares, scores each as its own input, and stitches the predictions back into a full-size anomaly map. Set tile_size equal to image_size and a tile reaches the model with s = 1: your 12 px scratch stays 12 px.

The arithmetic flips. At layer1 the scratch spans 12 / 4 = 3 positions per side, nine in all, and its share of the scored 256 × 256 tile is (12 × 12) / 65,536 = 0.22%. Resized whole-frame it was 9 / 65,536 = 0.014% — a 16× stronger signal, exactly the area factor (1 / 0.25)² = 16.

Scored input Whole frame resized Tiled: 256 px tile, 128 px stride
Scale to the model 0.25 1
12 px defect at the input 3 px 12 px
Positions covered at layer1 0.75 9
Defect share of scored pixels 0.014% 0.22%
Inputs scored per 1024 px frame 1 49

Coverage rule: a defect is fully inside at least one tile as long as stride ≤ tile_size − d, with d the largest defect in pixels. At 128 stride with 256 px tiles, defects up to 128 px are safe; a defect crossing a seam is split between two tiles and scored whole on neither.

Set the tiling options

  1. Measure d, the largest defect you must catch, in raw pixels. Open one frame from normal_dir unscaled and count across it. The pixel rule for defect detection and the minimum defect size in pixels guides set the floor if you are still choosing the sensor. Everything below hangs on this one number.

  2. Set tiling.apply: true. The config in this case ships with apply: false, which ignores the whole block and runs the resize at full frame.

  3. Set tiling.tile_size: 256, equal to image_size. Tiles then reach the backbone without any resize, at s = 1.

  4. Set tiling.stride: 128, half the tile. Every pixel then lands in four tiles (256² / 128² = 4) and any defect up to 128 px stays whole in at least one.

  5. Leave tiling.remove_border_count: 0. It trims pixels from tile edges — exactly the pixels a small defect lives on.

  6. Keep tiling.use_random_tiling: false (random_tile_count: 16 applies only to random tiling). A deterministic grid gives training and the line the same layout, so stitched maps reproduce run to run.

  7. Retrain and compare pixel metrics. padim rebuilds its per-position statistics in one pass (max_epochs: 1 here), so training cost scales with the tile count, not with epochs. With task: segmentation, pixel-level F1Score and AUROC are the numbers to judge; keep threshold method: adaptive on both runs, open the stitched maps (save_images: true) and check the seams. If the run fails on the folders first, the custom dataset training fix covers normal_dir, abnormal_dir and mask_dir.

Worked example: a 12 px scratch on a 1024 × 1024 frame at image_size 256

Inputs. Frame 1024 × 1024 px, read from one raw image in normal_dir — the folder layout does not record it. image_size 256 and center_crop null, from the config. Backbone resnet18 scoring layer1, layer2, layer3, so the finest stride is 4. Defect: one scratch, 12 px long × 2 px wide, measured on the raw frame. Tile inference time t = 8 ms on the target controller — measure yours; this one is illustrative.

Rule. The defect must span at least two positions at layer1 after the resize, and tile count × t must fit the cycle budget.

Without tiling. s = 256 / 1024 = 0.25. The scratch becomes 12 × 0.25 = 3 px long and 2 × 0.25 = 0.5 px wide — half a pixel. Positions at layer1: 3 / 4 = 0.75. Under one position, at sub-pixel width: the resize has erased the evidence, and no threshold move brings it back.

With tiling, tile_size 256 and stride 128. s = 1, the scratch keeps its 12 px. Positions at layer1: 12 / 4 = 3 per side, nine positions of evidence. Tile count: ((1024 − 256) / 128 + 1)² = (6 + 1)² = 49 per frame.

Budget. 49 tiles × 8 ms = 392 ms per frame, against a 250 ms cycle — over. Raise the stride but keep the overlap, tile_size − stride, at or above d. Stride 192 leaves 256 − 192 = 64 px of overlap (≥ 12 px) and gives ((1024 − 256) / 192 + 1)² = (4 + 1)² = 25 tiles: 25 × 8 ms = 200 ms, inside the cycle with 50 ms to spare. The hard cap is stride ≤ tile_size − d = 256 − 12 = 244 px; past it, some 12 px defect straddles a seam.

Decision. Fewer than two positions at layer1 without tiling: tile, starting at 256 / 128 and stepping the stride down in clean divisors of the frame until tiles × t fits with margin. Four or more positions without tiling (a defect of 16 px scaled or more): resizing alone is enough, do not tile.

When tiling is not the fix

Tiling preserves pixels; it cannot create them.

  • The defect is under about 8 px on the raw sensor. Even an unscaled tile then spans fewer than two layer1 positions (8 / 4 = 2 is the floor). Buy pixels: shorter working distance, longer focal length or a higher-resolution sensor. The camera resolution calculator sizes the sensor from defect size and field of view.
Camera resolution for the smallest feature
Units
Pixels needed
–
Sensor size
– MP
Smallest sensor that fits
–
Pixel on the part
– mm
Pixels across the feature, that sensor
–

Pixels needed = field / feature × pixels per feature on each axis (field incl. margin). The lens must resolve the pixels too: Nyquist = 1000 / (2 × pixel µm) lp/mm.

Find cameras with enough pixels Open the full calculator

  • The evidence never reached the sensor. Glare-saturated pixels and motion blur destroy contrast before training sees anything; fix the light or exposure first. The glare removal guide covers the saturated-pixel case.

  • The part is large and the defect zone is known. A 6 MP frame carries about 6,000,000 px, which is 92 tiles of 256 × 256 before overlap. Score an ROI around the defect zone instead — the ROI display guide shows the display side.

For the sensor side of that fix, the area scan cameras category runs from under 3 MP to over 25 MP. Line side, a 6 MP mono smart camera such as the MV-SC5060XM-08M-WBN scores the tiles on the device over GigE, with an 8 mm M12-mount lens fitted.

Smart cameras & vision sensorsHikrobot MV-SC5060XM-08M-WBN SC5000X series Smart Camera, 6 MP, Mono, 8 mm focal length, M12-mountMV-SC5060XM-08M-WBN
Interface
GigE
Resolution
6-10 MP
Sensor format
1/2.4"
Shutter
Rolling
$1,629.00View part

Where scoring runs off-camera, a vision controller hosts training and tiled inference; the MV-VC2140-128G40-1T pairs an N97 processor with 8 GB of memory and 2 USB 3 ports.

Vision controllersHikrobot MV-VC2140-128G40-1T VC2100 Vision Controller, N97, 8GB+128G SSD+ 1THDDMV-VC2140-128G40-1T
LAN ports
3-4 LAN ports
USB 3 ports
2 USB 3 ports
Light outputs
4 built-in channels
Memory
8 GB
$1,679.00View partBuild a system with it

Checks before you retrain

  • tile_size equals image_size — 256 = 256 here — so no tile is resized.
  • stride ≤ tile_size − d: 128 ≤ 256 − 12, overlap 128 px ≥ d.
  • use_random_tiling false and remove_border_count 0, so the grid is deterministic and untrimmed.
  • Training tiles = frames × 49 at 256 / 128 on 1024 px frames; the dataloader walk grows by the same factor.
  • pixel F1Score and AUROC compared against the old run at the same adaptive threshold method.
  • Stitched maps opened at the seams (save_images true), checking for half-scored defects.

Work the numbers before the config: measure d and the frame width, run the cell computation at your own scale, and tile only when it drops under two positions at layer1. If the sensor is the bottleneck, pick a replacement in the smart cameras category or start at the camera step of the configurator; multi-camera volumes go through the quote request page. When the retrained model is ready for the line, the ONNX export to pass/fail guide takes the tiled map to a PLC signal, and the systems guides cover the rest of the training pipeline.

Questions

What tile size and stride should I start with?

Start at tile_size 256 and stride 128: tile equal to image_size, stride half of it. Every pixel is scored in four tiles, any defect up to 128 px stays whole in at least one, and the grid stays deterministic. Change them only when the tile-count arithmetic on your own frame size says the budget needs it.

How much does tiling slow training and inference?

Roughly by the tile count. A 1024 × 1024 frame at tile 256 and stride 128 becomes 49 tiles, so the training walk and line-side scoring both grow by about that factor; padim trains in one epoch, so the extra cost is feature extraction, not extra epochs. Dropping the stride to 192 cuts the count to 25 tiles per frame.

Can a defect on a tile border be missed?

Yes, when the overlap is smaller than the defect. Overlap is tile_size minus stride: at 256 and 128 that is 128 px, so anything up to 128 px is whole in at least one tile. The hard cap is stride ≤ tile_size − d; with d = 12 px, never push the stride past 244 px.

Is use_random_tiling worth turning on?

Not on an inspection line. Random tiling shuffles tile placement as augmentation, so positions see inconsistent content in training and stitched maps are harder to reproduce between runs. Keep use_random_tiling false; random_tile_count only applies while random tiling is on.

When should I change the camera instead of the config?

When the defect is under about 8 px on the raw sensor: even an unscaled tile then spans fewer than two layer1 positions, so no training setting recovers it. Buy pixels — shorter working distance, longer focal length or a higher-resolution sensor — until the defect measures 8 px or more, then re-run the resize arithmetic to see whether tiling is still needed.