0

0

【论文复现】PatchCore: 面向全召回率的工业异常检测

P粉084495128

P粉084495128

发布时间:2025-07-22 10:51:18

|

740人浏览过

|

来源于php中文网

原创

本文基于PaddlePaddle复现PatchCore工业异常检测算法,改进特征提取与筛选,用KNN Greedy CoreSet采样构建记忆池,采用新策略算异常得分。在MVTec数据集精度达标,还介绍了相关流程与复现心得。

☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

【论文复现】patchcore: 面向全召回率的工业异常检测 - php中文网

PatchCore: Towards Total Recall in Industrial Anomaly Detection

1. 简介

本项目基于PaddlePaddle框架复现了PatchCore算法,并在MvTec数据集上进行了实验。【论文复现】PatchCore: 面向全召回率的工业异常检测 - php中文网        

PatchCore对SPADE,PaDiM等一系列基于图像Patch的无监督异常检测算法工作进行了扩展,主要解决了SPADE测试速度太慢的问题,并且在特征提取部分做了一些探索。相比SPADE,PaDiM,PatchCore 仅使用stage2、stage3的特征图进行建模,通过增加窗口大小为3、步长为1、padding为1的平均池化AvgPool2d增大感受野后拼接,使用KNN Greedy CoreSet 采样选取最具代表性的特征点(选择与其他特征点最远的点以实现尽可能平衡的采样,效果类似泊松圆盘),构建特征向量记忆池,只保留1%~10%的特征数,进而实现高效的特征筛选并用于异常检测。并提出采用了re-weighting策略计算Image-Level的异常得分代替此前的最大值异常得分。 PatchCore 论文: PatchCore: Towards Total Recall in Industrial Anomaly Detection

参考repo:

  • anomalib

在此非常感谢openvineo贡献的anomalib项目,提高了本repo复现论文的效率。

本项目为第六届论文复现赛异常检测项目。

  • 感谢百度提供的算力支持。

2 复现精度

按复现考核标准,使用resnet18 10%特征数 在MVTec AD数据集的测试效果如下表。

category Image_AUROC Pixel_AUROC PRO_score
carpet 0.995586 0.990199 0.959082
grid 0.969925 0.979229 0.921544
leather 1 0.989842 0.969135
tile 0.989899 0.940809 0.847143
wood 0.990351 0.943459 0.889898
bottle 1 0.9814 0.945257
cable 0.977699 0.984428 0.941403
capsule 0.979657 0.989979 0.942688
hazelnut 1 0.988504 0.936284
metal_nut 0.993157 0.985884 0.938088
pill 0.938352 0.97595 0.92767
screw 0.938512 0.993937 0.971481
toothbrush 0.936111 0.990976 0.917974
transistor 0.990417 0.955586 0.915834
zipper 0.971113 0.987035 0.954973
mean 0.978052 0.978481 0.931897

Image-Level AUC


Avg Carpet Grid Leather Tile Wood Bottle Cable Capsule Hazelnut Metal Nut Pill Screw Toothbrush Transistor Zipper
anomalib (ResNet-18) 0.973 0.970 0.947 1.000 0.997 0.997 1.000 0.986 0.965 1.000 0.991 0.916 0.943 0.931 0.996 0.953
复现 0.978052 0.995586 0.969925 1 0.989899 0.990351 1 0.977699 0.979657 1 0.993157 0.938352 0.938512 0.936111 0.990417 0.971113

Pixel-Level AUC


Avg Carpet Grid Leather Tile Wood Bottle Cable Capsule Hazelnut Metal Nut Pill Screw Toothbrush Transistor Zipper
ResNet-18 0.976 0.986 0.955 0.990 0.943 0.933 0.981 0.984 0.986 0.986 0.986 0.974 0.991 0.988 0.974 0.983
复现 0.978481 0.990199 0.979229 0.989842 0.940809 0.943459 0.9814 0.984428 0.989979 0.988504 0.985884 0.97595 0.993937 0.990976 0.955586 0.987035

达到论文复现验收标准.

3 数据集准备

In [1]
%cd /home/aistudio/data/
!tar xvf data116034/mvtec_anomaly_detection.tar.xz >/dev/null
       
/home/aistudio/data
       

4 依赖安装

In [1]
!pip install scikit-image >/dev/null
       
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
parl 1.4.1 requires pyzmq==18.1.1, but you have pyzmq 22.3.0 which is incompatible.
       

5 训练

