OpenVINO 2025 C++ Deployment of Anomaly Detection Models

OpenVINO 2025 C++ Deployment of Anomaly Detection Models

Click the blue text above to follow us

WeChat Official Account:OpenCV Academy

Follow us for more knowledge on computer vision and deep learning

Introduction to the Padim Model

Padim primarily generates feature vectors from a series of normal samples using a CNN network, calculates multiple variance Gaussian matrices of the feature vectors, and obtains the feature data distribution of normal samples. Then, for the input sample image, it calculates the Mahalanobis distance between its feature vector and the training-generated feature data distribution, thus achieving anomaly detection and localization.

OpenVINO 2025 C++ Deployment of Anomaly Detection Models

PadimModel Training and ExportSupports ONNX format orOpenVINOformat, please see the article below.

[Engineering Practice] Anomalib Anomaly Detection from Training to Deployment

OpenVINO 2025 Inference Steps

OpenVINO 2025 is more concise and user-friendly compared to previous versions of the C++ SDK, and it supports dynamic modification of input dimension parameters.

OpenVINO 2025 C++ Deployment of Anomaly Detection Models

Loading the Model and Creating Inference Requests

ov::CompiledModel compiled_model = ie.compile_model(onnxpath, "CPU");this->infer_request = compiled_model.create_infer_request();

Getting All Output Layer Names

auto outputs = compiled_model.outputs();for (auto item : outputs) { std::cout << item.get_any_name() << std::endl; names.push_back(item.get_any_name());}

Modifying Input Layer Dimension Information

ov::Tensor input_tensor = infer_request.get_input_tensor();auto input_shape = input_tensor.get_shape();input_shape[0] = 1;input_shape[1] = 3;input_shape[2] = input_h;input_shape[3] = input_w;input_tensor.set_shape(input_shape);

Getting Output Layer Dimension Information and Data Types

std::cout << "element type: " << output2.get_element_type().to_string() << std::endl;std::cout << "shape info: " << output2.get_shape().to_string() << std::endl;

Inference

cv::Mat blob = cv::dnn::blobFromImage(image, 1.0 / 255.0, cv::Size(input_w, input_h), cv::Scalar(), true, false);memcpy(input_tensor.data<float>(), blob.ptr<float>(), data_s * sizeof(float));// Inference and return resultsthis->infer_request.infer();

Getting Inference Output Data

this->infer_request.infer();auto output0 = this->infer_request.get_tensor(names[0]);const float* score = (float*)output0.data();std::cout << "anomaly score: " << score[0] << std::endl;auto output1 = this->infer_request.get_tensor(names[1]);const int* pred_label = (int*)output1.data();std::cout << "predict label: " <<  pred_label[0] << std::endl;

Code Demonstration

For anomaly detection models such as PatchCore, Padim, and EfficientAD, I have implemented a class wrapper that allows for the deployment of anomaly detection models with just a few lines of code. The calling code is as follows:

std::shared_ptr<PatchCoreDetector> detector(new PatchCoreDetector());detector->initConfig("D:/python/yolov5-7.0/padim/model.xml", 224, 224);cv::Mat image = cv::imread("D:/python/yolov5-7.0/breaksmall.png");detector->detect(image);cv::imshow("Input Image", image);cv::waitKey(0);cv::destroyAllWindows();

The results are as follows:

OpenVINO 2025 C++ Deployment of Anomaly Detection ModelsOpenVINO 2025 C++ Deployment of Anomaly Detection ModelsLearn OpenVINO 2025Large Model Deployment DevelopmentScan the code to view directly

OpenVINO 2025 C++ Deployment of Anomaly Detection Models

Essential skills for upper computer and machine vision developers

Systematically master OpenCV C# SDK and workflow engine development. The course includes dozens of common machine vision cases implemented with OpenCV, with over 3000+ lines of code, and all materials, including courseware and code, are available for download. Q&A support is provided! Purchase now to get group buying discounts:

OpenVINO 2025 C++ Deployment of Anomaly Detection Models

Recommended Reading

OpenCV 4.8 + YOLOv8 Object Detection C++ Inference Demonstration

ZXING + OpenCV Build Open Source Barcode Detection Application

Strategy | A Good Method to Learn Deep Learning in Just Three Months

Implement TensorRT 8.6 C++ Deep Learning Model Deployment in Three Lines of Code

Practice | YOLOv8 + OpenCV Implement DM Code Positioning Detection and Analysis

Object Detection Bounding Box Loss – From IOU to ProbIOU

Must-See for Beginners | Five Misconceptions in Learning Deep Learning

OpenVINO 2025 C++ Deployment of Anomaly Detection Models

Leave a Comment