赞
踩
这是google开源的一个项目,ncnn用来序列保存网络模型的。
1.1 下载protobuf,从这个博客
1.2 编译protobuf
看网络上的文章,这里有两种方式可以编译protobuf。
因为编译opencv的时候用的cmake-gui,这里用vs命令行试一下。
参考:https://blog.csdn.net/zhaotun123/article/details/99671286
https://blog.csdn.net/CosmosHua/article/details/81543844
https://blog.csdn.net/kai1001/article/details/91043439
1.3 流程
cd /d D:\profile\protobuf-3.4.0
mkdir build-vs2015
cd build-vs2015
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_MSVC_STATIC_RUNTIME=OFF ../cmake
nmake
nmake install
问题1
– Detecting CXX compile features – Detecting CXX compile features - done – Looking for pthread.h – Looking for pthread.h - not found – Found Threads: TRUE CMake Error at tests.cmake:2 (message): Cannot find gmock directory. Call Stack (most recent call first): CMakeLists.txt:172 (include)
打开build文件夹下的CMakeError.log,看一下问题原因:
无法打开包括文件: “pthread.h”
解决方案: 下载编译好的pthread.h放到vs2015的include和lib文件中
运行截图
cmake
<protobuf build root dir>
路径指向protobuf 编译的位置注意:我编译的Debug模型,可以用Release(已修改为release)
mkdir -p build-vs2015-wt
cd build-vs2015-wt
cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=%cd%/install -DProtobuf_INCLUDE_DIR=D:/profile/protobuf-3.4.0/build-vs2015/install/include -DProtobuf_LIBRARIES=D:/profile/protobuf-3.4.0/build-vs2015/install/lib/libprotobuf.lib -DProtobuf_PROTOC_EXECUTABLE=D:/profile/protobuf-3.4.0/build-vs2015/install/bin/protoc.exe ..
nmake
nmake install
运行截图
可能问题:
LINK Pass 1: command "F:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\link.exe /nologo @CMakeFiles\caffe2ncnn.dir\objects1.rsp /out:caffe2ncnn.exe /implib:caffe2ncnn.lib /pdb:D:\profile\ncnn-20200227\ncnn-20200226\build\tools\caffe\caffe2ncnn.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console D:\profile\protobuf-3.4.0\build-vs2015\install\lib\libprotobuf.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\caffe2ncnn.dir/intermediate.manifest CMakeFiles\caffe2ncnn.dir/manifest.res" failed (exit code 1319) with the following output:
libprotobuf.lib(common.cc.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”(caffe2ncnn.cpp.obj 中)
libprotobuf.lib(common.cc.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(caffe2ncnn.cpp.obj 中)
libprotobuf.lib(coded_stream.cc.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”(caffe2ncnn.cpp.obj 中)
libprotobuf.lib(coded_stream.cc.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(caffe2ncnn.cpp.obj 中)
libprotobuf.lib(zero_copy_stream_impl_lite.cc.obj) : error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”(caffe2ncnn.cpp.obj 中)
libprotobuf.lib(zero_copy_stream_impl_lite.cc.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(caffe2ncnn.cpp.obj 中)
解决 这是因为protobuf是release编译的,ncnn是debug模式编译的。ncnn改为release模式就行
参考自:https://blog.csdn.net/zhaotun123/article/details/99671286
#include <opencv2\opencv.hpp>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <net.h>
static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
{
ncnn::Net squeezenet;
squeezenet.load_param("D:/profile/ncnn-20200226/examples/squeezenet_v1.1.param");
squeezenet.load_model("D:/profile/ncnn-20200226/examples/squeezenet_v1.1.bin");
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 };
in.substract_mean_normalize(mean_vals, 0);
ncnn::Extractor ex = squeezenet.create_extractor();
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()
{
std::string imagepath = "./demo_image/test.jpg";
cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR);
if (m.empty())
{
std::cout << "cv::imread " << imagepath << " failed\n" << std::endl;
return -1;
}
std::vector<float> cls_scores;
detect_squeezenet(m, cls_scores);
print_topk(cls_scores, 3);
getchar();
return 0;
}
结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。