NCNN,protobuf編譯
首先需要根據(jù)官方編譯protobuf和NCNN 關(guān)于如何編譯NCNN和 protobuf 的方式我是參考這個(gè)的。
https://blog.csdn.net/qq_36890370/article/details/104966786
本文主要的內(nèi)容是如何使用cmake去配置NCNN環(huán)境,在編譯這塊不過多敘述。
文件放置
我們需要將NCNN和protobuf編譯生成文件中的install目錄下的bin,cmake,include,lib文件和并起來。如下圖:

install位置
然后我們創(chuàng)建cmake工程。
cmake配置
同時(shí)需要注意配置opencv和ncnn的環(huán)境變量,這樣cmake也可以直接搜索到cmake的第三方配置文件。
# CMakeList.txt: CMakeProject1 的 CMake 項(xiàng)目,在此處包括源代碼并定義
# 項(xiàng)目特定的邏輯。
#
cmake_minimum_required (VERSION 3.8)
project (CMakeProject1)
#OPENCV
set(OpenCV_DIR "D:/openCV/opencv/build/x64/vc15/lib") #如果配置了opencv環(huán)境變量也不用加
find_package(OpenCV REQUIRED)
#添加頭文件
include_directories(${OpenCV_INCLUDE_DIRS})
#NCNN
set(ncnn_DIR "D:/NCNN/ncnnDepen/lib/cmake/ncnn")
find_package(ncnn REQUIRED)
# 將源代碼添加到此項(xiàng)目的可執(zhí)行文件。
add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h")
#鏈接庫(kù)文件
target_link_libraries(CMakeProject1 ${OpenCV_LIBS})
target_link_libraries(CMakeProject1 ncnn)
# TODO: 如有需要,請(qǐng)?zhí)砑訙y(cè)試并安裝目標(biāo)。
模型驗(yàn)證
使用squeezenet驗(yàn)證NCNN環(huán)境是否配置上了,之所以使用squeezenet主要原因是因?yàn)镹CNN官方github里面提供了權(quán)重也就是bin和params,在sample文件夾里,所以懶死我算了。具體代碼如下,也可以直接copy NCNN包中sample下的squeezenet.cpp一樣的。
// CMakeProject1.cpp: 定義應(yīng)用程序的入口點(diǎn)。
//
#include "CMakeProject1.h"
#include "net.h"
#include <algorithm>
#if defined(USE_NCNN_SIMPLEOCV)
#include "simpleocv.h"
#else
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#endif
#include <stdio.h>
#include <vector>
using namespace std;
using namespace cv;
static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
{
//創(chuàng)建模型
ncnn::Net squeezenet;
squeezenet.opt.use_vulkan_compute = true;
//讀取權(quán)重網(wǎng)絡(luò)結(jié)構(gòu)
if (squeezenet.load_param("D:/NCNN/ncnn-master/examples/squeezenet_v1.1.param"))
exit(-1);
if (squeezenet.load_model("D:/NCNN/ncnn-master/examples/squeezenet_v1.1.bin"))
exit(-1);
//將opencv轉(zhuǎn)換為ncnn輸入格式 大小為(227,227)
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
const float mean_vals[3] = { 104.f, 117.f, 123.f };
//對(duì)圖像進(jìn)行均值方差的操作 mean_vals表示為減去的均值 norm_vals表示為方差
in.substract_mean_normalize(mean_vals, 0);
//抽取模型
ncnn::Extractor ex = squeezenet.create_extractor();
//輸入 這個(gè)根據(jù)use netron 查看入口,和出口
ex.input("data", in);
ncnn::Mat out;
ex.extract("prob", out);
cls_scores.resize(out.w);
for (int j = 0; j < out.w; j++)
{
cls_scores[j] = out[j];
}
return 0;
}
static int print_topk(const std::vector<float>& cls_scores, int topk)
{
// partial sort topk with index
int size = cls_scores.size();
std::vector<std::pair<float, int> > vec;
vec.resize(size);
for (int i = 0; i < size; i++)
{
vec[i] = std::make_pair(cls_scores[i], i);
}
std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
std::greater<std::pair<float, int> >());
// print topk and score
for (int i = 0; i < topk; i++)
{
float score = vec[i].first;
int index = vec[i].second;
fprintf(stderr, "%d = %f\n", index, score);
}
return 0;
}
int main()
{
//讀取圖片
const char* imagepath = "C:/Users/zheng/Pictures/lion.png";
Mat m = imread(imagepath, 1);
if (m.empty())
{
fprintf(stderr, "cv::imread %s failed\n", imagepath);
return -1;
}
//設(shè)置置信度
std::vector<float> cls_scores;
detect_squeezenet(m, cls_scores);
print_topk(cls_scores, 3);
return 0;
}
模型執(zhí)行成功就行了。

結(jié)果圖