Add next and previous navigation links to all tutorials
[platform/upstream/opencv.git] / doc / tutorials / dnn / dnn_googlenet / dnn_googlenet.markdown
1 Load Caffe framework models  {#tutorial_dnn_googlenet}
2 ===========================
3
4 @next_tutorial{tutorial_dnn_halide}
5
6 Introduction
7 ------------
8
9 In this tutorial you will learn how to use opencv_dnn module for image classification by using
10 GoogLeNet trained network from [Caffe model zoo](http://caffe.berkeleyvision.org/model_zoo.html).
11
12 We will demonstrate results of this example on the following picture.
13 ![Buran space shuttle](images/space_shuttle.jpg)
14
15 Source Code
16 -----------
17
18 We will be using snippets from the example application, that can be downloaded [here](https://github.com/opencv/opencv/blob/3.4/samples/dnn/classification.cpp).
19
20 @include dnn/classification.cpp
21
22 Explanation
23 -----------
24
25 -# Firstly, download GoogLeNet model files:
26    [bvlc_googlenet.prototxt  ](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/bvlc_googlenet.prototxt) and
27    [bvlc_googlenet.caffemodel](http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel)
28
29    Also you need file with names of [ILSVRC2012](http://image-net.org/challenges/LSVRC/2012/browse-synsets) classes:
30    [classification_classes_ILSVRC2012.txt](https://github.com/opencv/opencv/blob/3.4/samples/data/dnn/classification_classes_ILSVRC2012.txt).
31
32    Put these files into working dir of this program example.
33
34 -# Read and initialize network using path to .prototxt and .caffemodel files
35    @snippet dnn/classification.cpp Read and initialize network
36
37    You can skip an argument `framework` if one of the files `model` or `config` has an
38    extension `.caffemodel` or `.prototxt`.
39    This way function cv::dnn::readNet can automatically detects a model's format.
40
41 -# Read input image and convert to the blob, acceptable by GoogleNet
42    @snippet dnn/classification.cpp Open a video file or an image file or a camera stream
43
44    cv::VideoCapture can load both images and videos.
45
46    @snippet dnn/classification.cpp Create a 4D blob from a frame
47    We convert the image to a 4-dimensional blob (so-called batch) with `1x3x224x224` shape
48    after applying necessary pre-processing like resizing and mean subtraction
49    `(-104, -117, -123)` for each blue, green and red channels correspondingly using cv::dnn::blobFromImage function.
50
51 -# Pass the blob to the network
52    @snippet dnn/classification.cpp Set input blob
53
54 -# Make forward pass
55    @snippet dnn/classification.cpp Make forward pass
56    During the forward pass output of each network layer is computed, but in this example we need output from the last layer only.
57
58 -# Determine the best class
59    @snippet dnn/classification.cpp Get a class with a highest score
60    We put the output of network, which contain probabilities for each of 1000 ILSVRC2012 image classes, to the `prob` blob.
61    And find the index of element with maximal value in this one. This index corresponds to the class of the image.
62
63 -# Run an example from command line
64    @code
65    ./example_dnn_classification --model=bvlc_googlenet.caffemodel --config=bvlc_googlenet.prototxt --width=224 --height=224 --classes=classification_classes_ILSVRC2012.txt --input=space_shuttle.jpg --mean="104 117 123"
66    @endcode
67    For our image we get prediction of class `space shuttle` with more than 99% sureness.