opencv+ Deep Learning pre-training model for simple image recognition | Tutorial

Source: Internet
Author: User
Tags dnn

Reprint: Https://mp.weixin.qq.com/s/J6eo4MRQY7jLo7P-b3nvJg

Li Lin compiled from Pyimagesearch
Author Adrian rosebrock
Quantum bit Report | Public number Qbitai

OpenCV is a 2000 release of the open-source computer vision Library, with object recognition, image segmentation, face recognition, motion recognition and other functions, can be run on Linux, Windows, Android, Mac OS and other operating systems, with lightweight, efficient known, and provides multiple language interfaces.

OPENCV's latest version update gives us better deep learning support, and it's easy to use pre-trained deep learning models in OPENCV.

Pyimagesearch Web site today released a opencv+ deep learning pre-training model for image recognition tutorial, quantum bit compiled as follows:

Recently, OpenCV 3.3 has just been released, providing better support for deep learning (DNN modules), DNN modules currently support deep learning frameworks such as Caffe, TensorFlow, Torch, and Pytorch.

In addition, the API for using the pre-trained deep learning model in the new version is compatible with both C + + and Python, making the series easy to operate:

Load model from hard disk;

preprocessing the input image;

Enter the image into the network to get the classification of the output.

Of course, we cannot, and should not, use OPENCV to train deep learning models, but this new version allows us to use a model that has been trained with a deep learning framework and is effectively used in OPENCV.

This article shows how to identify images using a imagenet-trained deep learning model. deep Learning in OpenCV 3.3

Since OPENCV version 3.1, the DNN module has been part of the Opencv_contrib library, and in version 3.3 it has been referred to in the main repository.

Using OpenCV 3.3, the deep learning pre-training model can be used very well to classify them as classifiers.

The new OPENCV is compatible with the following popular network architectures:

AlexNet

Googlenet V1 (also called inception-5h)

Resnet-34/50/...

Squeezenet v1.1

Vgg-based FCN

ENet

Vgg-based SSD

Mobilenet-based SSD

The module's main contributor, Rynikov Alexander, has a big plan for the module, but he wrote the release notes in Russian, interested students please Google Translate read: https://habrahabr.ru/company/ intel/blog/333612/

I think the DNN module will have a big impact on the OPENCV community. functions and Frames

Using the deep learning pre-training model in OPENCV, the first step is to install OPENCV 3.3, and the installation process quantum bit is no longer described in detail ...

Here are some of the functions we will use.

To load a picture from a disk in DNN:

Cv2.dnn.blobFromImage

Cv2.dnn.blobFromImages

Use the "create" method to derive the model directly from the various frameworks:

Cv2.dnn.createCaffeImporter

Cv2.dnn.createTensorFlowImporter

Cv2.dnn.createTorchImporter

Use the Read method to load the serialization model directly from the disk:

Cv2.dnn.readNetFromCaffe

Cv2.dnn.readNetFromTensorFlow

Cv2.dnn.readNetFromTorch

Cv2.dnn.readhTorchBlob

After the model has been loaded from disk, you can use the. Forward method to propagate our images forward to get the classification results. using OPENCV and deep learning to classify images

Next, we'll learn how to use Python, OpenCV, and a pre-trained Caffe model for image recognition.

The deep learning model used below is a pre-trained googlelenet on the imagenet. Googlelenet from Szegedy and others 2014 paper going deeper with convolutions, see more: https://arxiv.org/abs/1409.4842

First, open a new file, name it deep_learning_with_opencv.py, and insert the following code to import the package we need:

Then disassemble the command-line arguments:

WHERE line 8th AP = argparse. Argumentparser () is used to create a parameter parser, and the following code is used to create 4 command-line arguments:

-image: Enter the path of the image;

-prototxt:caffe Deploying the Prototxt Path

-model: Pre-trained Caffe models, such as network weights;

The path to the-labels:imagenet tag, such as Syn-sets.

After we have created the parameters, we parse them and there is a variable, args, for later use.

Next, load the input images and tags:

Line 20th loads images from disk, rows 23rd and 24 are loaded with these tags:

After we've done the tag, let's take a look at the DNN module:

Note the comments in the code above, we use cv2.dnn.blobFromImage to perform mean subtraction to normalized the input image, resulting in a known blob shape.

Then load our model from disk:

We use Cv2.dnn.readNetFromCaffe to load the Caffe model definition Prototxt, as well as the pre-trained model.

Next, we complete a forward propagation in the neural network with the BLOB as input:

Note: We're not training CNN, we're using a pre-trained model, so we just have to pass the BLOB over the network to get the results, without the need for reverse propagation.

Finally, let's take out 5 of the highest-ranked predictions for the input image:

We can use NumPy to select the top 5 results, and then show them:

Classification Results

We've implemented deep learning image recognition with Python code in OpenCV, and now you can take some pictures to try it out.

Open your terminal and execute the following command:

You will get the result:

OpenCV and Googlelenet correctly recognized the Beagle Terrier, ranking the first result is correct, after the 4 results correlation is also very high.

Running this algorithm on the CPU will only take less than a second to get the results.

Here's another one:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.