一共有15个类别,这里需要对15个类别分别训练,最后取平均值作为验证指标.(https://github.com/openvinotoolkit/anomalib/blob/development/anomalib/models/stfpm/README.md).

单个类别训练: 以category=carpet为例。

In [3]
%cd /home/aistudio/Anomaly.Paddle/
!python train.py --category carpet --data_path=/home/aistudio/data/ --method=coreset --arch=resnet18 --k=10 --eval
       
/home/aistudio/Anomaly.Paddle
Namespace(arch='resnet18', batch_size=32, category='carpet', cpu=False, crop_size=256, data_path='/home/aistudio/data/', debug=False, einsum=False, eval=True, eval_PRO=False, eval_threthold_step=500, inc=False, k=10, load_projection=None, method='coreset', non_partial_AUC=False, num_workers=0, resize=256, save_model=True, save_model_subfolder=True, save_path='./output/coreset_resnet18_10', save_pic=True, seed=521, test_batch_size=1)
W0505 22:47:52.097200   463 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 22:47:52.102414   463 device_context.cc:465] device: 0, cuDNN Version: 7.6.
100%|██████████████████████████████████| 69183/69183 [00:04<00:00, 14500.91it/s]
model resnet18, nParams 2787264
Training model 1/1 for carpet
| feature extraction | train | carpet |:   0%|            | 0/9 [00:00<?, ?it/s]/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
| feature extraction | train | carpet |: 100%|████| 9/9 [00:20<00:00,  2.27s/it]
Creating CoreSet Sampler via k-Center Greedy
Getting the coreset from the main embedding.
Assigning the coreset as the memory bank with shape [28672, 384].
2022-05-05 23:06:01	Train ends, total 1067.16s
Saving model...
2022-05-05 23:06:01	Save model in ./output/coreset_resnet18_10/carpet.pdparams
2022-05-05 23:06:01	Starting eval model...
| feature extraction | test | carpet |: 100%|█| 117/117 [02:26<00:00,  1.25s/it]
max_score:3.2409136295318604 min_score:0.7035042643547058
F1 image:0.9828571428571428 threshold:3.2409136295318604
F1 pixel:0.5584764361267536 threshold:3.2409136295318604
['carpet', 0.9955858747993579, 0.9901991232207019, None]
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:101: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  ret = np.asscalar(ex)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return list(data) if isinstance(data, collections.MappingView) else data
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:425: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_min = np.asscalar(a_min.astype(scaled_dtype))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:426: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_max = np.asscalar(a_max.astype(scaled_dtype))
          Image_AUROC  Pixel_AUROC
category                          
carpet       0.995586     0.990199
Evaluation result saved at./output/coreset_resnet18_10/carpet_seed521.csv:
       

对所有类别进行训练:

In [4]
%cd /home/aistudio/Anomaly.Paddle/
!python train.py --category all --data_path=/home/aistudio/data/ --method=coreset --arch=resnet18 --k=10 --eval
       
/home/aistudio/Anomaly.Paddle
Namespace(arch='resnet18', batch_size=32, category='all', cpu=False, crop_size=256, data_path='/home/aistudio/data/', debug=False, einsum=False, eval=True, eval_PRO=False, eval_threthold_step=500, inc=False, k=10, load_projection=None, method='coreset', non_partial_AUC=False, num_workers=0, resize=256, save_model=True, save_model_subfolder=True, save_path='./output/coreset_resnet18_10', save_pic=True, seed=521, test_batch_size=1)
W0505 23:08:38.231904  2381 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:08:38.237079  2381 device_context.cc:465] device: 0, cuDNN Version: 7.6.
^C
       

6 测试

单个类别测试: 以category=carpet为例。

In [13]
%cd /home/aistudio/Anomaly.Paddle/
!python eval.py --data_path=/home/aistudio/data/ --category carpet --method=coreset --arch=resnet18 --k=10
       
/home/aistudio/Anomaly.Paddle
Namespace(arch='resnet18', batch_size=1, category='carpet', crop_size=256, data_path='/home/aistudio/data/', eval_PRO=False, eval_threthold_step=500, k=10, method='coreset', model_path=None, non_partial_AUC=False, num_workers=0, resize=256, save_path='./output/coreset_resnet18_10', save_pic=True, seed=521, test_batch_size=1)
Testing model 1/1 for carpet
W0505 23:14:08.773208  3548 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:14:08.778220  3548 device_context.cc:465] device: 0, cuDNN Version: 7.6.
model resnet18, nParams 2787264
2022-05-05 23:14:18	Starting eval model...
| feature extraction | test | carpet |:   0%|           | 0/117 [00:00<?, ?it/s]/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
| feature extraction | test | carpet |: 100%|█| 117/117 [01:38<00:00,  1.19it/s]
max_score:3.2409136295318604 min_score:0.7035042643547058
F1 image:0.9828571428571428 threshold:3.2409136295318604
F1 pixel:0.5584764361267536 threshold:3.2409136295318604
['carpet', 0.9955858747993579, 0.9901991232207019, None]
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:101: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  ret = np.asscalar(ex)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return list(data) if isinstance(data, collections.MappingView) else data
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:425: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_min = np.asscalar(a_min.astype(scaled_dtype))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:426: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_max = np.asscalar(a_max.astype(scaled_dtype))
          Image_AUROC  Pixel_AUROC
category                          
carpet       0.995586     0.990199
Evaluation result saved at./output/coreset_resnet18_10/carpet_seed521.csv:
       

对所有类别进行测试: 由于已在训练过程中进行测试此处不再重复

