Super simple pyTorch training->onnx model->C++ OpenCV DNN reasoning (with source code address)

learn better from others,

be the better one.

—— "Weika Zhixiang"

e3772c9c3c8c3fa77e5c3381e73e906e.jpeg

The length of this article is 1974 words , and it is expected to read for 5 minutes

foreword

I wanted to learn deep learning a long time ago, because I usually study by myself and have limited spare time. I have read a few introductory articles on pyTorch, but they are all fragmented things that cannot be connected together. Recently, I was just in time for the epidemic, and there were fewer business trips. When I watched the pyTorch video at station B, there was a comment that Mr. Liu Er’s "pyTorch Deep Learning Practice" was very good. After reading the whole tutorial, it was really easy to understand. Being in charge of the second branch by myself is considered an introduction.

The most important thing for getting started with pyTorch is to actually use it. The DNN module in OpenCV is used for reasoning, so this article is dedicated to the simplest example to verify the effect. The Github address of the source code is at the end of the article.

5004f8cf90fc02c25b8ee1f569027399.png

# Method to realize
1 pyTorch training data
2 Transfer the data model trained by pyTorch to an onnx file
3 Inference using C++ OpenCV DNN

Configuration Environment

OS: Windows 11 Home

pyTorch related: Miniconda + pyTorch1.12.1(cpu) + python3.9.12, IDE uses Visual Studio Code

OpenCV related: OpenCV 4.5.1 + C++, IDE uses Visual Studio 2022

The original environment is installed in the Anaconda family bucket, which requires a large resource space, and Miniconda is the smallest conda installation environment, so it is recommended to use Miniconda here. There are quite a lot of specific environment configuration and installation methods on the Internet, including videos, so you can search for it yourself.

I usually connect a large-screen monitor to the office, but now due to work reasons, I now travel a lot. In order to meet the needs of using a large screen and make it easy to carry on business trips, I changed the notebook with a folding screen, from the original ASUS Lingyao X DUO to ASUS Lingyao X Fold, correspondingly, originally had a discrete graphics card, but now it can only be an integrated graphics card, and pyTorch is also installed with a CPU version. The last few comparison pictures of the two notebooks

83e96114a511e33415ad4c61df050cc0.jpeg

6759c0bf62ef10cfbae9913741a9792b.jpeg

6a728284d90b8475255d091c04b6a770.png

Code

fb545bfb7211ff9f42ecf7d1295acb09.png

Micro card Zhixiang

To do the simplest training and reasoning, we don’t use images, it’s just a simple operation, as shown in the figure below:

1aa63ce6323e4e49c69473a1b2fcdcbb.png

The red box in the picture above represents the training set I want. We can see at a glance that the achieved effect is to multiply the input value by 2 to get the output result. The following two 4.0 and 100.0 are used to infer the result, and the result should also be 8.0 and 200.0

pyTorch training

01

Define the training set

f2ad9e7d963a72d8d5c9b898b3336b76.png

After importing the torch package, we directly define the input x_data as [1, 2, 3], and the output y_data as [2, 4, 6]. The input results are printed as follows:

a68a45126a04736a08d8418027470de9.png

02

Define the trained network model and loss function and optimizer

b64b18e4fa425d77f60281fe7eddc272.png

The training model is also very simple, there is only one layer of the Linear full connection layer, and the activation function is not used in the feedforward function, and it is directly output. MSE for loss function, optimizer SGD, where the learning rate parameter is set to 0.02 (lr=0.02)

03

training model

3246ac88e4fc99171001ed3a18e14a6b.png

Regarding training, there are mainly four steps:

  1. forward

  2. loss (calculate loss function)

  3. backward (backward propagation)

  4. step (optimization iteration)

Above we set the number of training times to 1000 times, print the loss every 100 times, and finally output the weight value. The training results are as follows:

7021ce4a13703ecca4af15b8292def26.png

04

Verification test and output onnx model

fa45721f84ec758a79d57a586f6d9b7b.png

After the training is complete, let's verify the test results. We input four values: 4, 8, 10, and 15. The printed results are as follows:

0fd88ac00c9572f05b1787eb13caf131.png

As can be seen in the figure above, the predicted results are completely accurate. Next, we will export the trained model to the onnx file for OpenCV reasoning.

5e29f94476af31cd77f9d8667ee00098.png

  1. Changing the model model to eval() is set to inference mode.

  2. Define an input parametric model dummy_input

  3. Set the output, the output parameter name input and output, and the file name of onnx

  4. Export with torch.onnx.export, where verbose=True is the debug description that will output the exported trajectory

After success, a test.onnx model file will be generated in the current directory, so that the model trained by pyTorch is completed. The next step is to see if OpenCV's DNN is used for inference.

C++ OpenCV inference

a617426b183fbfa832e41ccabf9cac93.png

The code of C++ OpenCV DNN reasoning is also very simple. The main thing is to define dnn::Net, then specify the directory of the onnx model file, and use readNetfromOnnx to load the model file.

The input parameter still uses OpenCV's Mat, because only one parameter is input, so the definition here is 1X1, the data type input is the type of float, so the definition is also CV_32F, and Mat is passed into the input parameter, net.setInput(Mat Value, input parameter name), the second parameter is the same as the parameter name we exported, and then get the returned result through net.forward (output parameter name).

5b2503d7c0eb76cdee9f88f3b9d0f00d.png

In the code, we input 1024, so the predicted result is 2048, which is completely correct. Such a simple pyTorch training model is transferred to onnx, and then the Demo of C++ OpenCV reasoning is completed.

over

03f2f1a80dc490a0b6466b5d8e00251e.png

Micro card Zhixiang

source address

https://github.com/Vaccae/OpenCVDemoCpp.git

Click to read the original text to see the code address of "Code Cloud"

28ddace8f433944132ec58780d3dd951.png

Wonderful review of the past

f0683ad2d9969228d966a6f41197acf0.jpeg

Kotlin uses Select expression in coroutine to select fastest result


6042b5dc6188777da35c96415f09843b.jpeg

C++ OpenCV manually intercepts images for perspective transformation


9fd98e5417b942887b5bba943491b10b.jpeg

Use OpenCV to make a simple color extractor


Guess you like

Origin blog.csdn.net/Vaccae/article/details/128310182