微信公眾號:OpenCV學堂
關注獲取更多計算機視覺與深度學習知識
Anomalib異常檢測庫介紹
異常檢測庫最新anomalib2.0 已經支持多種已經檢測模型從訓練到部署,同時支持多種推理方式。模型訓練支持包含:

模型訓練
以Padim模型為例,訓練該模型支持CLI命令行與代碼API兩種方式,分別如下:
anomalib train --model Padim --data anomalib.data.MVTecAD代碼調用方式如下:
from anomalib.data import MVTecADfrom anomalib.models import Patchcorefrom anomalib.engine import Engine# Initialize componentsdatamodule = MVTecAD()model = Patchcore()engine = Engine()# Train the modelengine.fit(datamodule=datamodule, model=model)
運行結果如下:

導出與部署
anomalib模型訓練保存的是pytorch light格式的ckpt文件,支持導出torch、onnx、openvino三種格式。可以通過相關的SDK直接完成導出,以導出torch格式模型為例,導出CLI如下:
anomalib export --model Padim --export_type torch --ckpt_path <PATH_TO_CHECKPOINT>

導出以后模型模型推理支持部署到OpenVINO、Pytorch、TensorRT等,以pt文件與xml文件為例,推理演示代碼如下:
# Get the inferencer.inferencer = OpenVINOInferencer(path=args.weights, device=args.device)filenames = get_image_filenames(path=args.input)for filename in tqdm(filenames, desc="Predicting images"):predictions = inferencer.predict(filename)# NOTE: This visualization approach is experimental and might change in the future.output = visualize_image_item(item=predictions.items[0],fields=["image"], # Can be used to visualize other fields such as anomaly_map, pred_mask, etc.overlay_fields=[# Can be used to overlay multiple other fields.("image", ["anomaly_map"]),("image", ["pred_mask"]),],)if args.output is None and args.show is False:msg = "Neither output path is provided nor show flag is set. Inferencer will run but return nothing."logger.warning(msg)if output is not None:if args.output:file_path = generate_output_image_filename(input_path=filename, output_path=args.output)output.save(file_path)# Show the image in case the flag is set by the user.if args.show:output.show("Output Image")
效果如下:


上位機與機器視覺開發者必備技能
系統化掌握OpenCV C# SDK與工作流引擎開發,課程包含數十個OpenCV實現的機器視覺常見案例,代碼行數超過3000+,課件、代碼、資料均可下載。支持答疑!現在購買還可以獲取團購優惠:

推薦閱讀
OpenCV4.8+YOLOv8對象檢測C++推理演示
ZXING+OpenCV打造開源條碼檢測應用
攻略 | 學習深度學習只需要三個月的好方法
三行代碼實現 TensorRT8.6 C++ 深度學習模型部署
實戰 | YOLOv8+OpenCV 實現DM碼定位檢測與解析
對象檢測邊界框損失 – 從IOU到ProbIOU
初學者必看 | 學習深度學習的五個誤區
