Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# AnomalyDetection

PyTorch implementations of FastFlow and SuperSimpleNet (SSN) for image-level
PyTorch implementations of FastFlow, SuperSimpleNet (SSN), and experimental
PaDiM, PatchCore and Dinomaly-style variants for image-level
and pixel-level industrial anomaly detection. The data loader supports MVTec AD,
MVTec-style folders and the original VisA layout.

All five detectors support frozen **DINOv2 ViT-S/14 and ViT-B/14** backbones with automatic
patch-grid padding for 416, 512, 704 and rectangular image sizes. Start with
`configs/ssn_dinov2.yaml`; see the [GPU and memory guide](docs/gpu_memory.md) for
the conservative RTX 5070 Ti profile and the actual CUDA memory smoke test.
See the [detector/backbone/loss matrix](docs/detector_choices.md) for compatible
losses, fitting behavior, runnable examples and differences from the papers.

## Repository layout

| Path | Purpose |
Expand All @@ -12,13 +20,18 @@ MVTec-style folders and the original VisA layout.
| `inference.py` | FastFlow inference, metrics and visualizations |
| `ssn_inference.py` | SuperSimpleNet inference, metrics and visualizations |
| `anomaly_detection/data.py` | MVTec, MVTec-style and VisA dataset loading |
| `anomaly_detection/modeling/` | FastFlow and SuperSimpleNet implementations |
| `anomaly_detection/modeling/` | Detector implementations and frozen features |
| `anomaly_detection/modeling/dinov2.py` | Frozen timm DINOv2 features and patch-grid padding |
| `anomaly_detection/training/` | Model-specific training and evaluation utilities |
| `anomaly_detection/builder.py` | Configuration-driven model, loss and trainer factories |
| `anomaly_detection/config.py` | YAML schema, defaults and validation |
| `anomaly_detection/pipeline.py` | Shared experiment lifecycle |
| `configs/` | Ready-to-edit FastFlow, SSN focal and SSN BCE YAML experiments |
| `anomaly_detection/smoke.py` | Synthetic training/reload and CUDA peak-memory checks |
| `anomaly_detection/predict.py` | Shared checkpoint reload and single-image prediction |
| `anomaly_detection/training/accumulation.py` | Sample-weighted gradient accumulation |
| `configs/` | Ready-to-edit YAML experiments for all five detectors |
| `docs/yaml_training.md` | YAML options, supported combinations and extension guide |
| `docs/gpu_memory.md` | DINOv2, larger inputs and GPU memory configuration |
| `tests/` | Regression tests for evaluation and model behavior |

## Installation
Expand Down Expand Up @@ -101,7 +114,7 @@ The output folder contains `best_model.pth`, `calibration.json`, `metrics.json`
and `resolved_config.yaml`, plus any model-supported plots. Checkpoints include
the architecture and preprocessing configuration required by inference.

FastFlow input height and width must be divisible by 16. Use
FastFlow with ResNet requires input height and width divisible by 16. Use
`--no-pretrained_backbone` to train SSN without ImageNet initialization.
Existing run folders require a new `--run_name` or explicit `--overwrite`.

Expand Down
23 changes: 20 additions & 3 deletions anomaly_detection/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
from anomaly_detection.config import resolve_config
from anomaly_detection.modeling import FastFlowModel, SuperSimpleNetModel
from anomaly_detection.training import FastFlowTrainer, SuperSimpleNetTrainer
from anomaly_detection.training.losses import FastFlowNLLLoss, SSNBCELoss, SSNLoss
from anomaly_detection.training.losses import FastFlowNLLLoss, SSNBCELoss, SSNLoss, SSNBCEDiceLoss, SSNFocalDiceLoss
from anomaly_detection.modeling.feature_heads import PadimModel, PatchcoreModel, DinomalyModel
from anomaly_detection.training.feature_heads import FeatureHeadTrainer, ReconstructionLoss


MODEL_BUILDERS = {"fastflow": FastFlowModel, "ssn": SuperSimpleNetModel}
MODEL_BUILDERS.update(padim=PadimModel, patchcore=PatchcoreModel, dinomaly=DinomalyModel)
LOSS_BUILDERS = {"fastflow_nll": FastFlowNLLLoss, "ssn_focal": SSNLoss, "ssn_bce": SSNBCELoss}
LOSS_BUILDERS.update(ssn_bce_dice=SSNBCEDiceLoss, ssn_focal_dice=SSNFocalDiceLoss)


