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.
