2017-06-26 18:35:51 +08:00
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 demonstrate results of this example on the following picture.
![Buran space shuttle ](images/space_shuttle.jpg )
Source Code
-----------
2017-06-26 20:01:30 +08:00
We will be using snippets from the example application, that can be downloaded [here ](https://github.com/opencv/opencv/blob/master/modules/samples/dnn/caffe_googlenet.cpp ).
2017-06-26 18:35:51 +08:00
2017-06-26 20:01:30 +08:00
@include dnn/caffe_googlenet.cpp
2017-06-26 18:35:51 +08:00
Explanation
-----------
-# Firstly, download GoogLeNet model files:
2017-06-26 20:01:30 +08:00
[bvlc_googlenet.prototxt ](https://raw.githubusercontent.com/opencv/opencv/master/modules/samples/data/dnn/bvlc_googlenet.prototxt ) and
2017-06-26 18:35:51 +08:00
[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:
2017-06-26 20:01:30 +08:00
[synset_words.txt ](https://raw.githubusercontent.com/opencv/opencv/master/modules/samples/data/dnn/synset_words.txt ).
2017-06-26 18:35:51 +08:00
Put these files into working dir of this program example.
-# Read and initialize network using path to .prototxt and .caffemodel files
2017-06-26 20:01:30 +08:00
@snippet dnn/caffe_googlenet.cpp Read and initialize network
2017-06-26 18:35:51 +08:00
-# Check that network was read successfully
2017-06-26 20:01:30 +08:00
@snippet dnn/caffe_googlenet.cpp Check that network was read successfully
2017-06-26 18:35:51 +08:00
-# Read input image and convert to the blob, acceptable by GoogleNet
2017-06-26 20:01:30 +08:00
@snippet dnn/caffe_googlenet.cpp Prepare blob
2017-06-26 18:35:51 +08:00
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 1x3x224x224 shape by using special cv::dnn::blobFromImages constructor.
-# Pass the blob to the network
2017-06-26 20:01:30 +08:00
@snippet dnn/caffe_googlenet.cpp Set input blob
2017-06-26 18:35:51 +08:00
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
2017-06-26 20:01:30 +08:00
@snippet dnn/caffe_googlenet.cpp Make forward pass
2017-06-26 18:35:51 +08:00
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
2017-06-26 20:01:30 +08:00
@snippet dnn/caffe_googlenet.cpp Gather output
2017-06-26 18:35:51 +08:00
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 results
2017-06-26 20:01:30 +08:00
@snippet dnn/caffe_googlenet.cpp Print results
2017-06-26 18:35:51 +08:00
For our image we get:
> Best class: #812 'space shuttle'
>
> Probability: 99.6378%