diff --git a/README.md b/README.md index 6b9f3a9..1b0cdf0 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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 @@ -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`. diff --git a/anomaly_detection/builder.py b/anomaly_detection/builder.py index 95078a9..e8ccd70 100644 --- a/anomaly_detection/builder.py +++ b/anomaly_detection/builder.py @@ -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): @@ -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 @@ -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"]) @@ -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, diff --git a/anomaly_detection/cli.py b/anomaly_detection/cli.py index 8668f4f..8ad693d 100644 --- a/anomaly_detection/cli.py +++ b/anomaly_detection/cli.py @@ -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): @@ -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) @@ -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) diff --git a/anomaly_detection/config.py b/anomaly_detection/config.py index cb8febf..1cbad8d 100644 --- a/anomaly_detection/config.py +++ b/anomaly_detection/config.py @@ -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 = { @@ -37,6 +51,8 @@ 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, @@ -44,6 +60,9 @@ class ConfigError(ValueError): "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"}, @@ -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", @@ -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"] @@ -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 diff --git a/anomaly_detection/data.py b/anomaly_detection/data.py index 30a2e3b..fe2b7eb 100644 --- a/anomaly_detection/data.py +++ b/anomaly_detection/data.py @@ -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 @@ -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 @@ -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, @@ -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: @@ -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 diff --git a/anomaly_detection/modeling/dinov2.py b/anomaly_detection/modeling/dinov2.py new file mode 100644 index 0000000..33ef9bc --- /dev/null +++ b/anomaly_detection/modeling/dinov2.py @@ -0,0 +1,65 @@ +"""Frozen DINOv2 patch features; no remote Python execution or xFormers required.""" + +import torch +from torch import nn +from torch.nn import functional as F + + +DINO_MODELS = { + "dinov2_vits14": "vit_small_patch14_dinov2.lvd142m", + "dinov2_vitb14": "vit_base_patch14_dinov2.lvd142m", +} + + +def pad_to_patch_grid(images, patch_size=14): + """Pad bottom/right only, retaining the original coordinate origin.""" + h, w = images.shape[-2:] + return F.pad(images, (0, (-w) % patch_size, 0, (-h) % patch_size)) + + +class DinoFeatureExtractor(nn.Module): + def __init__(self, backbone_name, layers=(11,), pretrained=True, precision="float32"): + super().__init__() + import timm + + self.layers = tuple(layers) + if not self.layers or any(type(i) is not int or not 0 <= i < 12 for i in self.layers): + raise ValueError("DINO layers must be block indices in [0, 11].") + if list(self.layers) != sorted(set(self.layers)): + raise ValueError("DINO layers must be unique and ordered.") + if precision not in ("float32", "bfloat16"): + raise ValueError("backbone_precision must be float32 or bfloat16.") + self.precision = precision + self.feature_extractor = timm.create_model( + DINO_MODELS[backbone_name], pretrained=pretrained, + num_classes=0, dynamic_img_size=True, + ) + self.feature_extractor.requires_grad_(False) + self.feature_extractor.eval() + self.channels = self.feature_extractor.num_features * len(self.layers) + self.eval() + + def train(self, mode=True): + super().train(False) + return self + + def get_channels_dim(self, probe_hw=None): + # Metadata avoids an expensive dummy transformer forward at construction. + return self.channels + + @torch.no_grad() + def forward(self, images): + if any(size % 14 for size in images.shape[-2:]): + raise ValueError("Pad images to the DINO patch grid before feature extraction.") + self.feature_extractor.eval() + use_bf16 = images.is_cuda and self.precision == "bfloat16" + if use_bf16 and not torch.cuda.is_bf16_supported(): + raise RuntimeError("BF16 backbone inference is unavailable; use backbone_precision: float32.") + with torch.autocast(device_type=images.device.type, dtype=torch.bfloat16, enabled=use_bf16): + features = self.feature_extractor.get_intermediate_layers( + images, n=self.layers, reshape=True, norm=True, + ) + # Retain the native patch grid. Upscaling large-channel features before + # the adapter wastes memory; only the final anomaly map is upscaled. + fused = features[0] if len(features) == 1 else torch.cat(features, dim=1) + return fused.float() diff --git a/anomaly_detection/modeling/fastflow.py b/anomaly_detection/modeling/fastflow.py index 55cf54c..d5e170d 100644 --- a/anomaly_detection/modeling/fastflow.py +++ b/anomaly_detection/modeling/fastflow.py @@ -4,6 +4,8 @@ from typing import List, Optional, Tuple import torchvision.models as models +from anomaly_detection.modeling.dinov2 import DINO_MODELS, DinoFeatureExtractor, pad_to_patch_grid + # ----------------------------- # Glow-style invertible parts @@ -197,33 +199,52 @@ def __init__( hidden_ratio: float = 1.0, clamp: float = 2.0, pretrained_backbone: bool = True, + dino_layers: Optional[List[int]] = None, + feature_channels: Optional[int] = None, + backbone_precision: str = "float32", ): super().__init__() - if input_size[0] % 16 != 0 or input_size[1] % 16 != 0: + self.is_dino = backbone_name in DINO_MODELS + if not self.is_dino and (input_size[0] % 16 != 0 or input_size[1] % 16 != 0): raise ValueError( "FastFlow input_size height and width must be divisible by 16 " f"for fixed LayerNorm shapes, got {input_size}." ) - self.input_size = input_size + self.input_size = tuple(input_size) + self.padded_size = tuple(((v + 13) // 14) * 14 for v in input_size) if self.is_dino else self.input_size + if feature_channels is not None and (type(feature_channels) is not int or feature_channels < 2 or feature_channels % 2): + raise ValueError("FastFlow feature_channels must be a positive even integer >= 2.") + if feature_channels is not None and reducer_channels is not None: + raise ValueError("Specify feature_channels or reducer_channels, not both.") self.flow_steps = flow_steps self.conv3x3_only = conv3x3_only self.hidden_ratio = hidden_ratio self.clamp = clamp - self.feat_drop = nn.ModuleList([ - nn.Dropout2d(p=0.2), # try 0.05~0.2 - nn.Dropout2d(p=0.2), - nn.Dropout2d(p=0.2), - ]) - self.backbone, backbone_channels, self.scales = self._build_backbone( - backbone_name, - input_size, - pretrained=pretrained_backbone, - ) + if self.is_dino: + self.backbone = DinoFeatureExtractor( + backbone_name, layers=dino_layers if dino_layers is not None else [11], + pretrained=pretrained_backbone, precision=backbone_precision, + ) + # Transformer blocks share one patch grid, unlike ResNet stages. + # Concatenate selected blocks and model this fused feature space. + backbone_channels = [self.backbone.get_channels_dim()] + self.scales = [14] + else: + self.backbone, backbone_channels, self.scales = self._build_backbone( + backbone_name, input_size, pretrained=pretrained_backbone, + ) + self.backbone.eval() + self.feat_drop = nn.ModuleList([nn.Dropout2d(p=0.2) for _ in backbone_channels]) # reducers (optional) self.reducers = None + if feature_channels is not None: + reducer_channels = [feature_channels] * len(backbone_channels) if reducer_channels is not None: + if (len(reducer_channels) != len(backbone_channels) or + any(type(c) is not int or c < 2 or c % 2 for c in reducer_channels)): + raise ValueError("reducer_channels must provide one positive even width per feature level.") self.reducers = nn.ModuleList([ nn.Conv2d(in_ch, out_ch, kernel_size=1, bias=False) for in_ch, out_ch in zip(backbone_channels, reducer_channels) @@ -237,8 +258,8 @@ def __init__( self.norms = nn.ModuleList() for ch, sc in zip(self.feature_channels, self.scales): - h = int(input_size[0] / sc) - w = int(input_size[1] / sc) + h = self.padded_size[0] // sc + w = self.padded_size[1] // sc self.norms.append(nn.LayerNorm([ch, h, w], elementwise_affine=True)) # flows per feature level @@ -253,7 +274,7 @@ def __init__( steps.append(FastFlowStep(ch, hidden_ratio=hidden_ratio, kernel_size=k, clamp=clamp)) self.blocks.append(steps) - self.anomaly_map_generator = AnomalyMapGenerator(input_size=input_size) + self.anomaly_map_generator = AnomalyMapGenerator(input_size=self.padded_size) def _build_backbone(self, name: str, input_size: Tuple[int, int], pretrained: bool = True): if name == "resnet18": @@ -281,19 +302,22 @@ def _build_backbone(self, name: str, input_size: Tuple[int, int], pretrained: bo def _extract_features(self, x: torch.Tensor) -> List[torch.Tensor]: # ResNet forward manually for layer1/2/3 features net = self.backbone - x = net.conv1(x) - x = net.bn1(x) - x = net.relu(x) - x = net.maxpool(x) - - f1 = net.layer1(x) # scale /4 - f2 = net.layer2(f1) # /8 - f3 = net.layer3(f2) # /16 + if self.is_dino: + feats = [net(pad_to_patch_grid(x))] + else: + with torch.no_grad(): + x = net.conv1(x) + x = net.bn1(x) + x = net.relu(x) + x = net.maxpool(x) - feats = [f1, f2, f3] + f1 = net.layer1(x) # scale /4 + f2 = net.layer2(f1) # /8 + f3 = net.layer3(f2) # /16 + feats = [f1, f2, f3] if self.reducers is not None: - feats = [self.reducers[i](feats[i]) for i in range(3)] + feats = [self.reducers[i](feat) for i, feat in enumerate(feats)] feats = [self.norms[i](feat) for i, feat in enumerate(feats)] @@ -302,7 +326,14 @@ def _extract_features(self, x: torch.Tensor) -> List[torch.Tensor]: feats = [self.feat_drop[i](feat) for i, feat in enumerate(feats)] return feats + def train(self, mode: bool = True): + super().train(mode) + self.backbone.eval() + return self + def forward(self, x: torch.Tensor, return_latents: Optional[bool] = None): + if tuple(x.shape[-2:]) != self.input_size: + raise ValueError(f"FastFlow expects configured input_size {self.input_size}, got {tuple(x.shape[-2:])}.") # extract frozen features self.backbone.eval() features = self._extract_features(x) @@ -326,4 +357,5 @@ def forward(self, x: torch.Tensor, return_latents: Optional[bool] = None): if return_latents: return hidden_variables, jacobians - return self.anomaly_map_generator(hidden_variables) + anomaly_map = self.anomaly_map_generator(hidden_variables) + return anomaly_map[..., :self.input_size[0], :self.input_size[1]] diff --git a/anomaly_detection/modeling/feature_heads.py b/anomaly_detection/modeling/feature_heads.py new file mode 100644 index 0000000..144777d --- /dev/null +++ b/anomaly_detection/modeling/feature_heads.py @@ -0,0 +1,222 @@ +"""Experimental frozen-feature detectors, with bounded fitting allocations. + +PaDiM uses per-location Gaussians; PatchCore uses a reservoir plus greedy +coreset. Dinomaly is a compact, patch-token reconstruction variant of the +published architecture, not an exact reproduction of its training recipe. +""" + +import torch +from torch import nn +from torch.nn import functional as F +from torch.utils.checkpoint import checkpoint + +from anomaly_detection.modeling.dinov2 import DINO_MODELS, DinoFeatureExtractor, pad_to_patch_grid + + +class FrozenFeatures(nn.Module): + def __init__(self, backbone_name, layers, dino_layers, pretrained_backbone, backbone_precision): + super().__init__() + self.is_dino = backbone_name in DINO_MODELS + if self.is_dino: + self.net = DinoFeatureExtractor(backbone_name, dino_layers, pretrained_backbone, backbone_precision) + self.channels = self.net.channels + else: + import timm + self.net = timm.create_model(backbone_name, pretrained=pretrained_backbone, + features_only=True, out_indices=tuple(int(s[-1]) for s in layers)) + self.channels = sum(self.net.feature_info.channels()) + self.requires_grad_(False) + self.eval() + + def train(self, mode=True): + super().train(False) + return self + + @torch.no_grad() + def forward(self, images): + if self.is_dino: + return self.net(pad_to_patch_grid(images)) + maps = self.net(images) + return torch.cat([F.interpolate(m, maps[0].shape[-2:], mode="bilinear", align_corners=False) + for m in maps], dim=1) + + +class FeatureDetector(nn.Module): + def __init__(self, backbone_name, input_size, pretrained_backbone=True, layers=("layer2", "layer3"), + dino_layers=(11,), backbone_precision="float32", **kwargs): + super().__init__() + self.input_size = tuple(input_size) + self.backbone = FrozenFeatures(backbone_name, layers, dino_layers, pretrained_backbone, backbone_precision) + + def features(self, images): + if tuple(images.shape[-2:]) != self.input_size: + raise ValueError(f"Expected image size {self.input_size}.") + return self.backbone(images) + + def output(self, patch_map): + h, w = self.input_size + size = ((h + 13) // 14 * 14, (w + 13) // 14 * 14) if self.backbone.is_dino else (h, w) + maps = F.interpolate(patch_map, size, mode="bilinear", align_corners=False)[..., :h, :w] + return maps, maps.flatten(1).amax(1) + + def _load_from_state_dict(self, state_dict, prefix, *args, **kwargs): + # Fitting determines bank/grid dimensions. Preserve destination device. + for name in ("mean", "precision", "memory_bank"): + if name in self._buffers and prefix + name in state_dict: + self._buffers[name] = torch.empty_like(state_dict[prefix + name], device=self._buffers[name].device) + super()._load_from_state_dict(state_dict, prefix, *args, **kwargs) + + +class PadimModel(FeatureDetector): + def __init__(self, padim_channels=32, covariance_regularization=0.01, fit_memory_mb=512, **kwargs): + super().__init__(**kwargs) + self.register_buffer("channel_indices", torch.randperm(self.backbone.channels)[:padim_channels]) + self.register_buffer("mean", torch.empty(0)) + self.register_buffer("precision", torch.empty(0)) + self.regularization = covariance_regularization + self.fit_memory_mb = fit_memory_mb + + @torch.no_grad() + def fit_features(self, batches): + count, mean, scatter = 0, None, None + for features in batches: + x = features[:, self.channel_indices].flatten(2).permute(2, 0, 1).cpu().double() + patches, n, channels = x.shape + # Budget includes scatter, covariance, inverse and factorization workspace. + if patches * channels * channels * 8 * 5 > self.fit_memory_mb * 2**20: + raise ValueError("PaDiM covariance exceeds fit_memory_mb; reduce padim_channels or image size.") + batch_mean = x.mean(1) + centered = x - batch_mean[:, None] + batch_scatter = centered.transpose(1, 2) @ centered + if mean is None: + mean, scatter = batch_mean, batch_scatter + else: + delta = batch_mean - mean + scatter.add_(batch_scatter).add_(delta.unsqueeze(2) * delta.unsqueeze(1), alpha=count * n / (count + n)) + mean.add_(delta, alpha=n / (count + n)) + count += n + if count < 2: + raise ValueError("PaDiM needs at least two normal training images.") + covariance = scatter / (count - 1) + covariance.diagonal(dim1=-2, dim2=-1).add_(self.regularization) + self.mean = mean.float().to(self.channel_indices.device) + self.precision = torch.linalg.inv(covariance).float().to(self.channel_indices.device) + + def forward(self, images): + if not self.mean.numel(): + raise RuntimeError("Fit PaDiM before inference.") + features = self.features(images) + x = features[:, self.channel_indices].flatten(2).permute(0, 2, 1) - self.mean + distances = torch.einsum("bpc,pcd,bpd->bp", x, self.precision, x).clamp_min(0).sqrt() + return self.output(distances.reshape(images.shape[0], 1, *features.shape[-2:])) + + +class PatchcoreModel(FeatureDetector): + def __init__(self, max_patches=10000, memory_bank_size=1000, distance_chunk_size=256, **kwargs): + super().__init__(**kwargs) + self.max_patches, self.memory_bank_size = max_patches, memory_bank_size + self.distance_chunk_size = distance_chunk_size + self.register_buffer("memory_bank", torch.empty(0)) + + @staticmethod + def embed(features): + return F.avg_pool2d(features, 3, stride=1, padding=1).permute(0, 2, 3, 1).reshape(-1, features.shape[1]) + + @torch.no_grad() + def fit_features(self, batches): + bank, keys = None, None + for features in batches: + # CPU reservoir with random priorities, processed in bounded chunks. + for x in self.embed(features).split(self.distance_chunk_size): + x = x.cpu() + k = torch.rand(len(x)) + bank = x if bank is None else torch.cat((bank, x)) + keys = k if keys is None else torch.cat((keys, k)) + keep = keys.topk(min(self.max_patches, len(keys))).indices + bank, keys = bank[keep], keys[keep] + if bank is None: + raise ValueError("PatchCore needs normal training images.") + # Greedy k-center on the bounded reservoir. No N x N distance matrix. + selected, nearest, index = [], torch.full((len(bank),), float("inf")), 0 + for _ in range(min(self.memory_bank_size, len(bank))): + selected.append(index) + for start in range(0, len(bank), self.distance_chunk_size): + stop = start + self.distance_chunk_size + distance = (bank[start:stop] - bank[index]).square().sum(1) + nearest[start:stop] = torch.minimum(nearest[start:stop], distance) + nearest[selected] = -1 + index = int(nearest.argmax()) + self.memory_bank = bank[selected].to(self.memory_bank.device) + + def forward(self, images): + if not self.memory_bank.numel(): + raise RuntimeError("Fit PatchCore before inference.") + features = self.features(images) + distances = [] + for query in self.embed(features).split(self.distance_chunk_size): + best = torch.full((len(query),), float("inf"), device=query.device) + for bank in self.memory_bank.split(self.distance_chunk_size): + best = torch.minimum(best, torch.cdist(query, bank).amin(1)) + distances.append(best) + maps = torch.cat(distances).reshape(images.shape[0], 1, *features.shape[-2:]) + return self.output(maps) + + +class LinearDecoderBlock(nn.Module): + """Positive-kernel linear attention avoids a quadratic token attention matrix.""" + def __init__(self, dim, heads): + super().__init__() + self.heads = heads + self.norm1, self.norm2 = nn.LayerNorm(dim), nn.LayerNorm(dim) + self.qkv, self.proj = nn.Linear(dim, 3 * dim), nn.Linear(dim, dim) + self.mlp = nn.Sequential(nn.Linear(dim, 4 * dim, bias=False), nn.GELU(), nn.Linear(4 * dim, dim, bias=False)) + + def forward(self, x): + b, n, c = x.shape + q, k, v = self.qkv(self.norm1(x)).reshape(b, n, 3, self.heads, c // self.heads).permute(2, 0, 3, 1, 4) + q, k = F.elu(q) + 1, F.elu(k) + 1 + kv = k.transpose(-2, -1) @ v / n + denominator = (q * k.mean(-2, keepdim=True)).sum(-1, keepdim=True).clamp_min(1e-6) + attention = ((q @ kv) / denominator).transpose(1, 2).reshape(b, n, c) + x = x + self.proj(attention) + return x + self.mlp(self.norm2(x)) + + +class DinomalyModel(FeatureDetector): + """Dinomaly-style variant: frozen patch features, dropout MLP, linear decoder. + + Uses selected normalized patch layers, two fusion groups, AdamW and optional + reconstruction losses. Does not reproduce original register tokens, optimizer + schedule or hard-mining hooks; benchmark separately before production use. + """ + def __init__(self, decoder_depth=4, bottleneck_dropout=0.2, gradient_checkpointing=True, **kwargs): + if kwargs["backbone_name"] not in DINO_MODELS: + raise ValueError("Dinomaly requires a DINOv2 backbone.") + super().__init__(**kwargs) + self.layer_count = len(kwargs.get("dino_layers", (11,))) + dim = self.backbone.channels // self.layer_count + self.checkpointing = gradient_checkpointing + self.bottleneck = nn.Sequential(nn.Dropout(bottleneck_dropout), nn.Linear(dim, dim * 4, bias=False), + nn.GELU(), nn.Dropout(bottleneck_dropout), nn.Linear(dim * 4, dim, bias=False)) + self.decoder = nn.ModuleList([LinearDecoderBlock(dim, 6 if dim == 384 else 12) for _ in range(decoder_depth)]) + + def reconstruct(self, images): + features = self.features(images).chunk(self.layer_count, dim=1) + tokens = [f.flatten(2).transpose(1, 2) for f in features] + x = self.bottleneck(sum(tokens) / len(tokens)) + decoded = [] + for block in self.decoder: + x = checkpoint(block, x, use_reentrant=False) if self.training and self.checkpointing else block(x) + decoded.append(x) + decoded.reverse() + def groups(values): + split = max(1, len(values) // 2) + return [values[:split], values[split:] or values[:split]] + targets = [sum(group) / len(group) for group in groups(tokens)] + predictions = [sum(group) / len(group) for group in groups(decoded)] + return targets, predictions, features[0].shape[-2:] + + def forward(self, images): + targets, predictions, size = self.reconstruct(images) + maps = sum(1 - F.cosine_similarity(a, b, dim=-1) for a, b in zip(targets, predictions)) / len(targets) + return self.output(maps.reshape(images.shape[0], 1, *size)) diff --git a/anomaly_detection/modeling/supersimplenet.py b/anomaly_detection/modeling/supersimplenet.py index 24ca358..0063088 100644 --- a/anomaly_detection/modeling/supersimplenet.py +++ b/anomaly_detection/modeling/supersimplenet.py @@ -7,6 +7,8 @@ import torchvision.models as models from torchvision.models.feature_extraction import create_feature_extractor +from anomaly_detection.modeling.dinov2 import DINO_MODELS, DinoFeatureExtractor, pad_to_patch_grid + def _init_weights(module: nn.Module) -> None: """Xavier init for Linear/Conv and constant=1 for BN weights.""" @@ -68,6 +70,10 @@ def __init__(self, backbone_name: str, layers: List[str], patch_size: int = 3, p raise ValueError(f"Unsupported backbone: {backbone_name}") return_nodes = {l: l for l in self.layers} + self.channels = 0 + for layer in self.layers: + block = getattr(backbone, layer)[-1] + self.channels += (block.bn3 if hasattr(block, "bn3") else block.bn2).num_features self.feature_extractor = create_feature_extractor(backbone, return_nodes=return_nodes) self.feature_extractor.eval() for p in self.feature_extractor.parameters(): @@ -89,18 +95,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: return self.pooler(fused) def get_channels_dim(self, probe_hw: Tuple[int, int] = (256, 256)) -> int: - self.feature_extractor.eval() - with torch.no_grad(): - feats = self.feature_extractor(torch.rand(1, 3, probe_hw[0], probe_hw[1])) - return sum(v.shape[1] for v in feats.values()) + # Channel metadata avoids allocating a full-resolution probe forward. + return self.channels class FeatureAdapter(nn.Module): """1x1 conv projection (linear per spatial location).""" - def __init__(self, channel_dim: int): + def __init__(self, channel_dim: int, output_channels: Optional[int] = None): super().__init__() - self.projection = nn.Conv2d(channel_dim, channel_dim, kernel_size=1, stride=1) + self.projection = nn.Conv2d(channel_dim, output_channels or channel_dim, kernel_size=1, stride=1) self.apply(_init_weights) def forward(self, x: torch.Tensor) -> torch.Tensor: @@ -245,23 +249,35 @@ def __init__( adapt_cls_features: bool = False, input_size: Tuple[int, int] = (256, 256), pretrained_backbone: bool = True, + dino_layers: Optional[List[int]] = None, + feature_channels: Optional[int] = None, + backbone_precision: str = "float32", ): super().__init__() self.input_size = tuple(input_size) self.adapt_cls_features = bool(adapt_cls_features) self.layers = list(layers) if layers is not None else ["layer2", "layer3"] - self.feature_extractor = UpscalingFeatureExtractor( - backbone_name=backbone_name, - layers=self.layers, - patch_size=3, - pretrained=pretrained_backbone, - ) + self.is_dino = backbone_name in DINO_MODELS + if feature_channels is not None and (type(feature_channels) is not int or feature_channels < 1): + raise ValueError("feature_channels must be a positive integer.") + if feature_channels is not None and not self.adapt_cls_features: + raise ValueError("feature_channels requires adapt_cls_features=True for both heads.") + if self.is_dino: + self.feature_extractor = DinoFeatureExtractor( + backbone_name, layers=dino_layers if dino_layers is not None else [11], + pretrained=pretrained_backbone, precision=backbone_precision, + ) + else: + self.feature_extractor = UpscalingFeatureExtractor( + backbone_name=backbone_name, layers=self.layers, patch_size=3, + pretrained=pretrained_backbone, + ) channels = self.feature_extractor.get_channels_dim(probe_hw=self.input_size) - self.adaptor = FeatureAdapter(channels) - self.segdec = SegmentationDetectionModule(channel_dim=channels, stop_grad=stop_grad) + self.adaptor = FeatureAdapter(channels, feature_channels) + self.segdec = SegmentationDetectionModule(channel_dim=feature_channels or channels, stop_grad=stop_grad) self.anomaly_generator = AnomalyGenerator(noise_mean=0.0, noise_std=0.015, threshold=perlin_threshold) self.anomaly_map_generator = SSNAnomalyMapGenerator(sigma=4.0) @@ -285,6 +301,12 @@ def forward( generate_synthetic: Optional[bool] = None, ): out_hw = images.shape[-2:] + if self.is_dino: + images = pad_to_patch_grid(images) + if masks is not None: + if masks.shape[-2:] != out_hw: + raise ValueError("Input masks must match the unpadded image dimensions.") + masks = pad_to_patch_grid(masks) features = self.feature_extractor(images) # (B,C,Hf,Wf) adapted = self.adaptor(features) @@ -329,6 +351,7 @@ def forward( cls_feats = adapted if self.adapt_cls_features else features pred_map, pred_score = self.segdec(seg_features=seg_feats, cls_features=cls_feats) - pred_map = self.anomaly_map_generator(pred_map, final_size=out_hw).sigmoid() + pred_map = self.anomaly_map_generator(pred_map, final_size=images.shape[-2:]).sigmoid() + pred_map = pred_map[..., :out_hw[0], :out_hw[1]] pred_score = pred_score.sigmoid() return pred_map, pred_score diff --git a/anomaly_detection/pipeline.py b/anomaly_detection/pipeline.py index 4262d42..1b1296a 100644 --- a/anomaly_detection/pipeline.py +++ b/anomaly_detection/pipeline.py @@ -97,6 +97,7 @@ def _build_data(self): dataset_type=self.cfg["dataset_type"], val_ratio=float(self.cfg["val_ratio"]), seed=int(self.cfg["seed"]), + pin_memory=self.device.type == "cuda", ) self.data_module.setup() @@ -128,6 +129,8 @@ def setup(self): def train(self): print("\n=== Starting Training ===") train_loader = self.data_module.train_dataloader() + if self.cfg["model"] in ("padim", "patchcore"): + train_loader = self.data_module.fit_dataloader() val_loader = self.data_module.val_dataloader() self.trainer.fit( @@ -139,9 +142,13 @@ def train(self): def evaluate(self) -> Dict[str, Any]: print("\n=== Evaluating ===") - self.trainer.load_checkpoint("best_model.pth") + # Evaluation needs neither gradients nor Adam momentum buffers on GPU. + if self.trainer.optimizer is not None: + self.trainer.optimizer.zero_grad(set_to_none=True) + self.trainer.optimizer.state.clear() + self.trainer.load_checkpoint("best_model.pth", load_optimizer=False) - if self.cfg["model"] == "ssn": + if self.cfg["model"] != "fastflow": self.evaluator = SuperSimpleNetEvaluator(model=self.model, device=str(self.device)) else: self.evaluator = FastFlowEvaluator(model=self.model, device=str(self.device)) @@ -154,6 +161,7 @@ def evaluate(self) -> Dict[str, Any]: image_quantile=float(self.cfg["image_threshold_quantile"]), pixel_quantile=float(self.cfg["pixel_threshold_quantile"]), ) + del val_preds # Do not retain validation maps alongside all test maps in RAM. with open(self.save_dir / "calibration.json", "w", encoding="utf-8") as f: json.dump(calibration, f, indent=4) diff --git a/anomaly_detection/predict.py b/anomaly_detection/predict.py new file mode 100644 index 0000000..09317d0 --- /dev/null +++ b/anomaly_detection/predict.py @@ -0,0 +1,57 @@ +"""Reload any YAML-built detector and predict one image using saved transforms.""" + +import argparse +import math +from pathlib import Path + +from PIL import Image +import torch + +from anomaly_detection.builder import build_model +from anomaly_detection.pipeline import make_det_tf + + +def load_model(checkpoint_path, device="cpu"): + saved = torch.load(checkpoint_path, map_location="cpu", weights_only=True) + cfg = {**saved["experiment_cfg"], "pretrained_backbone": False} + model = build_model(cfg) + model.load_state_dict(saved["model_state_dict"], strict=True) + return model.to(device).eval(), cfg + + +@torch.no_grad() +def predict_image(checkpoint_path, image_path, device="cpu"): + model, cfg = load_model(checkpoint_path, device) + h, w = cfg["image_size"] + transform = make_det_tf(math.ceil(h / cfg["crop_scale"]), math.ceil(w / cfg["crop_scale"]), h, w) + with Image.open(image_path) as source: + tensor = transform(source.convert("RGB")).unsqueeze(0).to(device) + result = model(tensor) + if cfg["model"] == "fastflow": + # Match FastFlowEvaluator's default top-one-percent aggregation. + maps = result + flat = maps.flatten(1) + scores = flat.topk(max(1, int(0.01 * flat.shape[1])), dim=1).values.mean(1) + else: + maps, scores = result + return {"anomaly_map": maps.cpu(), "score": scores.cpu()} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--checkpoint", required=True) + parser.add_argument("--image", required=True) + parser.add_argument("--output", required=True, help="Output .pt file containing map and raw anomaly score.") + parser.add_argument("--device", choices=("cpu", "cuda"), default="cpu") + args = parser.parse_args() + output = Path(args.output) + if output.exists(): + raise FileExistsError(output) + prediction = predict_image(args.checkpoint, args.image, args.device) + output.parent.mkdir(parents=True, exist_ok=True) + torch.save(prediction, output) + print(f"Raw anomaly score: {prediction['score'].item():.6f}; saved {output}") + + +if __name__ == "__main__": + main() diff --git a/anomaly_detection/smoke.py b/anomaly_detection/smoke.py new file mode 100644 index 0000000..a454d57 --- /dev/null +++ b/anomaly_detection/smoke.py @@ -0,0 +1,120 @@ +"""Synthetic training/reload check with measured CUDA peak memory, not accuracy.""" + +import argparse +import gc +import json +import math +from pathlib import Path +from tempfile import TemporaryDirectory + +import torch + +from anomaly_detection.builder import build_model, build_trainer +from anomaly_detection.config import load_config + + +class SyntheticBatches: + def __init__(self, cfg): + self.batch_size = cfg["batch_size"] + self.size = cfg["image_size"] + self.count = max(2, cfg["accumulate_grad_batches"] + 1) + self.seed = cfg["seed"] + + def __len__(self): + return self.count + + def __iter__(self): + rng = torch.Generator().manual_seed(self.seed) + for _ in range(self.count): + # Generate one host batch at a time, not an entire synthetic dataset. + yield { + "image": torch.randn(self.batch_size, 3, *self.size, generator=rng), + "mask": torch.zeros(self.batch_size, 1, *self.size), + "label": torch.zeros(self.batch_size), + } + + +def run_smoke(cfg, device="cuda", memory_budget_gb=14.0): + if not math.isfinite(memory_budget_gb) or memory_budget_gb <= 0: + raise ValueError("memory_budget_gb must be finite and positive.") + if device not in ("cpu", "cuda"): + raise ValueError("device must be cpu or cuda.") + if device == "cuda" and not torch.cuda.is_available(): + raise RuntimeError("CUDA is unavailable. Install a Blackwell-compatible CUDA PyTorch build for RTX 5070 Ti.") + info = {"device": device, "torch": torch.__version__, "cuda_build": torch.version.cuda, + "backbone": cfg["backbone"], "image_size": cfg["image_size"], + "batch_size": cfg["batch_size"], "pretrained": cfg["pretrained_backbone"], + "backbone_precision": cfg["backbone_precision"]} + info["effective_backbone_precision"] = ( + cfg["backbone_precision"] if device == "cuda" and cfg["backbone"].startswith("dinov2") else "float32" + ) + if device == "cuda": + info.update(gpu=torch.cuda.get_device_name(), capability=torch.cuda.get_device_capability(), + total_gib=torch.cuda.get_device_properties(torch.cuda.current_device()).total_memory / 2**30) + torch.cuda.reset_peak_memory_stats() + torch.manual_seed(cfg["seed"]) + with TemporaryDirectory(prefix="anomaly-smoke-") as temporary: + model = build_model(cfg) + trainer = build_trainer(cfg, model, device, Path(temporary)) + loss = None + if cfg["model"] in ("padim", "patchcore"): + trainer.fit_statistics(SyntheticBatches(cfg)) + else: + loss = trainer.train_epoch(SyntheticBatches(cfg)) + if loss is not None and not math.isfinite(loss): + raise RuntimeError("Training produced a non-finite loss.") + frozen = model.feature_extractor if cfg["model"] == "ssn" else model.backbone + if any(p.requires_grad or p.grad is not None for p in frozen.parameters()): + raise RuntimeError("Backbone was not fully frozen.") + model.eval() + images = torch.randn(1, 3, *cfg["image_size"], device=device) + with torch.no_grad(): + prediction = model(images) + expected = tuple(x.cpu() for x in prediction) if isinstance(prediction, tuple) else (prediction.cpu(),) + if any(not torch.isfinite(x).all() for x in expected): + raise RuntimeError("Inference produced non-finite outputs.") + trainer.save_checkpoint("smoke.pth") + if trainer.optimizer is not None: + trainer.optimizer.zero_grad(set_to_none=True) + trainer.optimizer.state.clear() + # Do not keep two complete models or optimizer states on the GPU at reload. + del prediction, trainer, model, frozen + gc.collect() + checkpoint = torch.load(Path(temporary) / "smoke.pth", map_location="cpu", weights_only=True) + restored = build_model({**cfg, "pretrained_backbone": False}) + restored.load_state_dict(checkpoint["model_state_dict"], strict=True) + del checkpoint + restored.to(device).eval() + with torch.no_grad(): + prediction = restored(images) + actual = prediction if isinstance(prediction, tuple) else (prediction,) + for before, after in zip(expected, actual): + torch.testing.assert_close(before, after.cpu(), atol=1e-5, rtol=1e-4) + if actual[0].shape[-2:] != tuple(cfg["image_size"]): + raise RuntimeError("Reloaded anomaly map has incorrect image dimensions.") + info.update(loss=loss, reload_matches=True, map_shape=list(actual[0].shape)) + if device == "cuda": + torch.cuda.synchronize() + info.update(peak_allocated_gib=torch.cuda.max_memory_allocated() / 2**30, + peak_reserved_gib=torch.cuda.max_memory_reserved() / 2**30, + memory_budget_gib=memory_budget_gb) + if info["peak_reserved_gib"] > memory_budget_gb: + raise RuntimeError(f"CUDA memory budget exceeded: {json.dumps(info)}") + return info + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--config", required=True) + parser.add_argument("--device", choices=("cpu", "cuda"), default="cuda") + parser.add_argument("--memory-budget-gb", type=float, default=14.0, help="GiB reserved by PyTorch; leaves headroom on 16 GB cards.") + parser.add_argument("--no-pretrained", action="store_true", help="Offline architecture test, not a pretrained-feature validation.") + args = parser.parse_args(argv) + cfg = load_config(args.config) + if args.no_pretrained: + cfg["pretrained_backbone"] = False + print(json.dumps(run_smoke(cfg, args.device, args.memory_budget_gb), indent=2)) + + +if __name__ == "__main__": + main() diff --git a/anomaly_detection/training/accumulation.py b/anomaly_detection/training/accumulation.py new file mode 100644 index 0000000..0c2dafb --- /dev/null +++ b/anomaly_detection/training/accumulation.py @@ -0,0 +1,33 @@ +"""Sample-weighted accumulation without retaining microbatch graphs.""" + +import torch + + +class GradientAccumulator: + def __init__(self, optimizer, batches=1, max_norm=None): + if type(batches) is not int or batches < 1: + raise ValueError("accumulate_grad_batches must be a positive integer.") + self.optimizer, self.batches, self.max_norm = optimizer, batches, max_norm + self.parameters = [p for group in optimizer.param_groups for p in group["params"] if p.requires_grad] + self.samples = self.microbatches = 0 + self.optimizer.zero_grad(set_to_none=True) + + def backward(self, loss, sample_count): + (loss * sample_count).backward() + self.samples += sample_count + self.microbatches += 1 + if self.microbatches == self.batches: + self.step() + + def step(self): + """Also flush a final partial accumulation window at the end of an epoch.""" + if not self.samples: + return + for parameter in self.parameters: + if parameter.grad is not None: + parameter.grad.div_(self.samples) + if self.max_norm is not None: + torch.nn.utils.clip_grad_norm_(self.parameters, self.max_norm) + self.optimizer.step() + self.optimizer.zero_grad(set_to_none=True) + self.samples = self.microbatches = 0 diff --git a/anomaly_detection/training/fastflow.py b/anomaly_detection/training/fastflow.py index e35d386..415ca59 100644 --- a/anomaly_detection/training/fastflow.py +++ b/anomaly_detection/training/fastflow.py @@ -12,6 +12,7 @@ from tqdm import tqdm from anomaly_detection.training.losses import FastFlowNLLLoss +from anomaly_detection.training.accumulation import GradientAccumulator class FastFlowTrainer: @@ -30,6 +31,7 @@ def __init__( model_cfg: Optional[Dict[str, Any]] = None, loss_fn: Optional[nn.Module] = None, experiment_cfg: Optional[Dict[str, Any]] = None, + accumulate_grad_batches: int = 1, ): self.model = model.to(device) self.device = device @@ -43,7 +45,10 @@ def __init__( self.model_cfg = model_cfg or {} self.experiment_cfg = experiment_cfg or {} self.loss_fn = (loss_fn if loss_fn is not None else FastFlowNLLLoss()).to(device) + self.accumulate_grad_batches = accumulate_grad_batches + # Keep parameter-group layout compatible with existing checkpoints. Adam + # allocates no momentum buffers for frozen parameters with grad=None. self.optimizer = Adam(self.model.parameters(), lr=learning_rate, weight_decay=weight_decay) self.history = { @@ -64,6 +69,7 @@ def train_epoch(self, dataloader) -> float: total_loss = 0.0 num_batches = 0 + accumulator = GradientAccumulator(self.optimizer, self.accumulate_grad_batches) pbar = tqdm(dataloader, desc="Training") for batch in pbar: images = batch["image"].to(self.device) @@ -71,14 +77,13 @@ def train_epoch(self, dataloader) -> float: hidden_vars, jacobians = self.model(images) # anomalib-style output in train loss = self._fastflow_loss(hidden_vars, jacobians) - self.optimizer.zero_grad(set_to_none=True) - loss.backward() - self.optimizer.step() + accumulator.backward(loss, images.shape[0]) total_loss += float(loss.item()) num_batches += 1 pbar.set_postfix({"loss": f"{loss.item():.4f}"}) + accumulator.step() return total_loss / max(1, num_batches) @torch.no_grad() @@ -177,11 +182,12 @@ def save_checkpoint(self, filename: str): } torch.save(checkpoint, self.save_dir / filename) - def load_checkpoint(self, filename: str): + def load_checkpoint(self, filename: str, load_optimizer: bool = True): path = self.save_dir / filename - checkpoint = torch.load(path, map_location=self.device) + checkpoint = torch.load(path, map_location="cpu", weights_only=True) self.model.load_state_dict(checkpoint["model_state_dict"]) - self.optimizer.load_state_dict(checkpoint["optimizer_state_dict"]) + if load_optimizer: + self.optimizer.load_state_dict(checkpoint["optimizer_state_dict"]) self.history = checkpoint["history"] self.model_cfg = checkpoint.get("model_cfg", self.model_cfg) print(f"Checkpoint loaded from {path}") diff --git a/anomaly_detection/training/feature_heads.py b/anomaly_detection/training/feature_heads.py new file mode 100644 index 0000000..3d2e711 --- /dev/null +++ b/anomaly_detection/training/feature_heads.py @@ -0,0 +1,124 @@ +"""Normal-only fitting and reconstruction training for frozen-feature heads.""" + +from pathlib import Path + +import torch +from torch import nn +from torch.nn import functional as F + +from anomaly_detection.training.accumulation import GradientAccumulator + + +class ReconstructionLoss(nn.Module): + def __init__(self, name="cosine", cosine_weight=1.0, mse_weight=1.0): + super().__init__() + self.name, self.cosine_weight, self.mse_weight = name, cosine_weight, mse_weight + + def forward(self, targets, predictions): + values = [] + for target, prediction in zip(targets, predictions): + target, prediction = target.detach().float(), prediction.float() + cosine = (1 - F.cosine_similarity(target, prediction, dim=-1)).mean() + if self.name == "cosine": + value = cosine + elif self.name == "mse": + value = F.mse_loss(prediction, target) + elif self.name == "smooth_l1": + value = F.smooth_l1_loss(prediction, target) + else: + value = self.cosine_weight * cosine + self.mse_weight * F.mse_loss(prediction, target) + values.append(value) + return sum(values) / len(values) + + +class FeatureHeadTrainer: + def __init__(self, model, device, save_dir, loss_fn, experiment_cfg, model_cfg, learning_rate, + weight_decay, accumulate_grad_batches, **kwargs): + self.model, self.device = model.to(device), device + self.save_dir = Path(save_dir) + self.save_dir.mkdir(parents=True, exist_ok=True) + self.loss_fn, self.experiment_cfg, self.model_cfg = loss_fn, experiment_cfg, model_cfg + self.accumulate_grad_batches = accumulate_grad_batches + self.optimizer = None if loss_fn is None else torch.optim.AdamW( + (p for p in model.parameters() if p.requires_grad), lr=learning_rate, weight_decay=weight_decay) + self.history = {} + + def normal_images(self, loader): + for batch in loader: + images = batch["image"] + if "label" in batch: + images = images[batch["label"].view(-1) == 0] + if len(images): + yield images.to(self.device) + + def fit_statistics(self, loader): + self.model.eval() + with torch.no_grad(): + self.model.fit_features(self.model.features(images) for images in self.normal_images(loader)) + + def train_epoch(self, dataloader): + if self.optimizer is None: + raise RuntimeError("PaDiM/PatchCore fit statistics; call fit_statistics, not train_epoch.") + self.model.train() + accumulator = GradientAccumulator(self.optimizer, self.accumulate_grad_batches, max_norm=1.0) + total, count = 0.0, 0 + for images in self.normal_images(dataloader): + targets, predictions, _ = self.model.reconstruct(images) + loss = self.loss_fn(targets, predictions) + if not torch.isfinite(loss): + raise RuntimeError("Non-finite reconstruction loss.") + accumulator.backward(loss, len(images)) + total += loss.item() * len(images) + count += len(images) + accumulator.step() + if not count: + raise ValueError("No normal training images.") + return total / count + + @torch.no_grad() + def validate(self, loader): + self.model.eval() + total, count = 0.0, 0 + for images in self.normal_images(loader): + targets, predictions, _ = self.model.reconstruct(images) + loss = self.loss_fn(targets, predictions) + total += loss.item() * len(images) + count += len(images) + if not count: + raise ValueError("No normal validation images.") + return total / count + + def fit(self, train_loader, val_loader, num_epochs=100, patience=10): + if self.optimizer is None: + self.fit_statistics(train_loader) + self.history = {"fit_passes": 1} + self.save_checkpoint("best_model.pth") + return + best, stale = float("inf"), 0 + for epoch in range(num_epochs): + train_loss = self.train_epoch(train_loader) + val_loss = self.validate(val_loader) + if not torch.isfinite(torch.tensor(val_loss)): + raise RuntimeError("Non-finite validation loss.") + self.history = {"epoch": epoch + 1, "train_loss": train_loss, "val_loss": val_loss} + print(f"Epoch {epoch + 1}: train={train_loss:.6f}, val={val_loss:.6f}") + if val_loss < best: + best, stale = val_loss, 0 + self.save_checkpoint("best_model.pth") + else: + stale += 1 + if stale >= patience: + break + + def save_checkpoint(self, filename): + torch.save({"model_state_dict": self.model.state_dict(), "experiment_cfg": self.experiment_cfg, + "model_cfg": self.model_cfg, "history": self.history, + "optimizer_state_dict": self.optimizer.state_dict() if self.optimizer else None}, + self.save_dir / filename) + + def load_checkpoint(self, filename, strict=True, load_optimizer=True): + checkpoint_data = torch.load(self.save_dir / filename, map_location="cpu", weights_only=True) + self.model.load_state_dict(checkpoint_data["model_state_dict"], strict=strict) + if load_optimizer and self.optimizer and checkpoint_data.get("optimizer_state_dict"): + self.optimizer.load_state_dict(checkpoint_data["optimizer_state_dict"]) + self.history = checkpoint_data.get("history", {}) diff --git a/anomaly_detection/training/losses.py b/anomaly_detection/training/losses.py index 15ec54d..8cd3331 100644 --- a/anomaly_detection/training/losses.py +++ b/anomaly_detection/training/losses.py @@ -56,12 +56,42 @@ def forward(self, pred_map_logits, pred_score_logits, target_mask, target_label) class SSNBCELoss(nn.Module): """Weighted segmentation/classification BCE on logits, for SSN ablations.""" - def __init__(self, seg_weight=1.0, cls_weight=1.0): + def __init__(self, seg_weight=1.0, cls_weight=1.0, seg_pos_weight=1.0, cls_pos_weight=1.0): super().__init__() self.seg_weight = float(seg_weight) self.cls_weight = float(cls_weight) + self.register_buffer("seg_pos_weight", torch.tensor(float(seg_pos_weight))) + self.register_buffer("cls_pos_weight", torch.tensor(float(cls_pos_weight))) def forward(self, pred_map_logits, pred_score_logits, target_mask, target_label): - segmentation = F.binary_cross_entropy_with_logits(pred_map_logits, target_mask) - classification = F.binary_cross_entropy_with_logits(pred_score_logits, target_label) + segmentation = F.binary_cross_entropy_with_logits(pred_map_logits, target_mask, pos_weight=self.seg_pos_weight) + classification = F.binary_cross_entropy_with_logits(pred_score_logits, target_label, pos_weight=self.cls_pos_weight) return self.seg_weight * segmentation + self.cls_weight * classification + + +def soft_dice_loss(logits, target, smooth=1e-6): + """Per-image soft Dice on segmentation probabilities; reductions stay FP32.""" + probability = logits.float().sigmoid().flatten(1) + target = target.float().flatten(1) + overlap = (probability * target).sum(1) + return (1 - (2 * overlap + smooth) / (probability.sum(1) + target.sum(1) + smooth)).mean() + + +class SSNBCEDiceLoss(SSNBCELoss): + def __init__(self, dice_weight=1.0, dice_smooth=1e-6, **kwargs): + super().__init__(**kwargs) + self.dice_weight, self.dice_smooth = float(dice_weight), float(dice_smooth) + + def forward(self, pred_map_logits, pred_score_logits, target_mask, target_label): + return (super().forward(pred_map_logits, pred_score_logits, target_mask, target_label) + + self.dice_weight * soft_dice_loss(pred_map_logits, target_mask, self.dice_smooth)) + + +class SSNFocalDiceLoss(SSNLoss): + def __init__(self, dice_weight=1.0, dice_smooth=1e-6, truncation_weight=0.0, **kwargs): + super().__init__(truncation_weight=truncation_weight, **kwargs) + self.dice_weight, self.dice_smooth = float(dice_weight), float(dice_smooth) + + def forward(self, pred_map_logits, pred_score_logits, target_mask, target_label): + return (super().forward(pred_map_logits, pred_score_logits, target_mask, target_label) + + self.dice_weight * soft_dice_loss(pred_map_logits, target_mask, self.dice_smooth)) diff --git a/anomaly_detection/training/supersimplenet.py b/anomaly_detection/training/supersimplenet.py index 4267c90..a57c9e3 100644 --- a/anomaly_detection/training/supersimplenet.py +++ b/anomaly_detection/training/supersimplenet.py @@ -21,6 +21,7 @@ from tqdm import tqdm from anomaly_detection.training.losses import SSNLoss +from anomaly_detection.training.accumulation import GradientAccumulator # ============================================================================= @@ -101,6 +102,7 @@ def __init__( adaptor_weight_decay: float = 0.01, loss_fn: Optional[nn.Module] = None, experiment_cfg: Optional[Dict[str, Any]] = None, + accumulate_grad_batches: int = 1, ): self.model = model.to(device) self.device = device @@ -113,6 +115,7 @@ def __init__( self.model_cfg = model_cfg or {} self.experiment_cfg = experiment_cfg or {} + self.accumulate_grad_batches = accumulate_grad_batches # anomalib-style optimizer: two param groups adaptor_params = list(getattr(self.model, "adaptor").parameters()) @@ -141,6 +144,7 @@ def train_epoch(self, dataloader) -> float: total = 0.0 n = 0 + accumulator = GradientAccumulator(self.optimizer, self.accumulate_grad_batches, max_norm=1.0) pbar = tqdm(dataloader, desc="Training(SSN)") for batch in pbar: images = batch["image"].to(self.device) @@ -158,15 +162,13 @@ def train_epoch(self, dataloader) -> float: ) loss = self.loss_fn(pred_map_logits, pred_score_logits, tgt_mask, tgt_label) - self.optimizer.zero_grad(set_to_none=True) - loss.backward() - torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=1.0) - self.optimizer.step() + accumulator.backward(loss, images.shape[0]) total += float(loss.item()) n += 1 pbar.set_postfix({"loss": f"{loss.item():.4f}"}) + accumulator.step() return total / max(1, n) @torch.no_grad() @@ -223,14 +225,14 @@ def save_checkpoint(self, filename: str, model_only: bool = False): } torch.save(ckpt, self.save_dir / filename) - def load_checkpoint(self, filename: str, strict: bool = True): - ckpt = torch.load(self.save_dir / filename, map_location=self.device) + def load_checkpoint(self, filename: str, strict: bool = True, load_optimizer: bool = True): + ckpt = torch.load(self.save_dir / filename, map_location="cpu", weights_only=True) if isinstance(ckpt, dict) and "model_state_dict" in ckpt: state = _strip_prefix_if_present(ckpt["model_state_dict"]) self.model.load_state_dict(state, strict=strict) - if "optimizer_state_dict" in ckpt: + if load_optimizer and "optimizer_state_dict" in ckpt: self.optimizer.load_state_dict(ckpt["optimizer_state_dict"]) if "history" in ckpt: self.history = ckpt["history"] diff --git a/configs/dinomaly_dinov2.yaml b/configs/dinomaly_dinov2.yaml new file mode 100644 index 0000000..28fdcce --- /dev/null +++ b/configs/dinomaly_dinov2.yaml @@ -0,0 +1,30 @@ +# Compact Dinomaly-style variant, not the original paper's complete recipe. +dataset: + path: ../data/MVTec + category: bottle +model: + name: dinomaly + backbone: dinov2_vits14 + pretrained: true + image_size: [512, 512] + crop_scale: 1.0 + params: + dino_layers: [2, 5, 8, 11] + backbone_precision: bfloat16 + decoder_depth: 4 + bottleneck_dropout: 0.2 + gradient_checkpointing: true +loss: + name: cosine # cosine | mse | smooth_l1 | cosine_mse +training: + device: cuda + batch_size: 1 + accumulate_grad_batches: 4 + epochs: 100 + num_workers: 0 + learning_rate: 0.0001 +evaluation: + num_visualizations: 0 +output: + save_dir: ../checkpoints + run_name: dinomaly_dino_512_cosine diff --git a/configs/fastflow_dinov2.yaml b/configs/fastflow_dinov2.yaml new file mode 100644 index 0000000..842a156 --- /dev/null +++ b/configs/fastflow_dinov2.yaml @@ -0,0 +1,36 @@ +# Frozen DINOv2 + FastFlow variant. Measure memory before full training. +dataset: + path: ../data/MVTec + category: bottle + +model: + name: fastflow + backbone: dinov2_vits14 + pretrained: true + image_size: [512, 512] # Also 416, 704, and rectangular sizes. + crop_scale: 1.0 + params: + dino_layers: [11] + feature_channels: 128 + backbone_precision: bfloat16 # Frozen backbone only; flow and NLL remain FP32. + flow_steps: 4 + hidden_ratio: 1.0 + +loss: + name: fastflow_nll + +training: + device: cuda + batch_size: 1 + accumulate_grad_batches: 4 + epochs: 100 + num_workers: 0 + learning_rate: 0.0001 + +evaluation: + num_visualizations: 0 + +output: + save_dir: ../checkpoints + run_name: fastflow_dinov2_512 + overwrite: false diff --git a/configs/padim_dinov2.yaml b/configs/padim_dinov2.yaml new file mode 100644 index 0000000..eb702f4 --- /dev/null +++ b/configs/padim_dinov2.yaml @@ -0,0 +1,28 @@ +# Experimental DINO features + PaDiM statistics; one fitting pass, no loss. +dataset: + path: ../data/MVTec + category: bottle +model: + name: padim + backbone: dinov2_vits14 + pretrained: true + image_size: [512, 512] + crop_scale: 1.0 + params: + dino_layers: [11] + backbone_precision: bfloat16 + padim_channels: 32 + covariance_regularization: 0.01 + fit_memory_mb: 512 +loss: + name: none +training: + device: cuda + batch_size: 1 + epochs: 1 + num_workers: 0 +evaluation: + num_visualizations: 0 +output: + save_dir: ../checkpoints + run_name: padim_dino_512 diff --git a/configs/patchcore_dinov2.yaml b/configs/patchcore_dinov2.yaml new file mode 100644 index 0000000..aa81703 --- /dev/null +++ b/configs/patchcore_dinov2.yaml @@ -0,0 +1,28 @@ +# Experimental DINO features + bounded-reservoir PatchCore variant. +dataset: + path: ../data/MVTec + category: bottle +model: + name: patchcore + backbone: dinov2_vits14 + pretrained: true + image_size: [512, 512] + crop_scale: 1.0 + params: + dino_layers: [11] + backbone_precision: bfloat16 + max_patches: 10000 + memory_bank_size: 1000 + distance_chunk_size: 256 +loss: + name: none +training: + device: cuda + batch_size: 1 + epochs: 1 + num_workers: 0 +evaluation: + num_visualizations: 0 +output: + save_dir: ../checkpoints + run_name: patchcore_dino_512 diff --git a/configs/ssn_dinov2.yaml b/configs/ssn_dinov2.yaml new file mode 100644 index 0000000..59e7fea --- /dev/null +++ b/configs/ssn_dinov2.yaml @@ -0,0 +1,42 @@ +# python train.py --config configs/ssn_dinov2.yaml +# Conservative STARTING profile, not a measured 16 GB guarantee. +dataset: + path: ../data/MVTec + category: bottle + +model: + name: ssn + backbone: dinov2_vits14 # Also dinov2_vitb14; higher memory cost. + pretrained: true + image_size: [512, 512] # Also [416, 416], [704, 704], or rectangular sizes. + crop_scale: 1.0 # Preserve the full resized field of view. + params: + dino_layers: [11] # Zero-based transformer block indices, not ResNet stages. + feature_channels: 128 + adapt_cls_features: true + backbone_precision: bfloat16 # CUDA only; CPU uses float32. + +loss: + name: ssn_bce_dice # ssn_bce | ssn_focal | ssn_bce_dice | ssn_focal_dice + params: + seg_weight: 1.0 + cls_weight: 1.0 + seg_pos_weight: 1.0 + cls_pos_weight: 1.0 + dice_weight: 1.0 + +training: + device: cuda # Fail explicitly if CUDA is missing; never silently use CPU. + batch_size: 1 + accumulate_grad_batches: 4 + epochs: 100 + num_workers: 0 # Conservative host RAM / Windows starting setting. + learning_rate: 0.0001 + +evaluation: + num_visualizations: 0 + +output: + save_dir: ../checkpoints + run_name: dinov2_512_bce_dice + overwrite: false diff --git a/docs/detector_choices.md b/docs/detector_choices.md new file mode 100644 index 0000000..1f23717 --- /dev/null +++ b/docs/detector_choices.md @@ -0,0 +1,110 @@ +# Backbone, detector and loss selection + +Select a complete detector with `model.name`, its frozen feature extractor with +`model.backbone`, and a compatible objective with `loss.name`. These detectors +have different fitting contracts; they are not interchangeable logit heads. + +| Detector | Backbones | Loss choices | Procedure | +| --- | --- | --- | --- | +| `fastflow` | All three ResNets; DINOv2 small/base | `fastflow_nll` | Train flow likelihood | +| `ssn` | All three ResNets; DINOv2 small/base | `ssn_focal`, `ssn_bce`, `ssn_bce_dice`, `ssn_focal_dice` | Train synthetic-anomaly segmentation/classification | +| `padim` | All three ResNets; DINOv2 small/base | `none` | Fit regularized per-location Gaussian statistics once | +| `patchcore` | All three ResNets; DINOv2 small/base | `none` | Fit patch reservoir and greedy coreset once | +| `dinomaly` | DINOv2 small/base | `cosine`, `mse`, `smooth_l1`, `cosine_mse` | Train feature reconstruction | + +Backbone names: `resnet18`, `resnet34`, `wide_resnet50_2`, `dinov2_vits14`, +`dinov2_vitb14`. DINO selectors are zero-based `dino_layers` indices 0–11. +The name DINO here means these two **DINOv2** architectures; DINOv1, DINOv3, +register-token and large/giant models are not implemented by this selector. +Incompatible losses fail validation before model construction. PaDiM and +PatchCore have no optimizer; `epochs` does not repeat their one fitting pass and +gradient accumulation must be 1. Their fitting loader uses deterministic +preprocessing and only the training partition; held-out validation calibrates +thresholds, and the test partition is used only for final evaluation. + +## Run an example + +Edit the dataset path/category and choose an example: + +```bash +python train.py --config configs/padim_dinov2.yaml --check-config +python train.py --config configs/padim_dinov2.yaml +``` + +Other examples: `patchcore_dinov2.yaml`, `dinomaly_dinov2.yaml`, +`fastflow_dinov2.yaml`, `ssn_dinov2.yaml`. Set `model.image_size` to +`[416, 416]`, `[512, 512]`, `[704, 704]` or another supported size. DINO pads to +the patch grid and crops the final map back to the requested dimensions. +For `cosine_mse`, `loss.params` accepts nonnegative `cosine_weight` and +`mse_weight` (both default 1; at least one must be positive). MSE and Smooth L1 +act on reconstructed features, not binary defect masks. Changing the loss +requires an accuracy experiment; no loss is promised to improve every dataset. + +All YAML-built detectors can reload without downloading weights: + +```bash +python -m anomaly_detection.predict --checkpoint path/to/best_model.pth \ + --image path/to/image.png --output prediction.pt --device cuda +``` + +The output contains the raw anomaly score and map at the configured input size. +It does not apply calibration or restore the source image's original size. + +## Implementation scope and memory + +These new combinations are experimental local variants, not wrappers around +anomalib and not claims to reproduce published benchmark scores: + +- **FastFlow + DINO:** selected blocks are concatenated on their native patch + grid and processed as one feature level. Optional even `feature_channels` + projects into a smaller space before the flow. The flow models that projected + distribution; it is not the original ResNet pyramid. +- **PaDiM:** randomly selects up to `padim_channels` feature channels (default + 32), fits streaming FP64 statistics on CPU, and stores FP32 means/inverse + covariances. `covariance_regularization` defaults to 0.01. The covariance + allocation estimate is checked against `fit_memory_mb` (default 512 MiB). + This is an allocation guard, not a total-process RAM limit. At least two + normal training images are required. Fixed-position statistics may be + unsuitable for poorly aligned objects. +- **PatchCore:** locally pools features, retains a CPU random-priority reservoir + capped by `max_patches` (default 10,000), then selects up to + `memory_bank_size` (1,000) patches by greedy k-center. Query and bank chunks + are bounded by `distance_chunk_size` (256); no full pairwise distance matrix + is constructed. Scores use the maximum nearest-neighbor distance, without + the original paper's image-score reweighting. A bounded reservoir trades + coverage for memory. Large banks can still take substantial fitting time. +- **Dinomaly-style:** frozen normalized DINO patch features feed a dropout MLP + and linear-attention reconstruction decoder with two layer-fusion groups. + `decoder_depth` defaults to 4 and must be at least 2; `bottleneck_dropout` + defaults to 0.2. `gradient_checkpointing: true` recomputes decoder activations + during backward. This compact variant omits original register/class-token + processing, original hard-mining hooks, StableAdamW and its learning-rate + schedule. It uses AdamW and the chosen reconstruction loss. Anomaly maps use + cosine reconstruction distance for every loss. It is **not the exact + official Dinomaly implementation**. + +All backbones remain frozen/eval. CUDA BF16 is confined to DINO extraction; +trainable heads, covariance evaluation, nearest-neighbor distances and losses +use FP32. Start with batch size 1 on a 16 GB GPU. Accumulation is available for +FastFlow, SSN and Dinomaly. Larger images, ViT-B and extra selected blocks still +increase memory; no arbitrary-size or 16 GB fit guarantee is made. + +Measure each configuration on the actual GPU: + +```bash +python -m anomaly_detection.smoke --config configs/dinomaly_dinov2.yaml --memory-budget-gb 14 +``` + +The smoke command also supports PaDiM/PatchCore fitting and checkpoint reload +(reported `loss: null` means no training objective). It uses a small synthetic +dataset, so it does not saturate a large PatchCore reservoir or measure accuracy. +Monitor host RAM and GPU memory during full fitting too. See +[GPU setup and geometry](gpu_memory.md). CPU software tests are not RTX 5070 Ti +hardware certification. + +## Algorithm references + +- [PaDiM paper](https://arxiv.org/abs/2011.08785) +- [PatchCore reference implementation](https://github.com/amazon-science/patchcore-inspection) +- [Official Dinomaly implementation](https://github.com/guojiajeremy/Dinomaly) +- [Anomalib Dinomaly architecture reference](https://anomalib.readthedocs.io/en/latest/markdown/guides/reference/models/image/dinomaly.html) diff --git a/docs/gpu_memory.md b/docs/gpu_memory.md new file mode 100644 index 0000000..231c084 --- /dev/null +++ b/docs/gpu_memory.md @@ -0,0 +1,144 @@ +# DINOv2, large images and the RTX 5070 Ti + +## Scope + +FastFlow, SSN, PaDiM, PatchCore and the Dinomaly-style variant support +`dinov2_vits14` and `dinov2_vitb14` through timm. All except Dinomaly also support +the three existing ResNet backbones. See [detector choices](detector_choices.md) +for fitting, losses and implementation scope. Unsupported combinations fail early. + +All backbones are frozen and kept in evaluation mode. SSN, FastFlow and Dinomaly +train their downstream modules; PaDiM/PatchCore fit statistics without gradients. +DINOv2 uses timm's native PyTorch attention; +there is no xFormers dependency or runtime import of Python from a GitHub URL. +Pretrained weights download from the official timm Hugging Face model repository +on first use. Checkpoints include the backbone weights, so inference rebuilds +without a download. `pretrained: false` is for offline software tests, not a +substitute for pretrained features in an anomaly-detection experiment. + +Prefer the full `best_model.pth` checkpoint with metadata. For a state-dict-only +file, SSN inference also accepts `--dino_layers`, `--feature_channels` and the +existing architecture flags explicitly. `--backbone_precision float32` can +override a full checkpoint's BF16 execution setting on another GPU; this changes +execution precision, not the stored model weights. + +## Conservative starting profile + +Edit the dataset in `configs/ssn_dinov2.yaml`, then run: + +```bash +python train.py --config configs/ssn_dinov2.yaml --check-config +python train.py --config configs/ssn_dinov2.yaml +``` + +The profile uses ViT-S/14, one transformer block (`dino_layers: [11]`), a trainable +128-channel projection, batch size 1, and accumulation over four microbatches. +Change `model.image_size` to `[416, 416]`, `[512, 512]`, `[704, 704]` or a +rectangular size. Use a different `output.run_name` for each experiment. +There is no unlimited-resolution or 16 GB fit guarantee. ViT-B, more feature +blocks, larger batches and larger resolutions need separate memory measurements. + +DINO block indices are zero-based, ordered and unique (0 through 11). The existing +`layers` option refers only to ResNet stages. DINO retains the native patch grid +instead of upscaling and concatenating large high-channel feature maps. Channel +projection requires `adapt_cls_features: true`, so both SSN heads consume the +projected features. This is a configurable SSN variant, not a claim of improved +accuracy over the original ResNet implementation. + +## Padding and coordinate alignment + +| Requested image | DINO padded input | Returned anomaly map | +| --- | --- | --- | +| 416 x 416 | 420 x 420 | 416 x 416 | +| 512 x 512 | 518 x 518 | 512 x 512 | +| 704 x 704 | 714 x 714 | 704 x 704 | +| 416 x 512 | 420 x 518 | 416 x 512 | + +After the existing resize/crop/ImageNet normalization, the model zero-pads only +the bottom/right edges to multiples of 14. Input masks receive the same padding. +The output map is interpolated to the padded dimensions and then cropped, not +squeezed into the original dimensions. Padding still participates in attention; +it is not an attention-masked region. The profile uses `crop_scale: 1.0` to keep +the full resized field of view. Larger input sizes should be evaluated for both +small-defect sensitivity and runtime; more pixels are not automatically better. + +## Memory behavior + +- No backbone gradients or retained backbone backward graph. +- `backbone_precision: bfloat16` autocasts only DINO's frozen feature extraction + on a CUDA device that supports BF16. CPU falls back to FP32. Heads and losses + remain FP32; FastFlow likelihood/log-determinant calculations are not put in + reduced precision. +- `feature_channels: 128` projects channels before synthetic-feature duplication + and the heads. `null` preserves the original channel count. +- `training.accumulate_grad_batches` works for FastFlow, SSN and Dinomaly. Each microbatch + is backpropagated immediately; gradients are sample-weighted, and a final + partial window is flushed. No list of computation graphs is retained. +- Accumulation is not mathematically identical to a larger physical batch for + models with BatchNorm, stochastic augmentation or synthetic anomaly generation. +- ResNet and DINO channel dimensions are obtained without full-image probe passes. +- Frozen parameters receive no gradient or Adam momentum buffers; gradients are + cleared with `set_to_none`. FastFlow retains its historical parameter-group + layout for optimizer-checkpoint compatibility. +- Checkpoints deserialize on CPU. Final evaluation releases gradients and + optimizer states and does not reload optimizer states to GPU. +- Validation predictions are released before test predictions are collected. + Exact AUROC still retains prediction arrays in host RAM; RAM use scales with + dataset size. CPU DataLoaders do not unnecessarily pin memory. +- The example uses zero loader workers to limit host prefetch memory. Increase + workers only after checking host RAM and throughput. + +## CUDA installation and verification + +The desktop RTX 5070 Ti has 16 GB memory and compute capability 12.0 (Blackwell). +Use a current NVIDIA driver and a matching CUDA-enabled PyTorch/torchvision pair +with Blackwell support. PyTorch 2.7 introduced support with CUDA 12.8 wheels; +the general project dependency floor alone does not guarantee GPU compatibility. +In a clean environment, a CUDA 12.8 wheel installation can be selected explicitly: + +```bash +python -m pip install "torch>=2.7,<3" "torchvision>=0.22,<1" --index-url https://download.pytorch.org/whl/cu128 +python -m pip install -r requirements.txt +``` + +Use the official PyTorch installer for another supported CUDA build or platform. +Do not assume that an older CUDA 11.8/12.1 build supports this card. + +Measure the actual configuration before full training: + +```bash +python -m anomaly_detection.smoke --config configs/ssn_dinov2.yaml --memory-budget-gb 14 +``` + +This command ignores the dataset and output paths. It generates synthetic images, +runs enough batches for optimizer initialization and a partial accumulation +window, checks frozen gradients, and saves/reloads a temporary checkpoint. +It checks map dimensions, finite outputs and matching reloaded predictions. +It reports GPU identity, CUDA build and peak PyTorch allocated/reserved GiB, and +fails on CUDA absence, a memory-budget excess, or a training/reload error. The +temporary checkpoint is removed automatically. The 14 GiB default leaves some +headroom; other applications and non-PyTorch allocations are not fully included +in PyTorch's allocator statistics. Close GPU-heavy applications when measuring. + +Repeat after changing the backbone, loss, feature blocks, image size or batch size. +If memory is insufficient, reduce physical batch size, selected blocks or feature +channels; accumulation cannot reduce the memory of an individual image. + +Offline CPU smoke test (no pretrained download, no GPU-memory measurement): + +```bash +python -m anomaly_detection.smoke --config configs/ssn_dinov2.yaml --device cpu --no-pretrained +``` + +Software tests exercise real randomly initialized DINO models at 416, 512 and 704 +pixels and rectangular inputs. They do not establish pretrained anomaly accuracy +or certify a run on a physical RTX 5070 Ti. Run the CUDA command on that card. + +## References + +- [timm DINOv2 ViT-S/14 model and weights](https://huggingface.co/timm/vit_small_patch14_dinov2.lvd142m) +- [DINOv2 reference implementation](https://github.com/facebookresearch/dinov2) +- [NVIDIA compute capabilities](https://developer.nvidia.com/cuda/gpus) +- [RTX 5070 family specifications](https://www.nvidia.com/en-us/geforce/graphics-cards/50-series/rtx-5070-family/) +- [PyTorch Blackwell / CUDA 12.8 introduction](https://pytorch.org/blog/pytorch-2-7/) +- [PyTorch installation selector](https://pytorch.org/get-started/locally/) diff --git a/docs/yaml_training.md b/docs/yaml_training.md index ef5896b..985b9c7 100644 --- a/docs/yaml_training.md +++ b/docs/yaml_training.md @@ -60,10 +60,12 @@ slashes (`C:/datasets/MVTec`) or single-quoted backslash paths. | Selection | Options | | --- | --- | -| `model.backbone` | `resnet18`, `resnet34`, `wide_resnet50_2` | -| `model.name` | `fastflow`, `ssn` | +| `model.backbone` | `resnet18`, `resnet34`, `wide_resnet50_2`, `dinov2_vits14`, `dinov2_vitb14` (Dinomaly requires DINOv2) | +| `model.name` | `fastflow`, `ssn`, `padim`, `patchcore`, `dinomaly` | | FastFlow `loss.name` | `fastflow_nll` | -| SSN `loss.name` | `ssn_focal`, `ssn_bce` | +| SSN `loss.name` | `ssn_focal`, `ssn_bce`, `ssn_bce_dice`, `ssn_focal_dice` | +| PaDiM / PatchCore `loss.name` | `none` (one fitting pass, no optimizer) | +| Dinomaly-style `loss.name` | `cosine`, `mse`, `smooth_l1`, `cosine_mse` | | `dataset.type` | `auto`, `mvtec`, `visa` | | `training.device` | `auto`, `cpu`, `cuda` | @@ -73,12 +75,20 @@ a claimed performance improvement. Arbitrary combinations are invalid: flow latents/Jacobians and SSN segmentation/classification logits have different loss contracts. An incompatible combination is rejected before building a model. -Both detectors support all three listed backbones. FastFlow uses ResNet stages +FastFlow, SSN, PaDiM and PatchCore support the three ResNet backbones. FastFlow uses ResNet stages 1–3; its `wide_resnet50_2` variant retains the existing channel reducers. SSN's feature stages are selectable. Pretrained initialization defaults to true; set `model.pretrained: false` for offline smoke tests. This is generally not a replacement for pretrained features in a real anomaly-detection experiment. +For DINOv2, start with `configs/ssn_dinov2.yaml`. The frozen transformer returns +patch-grid features, not ResNet stages. Bottom/right padding preserves coordinates +at non-multiples of 14, and output maps are cropped back to the requested size. +See [DINOv2 and GPU memory](gpu_memory.md) for the 416/512/704 configuration and +memory test. FastFlow concatenates selected DINO blocks into one flow feature +level. See [detector choices](detector_choices.md) for all new head parameters, +memory controls and experimental differences from the original algorithms. + ## Configuration sections Unspecified options use the defaults printed by `--check-config`. Unknown keys, @@ -89,11 +99,11 @@ duplicate keys, invalid types and unsupported combinations produce errors. | `dataset` | `path` and `category` (required); `type`, `val_ratio` | | `model` | `name`, `backbone`, `pretrained`, `image_size`, `crop_scale`, `params` | | `loss` | `name`, `params` | -| `training` | `epochs`, `batch_size`, `learning_rate`, `weight_decay`, `patience`, `num_workers`, `seed`, `device`; SSN-only `head_lr_multiplier`, `adaptor_weight_decay` | +| `training` | `epochs`, `batch_size`, `accumulate_grad_batches`, `learning_rate`, `weight_decay`, `patience`, `num_workers`, `seed`, `device`; SSN-only `head_lr_multiplier`, `adaptor_weight_decay` | | `evaluation` | `image_threshold_quantile`, `pixel_threshold_quantile`, `num_visualizations` | | `output` | `save_dir`, `run_name`, `overwrite` | -`image_size` is `[height, width]`, with both integers at least 32. FastFlow also +`image_size` is `[height, width]`, with both integers at least 32. FastFlow with ResNet also requires both to be divisible by 16. `crop_scale` must be in `(0, 1]` and `dataset.val_ratio` strictly between 0 and 1. The dataset needs at least two normal training images to make separate training and validation subsets. @@ -104,9 +114,15 @@ normal training images to make separate training and validation subsets. | --- | --- | | FastFlow | `flow_steps: 8`, `hidden_ratio: 1.0`, `clamp: 2.0`, `conv3x3_only: false` | | SSN | `layers: [layer2, layer3]`, `perlin_threshold: 0.2`, `adapt_cls_features: false` | +| SSN memory controls | `feature_channels: null` (positive integer requires `adapt_cls_features: true`) | +| FastFlow memory controls | `feature_channels: null` (or an even integer >= 2) | +| All DINOv2 detectors | `dino_layers: [11]` (ordered unique block indices 0–11), `backbone_precision: float32` (`bfloat16` optionally runs the frozen backbone in BF16 on CUDA) | SSN layers must be unique members of `layer1` through `layer4`, in shallow-to-deep -order. Parameters belonging to the other detector are rejected. +order. They are ResNet-only selectors; DINO uses `dino_layers`. The DINO selectors +retain their defaults when using ResNet. Parameters belonging to the other +detector are rejected. CPU DINO extraction always uses FP32; heads and losses +remain FP32 on both CPU and GPU. ### Loss parameters @@ -114,7 +130,9 @@ order. Parameters belonging to the other detector are rejected. | --- | --- | | `fastflow_nll` | None; sums Gaussian NLL minus log-Jacobian across feature levels | | `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` | +| `ssn_bce_dice` | BCE parameters plus `dice_weight: 1.0`, `dice_smooth: 1e-6` | +| `ssn_focal_dice` | Focal parameters plus `dice_weight: 1.0`, `dice_smooth: 1e-6`; `truncation_weight` defaults to `0.0` | SSN focal loss is the weighted sum of map focal loss, image focal loss and truncation loss on map probabilities. SSN BCE uses binary cross entropy with @@ -169,3 +187,24 @@ To add a new component in Python, extend the schema/compatibility tables and the matching factory, implement the required trainer/evaluator contract, and add validation and training tests. Merely adding an arbitrary name to YAML is not enough to support a new architecture. + +## Additional loss and accumulation choices + +`seg_pos_weight` and `cls_pos_weight` multiply positive-target BCE terms; they +must be positive and are independent of the overall segmentation/classification +weights. No weights are estimated from the test set. Choose them using training +data and a predefined validation protocol, rather than assuming a large value +will improve results. + +The Dice variants add a per-image soft segmentation Dice term: +`1 - (2 * sum(sigmoid(logits) * mask) + smooth) / (sum(sigmoid(logits)) + sum(mask) + smooth)`. +Classification still uses BCE or focal, respectively. Dice reductions are FP32, +`dice_smooth` is positive, and empty masks are handled without division by zero. +These are experimental objectives, not established accuracy improvements. + +`training.accumulate_grad_batches` defaults to 1. Larger values accumulate +sample-weighted gradients without retaining earlier microbatch graphs; the last +partial window is still stepped. BatchNorm and synthetic anomaly generation mean +this is not identical to increasing the physical batch size. Full-precision +training remains the default; the optional BF16 setting applies only to the +frozen DINO backbone, not the trainable heads or FastFlow likelihood calculations. diff --git a/inference.py b/inference.py index 3d95c7f..c86e6a2 100644 --- a/inference.py +++ b/inference.py @@ -375,6 +375,9 @@ def __init__( device: str, topk_ratio: float, crop_scale: float = 0.875, + dino_layers: Optional[List[int]] = None, + feature_channels: Optional[int] = None, + backbone_precision: Optional[str] = None, ): self.device = torch.device(device if (device == "cuda" and torch.cuda.is_available()) else "cpu") ckpt = torch.load(checkpoint_path, map_location="cpu") @@ -388,6 +391,7 @@ def __init__( pretrained_backbone = bool(model_cfg.get("pretrained_backbone", pretrained_backbone)) crop_scale = float(model_cfg.get("crop_scale", crop_scale)) reducer_channels = model_cfg.get("reducer_channels") + feature_channels = model_cfg.get("feature_channels", feature_channels) self.image_size = tuple(image_size) self.backbone = backbone @@ -395,29 +399,19 @@ def __init__( if not 0.0 < self.topk_ratio <= 1.0: raise ValueError(f"topk_ratio must be in (0, 1], got {self.topk_ratio}") - if backbone == "wide_resnet50_2": - self.model = FastFlowModel( - backbone_name=backbone, - flow_steps=flow_steps, - input_size=self.image_size, - hidden_ratio=hidden_ratio, - reducer_channels=tuple(reducer_channels or (128, 192, 256)), - clamp=clamp, - conv3x3_only=conv3x3_only, - # The checkpoint contains the frozen backbone weights; avoid a - # redundant network download before strict state loading. - pretrained_backbone=False, - ) - else: - self.model = FastFlowModel( - backbone_name=backbone, - flow_steps=flow_steps, - input_size=self.image_size, - hidden_ratio=hidden_ratio, - clamp=clamp, - conv3x3_only=conv3x3_only, - pretrained_backbone=False, - ) + if backbone == "wide_resnet50_2" and feature_channels is None and reducer_channels is None: + reducer_channels = (128, 192, 256) + self.model = FastFlowModel( + backbone_name=backbone, flow_steps=flow_steps, input_size=self.image_size, + hidden_ratio=hidden_ratio, reducer_channels=reducer_channels, + clamp=clamp, conv3x3_only=conv3x3_only, + # Frozen backbone weights are already in the checkpoint. + pretrained_backbone=False, + dino_layers=model_cfg.get("dino_layers", dino_layers), + feature_channels=feature_channels, + backbone_precision=(backbone_precision if backbone_precision is not None + else model_cfg.get("backbone_precision", "float32")), + ) state = ckpt["model_state_dict"] if isinstance(ckpt, dict) and "model_state_dict" in ckpt else ckpt self.model.load_state_dict(state, strict=True) @@ -760,6 +754,9 @@ def main(): parser.add_argument("--conv3x3_only", action="store_true") parser.add_argument("--pretrained_backbone", action=argparse.BooleanOptionalAction, default=True) parser.add_argument("--topk_ratio", type=float, default=0.01) + parser.add_argument("--dino_layers", type=int, nargs="+", default=None) + parser.add_argument("--feature_channels", type=int, default=None) + parser.add_argument("--backbone_precision", choices=["float32", "bfloat16"], default=None) parser.add_argument("--save_dir", type=str, default="./inference_results") parser.add_argument("--min_area", type=int, default=30) @@ -792,6 +789,9 @@ def main(): pretrained_backbone=bool(args.pretrained_backbone), device=args.device, topk_ratio=args.topk_ratio, + dino_layers=args.dino_layers, + feature_channels=args.feature_channels, + backbone_precision=args.backbone_precision, ) evaluator = MVTecFastFlowEvaluator( diff --git a/requirements.txt b/requirements.txt index e31662f..2010842 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ scikit-learn>=1.3,<2 tqdm>=4.66,<5 opencv-python-headless>=4.8,<5 PyYAML>=6.0,<7 +timm>=1.0.15,<1.1 diff --git a/ssn_inference.py b/ssn_inference.py index 7e83495..616b702 100644 --- a/ssn_inference.py +++ b/ssn_inference.py @@ -18,6 +18,7 @@ from PIL import Image from anomaly_detection.modeling import SuperSimpleNetModel +from anomaly_detection.config import BACKBONES # ============================================================ @@ -343,6 +344,9 @@ def __init__( pretrained_backbone: bool, device: str, crop_scale: float = 0.875, + dino_layers: Optional[List[int]] = None, + feature_channels: Optional[int] = None, + backbone_precision: Optional[str] = None, ): self.device = torch.device(device if (device == "cuda" and torch.cuda.is_available()) else "cpu") ckpt = torch.load(checkpoint_path, map_location="cpu") @@ -367,6 +371,10 @@ def __init__( input_size=self.image_size, # The checkpoint includes the frozen backbone weights. pretrained_backbone=False, + dino_layers=model_cfg.get("dino_layers", dino_layers), + feature_channels=model_cfg.get("feature_channels", feature_channels), + backbone_precision=(backbone_precision if backbone_precision is not None + else model_cfg.get("backbone_precision", "float32")), ) state = ckpt["model_state_dict"] if isinstance(ckpt, dict) and "model_state_dict" in ckpt else ckpt @@ -685,8 +693,11 @@ def main(): parser.add_argument("--category", type=str, required=True) # SSN model args (must match training) - parser.add_argument("--backbone", type=str, default="resnet18", choices=["resnet18", "resnet34", "wide_resnet50_2"]) + parser.add_argument("--backbone", type=str, default="resnet18", choices=BACKBONES) parser.add_argument("--layers", type=str, nargs="+", default=["layer2", "layer3"]) + parser.add_argument("--dino_layers", type=int, nargs="+", default=None) + parser.add_argument("--feature_channels", type=int, default=None) + parser.add_argument("--backbone_precision", choices=["float32", "bfloat16"], default=None) parser.add_argument("--perlin_threshold", type=float, default=0.2) parser.add_argument("--adapt_cls_features", action="store_true") parser.add_argument("--pretrained_backbone", action=argparse.BooleanOptionalAction, default=True) @@ -717,6 +728,9 @@ def main(): perlin_threshold=args.perlin_threshold, adapt_cls_features=args.adapt_cls_features, layers=list(args.layers), + dino_layers=args.dino_layers, + feature_channels=args.feature_channels, + backbone_precision=args.backbone_precision, pretrained_backbone=bool(args.pretrained_backbone), device=args.device, ) diff --git a/tests/test_dinov2.py b/tests/test_dinov2.py new file mode 100644 index 0000000..4e1b33a --- /dev/null +++ b/tests/test_dinov2.py @@ -0,0 +1,209 @@ +"""Real offline ViT tests: geometry, frozen features and trainable heads.""" + +from pathlib import Path + +import pytest +import torch +from torch.nn import functional as F + +from anomaly_detection.builder import build_model, build_loss +from anomaly_detection.config import ConfigError, as_yaml_config, load_config, resolve_config +from anomaly_detection.modeling.dinov2 import pad_to_patch_grid +from anomaly_detection.training.accumulation import GradientAccumulator + + +def dino_config(**changes): + return resolve_config({ + "data_path": "unused", "category": "bottle", "model": "ssn", "backbone": "dinov2_vits14", + "pretrained_backbone": False, "adapt_cls_features": True, "feature_channels": 128, + "image_size": [416, 416], "backbone_precision": "bfloat16", **changes, + }) + + +@pytest.mark.parametrize("height,width", [(416, 416), (512, 512), (704, 704), (416, 512)]) +def test_real_dino_large_image_training_and_output_geometry(height, width): + torch.set_num_threads(2) + cfg = dino_config(image_size=[height, width], loss_name="ssn_bce_dice") + model = build_model(cfg) + model.train() + backbone = model.feature_extractor + assert not backbone.training + assert not backbone.feature_extractor.training + images = torch.randn(1, 3, height, width) + logits, scores, masks, labels = model(images) + assert logits.shape == (2, 1, (height + 13) // 14, (width + 13) // 14) + loss = build_loss(cfg)(logits, scores, masks, labels) + assert torch.isfinite(loss) + loss.backward() + assert all(not p.requires_grad and p.grad is None for p in backbone.parameters()) + assert model.adaptor.projection.weight.grad is not None + assert torch.isfinite(model.adaptor.projection.weight.grad).all() + model.zero_grad(set_to_none=True) + model.eval() + with torch.no_grad(): + anomaly_map, score = model(images) + assert anomaly_map.shape == (1, 1, height, width) + assert score.shape == (1,) + assert torch.isfinite(anomaly_map).all() + + +def test_padding_keeps_coordinates_and_only_extends_bottom_right(): + image = torch.arange(416 * 512).reshape(1, 1, 416, 512).float() + padded = pad_to_patch_grid(image) + assert padded.shape[-2:] == (420, 518) + torch.testing.assert_close(padded[..., :416, :512], image) + assert padded[..., 416:, :].count_nonzero() == 0 + assert padded[..., :, 512:].count_nonzero() == 0 + + +def test_dino_rectangular_map_is_cropped_not_rescaled(monkeypatch): + model = build_model(dino_config(image_size=[32, 48])) + model.eval() + observed = {} + + def output_grid(logits, final_size): + observed["size"] = final_size + h, w = final_size + return torch.arange(h * w).reshape(1, 1, h, w).float() / (h * w) + + monkeypatch.setattr(model.anomaly_map_generator, "forward", output_grid) + with torch.no_grad(): + result, _ = model(torch.randn(1, 3, 32, 48)) + assert observed["size"] == (42, 56) + expected = torch.arange(42 * 56).reshape(1, 1, 42, 56).float() / (42 * 56) + torch.testing.assert_close(result, expected.sigmoid()[..., :32, :48]) + + +def test_base_backbone_and_multiple_blocks(): + model = build_model(dino_config(backbone="dinov2_vitb14", dino_layers=[8, 11], image_size=[32, 48])) + assert model.adaptor.projection.in_channels == 768 * 2 + model.eval() + with torch.no_grad(): + result, _ = model(torch.randn(1, 3, 32, 48)) + assert result.shape == (1, 1, 32, 48) + + +@pytest.mark.parametrize("changes", [ + {"model": "fastflow", "feature_channels": 127}, {"dino_layers": []}, {"dino_layers": [12]}, + {"dino_layers": [11, 8]}, {"dino_layers": [True]}, {"feature_channels": 0}, + {"adapt_cls_features": False}, {"backbone_precision": "float16"}, + {"accumulate_grad_batches": 0}, {"accumulate_grad_batches": True}, + {"layers": ["layer1"]}, {"backbone": "resnet18", "dino_layers": [8, 11]}, + {"loss_name": "ssn_bce", "loss_params": {"seg_pos_weight": 0}}, + {"loss_name": "ssn_bce_dice", "loss_params": {"dice_smooth": 0}}, +]) +def test_invalid_extensions_fail_before_model_construction(changes): + with pytest.raises(ConfigError): + dino_config(**changes) + + +@pytest.mark.parametrize("example", ["ssn_dinov2.yaml", "fastflow_dinov2.yaml", "padim_dinov2.yaml", + "patchcore_dinov2.yaml", "dinomaly_dinov2.yaml"]) +def test_example_roundtrip(tmp_path, example): + import yaml + + cfg = load_config(Path(__file__).resolve().parents[1] / "configs" / example) + path = tmp_path / "resolved.yaml" + path.write_text(yaml.safe_dump(as_yaml_config(cfg))) + assert load_config(path) == cfg + + +@pytest.mark.parametrize("loss_name", ["ssn_bce", "ssn_bce_dice", "ssn_focal_dice"]) +@pytest.mark.parametrize("target_value", [0., 1.]) +def test_loss_variants_have_finite_gradients_on_empty_and_full_masks(loss_name, target_value): + cfg = dino_config(loss_name=loss_name) + maps = torch.zeros(2, 1, 3, 3, requires_grad=True) + scores = torch.zeros(2, requires_grad=True) + loss = build_loss(cfg)(maps, scores, torch.full_like(maps, target_value), torch.full_like(scores, target_value)) + loss.backward() + assert torch.isfinite(loss) + assert torch.isfinite(maps.grad).all() and torch.isfinite(scores.grad).all() + + +def test_weighted_bce_matches_pytorch(): + cfg = dino_config(loss_name="ssn_bce", loss_params={"seg_pos_weight": 3., "cls_pos_weight": 2.}) + maps = torch.tensor([[[[-1., 2.]]]]) + scores = torch.tensor([0.4]) + mask, label = torch.tensor([[[[0., 1.]]]]), torch.ones(1) + expected = (F.binary_cross_entropy_with_logits(maps, mask, pos_weight=torch.tensor(3.)) + + F.binary_cross_entropy_with_logits(scores, label, pos_weight=torch.tensor(2.))) + torch.testing.assert_close(build_loss(cfg)(maps, scores, mask, label), expected) + + +def test_accumulation_matches_grouped_batches_and_flushes_short_window(): + small = torch.nn.Linear(2, 1, bias=False) + large = torch.nn.Linear(2, 1, bias=False) + large.load_state_dict(small.state_dict()) + small_opt, large_opt = torch.optim.SGD(small.parameters(), lr=.1), torch.optim.SGD(large.parameters(), lr=.1) + x, y = torch.randn(7, 2), torch.randn(7, 1) + accumulation = GradientAccumulator(small_opt, batches=3) + for start, end in [(0, 2), (2, 4), (4, 5), (5, 7)]: + accumulation.backward(F.mse_loss(small(x[start:end]), y[start:end]), end - start) + accumulation.step() + for start, end in [(0, 5), (5, 7)]: + large_opt.zero_grad(set_to_none=True) + F.mse_loss(large(x[start:end]), y[start:end]).backward() + large_opt.step() + torch.testing.assert_close(small.weight, large.weight) + assert all(p.grad is None for p in small.parameters()) + + +@pytest.mark.parametrize("loss_name", ["ssn_bce_dice", "ssn_focal_dice"]) +def test_dice_composite_matches_analytic_value(loss_name): + params = {"seg_weight": 2., "cls_weight": 3., "dice_weight": .25} + if loss_name == "ssn_focal_dice": + params.update(gamma=0., alpha=-1., truncation_weight=0.) + cfg = dino_config(loss_name=loss_name, loss_params=params) + loss = build_loss(cfg)(torch.zeros(1, 1, 1, 2), torch.zeros(1), + torch.tensor([[[[0., 1.]]]]), torch.ones(1)) + expected = 5 * torch.log(torch.tensor(2.)) + .25 * (1 - (1 + 1e-6) / (2 + 1e-6)) + torch.testing.assert_close(loss, expected) + + +@pytest.mark.parametrize("model_name", ["ssn", "fastflow"]) +def test_memory_smoke_runs_training_and_reload_on_cpu(model_name): + from anomaly_detection.smoke import run_smoke + + cfg = (dino_config(image_size=[32, 48], batch_size=1) if model_name == "ssn" else + resolve_config({"data_path": "unused", "category": "bottle", "image_size": [32, 48], + "pretrained_backbone": False, "flow_steps": 1, "batch_size": 1})) + result = run_smoke(cfg, device="cpu") + assert result["reload_matches"] + assert result["map_shape"] == [1, 1, 32, 48] + assert result["effective_backbone_precision"] == "float32" + assert "peak_reserved_gib" not in result + + +def test_cuda_smoke_fails_before_building_without_cuda(monkeypatch): + from anomaly_detection import smoke + + monkeypatch.setattr(torch.cuda, "is_available", lambda: False) + monkeypatch.setattr(smoke, "build_model", lambda cfg: pytest.fail("Must not construct a model")) + with pytest.raises(RuntimeError, match="CUDA is unavailable"): + smoke.run_smoke(dino_config()) + + +def test_raw_dino_checkpoint_inference_with_explicit_architecture(tmp_path): + from PIL import Image + from ssn_inference import SSNInferenceEngine + + cfg = dino_config(image_size=[32, 48], dino_layers=[8, 11], feature_channels=64) + model = build_model(cfg).eval() + checkpoint = tmp_path / "weights.pth" + torch.save(model.state_dict(), checkpoint) + image_path = tmp_path / "image.png" + Image.new("RGB", (48, 32), color=(30, 80, 120)).save(image_path) + engine = SSNInferenceEngine( + checkpoint_path=str(checkpoint), backbone="dinov2_vits14", image_size=(32, 48), + perlin_threshold=.2, adapt_cls_features=True, layers=["layer2", "layer3"], + pretrained_backbone=False, device="cpu", dino_layers=[8, 11], feature_channels=64, + backbone_precision="float32", + ) + score, anomaly_map, _ = engine.infer_one(str(image_path)) + assert anomaly_map.shape == (32, 48) + assert torch.isfinite(torch.tensor(score)) + x, _ = engine.preprocess(str(image_path)) + with torch.no_grad(): + expected, expected_score = model(x) + torch.testing.assert_close(torch.from_numpy(anomaly_map), expected[0, 0]) + assert score == pytest.approx(expected_score.item()) diff --git a/tests/test_feature_heads.py b/tests/test_feature_heads.py new file mode 100644 index 0000000..ae579ee --- /dev/null +++ b/tests/test_feature_heads.py @@ -0,0 +1,139 @@ +"""Real backbones, analytic fitting checks, loss gradients and YAML round trips.""" + +import pytest +import torch +import yaml + +from anomaly_detection.builder import build_model, build_loss +from anomaly_detection.config import resolve_config, ConfigError, as_yaml_config +from anomaly_detection.modeling.feature_heads import PadimModel, PatchcoreModel +from anomaly_detection.pipeline import AnomalyPipeline +from anomaly_detection.predict import predict_image +from anomaly_detection.smoke import run_smoke +from test_yaml_training import make_dataset + + +def config(model, **kwargs): + return resolve_config(dict(data_path="unused", category="bottle", model=model, backbone="dinov2_vits14", + pretrained_backbone=False, image_size=[32, 48], batch_size=1, num_workers=0, + padim_channels=4, max_patches=32, memory_bank_size=8, distance_chunk_size=7, + decoder_depth=2, **kwargs)) + + +@pytest.mark.parametrize("name", ["padim", "patchcore", "dinomaly"]) +@pytest.mark.parametrize("backbone", ["dinov2_vits14", "dinov2_vitb14"]) +def test_dino_heads_fit_train_reload(name, backbone): + torch.set_num_threads(2) + cfg = config(name) + cfg.update(backbone=backbone, dino_layers=[8, 11]) + report = run_smoke(cfg, device="cpu") + assert report["reload_matches"] + assert report["map_shape"] == [1, 1, 32, 48] + assert (report["loss"] is None) == (name != "dinomaly") + + +@pytest.mark.parametrize("head", ["padim", "patchcore"]) +@pytest.mark.parametrize("backbone", ["resnet18", "resnet34", "wide_resnet50_2"]) +def test_statistical_resnet_heads(head, backbone): + cfg = config(head) + cfg["backbone"] = backbone + assert run_smoke(cfg, device="cpu")["reload_matches"] + + +@pytest.mark.parametrize("name", ["cosine", "mse", "smooth_l1", "cosine_mse"]) +def test_reconstruction_loss_gradients_and_zero_at_match(name): + loss = build_loss(config("dinomaly", loss_name=name)) + target = torch.randn(2, 5, 8, requires_grad=True) + prediction = torch.randn_like(target, requires_grad=True) + value = loss([target], [prediction]) + value.backward() + assert target.grad is None + assert torch.isfinite(prediction.grad).all() and prediction.grad.abs().sum() > 0 + assert abs(loss([target], [target]).item()) < 1e-6 + + +@pytest.mark.parametrize("changes", [ + {"model": "padim", "loss_name": "mse"}, {"model": "patchcore", "loss_name": "ssn_bce"}, + {"model": "dinomaly", "backbone": "resnet18"}, {"model": "dinomaly", "decoder_depth": 1}, + {"model": "patchcore", "memory_bank_size": 20000}, {"model": "padim", "covariance_regularization": 0}, + {"model": "dinomaly", "loss_name": "cosine_mse", "loss_params": {"cosine_weight": 0, "mse_weight": 0}}, +]) +def test_invalid_combinations(changes): + cfg = config("dinomaly") + cfg.update(changes) + with pytest.raises(ConfigError): + resolve_config(cfg) + + +def test_padim_streaming_statistics_match_direct_covariance(): + model = PadimModel(backbone_name="resnet18", input_size=(32, 32), pretrained_backbone=False, padim_channels=3) + x = torch.randn(5, model.backbone.channels, 2, 2) + model.fit_features(iter((x[:2], x[2:4], x[4:]))) + selected = x[:, model.channel_indices].flatten(2).permute(2, 0, 1).double() + expected_mean = selected.mean(1) + delta = selected - expected_mean[:, None] + covariance = delta.transpose(1, 2) @ delta / 4 + 0.01 * torch.eye(3) + torch.testing.assert_close(model.mean, expected_mean.float()) + torch.testing.assert_close(model.precision, covariance.inverse().float()) + model.fit_memory_mb = 0 + with pytest.raises(ValueError, match="fit_memory_mb"): + model.fit_features(iter((x,))) + with pytest.raises(ValueError, match="at least two"): + model.fit_features(iter(())) + + +def test_patchcore_bank_bound_and_chunked_nearest_neighbors(monkeypatch): + model = PatchcoreModel(backbone_name="resnet18", input_size=(32, 32), pretrained_backbone=False, + max_patches=10, memory_bank_size=4, distance_chunk_size=3) + features = torch.randn(2, 6, 2, 2) + model.fit_features(iter((features, features + 2, features - 2))) + assert model.memory_bank.shape == (4, 6) + monkeypatch.setattr(model, "features", lambda images: features) + result, _ = model(torch.zeros(2, 3, 32, 32)) + exact = torch.cdist(model.embed(features), model.memory_bank).min(1).values.reshape(2, 1, 2, 2) + torch.testing.assert_close(result, model.output(exact)[0]) + + +@pytest.mark.parametrize("name", ["padim", "patchcore", "dinomaly"]) +def test_new_heads_full_yaml_pipeline_and_prediction(tmp_path, name): + make_dataset(tmp_path / "data") + cfg = config(name) + cfg.update(data_path=str(tmp_path / "data"), save_dir=str(tmp_path / "runs"), + num_epochs=1, device="cpu", num_visualizations=0) + path = tmp_path / "experiment.yaml" + path.write_text(yaml.safe_dump(as_yaml_config(cfg))) + from anomaly_detection.config import load_config + pipeline = AnomalyPipeline(load_config(path)) + metrics = pipeline.run() + assert 0 <= metrics["image_auroc"] <= 1 + assert set(pipeline.data_module.fit_dataset.indices).isdisjoint(pipeline.data_module.val_dataset.indices) + result = predict_image(pipeline.save_dir / "best_model.pth", tmp_path / "data/bottle/test/good/000.png") + assert result["anomaly_map"].shape == (1, 1, 32, 48) + assert torch.isfinite(result["score"]).all() + + +@pytest.mark.parametrize("name", ["fastflow", "padim", "patchcore", "dinomaly"]) +@pytest.mark.parametrize("size", [416, 512, 704]) +def test_large_dino_head_geometry(name, size): + cfg = config(name) + cfg.update(image_size=[size, size], flow_steps=1) + if name == "fastflow": + cfg["feature_channels"] = 32 + model = build_model(cfg) + images = torch.randn(1, 3, size, size) + if name in ("padim", "patchcore"): + model.fit_features(iter((model.features(images), model.features(images + 0.1)))) + elif name == "dinomaly": + target, prediction, _ = model.reconstruct(images) + build_loss(cfg)(target, prediction).backward() + assert any(p.grad is not None for p in model.decoder.parameters()) + else: + latent, jacobian = model(images) + build_loss(cfg)(latent, jacobian).backward() + assert all(p.grad is None and not p.requires_grad for p in model.backbone.parameters()) + model.eval() + with torch.no_grad(): + output = model(images) + maps = output[0] if isinstance(output, tuple) else output + assert maps.shape == (1, 1, size, size) + assert torch.isfinite(maps).all() diff --git a/tests/test_regressions.py b/tests/test_regressions.py index ad4e841..6d5d9e5 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -65,6 +65,7 @@ def test_fastflow_train_and_eval_output_contracts(): ) images = torch.rand(1, 3, 32, 32) model.train() + assert not model.backbone.training hidden, jacobians = model(images) assert len(hidden) == len(jacobians) == 3 diff --git a/tests/test_yaml_training.py b/tests/test_yaml_training.py index 5f0be4a..b9b41d2 100644 --- a/tests/test_yaml_training.py +++ b/tests/test_yaml_training.py @@ -89,16 +89,19 @@ def make_dataset(root): Image.fromarray(mask).save(mask_dir / "000_mask.png") -@pytest.mark.parametrize("model_name,loss_name", [ - ("fastflow", "fastflow_nll"), ("ssn", "ssn_focal"), ("ssn", "ssn_bce"), +@pytest.mark.parametrize("model_name,loss_name,backbone", [ + ("fastflow", "fastflow_nll", "resnet18"), ("ssn", "ssn_focal", "resnet18"), + ("ssn", "ssn_bce", "resnet18"), ("ssn", "ssn_bce_dice", "dinov2_vits14"), + ("ssn", "ssn_focal_dice", "dinov2_vits14"), + ("fastflow", "fastflow_nll", "dinov2_vits14"), ]) -def test_yaml_runs_real_training_and_existing_inference(tmp_path, monkeypatch, model_name, loss_name): +def test_yaml_runs_real_training_and_existing_inference(tmp_path, monkeypatch, model_name, loss_name, backbone): make_dataset(tmp_path / "data") loss_params = {} if model_name == "fastflow" else {"seg_weight": 2.0, "cls_weight": 0.5} doc = { "dataset": {"path": "data", "category": "bottle", "val_ratio": 0.25}, "model": { - "name": model_name, "backbone": "resnet18", "pretrained": False, "image_size": [32, 32], + "name": model_name, "backbone": backbone, "pretrained": False, "image_size": [32, 32], "params": {"flow_steps": 1} if model_name == "fastflow" else {"layers": ["layer2", "layer3"]}, }, "loss": {"name": loss_name, "params": loss_params}, @@ -111,6 +114,12 @@ def test_yaml_runs_real_training_and_existing_inference(tmp_path, monkeypatch, m } if model_name == "ssn": doc["training"].update(head_lr_multiplier=3.0, adaptor_weight_decay=0.005) + if backbone.startswith("dinov2"): + doc["model"]["params"].update(feature_channels=128, dino_layers=[11], + backbone_precision="bfloat16") + if model_name == "ssn": + doc["model"]["params"]["adapt_cls_features"] = True + doc["training"]["accumulate_grad_batches"] = 3 # Flush final two-batch partial window. path = tmp_path / "experiment.yaml" path.write_text(yaml.safe_dump(doc), encoding="utf-8") @@ -130,7 +139,7 @@ def capture_loss(cfg): assert np.isfinite(metrics["image_auroc"]) assert np.isfinite(metrics["pixel_auroc"]) - output = tmp_path / "runs/bottle" / model_name / "resnet18/smoke" + output = tmp_path / "runs/bottle" / model_name / backbone / "smoke" checkpoint_path = output / "best_model.pth" checkpoint = torch.load(checkpoint_path, map_location="cpu", weights_only=True) assert checkpoint["history"]["epoch"] == 1 @@ -163,7 +172,7 @@ def capture_loss(cfg): topk_ratio=0.01, **common) else: engine = SSNInferenceEngine(perlin_threshold=0.7, adapt_cls_features=True, layers=["layer1"], **common) - assert engine.backbone == "resnet18" + assert engine.backbone == backbone assert engine.image_size == (32, 32) score, anomaly_map, _ = engine.infer_one(str(tmp_path / "data/bottle/test/good/000.png")) assert np.isfinite(score) and np.isfinite(anomaly_map).all()