In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how C++ realizes the DNN network based on OpenCV". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how C++ realizes the DNN network based on OpenCV".
OpenCV has introduced the DNN module since version 3.3, and now it is version 4.5, and the support of the DNN module is even better. At present, OpenCV already supports model loading and reasoning in ONNX format, and the back-end reasoning engine also has a variety of options.
Pytorch, as the best deep learning and training framework for ease of use, is widely used. Pytorch's pth format model cannot be loaded directly with OpenCV, but can be converted to ONNX format for use.
1. OpenCV compilation
First compile OpenCV under Ubuntu, the default configuration option can already support loading ONNX model for reasoning, do not need contrib library support or other special configuration.
In the process of compilation, I only specify the CMAKE_INSTALL_PREFIX parameter according to the custom, which is used to specify the compiled installation path, which is convenient for later management and does not interfere with the system directory.
2. ONNX model export
Pytorch has native support for exporting ONNX models, which can be found in the official tutorials and documentation.
The simplest export Demo is as follows, with only one function torch.onnx.export at the core.
Import torchimport torchvisionmodel = torchvision.models.mobilenet_v2 (pretrained=True) x = torch.rand (1,3,224,224) save_path = ". / model_file/mobilenetv2.onnx" torch.onnx.export (model, x, save_path, input_names= ["input"], output_names= ["output"], opset_version=11) 3. OpenCV reasoning
To use OpenCV's DNN module for network reasoning, you can refer to the official tutorial and samples/dnn/classification.cpp in the source code package.
Here I have done a goal correction of the small network as an example to demonstrate a simplified Demo.
3.1 Model structure
The model draws lessons from the InvertedResidual module of MobilenetV2, sets up the feature extraction part, inputs 64 images, and finally outputs a 10-dimensional vector through pooling layer and fc layer.
The meaning of the output vector is:
0: whether the target Confidence contains the target
1-5: target Class, classification score
6-9:2D-BBox, expressed as xywh.
3.2 Model reasoning
A simplified reasoning Demo is shown below.
# include # include using namespace cv;int main (int argc, char** argv) {/ / load model String model = ".. / model_file/mobilenet.onnx"; dnn::Net net = dnn::readNetFromONNX (model); / / load image data float scale = 0.0078125; Scalar mean = Scalar (128.0, 128.0, 128.0); Mat frame = imread (".. / images/car.png"); Mat blob Dnn::blobFromImage (frame, blob, scale, Size (64, 64), mean, false, false); / / inference and time double t = getTickCount (); net.setInput (blob); Mat output = net.forward (); t = (getTickCount ()-t) / getTickFrequency (); std::cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.