AI Web Designer
AI Web Designer

AI网页设计师,快速生成个性化的网站设计

下载
In [6]
#%cd /home/aistudio/Anomaly.Paddle/#!python eval.py --data_path=/home/aistudio/data/ --category all --method=coreset --arch=resnet18 --k=10 --eval_PRO
   

7 单独预测

In [7]
%cd /home/aistudio/Anomaly.Paddle/
!python predict.py /home/aistudio/data/carpet/test/color/000.png --category carpet --method=coreset --arch=resnet18 --k=10
       
/home/aistudio/Anomaly.Paddle
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
Namespace(arch='resnet18', category='carpet', crop_size=256, k=10, method='coreset', model_path=None, norm=True, picture_path='/home/aistudio/data/carpet/test/color/000.png', resize=256, save_path='./output/coreset_resnet18_10', save_pic=True, seed=42, threshold=0.4)
Testing model for carpet
W0505 23:11:38.219069  2823 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:11:38.224303  2823 device_context.cc:465] device: 0, cuDNN Version: 7.6.
model resnet18, nParams 2787264
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
2022-05-05 23:11:53	Starting eval model...
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:101: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  ret = np.asscalar(ex)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return list(data) if isinstance(data, collections.MappingView) else data
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:425: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_min = np.asscalar(a_min.astype(scaled_dtype))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:426: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_max = np.asscalar(a_max.astype(scaled_dtype))
2022-05-05 23:11:55	Predict :  Picture /home/aistudio/data/carpet/test/color/000.png done!
Result saved at ./output/coreset_resnet18_10/carpet/carpet_predict.png
       

可以在output/找到如下的类似结果:

8 导出

In [8]
%cd /home/aistudio/Anomaly.Paddle/
!python export_model.py --method=coreset --arch=resnet18 --k=10 --model_path=./output/coreset_resnet18_10/carpet.pdparams --save_dir=./output/coreset_resnet18_10
       
/home/aistudio/Anomaly.Paddle
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
Namespace(arch='resnet18', category='leather', img_size=256, k=10, method='coreset', model_path='./output/coreset_resnet18_10/carpet.pdparams', save_dir='./output/coreset_resnet18_10')
W0505 23:11:59.439810  2902 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:11:59.445221  2902 device_context.cc:465] device: 0, cuDNN Version: 7.6.
model resnet18, nParams 2787264
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return (isinstance(seq, collections.Sequence) and
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmpzp85lm0v.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmpxdiplums.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmp22pggkiw.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmps33tfdg1.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmpht9nggz5.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmp11b99l2s.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
Model is saved in ./output/coreset_resnet18_10.
       

9 推理

In [4]
%cd /home/aistudio/Anomaly.Paddle/
!python infer.py --model_name='PatchCore' --enable_post_process --use_gpu=True --model_file=output/coreset_resnet18_10/model.pdmodel --input_file=/home/aistudio/data/carpet/test/color/000.png --params_file=output/coreset_resnet18_10/model.pdiparams --category=carpet  --stats=./output/coreset_resnet18_10/stats --save_path=./output/coreset_resnet18_10
       
/home/aistudio/Anomaly.Paddle
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
Inference model(PatchCore)...
load train set feature from: ./output/coreset_resnet18_10/stats
E0506 19:06:01.010391  1895 analysis_config.cc:91] Please compile with gpu to EnableGpu()--- Running analysis [ir_graph_build_pass]--- Running analysis [ir_graph_clean_pass]--- Running analysis [ir_analysis_pass]--- Running IR pass [simplify_with_basic_ops_pass]--- Running IR pass [layer_norm_fuse_pass]---    Fused 0 subgraphs into layer_norm op.--- Running IR pass [attention_lstm_fuse_pass]--- Running IR pass [seqconv_eltadd_relu_fuse_pass]--- Running IR pass [seqpool_cvm_concat_fuse_pass]--- Running IR pass [mul_lstm_fuse_pass]--- Running IR pass [fc_gru_fuse_pass]---    fused 0 pairs of fc gru patterns--- Running IR pass [mul_gru_fuse_pass]--- Running IR pass [seq_concat_fc_fuse_pass]--- Running IR pass [squeeze2_matmul_fuse_pass]--- Running IR pass [reshape2_matmul_fuse_pass]--- Running IR pass [flatten2_matmul_fuse_pass]--- Running IR pass [map_matmul_v2_to_mul_pass]--- Running IR pass [map_matmul_v2_to_matmul_pass]--- Running IR pass [map_matmul_to_mul_pass]--- Running IR pass [fc_fuse_pass]--- Running IR pass [repeated_fc_relu_fuse_pass]--- Running IR pass [squared_mat_sub_fuse_pass]--- Running IR pass [conv_bn_fuse_pass]I0506 19:06:01.055578  1895 fuse_pass_base.cc:57] ---  detected 15 subgraphs--- Running IR pass [conv_eltwiseadd_bn_fuse_pass]--- Running IR pass [conv_transpose_bn_fuse_pass]--- Running IR pass [conv_transpose_eltwiseadd_bn_fuse_pass]--- Running IR pass [is_test_pass]--- Running IR pass [runtime_context_cache_pass]--- Running analysis [ir_params_sync_among_devices_pass]--- Running analysis [adjust_cudnn_workspace_size_pass]--- Running analysis [inference_op_replace_pass]--- Running analysis [memory_optimize_pass]I0506 19:06:01.060403  1895 memory_optimize_pass.cc:216] Cluster name : conv2d_31.tmp_0  size: 262144
I0506 19:06:01.060425  1895 memory_optimize_pass.cc:216] Cluster name : batch_norm_0.tmp_2  size: 4194304
I0506 19:06:01.060428  1895 memory_optimize_pass.cc:216] Cluster name : pool2d_0.tmp_0  size: 1048576
I0506 19:06:01.060431  1895 memory_optimize_pass.cc:216] Cluster name : relu_0.tmp_0  size: 4194304
I0506 19:06:01.060443  1895 memory_optimize_pass.cc:216] Cluster name : conv2d_22.tmp_0  size: 1048576
I0506 19:06:01.060452  1895 memory_optimize_pass.cc:216] Cluster name : x  size: 786432--- Running analysis [ir_graph_to_program_pass]I0506 19:06:01.074777  1895 analysis_predictor.cc:714] ======= optimize end =======
I0506 19:06:01.075248  1895 naive_executor.cc:98] ---  skip [feed], feed -> x
I0506 19:06:01.076575  1895 naive_executor.cc:98] ---  skip [concat_0.tmp_0], fetch -> fetch
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
Inference mode
image_score:[2.66626549]
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:101: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  ret = np.asscalar(ex)
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return list(data) if isinstance(data, collections.MappingView) else data
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:425: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_min = np.asscalar(a_min.astype(scaled_dtype))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/image.py:426: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
  a_max = np.asscalar(a_max.astype(scaled_dtype))
