parent
e52a7eeedb
commit
3154fc0dba
4 changed files with 129 additions and 64 deletions
After Width: | Height: | Size: 27 KiB |
@ -0,0 +1,64 @@ |
||||
Load Caffe framework models {#tutorial_dnn_googlenet} |
||||
=========================== |
||||
|
||||
Introduction |
||||
------------ |
||||
|
||||
In this tutorial you will learn how to use opencv_dnn module for image classification by using |
||||
GoogLeNet trained network from [Caffe model zoo](http://caffe.berkeleyvision.org/model_zoo.html). |
||||
|
||||
We will demostrate results of this example on the following picture. |
||||
 |
||||
|
||||
Source Code |
||||
----------- |
||||
|
||||
We will be using snippets from the example application, that can be downloaded [here](https://github.com/ludv1x/opencv_contrib/blob/master/modules/dnn/samples/caffe_googlenet.cpp). |
||||
|
||||
Explanation |
||||
----------- |
||||
|
||||
-# Firstly, download GoogLeNet model files: |
||||
[bvlc_googlenet.prototxt ](https://raw.githubusercontent.com/ludv1x/opencv_contrib/master/modules/dnn/samples/bvlc_googlenet.prototxt) and |
||||
[bvlc_googlenet.caffemodel](http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel) |
||||
|
||||
Also you need file with names of [ILSVRC2012](http://image-net.org/challenges/LSVRC/2012/browse-synsets) classes: |
||||
[synset_words.txt](https://raw.githubusercontent.com/ludv1x/opencv_contrib/master/modules/dnn/samples/synset_words.txt). |
||||
|
||||
Put these files into working dir of this program example. |
||||
|
||||
-# Create the importer of Caffe models |
||||
@snippet dnn/samples/caffe_googlenet.cpp importer_creation |
||||
|
||||
-# Create the network and initialize its by using the created importer |
||||
@snippet dnn/samples/caffe_googlenet.cpp network_initialization |
||||
|
||||
-# Read input image and convert to the blob, acceptable by GoogleNet |
||||
@snippet dnn/samples/caffe_googlenet.cpp input_blob_preparation |
||||
Firstly, we resize the image and change its channel sequence order. |
||||
|
||||
Now image is actually a 3-dimensional array with 224x224x3 shape. |
||||
|
||||
Next, we convert the image to 4-dimensional blob (so-called batch) with 1x2x224x224 shape by using special @ref cv::dnn::Blob constructor. |
||||
|
||||
-# Pass the blob to the network |
||||
@snippet dnn/samples/caffe_googlenet.cpp setup_blob |
||||
In bvlc_googlenet.prototxt the network input blob named as "data", therefore this blob labeled as ".data" in opencv_dnn API. |
||||
|
||||
Other blobs labeled as "name_of_layer.name_of_layer_output". |
||||
|
||||
-# Make forward pass |
||||
@snippet dnn/samples/caffe_googlenet.cpp make_forward |
||||
During the forward pass output of each network layer is computed, but in this example we need output from "prob" layer only. |
||||
|
||||
-# Determine the best class |
||||
@snippet dnn/samples/caffe_googlenet.cpp get_output |
||||
We put the output of "prob" layer, which contain probabilities for each of 1000 ILSVRC2012 image classes, to the `prob` blob. |
||||
And find the index of element with maximal value in this one. This index correspond to the class of the image. |
||||
|
||||
-# Print the results |
||||
@snippet dnn/samples/caffe_googlenet.cpp print_info |
||||
For our image we get: |
||||
> Best class: #812 'space shuttle' |
||||
> |
||||
> Probability: 99.6378% |
Loading…
Reference in new issue