一. 整体代码逻辑

yolo中源码分为三个部分,\example,\include,以及\src文件夹下都有源代码存在.

结构如下所示

├── examples
│ ├── darknet.c(主程序)
│ │── xxx1.c
│ └── xxx2.c

├── include
│ ├── darknet.h


├── Makefile


└── src
├── yyy1.c
├── yyy2.h
└──......

\include文件夹中没有.h头文件, 里边的内容算作一个整体, 都是darknet.c中的一部分, 每个文件的内容共darknet.c调用, 除了darknet.c外, \include文件夹中的文件不存在互相调用, 各自完成不同的功能,如检测视频, 检测图片, 检测艺术品等, 通过darknet.c中的if条件进行选择调用. 因为这部分算作一个整体, 所以共用darknet.h这个头文件. 如果\include需要用到\src中的函数, 则在darknet.h中进行声明

在\src文件夹中, 每个c文件都对应一个同名的.h头文件; main函数存在于\example文件夹下的darknet.c文件中.

\include文件夹下的darknet.h的作用是联系\example与\src两部分, 在这两部分中都需要用的函数则在darknet.h中进行声明, 例如\example中有xxx1.c, \src中有yyy1.c及yyy1.h, xxx1.c与yyy1.c中都需要用到func()这个函数, 那么func()的声明需要放在darknet.h中, 然后在xxx1.c与yyy1.h分别引入头文件#include "darknet.h"

而如果\example\darknet.c中需要调用\example\xxx1.c中的函数, 则需要在\example\darknet.c加extern字段

多文件的实现方式(头文件的使用)

在本项目中, \includes\darknet.h是\examples中文件的头文件, 而在\includes\darknet.h中, 又对部分函数(例如 void forward_network(network *net); )进行了声明, 但是 forward_network 函数的定义是在\src\network.c中, 因为定义是在\src中, 所以定义时\src中的文件需要引入darknet.h这个头文件; 由此, \examples中的文件便可通过darknet.h中的声明调用\src中的函数了

举例

对于 ./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg 这条命令,首先传送到darknet.c文件, 然后darknet.c文件检测到含有detect字符, 所以进入if语句. 使用\src\utils.c中的find_char_arg函数来获取输出文件名等信息, 然后调用detector.c文件中的test_detector函数, 该函数负责检测并进行输出.

二. main函数

唉唉唉

三. makefile文件

入门见<并行程序设计(第四版)>

以yolo源码中的makefile文件为例

