Padim模型参数

Padim模型参数

  • [1 模型原理](#1 模型原理)
    • [1.1PaDiM 的核心"模型参数"组成](#1.1PaDiM 的核心“模型参数”组成)
  • [2 超参数](#2 超参数)
    • [2.1 骨干网络相关(Backbone-related)](#2.1 骨干网络相关(Backbone-related))
    • [2.2 特征处理相关](#2.2 特征处理相关)
    • [2.3 分布建模相关](#2.3 分布建模相关)
    • [2.4 输入与预处理相关](#2.4 输入与预处理相关)
    • [2.5 后处理与评估相关](#2.5 后处理与评估相关)
    • [2.6 设置阈值](#2.6 设置阈值)

1 模型原理

1.1PaDiM 的核心"模型参数"组成

对于输入图像被划分为 H×WH×W 个空间位置(patches),PaDiM 为每个位置 (i,j)(i,j) 存储以下两个统计量:

  1. 均值向量(Mean Vector)

    记作: μij∈RDμij​∈RD

    表示该位置在所有正常训练样本中提取的特征向量的平均值。

    DD 是特征维度(例如使用 ResNet18 + 多层拼接后可能为 384 或 576,再经随机降维至如 100)。

  2. 协方差矩阵(Covariance Matrix)

    记作: Σij∈RD×DΣij​∈RD×D

    描述该位置正常特征的波动范围和各维度间的相关性。

    实际实现中常使用对角协方差(即只保存方差,忽略特征间相关性)以降低存储和计算开销,但原始论文使用全协方差。

python 复制代码
    padim_model = {
    'means': np.array of shape (H, W, D),      # μ
    'covs':  np.array of shape (H, W, D, D) or (H, W, D),  # Σ
    'backbone': 'resnet18',                    # 仅记录使用的骨干网络
    'feature_layers': ['layer1', 'layer2', ...],
    'reduction_dim': 100,
}

2 超参数

PaDiM(Patch Distribution Modeling)模型本身不包含可训练参数,其"模型参数"实际上是指在训练阶段从正常样本中统计得到的分布参数。这些参数用于推理时计算异常分数。

2.1 骨干网络相关(Backbone-related)

  1. backbone: 用于提取特征的预训练 CNN 网络 'resnet18', 'wide_resnet50_2'(最常用)
  2. feature_layers: 从哪些网络层提取特征(多尺度融合) 如 'layer1', 'layer2', 'layer3'(ResNet 对应输出

2.2 特征处理相关

  1. embedding_dim 或 reduction_dim:特征降维后的维度(通过随机采样或投影) 100, 300, 500(原始拼接后维度可能达 1792,需降维)
  2. use_reducer:是否使用随机降维(Random Projection / Sampling) True / False
  3. interpolation:特征图上采样方法(对齐空间分辨率) 'bilinear', 'bicubic'。

2.3 分布建模相关

  1. covariance_type: 协方差矩阵形式 'full'(全协方差)或 'diag'(对角协方差)
  2. eps: 协方差矩阵正则化项(防止奇异) 1e-6 ~ 1e-3
    3

2.4 输入与预处理相关

input_size 输入图像尺寸(需与 backbone 兼容) (256, 256), (512, 512)

normalize_mean/std 图像归一化参数(需匹配 backbone 预训练设置) ImageNet: 0.485, 0.456, 0.406, 0.229, 0.224, 0.225

2.5 后处理与评估相关

  1. anomaly_threshold 异常分数阈值(用于二值分割) 通过验证集设定(如 99% 分位数)
  2. postprocess 是否使用形态学操作(如开运算) True / False

2.6 设置阈值

  1. 阈值计算触发 src/anomalib/models/padim/lightning_model.py → on_validation_epoch_end
  2. 阈值计算逻辑 src/anomalib/models/components/base/anomaly_module.py
python 复制代码
def _compute_metrics_and_update_best_score(self, outputs):
    # 收集所有验证样本的 image-level scores
    image_scores = torch.cat([output["image_score"] for output in outputs])
    
    # 默认使用 99% 分位数作为阈值(可配置)
    threshold = torch.quantile(image_scores, self.image_threshold.quantile)
    
    # 保存到模型属性
    self.image_threshold.value = threshold
  1. 阈值策略定义 src/anomalib/utils/metrics/threshold.py
    该类支持多种阈值策略(如 F1AdaptiveThreshold, ManualThreshold, AnomalyScoreThreshold),而 PaDiM 默认使用 AnomalyScoreThreshold,即基于分位数的固定阈值。
    anomalib/models/padim/config.yaml
yaml 复制代码
metrics:
  image:
    threshold:
      method: "adaptive"        # 实际对应 AnomalyScoreThreshold
      quantile: 0.99            # ← 关键!默认取 99% 分位数
  1. 默认配置 src/anomalib/models/padim/config.yaml
python 复制代码
metrics:
  image:
    threshold:
      quantile: 0.995   # 更严格
  pixel:
    threshold:
      quantile: 0.99
  1. 训练后手动赋值
python 复制代码
from anomalib.models import Padim

model = Padim.load_from_checkpoint("path/to/checkpoint.ckpt")
model.image_threshold.value = torch.tensor(5.0)  # 手动设为 5.0
  1. 推理时应用 Padim.post_process() 方法
python 复制代码
def post_process(
    self,
    anomaly_maps: Tensor,
    pred_scores: Tensor | None = None,
) -> tuple[Tensor, Tensor]:
    """Post-process predictions to get binary masks using threshold."""
    # 使用已学习的阈值
    pred_mask = (anomaly_maps >= self.pixel_threshold.value).long()
    pred_score = pred_scores if pred_scores is not None else anomaly_maps.amax(dim=(1, 2, 3))
    pred_label = (pred_score >= self.image_threshold.value).long()
    return pred_mask, pred_label
相关推荐
阳光是sunny9 分钟前
从链到图:LangGraph 入门基础全解析
前端·人工智能·后端
新知图书28 分钟前
11.3 详细实现与核心配置(作业批改智能体开发)
人工智能·agent·ai agent·智能体·扣子
深度研习笔记1 小时前
OpenCV工业视觉实战11|多线程解耦+视频流稳流+推理加速,彻底解决卡顿阻塞,实现毫秒级工业实时检测
人工智能·opencv·计算机视觉
delishcomcn1 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹2 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
cd_949217212 小时前
2026 AI Agent 落地指南:除了搭建工具,你还需要配套的搜索基础设施
人工智能
武子康2 小时前
Search Console Platform Properties 扩大 SEO 资产边界:从 Page Ranking 到 Topic Coverage(5 类误读边界 + 3 表数据层设计)
前端·人工智能·后端
大模型码小白2 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
麻雀飞吧2 小时前
最新量化学习路径,交易认知和技术实现要并行
人工智能·python