Skip to content

Data and Training ​

The weak scout learns from image-level OK/NOK labels only. This page covers the input files, the training command and its options, and how many defect labels the paper found you need.

Manifests ​

The training, scoring and cascade tools read JSONL manifests: one JSON object per line, one image per object.

json
{"image_path": "images/train/part_0001.png", "label": "ok"}
{"image_path": "images/train/part_0002.png", "label": "nok", "image_id": "part_0002"}
FieldRequiredMeaning
image_pathyesAbsolute path, or relative to the current directory, or relative to the manifest's directory, tried in that order.
labelyesok (normal) or nok (defective), in any letter case. Any other value stops the tool with label must be 'ok' or 'nok'.
image_idnoIdentifier of the image; defaults to the file name without extension. The calibration tools use it to join scores with labels.

Other fields are allowed and ignored. Empty lines are skipped, and an empty manifest is an error.

Splits ​

Build three manifests, train.jsonl, val.jsonl and test.jsonl, each with OK and NOK images. The validation set does double duty: its NOK scores calibrate the exit gate and its OK scores calibrate the decision layer. The test set is scored once, at the end, to report the rates the frozen thresholds give.

The splits must be disjoint at the image level, not only by file name: a renamed or re-exported copy of the same image counts as overlap. An image that has been used to evaluate or calibrate a model must never appear in a training split, and the thresholds are only valid if the model never trained on the images they were calibrated on.

MVTec AD ​

The paper uses MVTec AD. Each category folder has train/good/, test/good/, test/<defect_type>/ and ground_truth/<defect_type>/ (the defect masks, which are not inspection images). This builds one manifest of all the images of the 15 categories:

python
import json
from pathlib import Path

root = Path("mvtec_anomaly_detection")           # extracted dataset root
rows = []
for img in sorted(root.glob("*/*/*/*.png")):
    category, split, kind = img.parts[-4], img.parts[-3], img.parts[-2]
    if split not in ("train", "test"):            # skip ground_truth/ (masks)
        continue
    rows.append({
        "image_id": f"{category}_{split}_{kind}_{img.stem}",
        "image_path": str(img.resolve()),
        "label": "ok" if kind == "good" else "nok",
        "category": category,                     # extra field, ignored by the tools
    })
with open("mvtec_all.jsonl", "w") as f:
    f.writelines(json.dumps(r) + "\n" for r in rows)

Do not use the official folders as splits: MVTec AD's train/ holds OK images only, so a scout cannot learn from it. The paper re-carves all the images into its own pools and disjoint validation and test sets (§4.1 and §4.2); carve your three manifests from mvtec_all.jsonl the same way, for example at random with a fixed seed. The paper's own manifests are not in the repository.

Synthetic dataset ​

examples/make_dummy_dataset.py writes a small synthetic dataset to try the whole chain without downloading a dataset: OK images are noisy gradients, NOK images carry a bright scratch or a dark blob.

bash
python examples/make_dummy_dataset.py --out data/dummy
OptionDefaultMeaning
--outrequiredOutput folder
--n-train64Images in the training split
--n-val32Images in the validation split
--n-test16Images in the test split
--image-size256Side of the square images, in pixels
--seed0Random seed

Each split is half OK and half NOK. The script writes the images to <out>/images/<split>/ and the manifests train.jsonl, val.jsonl and test.jsonl to <out>, with image paths relative to the manifest.

Training ​

bash
python -m selective_inspection.train \
    --config configs/train_default.yaml \
    --train-manifest data/my_dataset/train.jsonl \
    --val-manifest data/my_dataset/val.jsonl \
    --device cuda \
    --out runs/scout_seed1337

The paper recipe ​

configs/train_default.yaml is the configuration of the paper's weak-scout runs:

