日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【caffe】OpenCV Load caffe model

發(fā)布時間:2025/3/21 编程问答 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【caffe】OpenCV Load caffe model 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

上一篇,我們介紹了opencv_contrib中的模塊在windows下的編譯,也提到了其中的dnn模塊可以讀取caffe的訓(xùn)練模型用于目標(biāo)檢測,這里我們具體介紹一下如何使用dnn讀取caffe模型并進(jìn)行目標(biāo)分類。


代碼如下:(代碼主要來自參考[2]和[3]):

#include <opencv2/dnn.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <fstream> #include <iostream> #include <cstdlib> /* Find best class for the blob (i. e. class with maximal probability) */ void getMaxClass(cv::dnn::Blob &probBlob, int *classId, double *classProb) {cv::Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix cv::Point classNumber;cv::minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);*classId = classNumber.x; }std::vector<cv::String> readClassNames(const char *filename = "synset_words.txt") {std::vector<cv::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; }int main(int argc, char **argv) {void cv::dnn::initModule();cv::String modelTxt = "bvlc_googlenet.prototxt";cv::String modelBin = "bvlc_googlenet.caffemodel";cv::String imageFile = "space_shuttle.jpg";cv::dnn::Net net = cv::dnn::readNetFromCaffe(modelTxt, modelBin);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);}//! [Prepare blob] cv::Mat img = cv::imread(imageFile, cv::IMREAD_COLOR);if (img.empty()){std::cerr << "Can't read image from the file: " << imageFile << std::endl;exit(-1);}cv::resize(img, img, cv::Size(224, 224)); cv::dnn::Blob inputBlob = cv::dnn::Blob(img); //Convert Mat to dnn::Blob image batch //! [Prepare blob] //! [Set input blob] net.setBlob(".data", inputBlob); //set the network input //! [Set input blob] //! [Make forward pass] net.forward(); //compute output //! [Make forward pass] //! [Gather output] cv::dnn::Blob prob = net.getBlob("prob"); //gather output of "prob" layer int classId;double classProb;getMaxClass(prob, &classId, &classProb);//find the best class //! [Gather output] //! [Print results] std::vector<cv::String> classNames = readClassNames();std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;std::cout << "Probability: " << classProb * 100 << "%" << std::endl;//! [Print results] return 0; } //main


代碼詳解 :
1、首先需要下載GoogLeNet模型及分類相關(guān)文件,可以從官網(wǎng)下載(或復(fù)制粘貼): ??bvlc_googlenet.prototxt、bvlc_googlenet.caffemodel以及synset_words.txt.也可以直接下載我長傳的打包好的資源(包括了2中的圖片)

2、下載待檢測圖片文件,如下:


Buran space shuttle

3、讀取.protxt文件和.caffemodel文件:

cv::dnn::Net net = cv::dnn::readNetFromCaffe(modelTxt, modelBin);

4、檢查網(wǎng)絡(luò)是否讀取成功:

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);}
5、讀取圖片并將其轉(zhuǎn)換成GoogleNet可以讀取的blob:

cv::Mat img = cv::imread(imageFile, cv::IMREAD_COLOR);if (img.empty()){std::cerr << "Can't read image from the file: " << imageFile << std::endl;exit(-1);}cv::resize(img, img, cv::Size(224, 224)); cv::dnn::Blob inputBlob = cv::dnn::Blob(img); //Convert Mat to dnn::Blob image batch

6、將blob傳遞給網(wǎng)絡(luò):

net.setBlob(".data", inputBlob); //set the network input

7、前向傳遞:

net.forward(); //compute output

8、分類:

getMaxClass(prob, &classId, &classProb);//find the best class

9、打印分類結(jié)果:

std::vector<cv::String> classNames = readClassNames();std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;std::cout << "Probability: " << classProb * 100 << "%" << std::endl;

運(yùn)行,報(bào)錯如下:



找了很久,終于在參考[3]中找到了解決方案,原因是這里將圖像數(shù)據(jù)轉(zhuǎn)換成blob的方法來自于老版本,在新版本中不兼容。解決方法如下:cv::dnn::Blob(img)?用cv::dnn::Blob::fromImages(img)替換掉。


修改后,再運(yùn)行,結(jié)果如下:



參考:

[1] http://docs.opencv.org/trunk/d5/de7/tutorial_dnn_googlenet.html
[2] http://blog.csdn.net/langb2014/article/details/50555910
[3] https://github.com/opencv/opencv_contrib/issues/749


-----------------------------------------

2017.07.24


總結(jié)

以上是生活随笔為你收集整理的【caffe】OpenCV Load caffe model的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。