GPU=
CUDNN=
OPENCV=
OPENMP=
DEBUG= ARCH= -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=[sm_50,compute_50] \
-gencode arch=compute_52,code=[sm_52,compute_52]
# -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated? # This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52 VPATH=./src/:./examples
# VTATH用来告诉make,源文件的路径, 参考https://blog.csdn.net/mcgrady_tracy/article/details/27240139
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/ CC=gcc
NVCC=nvcc
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread #gcc等编译器会用到的一些优化参数,也可以在里面指定库文件的位置
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC #指定头文件(.h文件)的路径,如:CFLAGS=-I/usr/include -I/path/include。 ifeq ($(OPENMP), )
CFLAGS+= -fopenmp
endif ifeq ($(DEBUG), )
OPTS=-O0 -g
endif CFLAGS+=$(OPTS) ifeq ($(OPENCV), )
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv`
COMMON+= `pkg-config --cflags opencv`
endif ifeq ($(GPU), )
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif ifeq ($(CUDNN), )
COMMON+= -DCUDNN
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o
EXECOBJA=my_test.o captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), )
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) #加前缀函数: $(addprefix <prefix>,<names...>),OBJDIR是obj存放的地址
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h #all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj results $(SLIB) $(ALIB) $(EXEC) $(EXEC): $(EXECOBJ) $(ALIB)
$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB) $(ALIB): $(OBJS)
$(AR) $(ARFLAGS) $@ $^ $(SLIB): $(OBJS)
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) $(OBJDIR)%.o: %.c $(DEPS)
$(CC) $(COMMON) $(CFLAGS) -c $< -o $@ $(OBJDIR)%.o: %.cu $(DEPS)
$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ obj:
mkdir -p obj
backup:
mkdir -p backup
results:
mkdir -p results .PHONY: clean clean:
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)/*

关于vpath,参考https://blog.csdn.net/mcgrady_tracy/article/details/27240139

(1)修改代码的第一次尝试

在\examples文件夹下新建my_test.c文件, 内容如下

#include "darknet.h"

void output_to_file()
{
FILE *fp;
fp=fopen("output.txt","w");
fprintf(fp,"adfsss");
printf("test\n");
fclose(fp);
}

在darknet.c中进行调用, 如下

#include "darknet.h"

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
// extern void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top); // 在\examples\classifier.c中
extern void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen); // 在\examples\detector.c中
extern void run_yolo(int argc, char **argv); // 在\examples\yolo.c中
extern void run_detector(int argc, char **argv); // 在\examples\detector.c中
extern void run_coco(int argc, char **argv); // 在\examples\coco.c中
extern void run_captcha(int argc, char **argv); // 在\examples\captcha.c中
extern void run_nightmare(int argc, char **argv); // 在\examples\nightmare.c中
extern void run_classifier(int argc, char **argv); // 在\examples\classifier.c中
extern void run_regressor(int argc, char **argv); // 在\examples\regressor.c中
extern void run_segmenter(int argc, char **argv); // 在\examples\segmenter.c中
extern void run_char_rnn(int argc, char **argv); // 在\examples\rnn.c中
extern void run_tag(int argc, char **argv); // 在\examples\tag.c中
extern void run_cifar(int argc, char **argv); // 在\examples\fun_cifar.c中
extern void run_go(int argc, char **argv); // 在\examples\go.c中
extern void run_art(int argc, char **argv); // 在\examples\art.c中
extern void run_super(int argc, char **argv); // 在\examples\super.c中
extern void run_lsd(int argc, char **argv); // 在\examples\nightmare.c中
extern void output_to_file(); // 在\examples\my_test.c中 void average(int argc, char *argv[])
{
char *cfgfile = argv[];
char *outfile = argv[];
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
network *sum = parse_network_cfg(cfgfile); char *weightfile = argv[];
load_weights(sum, weightfile); int i, j;
int n = argc - ;
for(i = ; i < n; ++i){
weightfile = argv[i+];
load_weights(net, weightfile);
for(j = ; j < net->n; ++j){
layer l = net->layers[j];
layer out = sum->layers[j];
if(l.type == CONVOLUTIONAL){
int num = l.n*l.c*l.size*l.size;
axpy_cpu(l.n, , l.biases, , out.biases, );
axpy_cpu(num, , l.weights, , out.weights, );
if(l.batch_normalize){
axpy_cpu(l.n, , l.scales, , out.scales, );
axpy_cpu(l.n, , l.rolling_mean, , out.rolling_mean, );
axpy_cpu(l.n, , l.rolling_variance, , out.rolling_variance, );
}
}
if(l.type == CONNECTED){
axpy_cpu(l.outputs, , l.biases, , out.biases, );
axpy_cpu(l.outputs*l.inputs, , l.weights, , out.weights, );
}
}
}
n = n+;
for(j = ; j < net->n; ++j){
layer l = sum->layers[j];
if(l.type == CONVOLUTIONAL){
int num = l.n*l.c*l.size*l.size;
scal_cpu(l.n, ./n, l.biases, );
scal_cpu(num, ./n, l.weights, );
if(l.batch_normalize){
scal_cpu(l.n, ./n, l.scales, );
scal_cpu(l.n, ./n, l.rolling_mean, );
scal_cpu(l.n, ./n, l.rolling_variance, );
}
}
if(l.type == CONNECTED){
scal_cpu(l.outputs, ./n, l.biases, );
scal_cpu(l.outputs*l.inputs, ./n, l.weights, );
}
}
save_weights(sum, outfile);
} long numops(network *net)
{
int i;
long ops = ;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL){
ops += 2l * l.n * l.size*l.size*l.c/l.groups * l.out_h*l.out_w;
} else if(l.type == CONNECTED){
ops += 2l * l.inputs * l.outputs;
} else if (l.type == RNN){
ops += 2l * l.input_layer->inputs * l.input_layer->outputs;
ops += 2l * l.self_layer->inputs * l.self_layer->outputs;
ops += 2l * l.output_layer->inputs * l.output_layer->outputs;
} else if (l.type == GRU){
ops += 2l * l.uz->inputs * l.uz->outputs;
ops += 2l * l.uh->inputs * l.uh->outputs;
ops += 2l * l.ur->inputs * l.ur->outputs;
ops += 2l * l.wz->inputs * l.wz->outputs;
ops += 2l * l.wh->inputs * l.wh->outputs;
ops += 2l * l.wr->inputs * l.wr->outputs;
} else if (l.type == LSTM){
ops += 2l * l.uf->inputs * l.uf->outputs;
ops += 2l * l.ui->inputs * l.ui->outputs;
ops += 2l * l.ug->inputs * l.ug->outputs;
ops += 2l * l.uo->inputs * l.uo->outputs;
ops += 2l * l.wf->inputs * l.wf->outputs;
ops += 2l * l.wi->inputs * l.wi->outputs;
ops += 2l * l.wg->inputs * l.wg->outputs;
ops += 2l * l.wo->inputs * l.wo->outputs;
}
}
return ops;
} void speed(char *cfgfile, int tics)
{
if (tics == ) tics = ;
network *net = parse_network_cfg(cfgfile);
set_batch_network(net, );
int i;
double time=what_time_is_it_now();
image im = make_image(net->w, net->h, net->c*net->batch);
for(i = ; i < tics; ++i){
network_predict(net, im.data);
}
double t = what_time_is_it_now() - time;
long ops = numops(net);
printf("\n%d evals, %f Seconds\n", tics, t);
printf("Floating Point Operations: %.2f Bn\n", (float)ops/.);
printf("FLOPS: %.2f Bn\n", (float)ops/.*tics/t);
printf("Speed: %f sec/eval\n", t/tics);
printf("Speed: %f Hz\n", tics/t);
} void operations(char *cfgfile)
{
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
long ops = numops(net);
printf("Floating Point Operations: %ld\n", ops);
printf("Floating Point Operations: %.2f Bn\n", (float)ops/.);
} void oneoff(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
int oldn = net->layers[net->n - ].n;
int c = net->layers[net->n - ].c;
scal_cpu(oldn*c, ., net->layers[net->n - ].weights, );
scal_cpu(oldn, , net->layers[net->n - ].biases, );
net->layers[net->n - ].n = ;
net->layers[net->n - ].biases += ;
net->layers[net->n - ].weights += *c;
if(weightfile){
load_weights(net, weightfile);
}
net->layers[net->n - ].biases -= ;
net->layers[net->n - ].weights -= *c;
net->layers[net->n - ].n = oldn;
printf("%d\n", oldn);
layer l = net->layers[net->n - ];
copy_cpu(l.n/, l.biases, , l.biases + l.n/, );
copy_cpu(l.n/, l.biases, , l.biases + *l.n/, );
copy_cpu(l.n/*l.c, l.weights, , l.weights + l.n/*l.c, );
copy_cpu(l.n/*l.c, l.weights, , l.weights + *l.n/*l.c, );
*net->seen = ;
save_weights(net, outfile);
} void oneoff2(char *cfgfile, char *weightfile, char *outfile, int l)
{
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights_upto(net, weightfile, , net->n);
load_weights_upto(net, weightfile, l, net->n);
}
*net->seen = ;
save_weights_upto(net, outfile, net->n);
} void partial(char *cfgfile, char *weightfile, char *outfile, int max)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
save_weights_upto(net, outfile, max);
} void print_weights(char *cfgfile, char *weightfile, int n)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
layer l = net->layers[n];
int i, j;
//printf("[");
for(i = ; i < l.n; ++i){
//printf("[");
for(j = ; j < l.size*l.size*l.c; ++j){
//if(j > 0) printf(",");
printf("%g ", l.weights[i*l.size*l.size*l.c + j]);
}
printf("\n");
//printf("]%s\n", (i == l.n-1)?"":",");
}
//printf("]");
} void rescale_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL){
rescale_weights(l, , -.);
break;
}
}
save_weights(net, outfile);
} void rgbgr_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL){
rgbgr_weights(l);
break;
}
}
save_weights(net, outfile);
} void reset_normalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for (i = ; i < net->n; ++i) {
layer l = net->layers[i];
if (l.type == CONVOLUTIONAL && l.batch_normalize) {
denormalize_convolutional_layer(l);
}
if (l.type == CONNECTED && l.batch_normalize) {
denormalize_connected_layer(l);
}
if (l.type == GRU && l.batch_normalize) {
denormalize_connected_layer(*l.input_z_layer);
denormalize_connected_layer(*l.input_r_layer);
denormalize_connected_layer(*l.input_h_layer);
denormalize_connected_layer(*l.state_z_layer);
denormalize_connected_layer(*l.state_r_layer);
denormalize_connected_layer(*l.state_h_layer);
}
}
save_weights(net, outfile);
} layer normalize_layer(layer l, int n)
{
int j;
l.batch_normalize=;
l.scales = calloc(n, sizeof(float));
for(j = ; j < n; ++j){
l.scales[j] = ;
}
l.rolling_mean = calloc(n, sizeof(float));
l.rolling_variance = calloc(n, sizeof(float));
return l;
} void normalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL && !l.batch_normalize){
net->layers[i] = normalize_layer(l, l.n);
}
if (l.type == CONNECTED && !l.batch_normalize) {
net->layers[i] = normalize_layer(l, l.outputs);
}
if (l.type == GRU && l.batch_normalize) {
*l.input_z_layer = normalize_layer(*l.input_z_layer, l.input_z_layer->outputs);
*l.input_r_layer = normalize_layer(*l.input_r_layer, l.input_r_layer->outputs);
*l.input_h_layer = normalize_layer(*l.input_h_layer, l.input_h_layer->outputs);
*l.state_z_layer = normalize_layer(*l.state_z_layer, l.state_z_layer->outputs);
*l.state_r_layer = normalize_layer(*l.state_r_layer, l.state_r_layer->outputs);
*l.state_h_layer = normalize_layer(*l.state_h_layer, l.state_h_layer->outputs);
net->layers[i].batch_normalize=;
}
}
save_weights(net, outfile);
} void statistics_net(char *cfgfile, char *weightfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for (i = ; i < net->n; ++i) {
layer l = net->layers[i];
if (l.type == CONNECTED && l.batch_normalize) {
printf("Connected Layer %d\n", i);
statistics_connected_layer(l);
}
if (l.type == GRU && l.batch_normalize) {
printf("GRU Layer %d\n", i);
printf("Input Z\n");
statistics_connected_layer(*l.input_z_layer);
printf("Input R\n");
statistics_connected_layer(*l.input_r_layer);
printf("Input H\n");
statistics_connected_layer(*l.input_h_layer);
printf("State Z\n");
statistics_connected_layer(*l.state_z_layer);
printf("State R\n");
statistics_connected_layer(*l.state_r_layer);
printf("State H\n");
statistics_connected_layer(*l.state_h_layer);
}
printf("\n");
}
} void denormalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for (i = ; i < net->n; ++i) {
layer l = net->layers[i];
if ((l.type == DECONVOLUTIONAL || l.type == CONVOLUTIONAL) && l.batch_normalize) {
denormalize_convolutional_layer(l);
net->layers[i].batch_normalize=;
}
if (l.type == CONNECTED && l.batch_normalize) {
denormalize_connected_layer(l);
net->layers[i].batch_normalize=;
}
if (l.type == GRU && l.batch_normalize) {
denormalize_connected_layer(*l.input_z_layer);
denormalize_connected_layer(*l.input_r_layer);
denormalize_connected_layer(*l.input_h_layer);
denormalize_connected_layer(*l.state_z_layer);
denormalize_connected_layer(*l.state_r_layer);
denormalize_connected_layer(*l.state_h_layer);
l.input_z_layer->batch_normalize = ;
l.input_r_layer->batch_normalize = ;
l.input_h_layer->batch_normalize = ;
l.state_z_layer->batch_normalize = ;
l.state_r_layer->batch_normalize = ;
l.state_h_layer->batch_normalize = ;
net->layers[i].batch_normalize=;
}
}
save_weights(net, outfile);
} void mkimg(char *cfgfile, char *weightfile, int h, int w, int num, char *prefix)
{
network *net = load_network(cfgfile, weightfile, );
image *ims = get_weights(net->layers[]);
int n = net->layers[].n;
int z;
for(z = ; z < num; ++z){
image im = make_image(h, w, );
fill_image(im, .);
int i;
for(i = ; i < ; ++i){
image r = copy_image(ims[rand()%n]);
rotate_image_cw(r, rand()%);
random_distort_image(r, , 1.5, 1.5);
int dx = rand()%(w-r.w);
int dy = rand()%(h-r.h);
ghost_image(r, im, dx, dy);
free_image(r);
}
char buff[];
sprintf(buff, "%s/gen_%d", prefix, z);
save_image(im, buff);
free_image(im);
}
} void visualize(char *cfgfile, char *weightfile)
{
network *net = load_network(cfgfile, weightfile, );
visualize_network(net);
#ifdef OPENCV
cvWaitKey();
#endif
} int main(int argc, char **argv)
{
// argv[0] 指向程序运行的全路径名;argv[1] 指向在DOS命令行中执行程序名后的第一个字符串;argv[2]第二个
//test_resize("data/bad.jpg");
//test_box();
//test_convolutional_layer();
if(argc < ){
fprintf(stderr, "usage: %s <function>\n", argv[]);
return ;
}
gpu_index = find_int_arg(argc, argv, "-i", );
if(find_arg(argc, argv, "-nogpu")) {
gpu_index = -;
} #ifndef GPU
gpu_index = -;
#else
if(gpu_index >= ){
cuda_set_device(gpu_index);
}
#endif if ( == strcmp(argv[], "average")){
average(argc, argv);
} else if ( == strcmp(argv[], "yolo")){
run_yolo(argc, argv);
} else if ( == strcmp(argv[], "super")){
run_super(argc, argv);
} else if ( == strcmp(argv[], "lsd")){
run_lsd(argc, argv);
} else if ( == strcmp(argv[], "detector")){
run_detector(argc, argv);
} else if ( == strcmp(argv[], "detect")){
float thresh = find_float_arg(argc, argv, "-thresh", .); //thresh用来控制检测的阈值
char *filename = (argc > ) ? argv[]: ;
char *outfile = find_char_arg(argc, argv, "-out", ); // 定义在\src\utils.c中
int fullscreen = find_arg(argc, argv, "-fullscreen");
test_detector("cfg/coco.data", argv[], argv[], filename, thresh, ., outfile, fullscreen); // 函数定义位于detector.c中
// 命令举例./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg //*修改//
output_to_file();
//*// } else if ( == strcmp(argv[], "cifar")){
run_cifar(argc, argv);
} else if ( == strcmp(argv[], "go")){
run_go(argc, argv);
} else if ( == strcmp(argv[], "rnn")){
run_char_rnn(argc, argv);
} else if ( == strcmp(argv[], "coco")){
run_coco(argc, argv);
} else if ( == strcmp(argv[], "classify")){
predict_classifier("cfg/imagenet1k.data", argv[], argv[], argv[], );
} else if ( == strcmp(argv[], "classifier")){
run_classifier(argc, argv);
} else if ( == strcmp(argv[], "regressor")){
run_regressor(argc, argv);
} else if ( == strcmp(argv[], "segmenter")){
run_segmenter(argc, argv);
} else if ( == strcmp(argv[], "art")){
run_art(argc, argv);
} else if ( == strcmp(argv[], "tag")){
run_tag(argc, argv);
} else if ( == strcmp(argv[], "3d")){
composite_3d(argv[], argv[], argv[], (argc > ) ? atof(argv[]) : );
} else if ( == strcmp(argv[], "test")){
test_resize(argv[]);
} else if ( == strcmp(argv[], "captcha")){
run_captcha(argc, argv);
} else if ( == strcmp(argv[], "nightmare")){
run_nightmare(argc, argv);
} else if ( == strcmp(argv[], "rgbgr")){
rgbgr_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "reset")){
reset_normalize_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "denormalize")){
denormalize_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "statistics")){
statistics_net(argv[], argv[]);
} else if ( == strcmp(argv[], "normalize")){
normalize_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "rescale")){
rescale_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "ops")){
operations(argv[]);
} else if ( == strcmp(argv[], "speed")){
speed(argv[], (argc > && argv[]) ? atoi(argv[]) : );
} else if ( == strcmp(argv[], "oneoff")){
oneoff(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "oneoff2")){
oneoff2(argv[], argv[], argv[], atoi(argv[]));
} else if ( == strcmp(argv[], "print")){
print_weights(argv[], argv[], atoi(argv[]));
} else if ( == strcmp(argv[], "partial")){
partial(argv[], argv[], argv[], atoi(argv[]));
} else if ( == strcmp(argv[], "average")){
average(argc, argv);
} else if ( == strcmp(argv[], "visualize")){
visualize(argv[], (argc > ) ? argv[] : );
} else if ( == strcmp(argv[], "mkimg")){
mkimg(argv[], argv[], atoi(argv[]), atoi(argv[]), atoi(argv[]), argv[]);
} else if ( == strcmp(argv[], "imtest")){
test_resize(argv[]);
} else {
fprintf(stderr, "Not an option: %s\n", argv[]);
}
return ;
}

然后修改Makefile文件, 在EXECOBJA=后追加my_test.o字段. 注意不可将该字段放在EXECOBJA=的最后, 否则编译不通过. 内容如下

GPU=
CUDNN=
OPENCV=
OPENMP=
DEBUG= ARCH= -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=[sm_50,compute_50] \
-gencode arch=compute_52,code=[sm_52,compute_52]
# -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated? # This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52 VPATH=./src/:./examples
# VTATH用来告诉make,源文件的路径, 参考https://blog.csdn.net/mcgrady_tracy/article/details/27240139
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/ CC=gcc
NVCC=nvcc
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread #gcc等编译器会用到的一些优化参数,也可以在里面指定库文件的位置
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC #指定头文件(.h文件)的路径,如:CFLAGS=-I/usr/include -I/path/include。 ifeq ($(OPENMP), )
CFLAGS+= -fopenmp
endif ifeq ($(DEBUG), )
OPTS=-O0 -g
endif CFLAGS+=$(OPTS) ifeq ($(OPENCV), )
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv`
COMMON+= `pkg-config --cflags opencv`
endif ifeq ($(GPU), )
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif ifeq ($(CUDNN), )
COMMON+= -DCUDNN
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o
EXECOBJA=my_test.o captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), )
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) #加前缀函数: $(addprefix <prefix>,<names...>),OBJDIR是obj存放的地址
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h #all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj results $(SLIB) $(ALIB) $(EXEC) $(EXEC): $(EXECOBJ) $(ALIB)
$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB) $(ALIB): $(OBJS)
$(AR) $(ARFLAGS) $@ $^ $(SLIB): $(OBJS)
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) $(OBJDIR)%.o: %.c $(DEPS)
$(CC) $(COMMON) $(CFLAGS) -c $< -o $@ $(OBJDIR)%.o: %.cu $(DEPS)
$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ obj:
mkdir -p obj
backup:
mkdir -p backup
results:
mkdir -p results .PHONY: clean clean:
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)/*

编译并可成功运行.

 (2)修改代码的第二次尝试

在\src目录下新建my_testinsrc.c以及my_testinsrc.h, 内容如下

// my_testinsrc.h
#include "darknet.h" // my_testinsrc.c
#include <stdio.h>
void my_testinsrc(){
printf("test in src\n");
}

修改Makefile, 在最后声明新加的函数

修改后内容如下

GPU=
CUDNN=
OPENCV=
OPENMP=
DEBUG= ARCH= -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=[sm_50,compute_50] \
-gencode arch=compute_52,code=[sm_52,compute_52]
# -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated? # This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52 VPATH=./src/:./examples
# VTATH用来告诉make,源文件的路径, 参考https://blog.csdn.net/mcgrady_tracy/article/details/27240139
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/ CC=gcc
NVCC=nvcc
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread #gcc等编译器会用到的一些优化参数,也可以在里面指定库文件的位置
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC #指定头文件(.h文件)的路径,如:CFLAGS=-I/usr/include -I/path/include。 ifeq ($(OPENMP), )
CFLAGS+= -fopenmp
endif ifeq ($(DEBUG), )
OPTS=-O0 -g
endif CFLAGS+=$(OPTS) ifeq ($(OPENCV), )
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv`
COMMON+= `pkg-config --cflags opencv`
endif ifeq ($(GPU), )
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif ifeq ($(CUDNN), )
COMMON+= -DCUDNN
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif OBJ=my_testinsrc.o gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o
EXECOBJA=my_test.o captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), )
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) #加前缀函数: $(addprefix <prefix>,<names...>),OBJDIR是obj存放的地址
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h #all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj results $(SLIB) $(ALIB) $(EXEC) $(EXEC): $(EXECOBJ) $(ALIB)
$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB) $(ALIB): $(OBJS)
$(AR) $(ARFLAGS) $@ $^ $(SLIB): $(OBJS)
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) $(OBJDIR)%.o: %.c $(DEPS)
$(CC) $(COMMON) $(CFLAGS) -c $< -o $@ $(OBJDIR)%.o: %.cu $(DEPS)
$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ obj:
mkdir -p obj
backup:
mkdir -p backup
results:
mkdir -p results .PHONY: clean clean:
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)/*

在darknet.c中进行调用, 内容如下

#include "darknet.h"

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
// extern void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top); // 在\examples\classifier.c中
extern void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen); // 在\examples\detector.c中
extern void run_yolo(int argc, char **argv); // 在\examples\yolo.c中
extern void run_detector(int argc, char **argv); // 在\examples\detector.c中
extern void run_coco(int argc, char **argv); // 在\examples\coco.c中
extern void run_captcha(int argc, char **argv); // 在\examples\captcha.c中
extern void run_nightmare(int argc, char **argv); // 在\examples\nightmare.c中
extern void run_classifier(int argc, char **argv); // 在\examples\classifier.c中
extern void run_regressor(int argc, char **argv); // 在\examples\regressor.c中
extern void run_segmenter(int argc, char **argv); // 在\examples\segmenter.c中
extern void run_char_rnn(int argc, char **argv); // 在\examples\rnn.c中
extern void run_tag(int argc, char **argv); // 在\examples\tag.c中
extern void run_cifar(int argc, char **argv); // 在\examples\fun_cifar.c中
extern void run_go(int argc, char **argv); // 在\examples\go.c中
extern void run_art(int argc, char **argv); // 在\examples\art.c中
extern void run_super(int argc, char **argv); // 在\examples\super.c中
extern void run_lsd(int argc, char **argv); // 在\examples\nightmare.c中
extern void output_to_file(); // 在\examples\my_test.c中 void average(int argc, char *argv[])
{
char *cfgfile = argv[];
char *outfile = argv[];
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
network *sum = parse_network_cfg(cfgfile); char *weightfile = argv[];
load_weights(sum, weightfile); int i, j;
int n = argc - ;
for(i = ; i < n; ++i){
weightfile = argv[i+];
load_weights(net, weightfile);
for(j = ; j < net->n; ++j){
layer l = net->layers[j];
layer out = sum->layers[j];
if(l.type == CONVOLUTIONAL){
int num = l.n*l.c*l.size*l.size;
axpy_cpu(l.n, , l.biases, , out.biases, );
axpy_cpu(num, , l.weights, , out.weights, );
if(l.batch_normalize){
axpy_cpu(l.n, , l.scales, , out.scales, );
axpy_cpu(l.n, , l.rolling_mean, , out.rolling_mean, );
axpy_cpu(l.n, , l.rolling_variance, , out.rolling_variance, );
}
}
if(l.type == CONNECTED){
axpy_cpu(l.outputs, , l.biases, , out.biases, );
axpy_cpu(l.outputs*l.inputs, , l.weights, , out.weights, );
}
}
}
n = n+;
for(j = ; j < net->n; ++j){
layer l = sum->layers[j];
if(l.type == CONVOLUTIONAL){
int num = l.n*l.c*l.size*l.size;
scal_cpu(l.n, ./n, l.biases, );
scal_cpu(num, ./n, l.weights, );
if(l.batch_normalize){
scal_cpu(l.n, ./n, l.scales, );
scal_cpu(l.n, ./n, l.rolling_mean, );
scal_cpu(l.n, ./n, l.rolling_variance, );
}
}
if(l.type == CONNECTED){
scal_cpu(l.outputs, ./n, l.biases, );
scal_cpu(l.outputs*l.inputs, ./n, l.weights, );
}
}
save_weights(sum, outfile);
} long numops(network *net)
{
int i;
long ops = ;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL){
ops += 2l * l.n * l.size*l.size*l.c/l.groups * l.out_h*l.out_w;
} else if(l.type == CONNECTED){
ops += 2l * l.inputs * l.outputs;
} else if (l.type == RNN){
ops += 2l * l.input_layer->inputs * l.input_layer->outputs;
ops += 2l * l.self_layer->inputs * l.self_layer->outputs;
ops += 2l * l.output_layer->inputs * l.output_layer->outputs;
} else if (l.type == GRU){
ops += 2l * l.uz->inputs * l.uz->outputs;
ops += 2l * l.uh->inputs * l.uh->outputs;
ops += 2l * l.ur->inputs * l.ur->outputs;
ops += 2l * l.wz->inputs * l.wz->outputs;
ops += 2l * l.wh->inputs * l.wh->outputs;
ops += 2l * l.wr->inputs * l.wr->outputs;
} else if (l.type == LSTM){
ops += 2l * l.uf->inputs * l.uf->outputs;
ops += 2l * l.ui->inputs * l.ui->outputs;
ops += 2l * l.ug->inputs * l.ug->outputs;
ops += 2l * l.uo->inputs * l.uo->outputs;
ops += 2l * l.wf->inputs * l.wf->outputs;
ops += 2l * l.wi->inputs * l.wi->outputs;
ops += 2l * l.wg->inputs * l.wg->outputs;
ops += 2l * l.wo->inputs * l.wo->outputs;
}
}
return ops;
} void speed(char *cfgfile, int tics)
{
if (tics == ) tics = ;
network *net = parse_network_cfg(cfgfile);
set_batch_network(net, );
int i;
double time=what_time_is_it_now();
image im = make_image(net->w, net->h, net->c*net->batch);
for(i = ; i < tics; ++i){
network_predict(net, im.data);
}
double t = what_time_is_it_now() - time;
long ops = numops(net);
printf("\n%d evals, %f Seconds\n", tics, t);
printf("Floating Point Operations: %.2f Bn\n", (float)ops/.);
printf("FLOPS: %.2f Bn\n", (float)ops/.*tics/t);
printf("Speed: %f sec/eval\n", t/tics);
printf("Speed: %f Hz\n", tics/t);
} void operations(char *cfgfile)
{
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
long ops = numops(net);
printf("Floating Point Operations: %ld\n", ops);
printf("Floating Point Operations: %.2f Bn\n", (float)ops/.);
} void oneoff(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
int oldn = net->layers[net->n - ].n;
int c = net->layers[net->n - ].c;
scal_cpu(oldn*c, ., net->layers[net->n - ].weights, );
scal_cpu(oldn, , net->layers[net->n - ].biases, );
net->layers[net->n - ].n = ;
net->layers[net->n - ].biases += ;
net->layers[net->n - ].weights += *c;
if(weightfile){
load_weights(net, weightfile);
}
net->layers[net->n - ].biases -= ;
net->layers[net->n - ].weights -= *c;
net->layers[net->n - ].n = oldn;
printf("%d\n", oldn);
layer l = net->layers[net->n - ];
copy_cpu(l.n/, l.biases, , l.biases + l.n/, );
copy_cpu(l.n/, l.biases, , l.biases + *l.n/, );
copy_cpu(l.n/*l.c, l.weights, , l.weights + l.n/*l.c, );
copy_cpu(l.n/*l.c, l.weights, , l.weights + *l.n/*l.c, );
*net->seen = ;
save_weights(net, outfile);
} void oneoff2(char *cfgfile, char *weightfile, char *outfile, int l)
{
gpu_index = -;
network *net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights_upto(net, weightfile, , net->n);
load_weights_upto(net, weightfile, l, net->n);
}
*net->seen = ;
save_weights_upto(net, outfile, net->n);
} void partial(char *cfgfile, char *weightfile, char *outfile, int max)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
save_weights_upto(net, outfile, max);
} void print_weights(char *cfgfile, char *weightfile, int n)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
layer l = net->layers[n];
int i, j;
//printf("[");
for(i = ; i < l.n; ++i){
//printf("[");
for(j = ; j < l.size*l.size*l.c; ++j){
//if(j > 0) printf(",");
printf("%g ", l.weights[i*l.size*l.size*l.c + j]);
}
printf("\n");
//printf("]%s\n", (i == l.n-1)?"":",");
}
//printf("]");
} void rescale_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL){
rescale_weights(l, , -.);
break;
}
}
save_weights(net, outfile);
} void rgbgr_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL){
rgbgr_weights(l);
break;
}
}
save_weights(net, outfile);
} void reset_normalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for (i = ; i < net->n; ++i) {
layer l = net->layers[i];
if (l.type == CONVOLUTIONAL && l.batch_normalize) {
denormalize_convolutional_layer(l);
}
if (l.type == CONNECTED && l.batch_normalize) {
denormalize_connected_layer(l);
}
if (l.type == GRU && l.batch_normalize) {
denormalize_connected_layer(*l.input_z_layer);
denormalize_connected_layer(*l.input_r_layer);
denormalize_connected_layer(*l.input_h_layer);
denormalize_connected_layer(*l.state_z_layer);
denormalize_connected_layer(*l.state_r_layer);
denormalize_connected_layer(*l.state_h_layer);
}
}
save_weights(net, outfile);
} layer normalize_layer(layer l, int n)
{
int j;
l.batch_normalize=;
l.scales = calloc(n, sizeof(float));
for(j = ; j < n; ++j){
l.scales[j] = ;
}
l.rolling_mean = calloc(n, sizeof(float));
l.rolling_variance = calloc(n, sizeof(float));
return l;
} void normalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for(i = ; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == CONVOLUTIONAL && !l.batch_normalize){
net->layers[i] = normalize_layer(l, l.n);
}
if (l.type == CONNECTED && !l.batch_normalize) {
net->layers[i] = normalize_layer(l, l.outputs);
}
if (l.type == GRU && l.batch_normalize) {
*l.input_z_layer = normalize_layer(*l.input_z_layer, l.input_z_layer->outputs);
*l.input_r_layer = normalize_layer(*l.input_r_layer, l.input_r_layer->outputs);
*l.input_h_layer = normalize_layer(*l.input_h_layer, l.input_h_layer->outputs);
*l.state_z_layer = normalize_layer(*l.state_z_layer, l.state_z_layer->outputs);
*l.state_r_layer = normalize_layer(*l.state_r_layer, l.state_r_layer->outputs);
*l.state_h_layer = normalize_layer(*l.state_h_layer, l.state_h_layer->outputs);
net->layers[i].batch_normalize=;
}
}
save_weights(net, outfile);
} void statistics_net(char *cfgfile, char *weightfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for (i = ; i < net->n; ++i) {
layer l = net->layers[i];
if (l.type == CONNECTED && l.batch_normalize) {
printf("Connected Layer %d\n", i);
statistics_connected_layer(l);
}
if (l.type == GRU && l.batch_normalize) {
printf("GRU Layer %d\n", i);
printf("Input Z\n");
statistics_connected_layer(*l.input_z_layer);
printf("Input R\n");
statistics_connected_layer(*l.input_r_layer);
printf("Input H\n");
statistics_connected_layer(*l.input_h_layer);
printf("State Z\n");
statistics_connected_layer(*l.state_z_layer);
printf("State R\n");
statistics_connected_layer(*l.state_r_layer);
printf("State H\n");
statistics_connected_layer(*l.state_h_layer);
}
printf("\n");
}
} void denormalize_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -;
network *net = load_network(cfgfile, weightfile, );
int i;
for (i = ; i < net->n; ++i) {
layer l = net->layers[i];
if ((l.type == DECONVOLUTIONAL || l.type == CONVOLUTIONAL) && l.batch_normalize) {
denormalize_convolutional_layer(l);
net->layers[i].batch_normalize=;
}
if (l.type == CONNECTED && l.batch_normalize) {
denormalize_connected_layer(l);
net->layers[i].batch_normalize=;
}
if (l.type == GRU && l.batch_normalize) {
denormalize_connected_layer(*l.input_z_layer);
denormalize_connected_layer(*l.input_r_layer);
denormalize_connected_layer(*l.input_h_layer);
denormalize_connected_layer(*l.state_z_layer);
denormalize_connected_layer(*l.state_r_layer);
denormalize_connected_layer(*l.state_h_layer);
l.input_z_layer->batch_normalize = ;
l.input_r_layer->batch_normalize = ;
l.input_h_layer->batch_normalize = ;
l.state_z_layer->batch_normalize = ;
l.state_r_layer->batch_normalize = ;
l.state_h_layer->batch_normalize = ;
net->layers[i].batch_normalize=;
}
}
save_weights(net, outfile);
} void mkimg(char *cfgfile, char *weightfile, int h, int w, int num, char *prefix)
{
network *net = load_network(cfgfile, weightfile, );
image *ims = get_weights(net->layers[]);
int n = net->layers[].n;
int z;
for(z = ; z < num; ++z){
image im = make_image(h, w, );
fill_image(im, .);
int i;
for(i = ; i < ; ++i){
image r = copy_image(ims[rand()%n]);
rotate_image_cw(r, rand()%);
random_distort_image(r, , 1.5, 1.5);
int dx = rand()%(w-r.w);
int dy = rand()%(h-r.h);
ghost_image(r, im, dx, dy);
free_image(r);
}
char buff[];
sprintf(buff, "%s/gen_%d", prefix, z);
save_image(im, buff);
free_image(im);
}
} void visualize(char *cfgfile, char *weightfile)
{
network *net = load_network(cfgfile, weightfile, );
visualize_network(net);
#ifdef OPENCV
cvWaitKey();
#endif
} int main(int argc, char **argv)
{
// argv[0] 指向程序运行的全路径名;argv[1] 指向在DOS命令行中执行程序名后的第一个字符串;argv[2]第二个
//test_resize("data/bad.jpg");
//test_box();
//test_convolutional_layer();
if(argc < ){
fprintf(stderr, "usage: %s <function>\n", argv[]);
return ;
}
gpu_index = find_int_arg(argc, argv, "-i", );
if(find_arg(argc, argv, "-nogpu")) {
gpu_index = -;
} #ifndef GPU
gpu_index = -;
#else
if(gpu_index >= ){
cuda_set_device(gpu_index);
}
#endif if ( == strcmp(argv[], "average")){
average(argc, argv);
} else if ( == strcmp(argv[], "yolo")){
run_yolo(argc, argv);
} else if ( == strcmp(argv[], "super")){
run_super(argc, argv);
} else if ( == strcmp(argv[], "lsd")){
run_lsd(argc, argv);
} else if ( == strcmp(argv[], "detector")){
run_detector(argc, argv);
} else if ( == strcmp(argv[], "detect")){
float thresh = find_float_arg(argc, argv, "-thresh", .); //thresh用来控制检测的阈值
char *filename = (argc > ) ? argv[]: ;
char *outfile = find_char_arg(argc, argv, "-out", ); // 定义在\src\utils.c中
int fullscreen = find_arg(argc, argv, "-fullscreen");
test_detector("cfg/coco.data", argv[], argv[], filename, thresh, ., outfile, fullscreen); // 函数定义位于detector.c中
// 命令举例./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg //*修改//
//output_to_file();
my_testinsrc();
//*// } else if ( == strcmp(argv[], "cifar")){
run_cifar(argc, argv);
} else if ( == strcmp(argv[], "go")){
run_go(argc, argv);
} else if ( == strcmp(argv[], "rnn")){
run_char_rnn(argc, argv);
} else if ( == strcmp(argv[], "coco")){
run_coco(argc, argv);
} else if ( == strcmp(argv[], "classify")){
predict_classifier("cfg/imagenet1k.data", argv[], argv[], argv[], );
} else if ( == strcmp(argv[], "classifier")){
run_classifier(argc, argv);
} else if ( == strcmp(argv[], "regressor")){
run_regressor(argc, argv);
} else if ( == strcmp(argv[], "segmenter")){
run_segmenter(argc, argv);
} else if ( == strcmp(argv[], "art")){
run_art(argc, argv);
} else if ( == strcmp(argv[], "tag")){
run_tag(argc, argv);
} else if ( == strcmp(argv[], "3d")){
composite_3d(argv[], argv[], argv[], (argc > ) ? atof(argv[]) : );
} else if ( == strcmp(argv[], "test")){
test_resize(argv[]);
} else if ( == strcmp(argv[], "captcha")){
run_captcha(argc, argv);
} else if ( == strcmp(argv[], "nightmare")){
run_nightmare(argc, argv);
} else if ( == strcmp(argv[], "rgbgr")){
rgbgr_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "reset")){
reset_normalize_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "denormalize")){
denormalize_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "statistics")){
statistics_net(argv[], argv[]);
} else if ( == strcmp(argv[], "normalize")){
normalize_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "rescale")){
rescale_net(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "ops")){
operations(argv[]);
} else if ( == strcmp(argv[], "speed")){
speed(argv[], (argc > && argv[]) ? atoi(argv[]) : );
} else if ( == strcmp(argv[], "oneoff")){
oneoff(argv[], argv[], argv[]);
} else if ( == strcmp(argv[], "oneoff2")){
oneoff2(argv[], argv[], argv[], atoi(argv[]));
} else if ( == strcmp(argv[], "print")){
print_weights(argv[], argv[], atoi(argv[]));
} else if ( == strcmp(argv[], "partial")){
partial(argv[], argv[], argv[], atoi(argv[]));
} else if ( == strcmp(argv[], "average")){
average(argc, argv);
} else if ( == strcmp(argv[], "visualize")){
visualize(argv[], (argc > ) ? argv[] : );
} else if ( == strcmp(argv[], "mkimg")){
mkimg(argv[], argv[], atoi(argv[]), atoi(argv[]), atoi(argv[]), argv[]);
} else if ( == strcmp(argv[], "imtest")){
test_resize(argv[]);
} else {
fprintf(stderr, "Not an option: %s\n", argv[]);
}
return ;
}