SettingValue
BackboneResNet-18 from timm, ImageNet-pretrained, three feature levels
Input256×256 letterbox, pixels scaled to [0, 1], no ImageNet mean/std normalization
Steps1500
Batch size16
OptimizerAdamW, learning rate 1e-4, weight decay 1e-4, gradient-norm clip 1.0
LossBinary cross-entropy of the image head against the label, plus a background BCE on the per-query class logits
Seed1337 (the paper's primary seed; 2027 and 4242 replicate the supervision-budget curve)

The configuration keys and the values of train_default.yaml:

KeyValueMeaning
model.backboneresnet18timm model name
model.pretrainedtrueStart from ImageNet weights (downloaded by timm on first use)
model.image_size256Side of the square input
model.feature_dim256Width of the transformer
model.num_queries100Learnable decoder queries
model.num_heads8Attention heads
model.num_encoder_layers2Encoder layers
model.num_decoder_layers2Decoder layers
model.multi_scaletrueThree backbone levels; false uses one
training.max_steps1500Optimizer steps (one batch each)
training.batch_size16Images per step
training.learning_rate1.0e-4AdamW learning rate
training.weight_decay1.0e-4AdamW weight decay
training.grad_clip_norm1.0Gradient-norm clip
training.seed1337Seed of the shuffling and of the initialization
training.log_every10Steps between log lines
training.loss.class_weight1.0Weight of the background BCE
training.loss.image_weight1.0Weight of the image BCE
manifests.traindata/my_dataset/train.jsonlTraining manifest
manifests.valdata/my_dataset/val.jsonlValidation manifest

Options ​

OptionDefaultMeaning
--configrequiredYAML configuration
--outrequiredRun folder, created if missing
--train-manifestmanifests.trainTraining manifest
--val-manifestmanifests.valValidation manifest; without one, the run writes no validation scores
--max-stepstraining.max_stepsNumber of steps
--batch-sizetraining.batch_sizeBatch size
--seedtraining.seedSeed
--deviceautoauto takes CUDA when available and prints its choice; cuda stops with an error when no CUDA device is available instead of falling back to the CPU; cpu
--init-checkpointnoneStart from a checkpoint (see fine-tuning)
--no-pretrainedoffDo not download ImageNet weights (smoke runs without network; accuracy suffers)

Outputs ​

FileContents
resolved_config.jsonThe configuration after the command-line overrides, written before training starts
checkpoint.ptWeights (model_state), architecture (model_params), step and seed
predictions_val.jsonlOne row per validation image: image_id, image_path, image_score, label. This is the input of the calibration tools
metrics_val.jsonn, n_ok, n_nok and image_auroc of the validation set (null when it holds a single class)

The last two files are written only when there is a validation manifest. The run prints the loss every log_every steps and a summary at the end. When the training manifest holds fewer than 200 NOK images it prints a warning (see how many NOK labels).

On a CPU, training is slow and uses a lot of memory: at 256×256 the encoder attends over about 5,400 tokens. For a CPU smoke run, keep the batch size at 4 or less.

The model ​

WeakScout (in selective_inspection/model.py):

  • The backbone returns three feature maps (strides 4, 8 and 16 for ResNet-18), each projected to 256 channels by a 1×1 convolution, given a 2-D sine position embedding and flattened into tokens.
  • A 2-layer transformer encoder runs over the tokens; a 2-layer decoder runs 100 learnable queries over the encoder output.
  • Three heads: a class logit per query, a box per query (kept so checkpoints match the research code; never supervised), and the image head, a LayerNorm and a small MLP over the mean of the encoder output. The sigmoid of the image head is the score every later stage uses.

The loss is class_weight × BCE of every query's class logit against "background" + image_weight × BCE of the image logit against the label (1 for NOK, 0 for OK). No box or mask is ever read.

How many NOK labels ​

The paper fixed the OK pool at 2,361 images and varied only the number of NOK images (§5.2, Table 3), on its shared test set of 802 images:

NOK images in trainingImage AUROC (seed 1337)Across three seeds
250.583Close to chance, replicated
500.512 (95 % CI 0.464 to 0.560)Close to chance, replicated
1000.749Seed-dependent: 0.569 to 0.810
2000.860Reliable, replicated (cross-seed range at most 0.042)
4000.911Reliable, replicated
727 (full pool)0.9339Reliable, replicated

Plan for at least 200 image-level NOK labels before trusting a scout; that is why the training script warns below 200.

The balance of the pool matters as well. On MVTec AD, the few-NOK pool (2,361 OK, 727 NOK) gave 0.9339 and a balanced pool (729 OK, 727 NOK) gave 0.9110. On two harder benchmarks the few-NOK recipe collapsed (VisA 0.5451, Real-IAD 0.5933) while balanced pools reached 0.8289 and 0.8700 (§5.3, each on its own test set). The paper's advice: if you can choose the training images, balance OK and NOK (§8). If the validation AUROC is close to 0.5, check the number of NOK images and the balance before the architecture.

Fine-tuning ​

To adapt a trained scout to a new production line, collect OK images and at least 200 image-level NOK labels, build the three disjoint manifests, and start from the previous checkpoint:

bash
python -m selective_inspection.train \
    --config configs/finetune_example.yaml \
    --train-manifest data/my_line/train.jsonl \
    --val-manifest data/my_line/val.jsonl \
    --init-checkpoint checkpoints/scout_prior.pt \
    --device cuda \
    --out runs/my_line_ft

With --init-checkpoint (or init_checkpoint: in the configuration; the option wins), both the weights and the architecture come from the checkpoint and the model: block is ignored. A plain state dict, as saved by the research code, is loaded into the default architecture. configs/finetune_example.yaml keeps the paper recipe with a shorter schedule (500 steps) and a lower learning rate (5e-5). Training from ImageNet weights with train_default.yaml remains an option for a large dataset or one far from the previous line.

Then calibrate the new run on its own validation set (Calibration): thresholds of an earlier model do not carry over.