saved
       

输出结果如下:

10 TIPC

因为test_tipc 不支持后处理的可视化环境,tipc中不进行异常图可视化。

首先安装auto_log,需要进行安装,安装方式如下: auto_log的详细介绍参考https://github.com/LDOUBLEV/AutoLog。

git clone https://github.com/LDOUBLEV/AutoLog
cd AutoLog/
pip3 install -r requirements.txt
python3 setup.py bdist_wheel
pip3 install ./dist/auto_log-1.2.0-py3-none-any.whl
   
In [2]
#%cd /home/aistudio/#!git clone https://github.com/LDOUBLEV/AutoLog%cd /home/aistudio/AutoLog/
!pip3 install -r requirements.txt >/dev/null#!python3 setup.py bdist_wheel!pip3 install ./dist/auto_log-1.2.0-py3-none-any.whl >/dev/null
       
/home/aistudio/AutoLog
       

进行TIPC:

In [11]
%cd /home/aistudio/Anomaly.Paddle/
!bash test_tipc/prepare.sh test_tipc/configs/PatchCore/train_infer_python.txt 'lite_train_lite_infer'
       
/home/aistudio/Anomaly.Paddle
Archive:  MVTec.zip
   creating: MVTec/
  inflating: MVTec/.DS_Store         
   creating: MVTec/capsule/
  inflating: MVTec/capsule/.DS_Store  
   creating: MVTec/capsule/test/
   creating: MVTec/capsule/test/crack/
  inflating: MVTec/capsule/test/crack/001.png  
  inflating: MVTec/capsule/test/crack/000.png  
  inflating: MVTec/capsule/test/.DS_Store  
   creating: MVTec/capsule/test/scratch/
  inflating: MVTec/capsule/test/scratch/001.png  
  inflating: MVTec/capsule/test/scratch/000.png  
  inflating: MVTec/capsule/test/scratch/.DS_Store  
   creating: MVTec/capsule/test/poke/
  inflating: MVTec/capsule/test/poke/001.png  
  inflating: MVTec/capsule/test/poke/000.png  
  inflating: MVTec/capsule/test/poke/.DS_Store  
   creating: MVTec/capsule/test/good/
  inflating: MVTec/capsule/test/good/001.png  
  inflating: MVTec/capsule/test/good/000.png  
  inflating: MVTec/capsule/test/good/.DS_Store  
   creating: MVTec/capsule/test/faulty_imprint/
  inflating: MVTec/capsule/test/faulty_imprint/001.png  
  inflating: MVTec/capsule/test/faulty_imprint/000.png  
  inflating: MVTec/capsule/test/faulty_imprint/.DS_Store  
   creating: MVTec/capsule/test/squeeze/
  inflating: MVTec/capsule/test/squeeze/001.png  
  inflating: MVTec/capsule/test/squeeze/000.png  
  inflating: MVTec/capsule/test/squeeze/.DS_Store  
   creating: MVTec/capsule/ground_truth/
   creating: MVTec/capsule/ground_truth/crack/
  inflating: MVTec/capsule/ground_truth/crack/000_mask.png  
  inflating: MVTec/capsule/ground_truth/crack/001_mask.png  
  inflating: MVTec/capsule/ground_truth/.DS_Store  
   creating: MVTec/capsule/ground_truth/scratch/
  inflating: MVTec/capsule/ground_truth/scratch/000_mask.png  
  inflating: MVTec/capsule/ground_truth/scratch/001_mask.png  
   creating: MVTec/capsule/ground_truth/poke/
  inflating: MVTec/capsule/ground_truth/poke/000_mask.png  
  inflating: MVTec/capsule/ground_truth/poke/001_mask.png  
   creating: MVTec/capsule/ground_truth/faulty_imprint/
  inflating: MVTec/capsule/ground_truth/faulty_imprint/000_mask.png  
  inflating: MVTec/capsule/ground_truth/faulty_imprint/001_mask.png  
   creating: MVTec/capsule/ground_truth/squeeze/
  inflating: MVTec/capsule/ground_truth/squeeze/000_mask.png  
  inflating: MVTec/capsule/ground_truth/squeeze/001_mask.png  
   creating: MVTec/capsule/train/
   creating: MVTec/capsule/train/good/
  inflating: MVTec/capsule/train/good/001.png  
  inflating: MVTec/capsule/train/good/000.png
       