def model_kwargs(cfg):
Expand All @@ -20,13 +24,19 @@ def model_kwargs(cfg):
"backbone_name": cfg["backbone"],
"input_size": tuple(cfg["image_size"]),
"pretrained_backbone": cfg["pretrained_backbone"],
**{key: cfg[key] for key in ("dino_layers", "feature_channels", "backbone_precision")},
}
if cfg["model"] == "fastflow":
kwargs.update({key: cfg[key] for key in ("flow_steps", "hidden_ratio", "clamp", "conv3x3_only")})
kwargs["reducer_channels"] = (128, 192, 256) if cfg["backbone"] == "wide_resnet50_2" else None
else:
kwargs["reducer_channels"] = ((128, 192, 256)
if cfg["backbone"] == "wide_resnet50_2" and cfg["feature_channels"] is None else None)
elif cfg["model"] == "ssn":
kwargs.update({key: cfg[key] for key in ("perlin_threshold", "layers", "adapt_cls_features")})
kwargs["stop_grad"] = True
else:
from anomaly_detection.config import MODEL_PARAMS
kwargs.pop("feature_channels")
kwargs.update({key: cfg[key] for key in MODEL_PARAMS[cfg["model"]]})
return kwargs


Expand All @@ -37,6 +47,10 @@ def build_model(cfg):

def build_loss(cfg):
cfg = resolve_config(cfg)
if cfg["loss_name"] == "none":
return None
if cfg["model"] == "dinomaly":
return ReconstructionLoss(name=cfg["loss_name"], **cfg["loss_params"])
return LOSS_BUILDERS[cfg["loss_name"]](**cfg["loss_params"])


