@@ -13,50 +13,53 @@ We will demonstrate results of this example on the following picture.
...
@@ -13,50 +13,53 @@ We will demonstrate results of this example on the following picture.
Source Code
Source Code
-----------
-----------
We will be using snippets from the example application, that can be downloaded [here](https://github.com/opencv/opencv/blob/master/samples/dnn/caffe_googlenet.cpp).
We will be using snippets from the example application, that can be downloaded [here](https://github.com/opencv/opencv/blob/master/samples/dnn/classification.cpp).
@include dnn/caffe_googlenet.cpp
@include dnn/classification.cpp
Explanation
Explanation
-----------
-----------
-# Firstly, download GoogLeNet model files:
-# Firstly, download GoogLeNet model files:
[bvlc_googlenet.prototxt ](https://raw.githubusercontent.com/opencv/opencv/master/samples/data/dnn/bvlc_googlenet.prototxt) and
[bvlc_googlenet.prototxt ](https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/bvlc_googlenet.prototxt) and
Put these files into working dir of this program example.
Put these files into working dir of this program example.
-# Read and initialize network using path to .prototxt and .caffemodel files
-# Read and initialize network using path to .prototxt and .caffemodel files
@snippet dnn/caffe_googlenet.cpp Read and initialize network
@snippet dnn/classification.cpp Read and initialize network
-# Check that network was read successfully
You can skip an argument `framework` if one of the files `model` or `config` has an
@snippet dnn/caffe_googlenet.cpp Check that network was read successfully
extension `.caffemodel` or `.prototxt`.
This way function cv::dnn::readNet can automatically detects a model's format.
-# Read input image and convert to the blob, acceptable by GoogleNet
-# Read input image and convert to the blob, acceptable by GoogleNet
@snippet dnn/caffe_googlenet.cpp Prepare blob
@snippet dnn/classification.cpp Open a video file or an image file or a camera stream
We convert the image to a 4-dimensional blob (so-called batch) with 1x3x224x224 shape after applying necessary pre-processing like resizing and mean subtraction using cv::dnn::blobFromImage constructor.
-# Pass the blob to the network
cv::VideoCapture can load both images and videos.
@snippet dnn/caffe_googlenet.cpp Set input blob
In bvlc_googlenet.prototxt the network input blob named as "data", therefore this blob labeled as ".data" in opencv_dnn API.
@snippet dnn/classification.cpp Create a 4D blob from a frame
We convert the image to a 4-dimensional blob (so-called batch) with `1x3x224x224` shape
after applying necessary pre-processing like resizing and mean subtraction
`(-104, -117, -123)` for each blue, green and red channels correspondingly using cv::dnn::blobFromImage function.
Other blobs labeled as "name_of_layer.name_of_layer_output".
-# Pass the blob to the network
@snippet dnn/classification.cpp Set input blob
-# Make forward pass
-# Make forward pass
@snippet dnn/caffe_googlenet.cpp Make forward pass
@snippet dnn/classification.cpp Make forward pass
During the forward pass output of each network layer is computed, but in this example we need output from "prob" layer only.
During the forward pass output of each network layer is computed, but in this example we need output from the last layer only.
-# Determine the best class
-# Determine the best class
@snippet dnn/caffe_googlenet.cpp Gather output
@snippet dnn/classification.cpp Get a class with a highest score
We put the output of "prob" layer, which contain probabilities for each of 1000 ILSVRC2012 image classes, to the `prob` blob.
We put the output of network, 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.
And find the index of element with maximal value in this one. This index corresponds to the class of the image.
@@ -74,46 +74,7 @@ When you build OpenCV add the following configuration flags:
...
@@ -74,46 +74,7 @@ When you build OpenCV add the following configuration flags:
-`HALIDE_ROOT_DIR` - path to Halide build directory
-`HALIDE_ROOT_DIR` - path to Halide build directory
## Sample
## Set Halide as a preferable backend
@code
@include dnn/squeezenet_halide.cpp
net.setPreferableBackend(DNN_BACKEND_HALIDE);
@endcode
## Explanation
Download Caffe model from SqueezeNet repository: [train_val.prototxt](https://github.com/DeepScale/SqueezeNet/blob/master/SqueezeNet_v1.1/train_val.prototxt) and [squeezenet_v1.1.caffemodel](https://github.com/DeepScale/SqueezeNet/blob/master/SqueezeNet_v1.1/squeezenet_v1.1.caffemodel).
Also you need file with names of [ILSVRC2012](http://image-net.org/challenges/LSVRC/2012/browse-synsets) classes: