Changed Caffe googlenet tutorial

pull/265/head
Vitaliy Lyudvichenko 9 years ago
parent 83e39a9345
commit 0a64a9d544
  1. 68
      modules/dnn/samples/caffe_googlenet.cpp
  2. 16
      modules/dnn/tutorials/tutorial_dnn_googlenet.markdown

@ -1,4 +1,4 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
/**M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
@ -38,14 +38,12 @@
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace cv::dnn;
#include <algorithm>
#include <fstream>
#include <iostream>
#include <cstdlib>
@ -68,8 +66,7 @@ std::vector<String> readClassNames(const char *filename = "synset_words.txt")
std::ifstream fp(filename);
if (!fp.is_open())
{
std::cerr << "File with classes labels not found" << std::endl;
std::cerr << "Check it: " << filename << std::endl;
std::cerr << "File with classes labels not found: " << filename << std::endl;
exit(-1);
}
@ -89,19 +86,19 @@ int main(int argc, char **argv)
{
String modelTxt = "bvlc_googlenet.prototxt";
String modelBin = "bvlc_googlenet.caffemodel";
String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";
//! [importer_creation]
//! [Create the importer of Caffe model]
Ptr<dnn::Importer> importer;
try //Try to import Caffe GoogleNet model
try //Try to import Caffe GoogleNet model
{
importer = dnn::createCaffeImporter(modelTxt, modelBin);
}
catch (const cv::Exception &err) //importer can throw errors, we will catch them
catch (const cv::Exception &err) //Importer can throw errors, we will catch them
{
std::cerr << err.msg << std::endl;
importer = Ptr<Importer>(); //NULL
}
//! [importer_creation]
//! [Create the importer of Caffe model]
if (!importer)
{
@ -113,52 +110,45 @@ int main(int argc, char **argv)
exit(-1);
}
//! [network_initialization]
//! [Initialize network]
dnn::Net net;
importer->populateNet(net);
importer.release(); //We don't need importer anymore
//! [Initialize network]
delete importer;
//! [network_initialization]
String imagefile = (argc > 1) ? argv[1] : "space_shuttle.jpg";
//! [input_blob_preparation]
Mat img = imread(imagefile);
//! [Prepare blob]
Mat img = imread(imageFile);
if (img.empty())
{
std::cerr << "Can't read image from the file: " << imagefile << std::endl;
std::cerr << "Can't read image from the file: " << imageFile << std::endl;
exit(-1);
}
//GoogLeNet accepts only 224x224 RGB-images
cvtColor(img, img, COLOR_BGR2RGB);
resize(img, img, Size(224, 224));
dnn::Blob inputBlob = dnn::Blob(img);
//! [input_blob_preparation]
resize(img, img, Size(224, 224)); //GoogLeNet accepts only 224x224 RGB-images
dnn::Blob inputBlob = dnn::Blob(img); //Convert Mat to dnn::Blob image batch
//! [Prepare blob]
//! [setup_blob]
net.setBlob(".data", inputBlob); //set the network input
//! [setup_blob]
//! [Set input blob]
net.setBlob(".data", inputBlob); //set the network input
//! [Set input blob]
//! [make_forward]
net.forward(); //compute output
//! [make_forward]
//! [Make forward pass]
net.forward(); //compute output
//! [Make forward pass]
//! [get_output]
dnn::Blob prob = net.getBlob("prob"); //gather output of "prob" layer
//! [Gather output]
dnn::Blob prob = net.getBlob("prob"); //gather output of "prob" layer
int classId;
double classProb;
getMaxClass(prob, &classId, &classProb); //find the best class
//! [get_output]
getMaxClass(prob, &classId, &classProb);//find the best class
//! [Gather output]
//! [print_info]
//! [Print results]
std::vector<String> classNames = readClassNames();
std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
//! [print_info]
//! [Print results]
return 0;
}
} //main

@ -15,6 +15,8 @@ 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).
@include dnn/samples/caffe_googlenet.cpp
Explanation
-----------
@ -28,13 +30,13 @@ Explanation
Put these files into working dir of this program example.
-# Create the importer of Caffe models
@snippet dnn/samples/caffe_googlenet.cpp importer_creation
@snippet dnn/samples/caffe_googlenet.cpp Create the importer of Caffe model
-# Create the network and initialize its by using the created importer
@snippet dnn/samples/caffe_googlenet.cpp network_initialization
@snippet dnn/samples/caffe_googlenet.cpp Initialize network
-# Read input image and convert to the blob, acceptable by GoogleNet
@snippet dnn/samples/caffe_googlenet.cpp input_blob_preparation
@snippet dnn/samples/caffe_googlenet.cpp Prepare blob
Firstly, we resize the image and change its channel sequence order.
Now image is actually a 3-dimensional array with 224x224x3 shape.
@ -42,22 +44,22 @@ Explanation
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
@snippet dnn/samples/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.
Other blobs labeled as "name_of_layer.name_of_layer_output".
-# Make forward pass
@snippet dnn/samples/caffe_googlenet.cpp make_forward
@snippet dnn/samples/caffe_googlenet.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.
-# Determine the best class
@snippet dnn/samples/caffe_googlenet.cpp get_output
@snippet dnn/samples/caffe_googlenet.cpp Gather 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 results
@snippet dnn/samples/caffe_googlenet.cpp print_info
@snippet dnn/samples/caffe_googlenet.cpp Print results
For our image we get:
> Best class: #812 'space shuttle'
>

Loading…
Cancel
Save