In [12]
%cd /home/aistudio/Anomaly.Paddle/
! bash test_tipc/test_train_inference_python.sh test_tipc/configs/PatchCore/train_infer_python.txt 'lite_train_lite_infer'
       
/home/aistudio/Anomaly.Paddle
Namespace(arch='resnet18', batch_size=32, category='capsule', cpu=False, crop_size=256, data_path='./test_tipc/data/MVTec', debug=False, einsum=False, eval=False, eval_PRO=False, eval_threthold_step=500, inc=False, k=10, load_projection=None, method='coreset', non_partial_AUC=False, num_workers=0, resize=256, save_model=True, save_model_subfolder=False, save_path='./test_tipc/output/PatchCore/', save_pic=False, seed=521, test_batch_size=1)
W0505 23:12:50.398147  3193 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:12:50.403101  3193 device_context.cc:465] device: 0, cuDNN Version: 7.6.
model resnet18, nParams 2787264
Training model 1/1 for capsule
| feature extraction | train | capsule |:   0%|           | 0/1 [00:00<?, ?it/s]/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
| feature extraction | train | capsule |: 100%|███| 1/1 [00:00<00:00,  3.47it/s]
Creating CoreSet Sampler via k-Center Greedy
Getting the coreset from the main embedding.
Assigning the coreset as the memory bank with shape [204, 384].
2022-05-05 23:13:11	Train ends, total 5.96s
Saving model...
2022-05-05 23:13:11	Save model in ./test_tipc/output/PatchCore/capsule.pdparams Run successfully with command - python3.7 train.py --data_path=./test_tipc/data/MVTec --category capsule --method=coreset --arch=resnet18 --k=10 --save_path=./test_tipc/output/PatchCore/ --save_model_subfolder=False --save_pic=False!  /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
Namespace(arch='resnet18', category='leather', img_size=256, k=10, method='coreset', model_path='./test_tipc/output/PatchCore/capsule.pdparams', save_dir='./test_tipc/output/PatchCore/')
W0505 23:13:14.875043  3308 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:13:14.880427  3308 device_context.cc:465] device: 0, cuDNN Version: 7.6.
model resnet18, nParams 2787264
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  return (isinstance(seq, collections.Sequence) and
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmpeu0e3616.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmphsdl18dg.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmp3pdqc646.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmpcndbd9vo.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmp8vecen64.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/math_op_patch.py:341: UserWarning: /tmp/tmpw8r51_rv.py:21
The behavior of expression A + B has been unified with elementwise_add(X, Y, axis=-1) from Paddle 2.0. If your code works well in the older versions but crashes in this version, try to use elementwise_add(X, Y, axis=0) instead of A + B. This transitional warning will be dropped in the future.
  op_type, op_type, EXPRESSION_MAP[method_name]))
