opencv在3.0之后就支持調(diào)用深度學(xué)習(xí)模型奋岁。OpenCV dnn模塊目前支持Caffe、TensorFlow荸百、Torch闻伶、PyTorch等深度學(xué)習(xí)框架。另外够话,新版本中使用預(yù)訓(xùn)練深度學(xué)習(xí)模型的API同時(shí)兼容C++和Python蓝翰。
參照官方教程進(jìn)行一個(gè)分類模型的調(diào)用caffe模型進(jìn)行分類,Load Caffe framework models女嘲。注意版本畜份,我的版本是3.4.1,選擇對(duì)應(yīng)版本欣尼。
在opencv的解壓文件下面有sample/dnn/所有的例子爆雹。這個(gè)例子來自sample/dnn//caffe_googlenet.cpp
下載兩個(gè)文件bvlc_googlenet.prototxt and bvlc_googlenet.caffemodel
3.也要下載 synset_words.txt.
把上面三個(gè)文件放在與caffe_googlenet.cpp同級(jí)目錄。
新建工程把caffe_googlenet.cpp添加進(jìn)去愕鼓。
caffe_googlenet.cpp 代碼
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/core/utils/trace.hpp>
using namespace cv;
using namespace cv::dnn;
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
/* Find best class for the blob (i. e. class with maximal probability) */
static void getMaxClass(const Mat &probBlob, int *classId, double *classProb)
{
Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 1x1000 matrix
Point classNumber;
minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
*classId = classNumber.x;
}
static std::vector<String> readClassNames(const char *filename)
{
std::vector<String> classNames;
std::ifstream fp(filename);
if (!fp.is_open())
{
std::cerr << "File with classes labels not found: " << filename << std::endl;
exit(-1);
}
std::string name;
while (!fp.eof())
{
std::getline(fp, name);
if (name.length())
classNames.push_back(name.substr(name.find(' ') + 1));
}
fp.close();
return classNames;
}
const char* params
= "{ help | false | Sample app for loading googlenet model }"
"{ proto | bvlc_googlenet.prototxt | model configuration }"
"{ model | bvlc_googlenet.caffemodel | model weights }"
"{ label | classification_classes_ILSVRC2012.txt | names of ILSVRC2012 classes }"
"{ image | space_shuttle.jpg | path to image file }"
"{ opencl | false | enable OpenCL }"
;
int main(int argc, char **argv)
{
CV_TRACE_FUNCTION();
CommandLineParser parser(argc, argv, params);
if (parser.get<bool>("help"))
{
parser.printMessage();
return 0;
}
String modelTxt = parser.get<string>("proto");
String modelBin = parser.get<string>("model");
String imageFile = parser.get<String>("image");
String classNameFile = parser.get<String>("label");
Net net;
try {
//! [Read and initialize network]
net = dnn::readNetFromCaffe(modelTxt, modelBin);
//! [Read and initialize network]
}
catch (const cv::Exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
//! [Check that network was read successfully]
if (net.empty())
{
std::cerr << "Can't load network by using the following files: " << std::endl;
std::cerr << "prototxt: " << modelTxt << std::endl;
std::cerr << "caffemodel: " << modelBin << std::endl;
std::cerr << "bvlc_googlenet.caffemodel can be downloaded here:" << std::endl;
std::cerr << "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel" << std::endl;
exit(-1);
}
//! [Check that network was read successfully]
}
if (parser.get<bool>("opencl"))
{
net.setPreferableTarget(DNN_TARGET_OPENCL);
}
//! [Prepare blob]
Mat img = imread(imageFile);
if (img.empty())
{
std::cerr << "Can't read image from the file: " << imageFile << std::endl;
exit(-1);
}
//GoogLeNet accepts only 224x224 BGR-images
Mat inputBlob = blobFromImage(img, 1.0f, Size(224, 224),
Scalar(104, 117, 123), false); //Convert Mat to batch of images
//! [Prepare blob]
net.setInput(inputBlob, "data"); //set the network input
Mat prob = net.forward("prob"); //compute output
cv::TickMeter t;
for (int i = 0; i < 10; i++)
{
CV_TRACE_REGION("forward");
//! [Set input blob]
net.setInput(inputBlob, "data"); //set the network input
//! [Set input blob]
t.start();
//! [Make forward pass]
prob = net.forward("prob"); //compute output
//! [Make forward pass]
t.stop();
}
//! [Gather output]
int classId;
double classProb;
getMaxClass(prob, &classId, &classProb);//find the best class
//! [Gather output]
//! [Print results]
std::vector<String> classNames = readClassNames(classNameFile.c_str());
std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
//! [Print results]
std::cout << "Time: " << (double)t.getTimeMilli() / t.getCounter() << " ms (average from " << t.getCounter() << " iterations)" << std::endl;
// input samething
int a;
std::cin >> a;
return 0;
} //main
按照對(duì)應(yīng)的版本使用例子钙态,一般不會(huì)出錯(cuò)。
這里有幾個(gè)比較重要的參數(shù)菇晃,需改對(duì)應(yīng)的參數(shù)就可進(jìn)行不同圖片的分類了册倒。
const char* params
= "{ help | false | Sample app for loading googlenet model }"
"{ proto | bvlc_googlenet.prototxt | model configuration }"
"{ model | bvlc_googlenet.caffemodel | model weights }"
"{ label | synset_words.txt | names of ILSVRC2012 classes }"
"{ image | space_shuttle.jpg | path to image file }"
"{ opencl | false | enable OpenCL }"
proto: caffe的配置文件
model:caffe的參數(shù)文件
image:圖片
opencl:是否使用opencl進(jìn)行加速
result
Attempting to upgrade input file specified using deprecated V1LayerParameter: bvlc_googlenet.caffemodel
Successfully upgraded file specified using deprecated V1LayerParameter
[ INFO:0] Initialize OpenCL runtime...
Net Outputs(1):
prob
Best class: #812 'shuttle'
Probability: 99.993%
Time: 1492.05 ms (average from 10 iterations)
使用c++ opencv調(diào)用tensorflow訓(xùn)練好的卷積神經(jīng)網(wǎng)絡(luò)
opencv官方教程caffe model調(diào)用
OpenCV調(diào)用TensorFlow預(yù)訓(xùn)練模型
基于opencv dnn模塊 的caffe模型的調(diào)用