CutPaste-paddle

August 25, 2022 · View on GitHub

目录

1. 简介

cutpaste是一种简单有效的自监督学习方法,其目标是构建一个高性能的两阶段缺陷检测模型,在没有异常数据的情况下检测图像的未知异常模式。首先通过cutpaste数据增强方法学习自监督深度表示,然后在学习的表示上构建生成的单类分类器,从而实现自监督的异常检测。

论文: CutPaste: Self-Supervised Learning for Anomaly Detection and Localization

参考repo: pytorch-cutpaste

在此非常感谢Runinho等人贡献的pytorch-cutpaste ,提高了本repo复现论文的效率。

aistudio体验教程: CutPaste_paddle

2. 数据集和复现精度

  • 数据集大小:共包含15个物品类别,解压后总大小在4.92G左右
  • 数据集下载链接:mvtec-ad
  • 训练权重下载链接:logs (提取码:u6qk)

复现精度(Comparison to Li et al.)

defect_typeCutPaste (3-way)Runinho. CutPaste (3-way)Li et al. CutPaste (3-way)
bottle90.799.698.3
cable91.777.280.6
capsule93.292.496.2
carpet97.360.193.1
grid100.0100.099.9
hazelnut99.086.897.3
leather100.0100.0100.0
metal_nut98.687.899.3
pill97.391.792.4
screw88.486.886.3
tile98.897.293.4
toothbrush98.694.798.3
transistor98.793.095.5
wood99.399.498.6
zipper100.098.899.4
average96.791.095.2

3. 准备数据与环境

3.1 准备环境

首先介绍下支持的硬件和框架版本等环境的要求:

  • 硬件:GPU显存建议在6G以上
  • 框架:
    • PaddlePaddle >= 2.2.0
  • 环境配置:直接使用pip install -r requirements.txt安装依赖即可。

3.2 准备数据

  • 全量数据训练:
    • 下载好 metec-ad 数据集
    • 将其解压到 Data 文件夹下
  • 少量数据训练:
    • 无需下载数据集,使用lite_data里的数据即可

3.3 准备模型

  • 默认不使用resnet18预训练模型进行训练,如想使用,需要预先下载预训练权重 (提取码:l7c3)至项目根目录下并传入参数:python train.py --pretrained True

4. 开始使用

4.1 模型训练

  • 全量数据训练:
    • 下载好 metec-ad 数据集后,将其解压到 ./Data 文件夹下
    • 运行指令python tools/train.py --epochs 7000 --batch_size 32 --workers 4 --log_interval 10 --pretrained True
  • 少量数据训练:
    • 运行指令python tools/train.py --data_dir lite_data --type lite --epochs 5 --batch_size 4
  • 部分训练日志如下所示:
> python tools/train.py --data_dir lite_data --type lite --epochs 5 --batch_size 4 --cuda False
Namespace(batch_size=4, cuda='False', data_dir='lite_data', epochs=5, freeze_resnet=20, head_layer=1, lr=0.03, model_dir='logs', optim='sgd', pretrained=False, save_interval=500, test_epochs=-1, type='l
ite', variant='3way', workers=0)
using device: cpu
training bottle
loading images
loaded 209 images
epoch:1/5 loss:1.2578 avg_reader_cost:0.05 avg_batch_cost:3.01 avg_ips:0.75
epoch:2/5 loss:1.6850 avg_reader_cost:0.02 avg_batch_cost:2.81 avg_ips:0.70
epoch:3/5 loss:1.5016 avg_reader_cost:0.02 avg_batch_cost:2.75 avg_ips:0.69
...

4.2 模型评估

  • 全量数据模型评估:python eval.py
  • 少量数据模型评估:python tools/eval.py --data_dir lite_data --type lite
> python tools/eval.py --data_dir lite_data --type lite --cuda False
Namespace(cuda='False', data_dir='lite_data', density='sklearn', head_layer=1, model_dir='logs', save_plots=True, type='lite')
evaluating bottle
loading model logs/bottle/final.pdparams
loading images
loaded 8 images
using density estimation GaussianDensitySklearn
bottle AUC: 0.875
average auroc:0.8750

4.3 模型预测(需要预先完成4.1训练及4.2验证)

  • 基于原始代码的模型预测:python tools/predict.py --data_type bottle --img-path images/demo0.png --dist_th 1.0

5. 模型推理部署

  • 基于推理引擎的模型预测:
python deploy/export_model.py
python deploy/infer.py --data_type bottle --img-path images/demo0.png --dist_th 1.0

部分结果如下:

> python deploy/export_model.py
inference model has been saved into deploy

> python deploy/infer.py --data_type bottle --img-path images/demo0.png --dist_th 1.0
image_name: images/demo0.png, class_id: 0, prob: 0.07689752858017344

6. 自动化测试脚本

#测试环境准备脚本
bash test_tipc/prepare.sh test_tipc/configs/resnet18/train_infer_python.txt lite_train_lite_infer
#测试训练验证推理一体化脚本
bash test_tipc/test_train_inference_python.sh test_tipc/configs/resnet18/train_infer_python.txt lite_train_lite_infer

输出结果如下,表示命令运行成功。

 Run successfully with command - python3.7 tools/train.py --type lite --model_dir logs --output=./log/resnet18/lite_train_lite_infer/norm_train_gpus_0 --epochs=2   --batch_size=1!
......
 Run successfully with command - python3.7 tools/eval.py --type lite --pretrained=./log/resnet18/lite_train_lite_infer/norm_train_gpus_0/final.pdparams! 
......
 Run successfully with command - python3.7 deploy/export_model.py  --pretrained=./log/resnet18/lite_train_lite_infer/norm_train_gpus_0/final.pdparams --save-inference-dir=./log/resnet18/lite_train_lite_infer/norm_train_gpus_0!
......
 Run successfully with command - python3.7 deploy/infer.py --use-gpu=True --model-dir=./log/resnet18/lite_train_lite_infer/norm_train_gpus_0 --batch-size=1   --benchmark=False > ./log/resnet18/lite_train_lite_infer/python_infer_gpu_batchsize_1.log 2>&1 !  
......
 Run successfully with command - python3.7 deploy/infer.py --use-gpu=False --model-dir=./log/resnet18/lite_train_lite_infer/norm_train_gpus_0 --batch-size=1   --benchmark=False > ./log/resnet18/lite_train_lite_infer/python_infer_cpu_batchsize_1.log 2>&1 !   

7. LICENSE

本项目的发布受Apache 2.0 license许可认证。

8. 参考链接与文献

参考论文: CutPaste: Self-Supervised Learning for Anomaly Detection and Localization

参考repo: pytorch-cutpaste