Model is saved in ./test_tipc/output/PatchCore/. Run successfully with command - python3.7 export_model.py --method=coreset --arch=resnet18 --k=10 --model_path=./test_tipc/output/PatchCore/capsule.pdparams --save_dir=./test_tipc/output/PatchCore/!  W0505 23:13:31.946898  3372 analysis_predictor.cc:795] The one-time configuration of analysis predictor failed, which may be due to native predictor called first and its configurations taken effect.--- Running analysis [ir_graph_build_pass]--- Running analysis [ir_graph_clean_pass]--- Running analysis [ir_analysis_pass]--- Running IR pass [is_test_pass]--- Running IR pass [simplify_with_basic_ops_pass]--- Running IR pass [conv_affine_channel_fuse_pass]--- Running IR pass [conv_eltwiseadd_affine_channel_fuse_pass]--- Running IR pass [conv_bn_fuse_pass]I0505 23:13:31.992252  3372 fuse_pass_base.cc:57] ---  detected 15 subgraphs--- Running IR pass [conv_eltwiseadd_bn_fuse_pass]--- Running IR pass [embedding_eltwise_layernorm_fuse_pass]--- Running IR pass [multihead_matmul_fuse_pass_v2]--- Running IR pass [squeeze2_matmul_fuse_pass]--- Running IR pass [reshape2_matmul_fuse_pass]--- Running IR pass [flatten2_matmul_fuse_pass]--- Running IR pass [map_matmul_v2_to_mul_pass]--- Running IR pass [map_matmul_v2_to_matmul_pass]--- Running IR pass [map_matmul_to_mul_pass]--- Running IR pass [fc_fuse_pass]--- Running IR pass [fc_elementwise_layernorm_fuse_pass]--- Running IR pass [conv_elementwise_add_act_fuse_pass]--- Running IR pass [conv_elementwise_add2_act_fuse_pass]--- Running IR pass [conv_elementwise_add_fuse_pass]--- Running IR pass [transpose_flatten_concat_fuse_pass]--- Running IR pass [runtime_context_cache_pass]--- Running analysis [ir_params_sync_among_devices_pass]I0505 23:13:32.002573  3372 ir_params_sync_among_devices_pass.cc:45] Sync params from CPU to GPU--- Running analysis [adjust_cudnn_workspace_size_pass]--- Running analysis [inference_op_replace_pass]--- Running analysis [memory_optimize_pass]I0505 23:13:32.230777  3372 memory_optimize_pass.cc:216] Cluster name : batch_norm_14.tmp_2  size: 262144
I0505 23:13:32.230805  3372 memory_optimize_pass.cc:216] Cluster name : x  size: 786432
I0505 23:13:32.230809  3372 memory_optimize_pass.cc:216] Cluster name : pool2d_0.tmp_0  size: 1048576
I0505 23:13:32.230813  3372 memory_optimize_pass.cc:216] Cluster name : relu_0.tmp_0  size: 4194304
I0505 23:13:32.230816  3372 memory_optimize_pass.cc:216] Cluster name : batch_norm_2.tmp_2  size: 1048576--- Running analysis [ir_graph_to_program_pass]I0505 23:13:32.245414  3372 analysis_predictor.cc:714] ======= optimize end =======
I0505 23:13:32.246016  3372 naive_executor.cc:98] ---  skip [feed], feed -> x
I0505 23:13:32.247359  3372 naive_executor.cc:98] ---  skip [concat_0.tmp_0], fetch -> fetch
W0505 23:13:32.313050  3372 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:13:32.317571  3372 device_context.cc:465] device: 0, cuDNN Version: 7.6.
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
Inference model(PaDiM)...
load train set feature from: ./test_tipc/output/PatchCore/stats Run successfully with command - python3.7 infer.py --stats=./test_tipc/output/PatchCore/stats --use_gpu=True --use_tensorrt=False --precision=fp32 --model_file=./test_tipc/output/PatchCore/model.pdmodel --batch_size=1 --input_file=./test_tipc/data/MVTec/capsule/test/crack/001.png  --params_file=./test_tipc/output/PatchCore/model.pdiparams > ./test_tipc/output/PatchCore/python_infer_gpu_usetrt_False_precision_fp32_batchsize_1.log 2>&1 !  --- Running analysis [ir_graph_build_pass]--- Running analysis [ir_graph_clean_pass]--- Running analysis [ir_analysis_pass]--- Running IR pass [simplify_with_basic_ops_pass]--- Running IR pass [layer_norm_fuse_pass]---    Fused 0 subgraphs into layer_norm op.--- Running IR pass [attention_lstm_fuse_pass]--- Running IR pass [seqconv_eltadd_relu_fuse_pass]--- Running IR pass [seqpool_cvm_concat_fuse_pass]--- Running IR pass [mul_lstm_fuse_pass]--- Running IR pass [fc_gru_fuse_pass]---    fused 0 pairs of fc gru patterns--- Running IR pass [mul_gru_fuse_pass]--- Running IR pass [seq_concat_fc_fuse_pass]--- Running IR pass [squeeze2_matmul_fuse_pass]--- Running IR pass [reshape2_matmul_fuse_pass]--- Running IR pass [flatten2_matmul_fuse_pass]--- Running IR pass [map_matmul_v2_to_mul_pass]--- Running IR pass [map_matmul_v2_to_matmul_pass]--- Running IR pass [map_matmul_to_mul_pass]--- Running IR pass [fc_fuse_pass]--- Running IR pass [repeated_fc_relu_fuse_pass]--- Running IR pass [squared_mat_sub_fuse_pass]--- Running IR pass [conv_bn_fuse_pass]I0505 23:13:44.782842  3435 fuse_pass_base.cc:57] ---  detected 15 subgraphs--- Running IR pass [conv_eltwiseadd_bn_fuse_pass]--- Running IR pass [conv_transpose_bn_fuse_pass]--- Running IR pass [conv_transpose_eltwiseadd_bn_fuse_pass]--- Running IR pass [is_test_pass]--- Running IR pass [runtime_context_cache_pass]--- Running analysis [ir_params_sync_among_devices_pass]--- Running analysis [adjust_cudnn_workspace_size_pass]--- Running analysis [inference_op_replace_pass]--- Running analysis [memory_optimize_pass]I0505 23:13:44.787526  3435 memory_optimize_pass.cc:216] Cluster name : conv2d_31.tmp_0  size: 262144
I0505 23:13:44.787545  3435 memory_optimize_pass.cc:216] Cluster name : batch_norm_0.tmp_2  size: 4194304
I0505 23:13:44.787549  3435 memory_optimize_pass.cc:216] Cluster name : pool2d_0.tmp_0  size: 1048576
I0505 23:13:44.787554  3435 memory_optimize_pass.cc:216] Cluster name : relu_0.tmp_0  size: 4194304
I0505 23:13:44.787556  3435 memory_optimize_pass.cc:216] Cluster name : conv2d_22.tmp_0  size: 1048576
I0505 23:13:44.787559  3435 memory_optimize_pass.cc:216] Cluster name : x  size: 786432--- Running analysis [ir_graph_to_program_pass]I0505 23:13:44.802613  3435 analysis_predictor.cc:714] ======= optimize end =======
I0505 23:13:44.803148  3435 naive_executor.cc:98] ---  skip [feed], feed -> x
I0505 23:13:44.804566  3435 naive_executor.cc:98] ---  skip [concat_0.tmp_0], fetch -> fetch
W0505 23:13:44.870177  3435 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:13:44.874680  3435 device_context.cc:465] device: 0, cuDNN Version: 7.6.
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
Inference model(PaDiM)...
load train set feature from: ./test_tipc/output/PatchCore/stats Run successfully with command - python3.7 infer.py --stats=./test_tipc/output/PatchCore/stats --use_gpu=False --enable_mkldnn=False --cpu_threads=1 --model_file=./test_tipc/output/PatchCore/model.pdmodel --batch_size=1 --input_file=./test_tipc/data/MVTec/capsule/test/crack/001.png   --precision=fp32 --params_file=./test_tipc/output/PatchCore/model.pdiparams > ./test_tipc/output/PatchCore/python_infer_cpu_usemkldnn_False_threads_1_precision_fp32_batchsize_1.log 2>&1 !  --- Running analysis [ir_graph_build_pass]--- Running analysis [ir_graph_clean_pass]--- Running analysis [ir_analysis_pass]--- Running IR pass [simplify_with_basic_ops_pass]--- Running IR pass [layer_norm_fuse_pass]---    Fused 0 subgraphs into layer_norm op.--- Running IR pass [attention_lstm_fuse_pass]--- Running IR pass [seqconv_eltadd_relu_fuse_pass]--- Running IR pass [seqpool_cvm_concat_fuse_pass]--- Running IR pass [mul_lstm_fuse_pass]--- Running IR pass [fc_gru_fuse_pass]---    fused 0 pairs of fc gru patterns--- Running IR pass [mul_gru_fuse_pass]--- Running IR pass [seq_concat_fc_fuse_pass]--- Running IR pass [squeeze2_matmul_fuse_pass]--- Running IR pass [reshape2_matmul_fuse_pass]--- Running IR pass [flatten2_matmul_fuse_pass]--- Running IR pass [map_matmul_v2_to_mul_pass]--- Running IR pass [map_matmul_v2_to_matmul_pass]--- Running IR pass [map_matmul_to_mul_pass]--- Running IR pass [fc_fuse_pass]--- Running IR pass [repeated_fc_relu_fuse_pass]--- Running IR pass [squared_mat_sub_fuse_pass]--- Running IR pass [conv_bn_fuse_pass]I0505 23:13:57.568568  3490 fuse_pass_base.cc:57] ---  detected 15 subgraphs--- Running IR pass [conv_eltwiseadd_bn_fuse_pass]--- Running IR pass [conv_transpose_bn_fuse_pass]--- Running IR pass [conv_transpose_eltwiseadd_bn_fuse_pass]--- Running IR pass [is_test_pass]--- Running IR pass [runtime_context_cache_pass]--- Running analysis [ir_params_sync_among_devices_pass]--- Running analysis [adjust_cudnn_workspace_size_pass]--- Running analysis [inference_op_replace_pass]--- Running analysis [memory_optimize_pass]I0505 23:13:57.573712  3490 memory_optimize_pass.cc:216] Cluster name : conv2d_31.tmp_0  size: 262144
I0505 23:13:57.573740  3490 memory_optimize_pass.cc:216] Cluster name : batch_norm_0.tmp_2  size: 4194304
I0505 23:13:57.573742  3490 memory_optimize_pass.cc:216] Cluster name : pool2d_0.tmp_0  size: 1048576
I0505 23:13:57.573746  3490 memory_optimize_pass.cc:216] Cluster name : relu_0.tmp_0  size: 4194304
I0505 23:13:57.573750  3490 memory_optimize_pass.cc:216] Cluster name : conv2d_22.tmp_0  size: 1048576
I0505 23:13:57.573752  3490 memory_optimize_pass.cc:216] Cluster name : x  size: 786432--- Running analysis [ir_graph_to_program_pass]I0505 23:13:57.589735  3490 analysis_predictor.cc:714] ======= optimize end =======
I0505 23:13:57.590353  3490 naive_executor.cc:98] ---  skip [feed], feed -> x
I0505 23:13:57.591871  3490 naive_executor.cc:98] ---  skip [concat_0.tmp_0], fetch -> fetch
W0505 23:13:57.659296  3490 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0505 23:13:57.663693  3490 device_context.cc:465] device: 0, cuDNN Version: 7.6.
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  from collections import Sized
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
Inference model(PaDiM)...
load train set feature from: ./test_tipc/output/PatchCore/stats Run successfully with command - python3.7 infer.py --stats=./test_tipc/output/PatchCore/stats --use_gpu=False --enable_mkldnn=False --cpu_threads=2 --model_file=./test_tipc/output/PatchCore/model.pdmodel --batch_size=1 --input_file=./test_tipc/data/MVTec/capsule/test/crack/001.png   --precision=fp32 --params_file=./test_tipc/output/PatchCore/model.pdiparams > ./test_tipc/output/PatchCore/python_infer_cpu_usemkldnn_False_threads_2_precision_fp32_batchsize_1.log 2>&1 !
       

