2025年3月17日 星期一 甲辰(龙)年 月十六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 人工智能

Darknet windows移植(YOLO v2)

时间:02-15来源:作者:点击数:14
CDSY,CDSY.XYZ

Darknet windows移植

代码地址: https://github.com/makefile/darknet

编译要求: VS2013 update5 及其之后的版本(低版本对C++标准支持较差)

配置opencv来显示图片结果,如果不配置OpenCV,则支持的图片类型较少,结果将直接保存到文件.

pthread库

下载windows版pthread库,将头文件与编译好的lib与dll文件挑出来供Darknet使用.在VS配置中添加pthreadVC2.lib.

时间函数

linux下的时间函数库#include <sys/time.h>需要被替换.可以用clock()来替换what_time_is_it_now(),或者如下代码来定义what_time_is_it_now:

  • #include "gettimeofday.h"
  • #define CLOCK_REALTIME 0
  • #pragma warning(disable: 4996) //disable deprecated warning as error
  • #define BILLION (1E9)
  • static BOOL g_first_time = 1;
  • static LARGE_INTEGER g_counts_per_sec;
  • int clock_gettime(int dummy, struct timespec *ct)
  • {
  • LARGE_INTEGER count;
  • if (g_first_time)
  • {
  • g_first_time = 0;
  • if (0 == QueryPerformanceFrequency(&g_counts_per_sec))
  • {
  • g_counts_per_sec.QuadPart = 0;
  • }
  • }
  • if ((NULL == ct) || (g_counts_per_sec.QuadPart <= 0) ||
  • (0 == QueryPerformanceCounter(&count)))
  • {
  • return -1;
  • }
  • ct->tv_sec = count.QuadPart / g_counts_per_sec.QuadPart;
  • ct->tv_nsec = ((count.QuadPart % g_counts_per_sec.QuadPart) * BILLION) / g_counts_per_sec.QuadPart;
  • return 0;
  • }
  • double what_time_is_it_now()
  • {
  • struct timespec now;
  • clock_gettime(CLOCK_REALTIME, &now);
  • return now.tv_sec + now.tv_nsec*1e-9;
  • }

gettimeofday.h与gettimeofday.c实现在 这里 .

void *指针加法

访问地址时对void *arr进行加法:arr+偏移数字时VS编译不通过,强制类型转换为char*可通过且没有错误.

动态数组

static const int nind = 2;之后创建了大小了nind 的数组,VS的低版本可能不支持arr[变量]这样的数组定义.

替换成宏定义define nind 2,也可简单将数组定义为arr[2].

其它函数定义,修饰符,头文件

  • #if defined(_MSC_VER) && _MSC_VER < 1900
  • #define inline __inline
  • #define snprintf(buf,len, format,...) _snprintf_s(buf, len,len, format, __VA_ARGS__)
  • #endif
  • #ifdef _WIN32
  • #define popen _popen
  • #define pclose _pclose
  • //#include <windows.h>
  • #define sleep(secs) Sleep(secs * 1000) //windows.h millsecs
  • #endif

getopt.h与unistd.h均为非标准C的linux API,windows版移植源码在 这里 .

OpenMP

Darknet的OpenMP加速在多个for循环中,代码在这,在windows上运行时有时出现访问冲突.按照(这里的代码)修改即可,使用一个for循环多次调用原各个gemm函数,在for前边加#pragma omp parallel for语句.

VS的其它小毛病

可能出现名字冲突等,需要改一些变量的名字.

导出动态链接库

将框架的初始化和模型的加载,网络预测等操作封装到了一个类中,使用__declspec(dllexport)修饰符来将函数导出到dll中.头文件与lib,dll文件可供其它Windows项目使用.

代码在这里:yolo_v2_class.hpp,yolo_v2_class.cpp,yolo_console_dll.cpp.

使用测试

运行命令:darknet.exe detector test data/coco.data yolo.cfg yolo.weights -i 0 -thresh 0.2

大小1176x510的图片,测试耗时4min,开启OpenMP加速后60s (i7,8线程).

其它加速方法:使用其它的库,如MKL等.参考https://groups.google.com/forum/#!topic/darknet/AZ-jp45bDpI.

CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