异常检测-FastFlow
code:https://github.com/gathierry/FastFlow
https://paperswithcode.com/paper/fastflow-unsupervised-anomaly-detection-and
数据集
链接:https://pan.baidu.com/s/1F2O-2bbRYykI1FVODicG6A?pwd=1234
提取码:1234
安装环境
pip install timm==0.5.4 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install FrEIA -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pytorch-ignite -i https://pypi.tuna.tsinghua.edu.cn/simple
训练
python main.py -cfg configs/resnet18.yaml --data D:\dataset\mvtec -cat bottle
评估
python main.py -cfg configs/resnet18.yaml --data path/to/mvtec-ad -cat [category] --eval -ckpt _fastflow_experiment_checkpoints/exp[index]/[epoch#].pt
1.视频教程:
B站、网易云课堂、腾讯课堂
2.代码地址:
Gitee
Github
3.存储地址:
Google云
百度云:
提取码:
《FastFlow: Unsupervised Anomaly Detection and Localization via 2D Normalizing Flows》
—FastFlow
作者:Jiawei Yu, Ye Zheng, Xiang Wang, Wei Li, Yushuang Wu, Rui Zhao, Liwei Wu
单位:
发表会议及时间:
Submission history
From: Ye Zheng [view email]
[v1] Mon, 15 Nov 2021 11:15:02 UTC (2,111 KB)
[v2] Tue, 16 Nov 2021 06:33:39 UTC (2,111 KB)
- Abstract
Unsupervised anomaly detection and localization is crucial to the practical application when collecting and labeling sufficient anomaly data is infeasible. Most existing representation-based approaches extract normal image features with a deep convolutional neural network and characterize the corresponding distribution through non-parametric distribution estimation methods. The anomaly score is calculated by measuring the distance between the feature of the test image and the estimated distribution. However, current methods can not effectively map image features to a tractable base distribution and ignore the relationship between local and global features which are important to identify anomalies. To this end, we propose FastFlow implemented with 2D normalizing flows and use it as the probability distribution estimator. Our FastFlow can be used as a plug-in module with arbitrary deep feature extractors such as ResNet and vision transformer for unsupervised anomaly detection and localization. In training phase, FastFlow learns to transform the input visual feature into a tractable distribution and obtains the likelihood to recognize anomalies in inference phase. Extensive experimental results on the MVTec AD dataset show that FastFlow surpasses previous state-of-the-art methods in terms of accuracy and inference efficiency with various backbone networks. Our approach achieves 99.4% AUC in anomaly detection with high inference efficiency.
一 原理解析
二 代码实现
2.1 模型构建
训练
features = self.feature_extractor(x)
features = [self.norms[i](feature) for i, feature in enumerate(features)]
loss = 0
outputs = []
for i, feature in enumerate(features):
output, log_jac_dets = self.nf_flows[i](feature)
loss += torch.mean(
0.5 * torch.sum(output**2, dim=(1, 2, 3)) - log_jac_dets
)
outputs.append(output)
ret = {"loss": loss}
验证预测
# 热图
features = self.feature_extractor(x)
features = [self.norms[i](feature) for i, feature in enumerate(features)]
outputs = []
anomaly_map_list = []
for output in outputs:
log_prob = -torch.mean(output**2, dim=1, keepdim=True) * 0.5
# 取工程函数e的概率值
prob = torch.exp(log_prob)
# 插值算法
a_map = F.interpolate(
-prob,
size=[self.input_size, self.input_size],
mode="bilinear",
align_corners=False,
)
anomaly_map_list.append(a_map)
anomaly_map_list = torch.stack(anomaly_map_list, dim=-1)
anomaly_map = torch.mean(anomaly_map_list, dim=-1)
ret["anomaly_map"] = anomaly_map
return ret
#
log_prob = -torch.mean(output**2, dim=1, keepdim=True) * 0.5
# 取工程函数e的概率值,因为log_prob 输入为负数,所以prob取值在(0-1)之间
prob = torch.exp(log_prob)