11 复现心得

cdist运算

利用 broadcast 和 norm 实现 cdist,缺点是目前的 broadcast 机制会成倍消耗显存 使用条件判断适应2d/3d data,说起来没有atleast_nd这种函数还是不太方便

def cdist(X, Y, p=2.0):
    dim = max(len(X.shape), len(Y.shape))    if dim==3:        if len(Y.shape)==2:
            Y = Y.unsqueeze(0)        elif len(Y.shape)==1:
            Y = Y.unsqueeze(0).unsqueeze(0)        else:            assert len(Y.shape)==3
            assert Y.shape[0]=X.shape[0]    #B, P, C = X.shape
    #1|B, R, C = Y.shape
    D = paddle.linalg.norm(X[:, :, None, :]-Y[None, :, :, :], p, axis=-1)    return D
       

减少显存占用的写法:

    def cdist(X, Y, p=2.0):
    #2d P, C = X.shape| R, C = Y.shape -> P,R
    P, C = X.shape
    R, C = Y.shape 
    #3d B, P, C = X.shape|1, R, C = Y.shape -> B, P,R
    #D = paddle.linalg.norm(X[:, None, :]-Y[None, :, :], axis=-1)
    D = paddle.zeros((P, R))    for i in range(P):
        D[i,:] = paddle.linalg.norm(X[i, None, :]-Y, axis=-1)        #D[i,:] = (X[i, None, :]-Y).square().sum(-1).sqrt_()
    return D
       