Expand All @@ -48,9 +62,12 @@ def build_trainer(cfg, model, device, save_dir):
"model_cfg": {**model_kwargs(cfg), "crop_scale": cfg["crop_scale"]},
"loss_fn": build_loss(cfg), "experiment_cfg": cfg,
"monitor": "val_loss", "maximize": False,
"accumulate_grad_batches": cfg["accumulate_grad_batches"],
}
if cfg["model"] == "fastflow":
return FastFlowTrainer(backbone_name=cfg["backbone"], **kwargs)
if cfg["model"] in ("padim", "patchcore", "dinomaly"):
return FeatureHeadTrainer(**kwargs)
return SuperSimpleNetTrainer(
head_lr_multiplier=cfg["head_lr_multiplier"],
adaptor_weight_decay=cfg["adaptor_weight_decay"], **kwargs,
Expand Down
7 changes: 4 additions & 3 deletions anomaly_detection/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import yaml

from anomaly_detection.config import BACKBONES, ConfigError, as_yaml_config, load_config, resolve_config
from anomaly_detection.config import BACKBONES, LOSS_DEFAULTS, MODEL_PARAMS, ConfigError, as_yaml_config, load_config, resolve_config


def parse_args(argv=None):
Expand All @@ -16,7 +16,7 @@ def parse_args(argv=None):
parser.add_argument("--check-config", action="store_true", help="Validate and print resolved settings without training.")
parser.add_argument("--data_path")
parser.add_argument("--category")
parser.add_argument("--model", choices=["fastflow", "ssn"])
parser.add_argument("--model", choices=MODEL_PARAMS)
parser.add_argument("--backbone", choices=BACKBONES)
parser.add_argument("--image_size", type=int, nargs=2)
parser.add_argument("--crop_scale", type=float)
Expand All @@ -39,7 +39,8 @@ def parse_args(argv=None):
parser.add_argument("--save_dir")
parser.add_argument("--run_name")
parser.add_argument("--device", choices=["auto", "cpu", "cuda"])
parser.add_argument("--loss", dest="loss_name", choices=["fastflow_nll", "ssn_focal", "ssn_bce"])
parser.add_argument("--loss", dest="loss_name", choices=LOSS_DEFAULTS)
parser.add_argument("--accumulate_grad_batches", type=int)
parser.add_argument("--overwrite", action="store_true")
args = vars(parser.parse_args(argv))
config_path = args.pop("config", None)
Expand Down
74 changes: 65 additions & 9 deletions anomaly_detection/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@ class ConfigError(ValueError):
"""An invalid or unsupported experiment configuration."""


BACKBONES = ("resnet18", "resnet34", "wide_resnet50_2")
RESNET_BACKBONES = ("resnet18", "resnet34", "wide_resnet50_2")
DINO_BACKBONES = ("dinov2_vits14", "dinov2_vitb14")
BACKBONES = RESNET_BACKBONES + DINO_BACKBONES
MODEL_PARAMS = {
"fastflow": {"flow_steps", "hidden_ratio", "clamp", "conv3x3_only"},
"ssn": {"perlin_threshold", "adapt_cls_features", "layers"},
"fastflow": {"flow_steps", "hidden_ratio", "clamp", "conv3x3_only", "dino_layers", "feature_channels", "backbone_precision"},
"ssn": {"perlin_threshold", "adapt_cls_features", "layers", "dino_layers", "feature_channels", "backbone_precision"},
}
FEATURE_PARAMS = {"layers", "dino_layers", "backbone_precision"}
MODEL_PARAMS.update(
padim=FEATURE_PARAMS | {"padim_channels", "covariance_regularization", "fit_memory_mb"},
patchcore=FEATURE_PARAMS | {"max_patches", "memory_bank_size", "distance_chunk_size"},
dinomaly=FEATURE_PARAMS | {"decoder_depth", "bottleneck_dropout", "gradient_checkpointing"},
)
LOSS_DEFAULTS = {
"fastflow_nll": {},
"ssn_focal": {
"gamma": 4.0, "alpha": -1.0, "truncation_term": 0.5,
"seg_weight": 1.0, "cls_weight": 1.0, "truncation_weight": 1.0,
},
"ssn_bce": {"seg_weight": 1.0, "cls_weight": 1.0},
"ssn_bce": {"seg_weight": 1.0, "cls_weight": 1.0, "seg_pos_weight": 1.0, "cls_pos_weight": 1.0},
}
MODEL_LOSSES = {"fastflow": ("fastflow_nll",), "ssn": ("ssn_focal", "ssn_bce")}
LOSS_DEFAULTS["ssn_bce_dice"] = {**LOSS_DEFAULTS["ssn_bce"], "dice_weight": 1.0, "dice_smooth": 1e-6}
LOSS_DEFAULTS["ssn_focal_dice"] = {
**LOSS_DEFAULTS["ssn_focal"], "truncation_weight": 0.0, "dice_weight": 1.0, "dice_smooth": 1e-6,
}
MODEL_LOSSES = {"fastflow": ("fastflow_nll",), "ssn": ("ssn_focal", "ssn_bce", "ssn_bce_dice", "ssn_focal_dice")}
MODEL_LOSSES.update(padim=("none",), patchcore=("none",), dinomaly=("cosine", "mse", "smooth_l1", "cosine_mse"))
LOSS_DEFAULTS.update(none={}, cosine={}, mse={}, smooth_l1={}, cosine_mse={"cosine_weight": 1.0, "mse_weight": 1.0})

# Keep the existing command-line defaults, and share them with YAML experiments.
DEFAULTS = {
Expand All @@ -37,13 +51,18 @@ class ConfigError(ValueError):
"image_size": [416, 416], "crop_scale": 0.875,
"flow_steps": 8, "hidden_ratio": 1.0, "clamp": 2.0, "conv3x3_only": False,
"perlin_threshold": 0.2, "adapt_cls_features": False, "layers": ["layer2", "layer3"],
"dino_layers": [11], "feature_channels": None, "backbone_precision": "float32",
"accumulate_grad_batches": 1,
"loss_name": None, "loss_params": {},
"batch_size": 32, "num_epochs": 100, "learning_rate": 1e-4,
"weight_decay": 1e-5, "head_lr_multiplier": 2.0, "adaptor_weight_decay": 0.01,
"patience": 30, "val_ratio": 0.2, "seed": 42, "device": "auto", "num_workers": 4,
"image_threshold_quantile": 0.99, "pixel_threshold_quantile": 0.999,
"save_dir": "./checkpoints", "run_name": None, "overwrite": False,
"num_visualizations": 10,
"padim_channels": 32, "covariance_regularization": 0.01, "fit_memory_mb": 512,
"max_patches": 10000, "memory_bank_size": 1000, "distance_chunk_size": 256,
"decoder_depth": 4, "bottleneck_dropout": 0.2, "gradient_checkpointing": True,
}
SECTIONS = {
"dataset": {"path": "data_path", "category": "category", "type": "dataset_type", "val_ratio": "val_ratio"},
Expand All @@ -57,6 +76,7 @@ class ConfigError(ValueError):
"weight_decay": "weight_decay", "patience": "patience", "seed": "seed",
"device": "device", "num_workers": "num_workers",
"head_lr_multiplier": "head_lr_multiplier", "adaptor_weight_decay": "adaptor_weight_decay",
"accumulate_grad_batches": "accumulate_grad_batches",
},
"evaluation": {
"image_threshold_quantile": "image_threshold_quantile",
Expand Down Expand Up @@ -120,24 +140,58 @@ def resolve_config(options):
_component(cfg["run_name"], "output.run_name")
_choice(cfg["model"], MODEL_PARAMS, "model.name")
_choice(cfg["backbone"], BACKBONES, "model.backbone")
if cfg["model"] == "dinomaly" and cfg["backbone"] not in DINO_BACKBONES:
raise ConfigError("Dinomaly requires a DINOv2 backbone.")
if cfg["model"] in ("padim", "patchcore", "dinomaly") and cfg["feature_channels"] is not None:
raise ConfigError("feature_channels is only supported by FastFlow and SSN.")
if cfg["model"] in ("padim", "patchcore") and cfg["accumulate_grad_batches"] != 1:
raise ConfigError("PaDiM/PatchCore fit once without gradients; accumulate_grad_batches must be 1.")
if cfg["backbone"] in DINO_BACKBONES and cfg["layers"] != DEFAULTS["layers"]:
raise ConfigError("DINOv2 uses dino_layers; layers selects ResNet stages only.")
if cfg["backbone"] not in DINO_BACKBONES and cfg["dino_layers"] != DEFAULTS["dino_layers"]:
raise ConfigError("dino_layers selects DINOv2 blocks only.")
_choice(cfg["backbone_precision"], ("float32", "bfloat16"), "backbone_precision")
if cfg["backbone"] not in DINO_BACKBONES and cfg["backbone_precision"] != "float32":
raise ConfigError("bfloat16 backbone_precision is currently DINOv2-only.")
dino_layers = cfg["dino_layers"]
if (not isinstance(dino_layers, list) or not dino_layers or
any(type(i) is not int or not 0 <= i < 12 for i in dino_layers) or
dino_layers != sorted(set(dino_layers))):
raise ConfigError("dino_layers must be unique ordered block indices in [0, 11].")
channels = cfg["feature_channels"]
if channels is not None:
if type(channels) is not int or channels < 1:
raise ConfigError("feature_channels must be a positive integer or null.")
if cfg["model"] == "ssn" and not cfg["adapt_cls_features"]:
raise ConfigError("feature_channels requires SSN with adapt_cls_features: true.")
if cfg["model"] == "fastflow" and (channels < 2 or channels % 2):
raise ConfigError("FastFlow feature_channels must be an even integer >= 2.")
_choice(cfg["dataset_type"], ("auto", "mvtec", "visa"), "dataset.type")
_choice(cfg["device"], ("auto", "cpu", "cuda"), "training.device")
for name in ("pretrained_backbone", "conv3x3_only", "adapt_cls_features", "overwrite"):
for name in ("pretrained_backbone", "conv3x3_only", "adapt_cls_features", "overwrite", "gradient_checkpointing"):
if type(cfg[name]) is not bool:
raise ConfigError(f"{name} must be true or false (not a quoted string).")
for name, minimum in {
"flow_steps": 1, "batch_size": 1, "num_epochs": 1, "patience": 1,
"num_workers": 0, "seed": 0, "num_visualizations": 0,
"num_workers": 0, "seed": 0, "num_visualizations": 0, "accumulate_grad_batches": 1,
"padim_channels": 1, "fit_memory_mb": 1, "max_patches": 1, "memory_bank_size": 1,
"distance_chunk_size": 1, "decoder_depth": 2,
}.items():
if type(cfg[name]) is not int or cfg[name] < minimum:
raise ConfigError(f"{name} must be an integer >= {minimum}.")
if cfg["memory_bank_size"] > cfg["max_patches"]:
raise ConfigError("memory_bank_size cannot exceed max_patches.")
cfg["covariance_regularization"] = _number(cfg["covariance_regularization"], "covariance_regularization", inclusive=False)
cfg["bottleneck_dropout"] = _number(cfg["bottleneck_dropout"], "bottleneck_dropout", upper=1.0)
if cfg["bottleneck_dropout"] == 1:
raise ConfigError("bottleneck_dropout must be less than 1.")
if cfg["seed"] >= 2**32:
raise ConfigError("seed must be less than 2**32.")
size = cfg["image_size"]
if not isinstance(size, (list, tuple)) or len(size) != 2 or any(type(v) is not int or v < 32 for v in size):
raise ConfigError("model.image_size must be [height, width], with each integer >= 32.")
cfg["image_size"] = list(size)
if cfg["model"] == "fastflow" and any(v % 16 for v in size):
if cfg["model"] == "fastflow" and cfg["backbone"] in RESNET_BACKBONES and any(v % 16 for v in size):
raise ConfigError("FastFlow image_size height and width must be divisible by 16.")
layers = cfg["layers"]
allowed_layers = ["layer1", "layer2", "layer3", "layer4"]
Expand Down Expand Up @@ -166,9 +220,11 @@ def resolve_config(options):
lower = -1.0 if name == "alpha" else 0.0
upper = 1.0 if name in ("alpha", "truncation_term") else None
params[name] = _number(params[name], f"loss.params.{name}", lower, upper)
if name in ("seg_pos_weight", "cls_pos_weight", "dice_smooth") and params[name] <= 0:
raise ConfigError(f"loss.params.{name} must be positive.")
if "alpha" in params and params["alpha"] != -1.0 and params["alpha"] < 0:
raise ConfigError("loss.params.alpha must be -1 or between 0 and 1.")
weights = [v for k, v in params.items() if k.endswith("_weight")]
weights = [v for k, v in params.items() if k in {"seg_weight", "cls_weight", "truncation_weight", "dice_weight", "cosine_weight", "mse_weight"}]
if weights and not any(weights):
raise ConfigError("At least one loss weight must be positive.")
cfg["loss_name"], cfg["loss_params"] = loss_name, params
Expand Down
14 changes: 11 additions & 3 deletions anomaly_detection/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def __init__(
dataset_type: str = "auto",
val_ratio: float = 0.2,
seed: int = 42,
pin_memory: Optional[bool] = None,
):
self.root_dir = root_dir
self.category = category
Expand All @@ -360,6 +361,7 @@ def __init__(
raise ValueError(f"val_ratio must be between 0 and 1, got {val_ratio}")
self.val_ratio = float(val_ratio)
self.seed = int(seed)
self.pin_memory = torch.cuda.is_available() if pin_memory is None else bool(pin_memory)

self.train_dataset = None
self.val_dataset = None
Expand Down Expand Up @@ -398,6 +400,7 @@ def setup(self):
# Separate dataset instances keep augmentation confined to training while
# validation uses the same deterministic preprocessing as testing.
self.train_dataset = Subset(train_augmented, train_indices)
self.fit_dataset = Subset(train_deterministic, train_indices)
self.val_dataset = Subset(train_deterministic, val_indices)
self.test_dataset = MVTecDataset(
root_dir=self.root_dir,
Expand All @@ -418,16 +421,21 @@ def train_dataloader(self) -> DataLoader:
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
pin_memory=True,
pin_memory=self.pin_memory,
)

def fit_dataloader(self) -> DataLoader:
"""Deterministic normal training images, disjoint from validation/test."""
return DataLoader(self.fit_dataset, batch_size=self.batch_size, shuffle=False,
num_workers=self.num_workers, pin_memory=self.pin_memory)

def test_dataloader(self) -> DataLoader:
return DataLoader(
self.test_dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True,
pin_memory=self.pin_memory,
)

def val_dataloader(self) -> DataLoader:
Expand All @@ -436,7 +444,7 @@ def val_dataloader(self) -> DataLoader:
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_workers,
pin_memory=True,
pin_memory=self.pin_memory,
)

@staticmethod
Expand Down
Loading
Loading