可成功编译并运行

(3)修改代码的第三次尝试

在darknet下新建目录\my, 用于存放自己新写的代码. 新建两个文件my_tofile.c与my_file.h, 其内容如下

//my_tofile.h

#ifndef TOFILE
#define TOFLIE
#include "darknet.h" void my_output_to_file(); #endif // my_tofile.c
#include "my_tofile.h" void my_output_to_file()
{
FILE *fp;
fp=fopen("output.txt","w");
fprintf(fp,"adfsss");
fclose(fp);
printf("test in \\my\n");
}

修改Makefile文件, 在最后对函数进行声明, 在VPATH处添加路径 VPATH=./src/:./examples:./my , 修改完后内容如下

GPU=
CUDNN=
OPENCV=
OPENMP=
DEBUG= ARCH= -gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=[sm_50,compute_50] \
-gencode arch=compute_52,code=[sm_52,compute_52]
# -gencode arch=compute_20,code=[sm_20,sm_21] \ This one is deprecated? # This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52 VPATH=./src/:./examples:./my
# VTATH用来告诉make,源文件的路径, 参考https://blog.csdn.net/mcgrady_tracy/article/details/27240139
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/ CC=gcc
NVCC=nvcc
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread #gcc等编译器会用到的一些优化参数,也可以在里面指定库文件的位置
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC #指定头文件(.h文件)的路径,如:CFLAGS=-I/usr/include -I/path/include。 ifeq ($(OPENMP), )
CFLAGS+= -fopenmp
endif ifeq ($(DEBUG), )
OPTS=-O0 -g
endif CFLAGS+=$(OPTS) ifeq ($(OPENCV), )
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv`
COMMON+= `pkg-config --cflags opencv`
endif ifeq ($(GPU), )
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif ifeq ($(CUDNN), )
COMMON+= -DCUDNN
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif OBJ=my_tofile.o my_testinsrc.o gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o l2norm_layer.o yolo_layer.o
EXECOBJA=my_test.o captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), )
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA)) #加前缀函数: $(addprefix <prefix>,<names...>),OBJDIR是obj存放的地址
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h #all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj results $(SLIB) $(ALIB) $(EXEC) $(EXEC): $(EXECOBJ) $(ALIB)
$(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB) $(ALIB): $(OBJS)
$(AR) $(ARFLAGS) $@ $^ $(SLIB): $(OBJS)
$(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS) $(OBJDIR)%.o: %.c $(DEPS)
$(CC) $(COMMON) $(CFLAGS) -c $< -o $@ $(OBJDIR)%.o: %.cu $(DEPS)
$(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@ obj:
mkdir -p obj
backup:
mkdir -p backup
results:
mkdir -p results .PHONY: clean clean:
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)/*

最后在\exampes中的文件中进行调用, 可顺利编译并运行

├── examples
│ ├── darknet.c(主程序)
│ │── xxx1.c
│ └── xxx2.c

├── include
│ ├── darknet.h


├── Makefile

├── my
│ ├── my_zzz1.c
│ │── my_zzz1.h
│ └── ......

└── src
├── yyy1.c
├── yyy2.h
└──......

最终代码结构会如下所示

yolo源码解析(1):代码逻辑的更多相关文章

  1. 第三十六节,目标检测之yolo源码解析

    在一个月前,我就已经介绍了yolo目标检测的原理,后来也把tensorflow实现代码仔细看了一遍.但是由于这个暑假事情比较大,就一直搁浅了下来,趁今天有时间,就把源码解析一下.关于yolo目标检测的 ...

  2. yolo源码解析(3):视频检测流程

    代码在自己电脑中!!!!不在服务器 根据前文所说yolo代码逻辑: ├── examples │ ├── darknet.c(主程序) │ │── xxx1.c │ └── xxx2.c │ ├── ...

  3. yolo源码解析(一)

    原文:https://www.cnblogs.com/zyly/p/9534063.html yolo源码来源于网址:https://github.com/hizhangp/yolo_tensorfl ...

  4. jquery源码解析:代码结构分析

    本系列是针对jquery2.0.3版本进行的讲解.此版本不支持IE8及以下版本. (function(){ (21, 94)     定义了一些变量和函数,   jQuery = function() ...

  5. axios 源码解析(中) 代码结构

    axios现在最新的版本的是v0.19.0,本节我们来分析一下它的实现源码,首先通过 gitHub地址获取到它的源代码,地址:https://github.com/axios/axios/tree/v ...

  6. yolo源码解析(三)

    七 测试网络 模型测试包含于test.py文件,Detector类的image_detector()函数用于检测目标. import os import cv2 import argparse imp ...

  7. yolo源码解析(二)

    五 读取数据pascal_voc.py文件解析 我们在YOLENet类中定义了两个占位符,一个是输入图片占位符,一个是图片对应的标签占位符,如下: #输入图片占位符 [NONE,image_size, ...

  8. yolo源码解析(3):进行简单跳帧

    视频检测命令  ./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights ../../dataset/ ...

  9. yolo源码解析(2):处理图片

    首先安装ffmpeg, 参考https://blog.csdn.net/lwgkzl/article/details/77836207 然后将视频切分为图片, 参考:https://zhuanlan. ...

随机推荐

  1. Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the server requires

    Server Tomcat v8.0 Server at localhost was unable to start within 45 seconds. If the server requires ...

  2. hdoj--3062--party(2-sat 可行解)

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. python中黏包现象

    #黏包:发送端发送数据,接收端不知道应如何去接收造成的一种数据混乱现象. #关于分包和黏包: #黏包:发送端发送两个字符串"hello"和"word",接收方却 ...

  4. POJ 3020 Hungary

    一道建图题-- // by SiriusRen #include <cstdio> #include <cstring> using namespace std; #defin ...

  5. 在ubuntu下安装redis

    最简单在线安装的方式: #安装Redis服务器端 ~ sudo apt-get install redis-server 安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序 # 检查 ...

  6. 微软认证Hyper-V咨询工程师认证课程

    课程链接:http://www.microsoftvirtualacademy.com/colleges/hyper-V-Certificate STEP 1:完成课程链接内的认证课程. STEP 2 ...

  7. javascript中手风琴特效

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. hdu 5375 Gray code 【 dp 】

    dp[i][j]表示第i位取j的时候取得的最大的分数 然后分s[i]是不是问号,s[i-1]是不是问号这大的四种情况讨论 #include<cstdio> #include<cstr ...

  9. 使用 Sublime 或其他编辑器调试 Tampermonkey 油猴脚本

    作者说由于 Chrome 安全限制,没办法调用外部编辑器调试,但提供了一个间接办法,那就是脚本中使用@require file:///引入本地文件的形式,具体的方法是 打开 chrome://exte ...

  10. Hihocoder1061-Beautiful String

    时间限制:10000ms单点时限:1000ms内存限制:256MB 描述 We say a string is beautiful if it has the equal amount of 3 or ...