希望paddle Hackathon 能够早点补上原生实现(https://github.com/PaddlePaddle/community/blob/master/rfcs/APIs/20220316_api_design_for_cdist.md)

Avgpool2D 默认表现与pytorch不一致

paddle.nn.AvgPool2D 默认exclusive=True, 与pytorch对应的参数为exclusive=False

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

26

2026.03.13

Python异步编程与Asyncio高并发应用实践
Python异步编程与Asyncio高并发应用实践

本专题围绕 Python 异步编程模型展开,深入讲解 Asyncio 框架的核心原理与应用实践。内容包括事件循环机制、协程任务调度、异步 IO 处理以及并发任务管理策略。通过构建高并发网络请求与异步数据处理案例,帮助开发者掌握 Python 在高并发场景中的高效开发方法,并提升系统资源利用率与整体运行性能。

46

2026.03.12

C# ASP.NET Core微服务架构与API网关实践
C# ASP.NET Core微服务架构与API网关实践

本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

178

2026.03.11

Go高并发任务调度与Goroutine池化实践
Go高并发任务调度与Goroutine池化实践

本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。

51

2026.03.10

Kotlin Android模块化架构与组件化开发实践
Kotlin Android模块化架构与组件化开发实践

本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。

92

2026.03.09

JavaScript浏览器渲染机制与前端性能优化实践
JavaScript浏览器渲染机制与前端性能优化实践

本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。

102

2026.03.06

Rust内存安全机制与所有权模型深度实践
Rust内存安全机制与所有权模型深度实践

本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

227

2026.03.05

PHP高性能API设计与Laravel服务架构实践
PHP高性能API设计与Laravel服务架构实践

本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

532

2026.03.04

AI安装教程大全
AI安装教程大全

2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

171

2026.03.04

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 22.5万人学习

Django 教程
Django 教程

共28课时 | 5万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号