【ncnn】win10 vs2015编译ncnn项目

1. 编译protobuf

这是google开源的一个项目,ncnn用来序列保存网络模型的。

1.1 下载protobuf,从这个博客

1.2 编译protobuf
看网络上的文章,这里有两种方式可以编译protobuf。

  • 采用cmake-gui
  • 采用vs2015命令行

因为编译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 流程

  • 打开VS2015 X64本机工具命令提示符
    在这里插入图片描述
  • 切换到protobuf文件目录下,例: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
在这里插入图片描述
在这里插入图片描述

2. 编译ncnn

  • 从以下地址下载ncnn source code,下载后解压。
  • 解压之后按以下命令进行编译,注意修改<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模式就行

3. 配置和测试

参考自:https://blog.csdn.net/zhaotun123/article/details/99671286

  • 新建一个vs项目
    新建vs2015项目,添加以下包含目录,第一个目录为opencv的include路径,第二个为编译生成的ncnn里的include路径,第三个为编译生成的protobuf里的include路径。
    其他的是为opencv配置的,可参看原博客配置
  • 将ncnn include的路径后面加上\ncnn
    在这里插入图片描述
  • 继续添加库目录,
    第一个目录为opencv的lib路径,第二个为编译生成的ncnn里的lib路径,第三个为编译生成的protobuf里的lib路径。
    在这里插入图片描述
  • 继续添加windows运行库目录,该目录为protobuf的bin路径
    在这里插入图片描述
  • 在链接器-输入中添加附加依赖项,分别是
    在这里插入图片描述
    测试
  • 修改模型和图片的位置
  • 测试图片
    在这里插入图片描述
#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;
}

结果
在这里插入图片描述

TO DO

  • 源码阅读理解
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值