Created
December 2, 2019 13:28
-
-
Save ixtiyoruz/a37aab3301813cd59a4502889824af6c to your computer and use it in GitHub Desktop.
caffe ni c++ da ishlatish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <caffe/caffe.hpp> | |
#include <opencv2/opencv.hpp> | |
#include <opencv2/dnn.hpp> | |
int main(int argc, char** argv) { | |
// Load input image. | |
cv::Mat inputMat = cv::imread("accordion.jpg"); | |
inputMat.convertTo(inputMat, CV_32F); // Cast to floats | |
cv::resize(inputMat, inputMat, cv::Size(227, 227)); // Resize. | |
// Convert from interleaved image (BGRBGR) to planar with swapped channels (RRRGGGBBB). | |
inputMat = cv::dnn::blobFromImage(inputMat); | |
// Load Caffe model using Caffe. | |
caffe::Caffe::set_mode(caffe::Caffe::CPU); | |
caffe::Net<float> caffeNet("bvlc_alexnet.prototxt", caffe::TEST); | |
caffeNet.CopyTrainedLayersFrom("bvlc_alexnet.caffemodel"); | |
// Load Caffe model using DNN. | |
cv::dnn::Net dnnNet = cv::dnn::readNetFromCaffe("bvlc_alexnet.prototxt", | |
"bvlc_alexnet.caffemodel"); | |
// Run Caffe model using Caffe. | |
caffe::Blob<float>* caffeInput = caffeNet.input_blobs()[0]; | |
// Wrap Caffe's input blob to cv::Mat. | |
cv::Mat caffeInputMat(caffeInput->shape(), CV_32F, (char*)caffeInput->cpu_data()); | |
// Copy image. | |
inputMat.copyTo(caffeInputMat); | |
caffe::Blob<float>* caffeOut = caffeNet.Forward()[0]; | |
// Run Caffe model using DNN. | |
dnnNet.setInput(inputMat); | |
cv::Mat dnnOut = dnnNet.forward(); | |
// Print results. | |
cv::Mat caffeOutMat(caffeOut->shape(), CV_32F, (char*)caffeOut->cpu_data()); | |
cv::Point classNumber; | |
double classProb; | |
cv::minMaxLoc(caffeOutMat, NULL, &classProb, NULL, &classNumber); | |
std::cout << "caffe: " << classNumber.x << " " << classProb << std::endl; | |
cv::minMaxLoc(dnnOut, NULL, &classProb, NULL, &classNumber); | |
std::cout << "dnn: " << classNumber.x << " " << classProb << std::endl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ref : opencv/opencv#9427