【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
曾经有一段时间,我自己很喜欢阅读代码,但是对编译代码不是很有兴趣。这其实是本末倒置的做法。真正去学一门语言或者一个开源软件,必须去实际操作、实际编译、实际调试才会有收获。看了几十行、几百行的代码,但是不去编译、不用gdb调试一下,得到的知识总是有限的。所以,这里也建议喜欢阅读代码、但是不喜欢debug的同学可以稍微调整一下自己的习惯。
相比较其他的深度学习框架,darknet比较小众。但是darknet结构比较清晰,并且不依赖于第三方框架,可以在多个os上面编译成功。下面,就开始进行编译操作。
1、下载代码
git clone https://github.com/pjreddie/darknet.git
2、编译
cd darknet
make
3、目前支持的平台
我自己在ubuntu和mac上面均编译成功
只是如果需要在ubuntu上面编译成功,需要对Makefile稍作修改
-LDFLAGS= -lm -pthread
+LDFLAGS= -lm -lpthread
4、不同的cpu进行编译
4.1 用arm编译器进行编译
a,确保当前ubuntu中含有arm的gcc、g++编译器,如果没有也没有关系,
shell> sudo apt-get install gcc-arm-linux-gnueabi
shell> sudo apt-get install g++-arm-linux-gnueabi
b,修改makefile
CC=arm-linux-gnueabi-gcc
CPP=arm-linux-gnueabi-g++
c,剩下来的事情就是直接make就可以了
4.2 用aarch64编译器进行编译
a,确保当前ubuntu中含有aarch64的gcc、g++编译器,如果没有也没有关系
shell> sudo apt-get install gcc-aarch64-linux-gnu
shell> sudo apt-get install g++-aarch64-linux-gnu
b,修改makefile
CC=aarch64-linux-gnu-gcc
CPP=aarch64-linux-gnu-g++
c,后面一样,直接make就可以了
5,其他编译事项
GPU=0
CUDNN=0
OPENCV=0
OPENMP=0
DEBUG=0
除了正常的编译之外,darknet还支持5个基本选项。第一个、第二个都和GPU相关,如果自己机器有nvidia独立显卡,可以看看是否可以用的起来。第三个和opencv相关,如果打开,需要本地有opencv的库。比如,sudo apt-get install libopencv-dev。第四个是一个加速选项,之前在x86用过,不清楚arm是否有效果,但是编译没有出错。第五个就是普通的选项,可以在编译的时候添加一些调试信息。
6、编译的效果
如果编译成功,一般会在根目录生成三个文件,分别是libdarknet.a,libdarknet.so和darknet可执行文件。
7、代码入口点
shell> grep "main" * -R -w
examples/darknet.c:int main(int argc, char **argv)
8、使用darknet
shell>$ ./darknet
usage: ./darknet <function>
9、下载模型并且执行
shell>$ wget https://pjreddie.com/media/files/tiny.weights
shell>$ ./darknet classify cfg/tiny.cfg tiny.weights data/dog.jpg
layer filters size input output
0 conv 16 3 x 3 / 1 224 x 224 x 3 -> 224 x 224 x 16 0.043 BFLOPs
1 max 2 x 2 / 2 224 x 224 x 16 -> 112 x 112 x 16
2 conv 32 3 x 3 / 1 112 x 112 x 16 -> 112 x 112 x 32 0.116 BFLOPs
3 max 2 x 2 / 2 112 x 112 x 32 -> 56 x 56 x 32
4 conv 16 1 x 1 / 1 56 x 56 x 32 -> 56 x 56 x 16 0.003 BFLOPs
5 conv 128 3 x 3 / 1 56 x 56 x 16 -> 56 x 56 x 128 0.116 BFLOPs
6 conv 16 1 x 1 / 1 56 x 56 x 128 -> 56 x 56 x 16 0.013 BFLOPs
7 conv 128 3 x 3 / 1 56 x 56 x 16 -> 56 x 56 x 128 0.116 BFLOPs
8 max 2 x 2 / 2 56 x 56 x 128 -> 28 x 28 x 128
9 conv 32 1 x 1 / 1 28 x 28 x 128 -> 28 x 28 x 32 0.006 BFLOPs
10 conv 256 3 x 3 / 1 28 x 28 x 32 -> 28 x 28 x 256 0.116 BFLOPs
11 conv 32 1 x 1 / 1 28 x 28 x 256 -> 28 x 28 x 32 0.013 BFLOPs
12 conv 256 3 x 3 / 1 28 x 28 x 32 -> 28 x 28 x 256 0.116 BFLOPs
13 max 2 x 2 / 2 28 x 28 x 256 -> 14 x 14 x 256
14 conv 64 1 x 1 / 1 14 x 14 x 256 -> 14 x 14 x 64 0.006 BFLOPs
15 conv 512 3 x 3 / 1 14 x 14 x 64 -> 14 x 14 x 512 0.116 BFLOPs
16 conv 64 1 x 1 / 1 14 x 14 x 512 -> 14 x 14 x 64 0.013 BFLOPs
17 conv 512 3 x 3 / 1 14 x 14 x 64 -> 14 x 14 x 512 0.116 BFLOPs
18 conv 128 1 x 1 / 1 14 x 14 x 512 -> 14 x 14 x 128 0.026 BFLOPs
19 conv 1000 1 x 1 / 1 14 x 14 x 128 -> 14 x 14 x1000 0.050 BFLOPs
20 avg 14 x 14 x1000 -> 1000
21 softmax 1000
Loading weights from tiny.weights...Done!
data/dog.jpg: Predicted in 0.181403 seconds.
14.51%: malamute
6.09%: Newfoundland
5.59%: dogsled
4.55%: standard schnauzer
4.05%: Eskimo dog