Skip to content

Commit

Permalink
faster nms and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreddie committed Mar 15, 2018
1 parent 0b64cb4 commit 0f11083
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 88 deletions.
6 changes: 3 additions & 3 deletions examples/coco.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void validate_coco(char *cfg, char *weights)
FILE *fp = fopen(buff, "w");
fprintf(fp, "[\n");

detection *dets = make_network_boxes(net);
detection *dets = make_network_boxes(net, 0);

int m = plist->size;
int i=0;
Expand Down Expand Up @@ -231,7 +231,7 @@ void validate_coco_recall(char *cfgfile, char *weightfile)
snprintf(buff, 1024, "%s%s.txt", base, coco_classes[j]);
fps[j] = fopen(buff, "w");
}
detection *dets = make_network_boxes(net);
detection *dets = make_network_boxes(net, 0);

int m = plist->size;
int i=0;
Expand Down Expand Up @@ -302,7 +302,7 @@ void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
clock_t time;
char buff[256];
char *input = buff;
detection *dets = make_network_boxes(net);
detection *dets = make_network_boxes(net, 0);
while(1){
if(filename){
strncpy(input, filename, 256);
Expand Down
47 changes: 23 additions & 24 deletions examples/detector.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ void validate_detector_flip(char *datacfg, char *cfgfile, char *weightfile, char
}
}

detection *dets = make_network_boxes(net);

int m = plist->size;
int i=0;
int t;
Expand Down Expand Up @@ -333,15 +331,17 @@ void validate_detector_flip(char *datacfg, char *cfgfile, char *weightfile, char
network_predict(net, input.data);
int w = val[t].w;
int h = val[t].h;
fill_network_boxes(net, w, h, thresh, .5, map, 0, dets);
if (nms) do_nms_sort(dets, l.w*l.h*l.n, classes, nms);
int num = 0;
detection *dets = get_network_boxes(net, w, h, thresh, .5, map, 0, &num);
if (nms) do_nms_sort(dets, num, classes, nms);
if (coco){
print_cocos(fp, path, dets, l.w*l.h*l.n, classes, w, h);
print_cocos(fp, path, dets, num, classes, w, h);
} else if (imagenet){
print_imagenet_detections(fp, i+t-nthreads+1, dets, l.w*l.h*l.n, classes, w, h);
print_imagenet_detections(fp, i+t-nthreads+1, dets, num, classes, w, h);
} else {
print_detector_detections(fps, id, dets, l.w*l.h*l.n, classes, w, h);
print_detector_detections(fps, id, dets, num, classes, w, h);
}
free_detections(dets, num);
free(id);
free_image(val[t]);
free_image(val_resized[t]);
Expand Down Expand Up @@ -409,8 +409,6 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out
}
}

detection *dets = make_network_boxes(net);
int nboxes = num_boxes(net);

int m = plist->size;
int i=0;
Expand Down Expand Up @@ -459,7 +457,8 @@ void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *out
network_predict(net, X);
int w = val[t].w;
int h = val[t].h;
fill_network_boxes(net, w, h, thresh, .5, map, 0, dets);
int nboxes = 0;
detection *dets = get_network_boxes(net, w, h, thresh, .5, map, 0, &nboxes);
if (nms) do_nms_sort(dets, nboxes, classes, nms);
if (coco){
print_cocos(fp, path, dets, nboxes, classes, w, h);
Expand Down Expand Up @@ -497,7 +496,6 @@ void validate_detector_recall(char *cfgfile, char *weightfile)
layer l = net->layers[net->n-1];

int j, k;
detection *dets = make_network_boxes(net);

int m = plist->size;
int i=0;
Expand All @@ -510,15 +508,15 @@ void validate_detector_recall(char *cfgfile, char *weightfile)
int correct = 0;
int proposals = 0;
float avg_iou = 0;
int nboxes = num_boxes(net);

for(i = 0; i < m; ++i){
char *path = paths[i];
image orig = load_image_color(path, 0, 0);
image sized = resize_image(orig, net->w, net->h);
char *id = basecfg(path);
network_predict(net, sized.data);
fill_network_boxes(net, sized.w, sized.h, thresh, .5, 0, 1, dets);
int nboxes = 0;
detection *dets = get_network_boxes(net, sized.w, sized.h, thresh, .5, 0, 1, &nboxes);
if (nms) do_nms_obj(dets, nboxes, 1, nms);

char labelpath[4096];
Expand Down Expand Up @@ -590,18 +588,18 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
//resize_network(net, sized.w, sized.h);
layer l = net->layers[net->n-1];

int nboxes = num_boxes(net);
printf("%d\n", nboxes);

float *X = sized.data;
time=what_time_is_it_now();
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time);
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1);
int nboxes = 0;
detection *dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes);
printf("%d\n", nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
draw_detections(im, dets, nboxes, thresh, names, alphabet, l.classes);
free_detections(dets, num_boxes(net));
free_detections(dets, nboxes);
if(outfile){
save_image(im, outfile);
}
Expand Down Expand Up @@ -673,11 +671,10 @@ void censor_detector(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
image in_s = letterbox_image(in, net->w, net->h);
layer l = net->layers[net->n-1];

int nboxes = num_boxes(net);

float *X = in_s.data;
network_predict(net, X);
detection *dets = get_network_boxes(net, in.w, in.h, thresh, 0, 0, 0);
int nboxes = 0;
detection *dets = get_network_boxes(net, in.w, in.h, thresh, 0, 0, 0, &nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);

Expand All @@ -691,7 +688,7 @@ void censor_detector(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
}
show_image(in, base);
cvWaitKey(10);
free_detections(dets, num_boxes(net));
free_detections(dets, nboxes);


free_image(in_s);
Expand Down Expand Up @@ -756,12 +753,12 @@ void extract_detector(char *datacfg, char *cfgfile, char *weightfile, int cam_in
image in_s = letterbox_image(in, net->w, net->h);
layer l = net->layers[net->n-1];

int nboxes = num_boxes(net);
show_image(in, base);

int nboxes = 0;
float *X = in_s.data;
network_predict(net, X);
detection *dets = get_network_boxes(net, in.w, in.h, thresh, 0, 0, 1);
detection *dets = get_network_boxes(net, in.w, in.h, thresh, 0, 0, 1, &nboxes);
//if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);

Expand All @@ -779,7 +776,7 @@ void extract_detector(char *datacfg, char *cfgfile, char *weightfile, int cam_in
free_image(bim);
}
}
free_detections(dets, num_boxes(net));
free_detections(dets, nboxes);


free_image(in_s);
Expand All @@ -795,6 +792,7 @@ void extract_detector(char *datacfg, char *cfgfile, char *weightfile, int cam_in
}
}

/*
void network_detect(network *net, image im, float thresh, float hier_thresh, float nms, detection *dets)
{
network_predict_image(net, im);
Expand All @@ -803,6 +801,7 @@ void network_detect(network *net, image im, float thresh, float hier_thresh, flo
fill_network_boxes(net, im.w, im.h, thresh, hier_thresh, 0, 0, dets);
if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
}
*/

void run_detector(int argc, char **argv)
{
Expand Down
6 changes: 3 additions & 3 deletions examples/yolo.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void validate_yolo(char *cfg, char *weights)
image *buf = calloc(nthreads, sizeof(image));
image *buf_resized = calloc(nthreads, sizeof(image));
pthread_t *thr = calloc(nthreads, sizeof(pthread_t));
detection *dets = make_network_boxes(net);
detection *dets = make_network_boxes(net, 0);

load_args args = {0};
args.w = net->w;
Expand Down Expand Up @@ -200,7 +200,7 @@ void validate_yolo_recall(char *cfg, char *weights)
snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
fps[j] = fopen(buff, "w");
}
detection *dets = make_network_boxes(net);
detection *dets = make_network_boxes(net, 0);

int m = plist->size;
int i=0;
Expand Down Expand Up @@ -271,7 +271,7 @@ void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh)
char buff[256];
char *input = buff;
float nms=.4;
detection *dets = make_network_boxes(net);
detection *dets = make_network_boxes(net, 0);
while(1){
if(filename){
strncpy(input, filename, 256);
Expand Down
7 changes: 2 additions & 5 deletions include/darknet.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ void save_weights_upto(network *net, char *filename, int cutoff);
void load_weights_upto(network *net, char *filename, int start, int cutoff);

void zero_objectness(layer l);
void get_region_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, float tree_thresh, int relative, detection *dets);
int get_region_detections(layer l, int w, int h, int netw, int neth, float thresh, int *map, float tree_thresh, int relative, detection *dets);
void free_network(network *net);
void set_batch_network(network *net, int b);
void set_temp_network(network *net, float t);
Expand Down Expand Up @@ -739,10 +739,7 @@ int network_width(network *net);
int network_height(network *net);
float *network_predict_image(network *net, image im);
void network_detect(network *net, image im, float thresh, float hier_thresh, float nms, detection *dets);
int num_boxes(network *net);
detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative);
void fill_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, detection *dets);
detection *make_network_boxes(network *net);
detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, int *num);
void free_detections(detection *dets, int n);

void reset_network_state(network *net, int b);
Expand Down
12 changes: 5 additions & 7 deletions python/darknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class METADATA(Structure):
make_image.restype = IMAGE

get_network_boxes = lib.get_network_boxes
get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int]
get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int)]
get_network_boxes.restype = POINTER(DETECTION)

make_network_boxes = lib.make_network_boxes
Expand All @@ -76,10 +76,6 @@ class METADATA(Structure):
free_ptrs = lib.free_ptrs
free_ptrs.argtypes = [POINTER(c_void_p), c_int]

num_boxes = lib.num_boxes
num_boxes.argtypes = [c_void_p]
num_boxes.restype = c_int

network_predict = lib.network_predict
network_predict.argtypes = [c_void_p, POINTER(c_float)]

Expand Down Expand Up @@ -128,9 +124,11 @@ def classify(net, meta, im):

def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
im = load_image(image, 0, 0)
num = num_boxes(net)
num = c_int(0)
pnum = pointer(num)
predict_image(net, im)
dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0)
dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)
num = pnum[0]
if (nms): do_nms_obj(dets, num, meta.classes, nms);

res = []
Expand Down
11 changes: 11 additions & 0 deletions src/box.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ int nms_comparator(const void *pa, const void *pb)
void do_nms_obj(detection *dets, int total, int classes, float thresh)
{
int i, j, k;
k = total-1;
for(i = 0; i <= k; ++i){
if(dets[i].objectness == 0){
detection swap = dets[i];
dets[i] = dets[k];
dets[k] = swap;
--k;
--i;
}
}
total = k+1;

for(i = 0; i < total; ++i){
dets[i].sort_class = -1;
Expand Down
22 changes: 9 additions & 13 deletions src/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,12 @@ static int running = 0;

static int demo_frame = 3;
static int demo_index = 0;
static int demo_detections = 0;
//static float **predictions;
static detection **dets;
static detection *avg;
//static float *avg;
static int demo_done = 0;
double demo_time;

detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative);
detection *make_network_boxes(network *net);
void fill_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, detection *dets);
detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, int *num);

void *detect_in_thread(void *ptr)
{
Expand All @@ -55,12 +50,15 @@ void *detect_in_thread(void *ptr)
if(l.type == DETECTION){
get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
} else */
detection *dets;
int nboxes = 0;
if (l.type == REGION){
fill_network_boxes(net, buff[0].w, buff[0].h, demo_thresh, demo_hier, 0, 1, dets[demo_index]);
dets = get_network_boxes(net, buff[0].w, buff[0].h, demo_thresh, demo_hier, 0, 1, &nboxes);
} else {
error("Last layer must produce detections\n");
}

/*
int i,j;
box zero = {0};
int classes = l.classes;
Expand All @@ -79,15 +77,17 @@ void *detect_in_thread(void *ptr)
//copy_cpu(classes, dets[0][i].prob, 1, avg[i].prob, 1);
//avg[i].objectness = dets[0][i].objectness;
}
*/

if (nms > 0) do_nms_obj(avg, demo_detections, l.classes, nms);
if (nms > 0) do_nms_obj(dets, nboxes, l.classes, nms);

printf("\033[2J");
printf("\033[1;1H");
printf("\nFPS:%.1f\n",fps);
printf("Objects:\n\n");
image display = buff[(buff_index+2) % 3];
draw_detections(display, avg, demo_detections, demo_thresh, demo_names, demo_alphabet, demo_classes);
draw_detections(display, dets, nboxes, demo_thresh, demo_names, demo_alphabet, demo_classes);
free_detections(dets, nboxes);

demo_index = (demo_index + 1)%demo_frame;
running = 0;
Expand Down Expand Up @@ -174,11 +174,7 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch

if(!cap) error("Couldn't connect to webcam.\n");

demo_detections = num_boxes(net);
avg = make_network_boxes(net);
dets = calloc(demo_frame, sizeof(detection*));
int i;
for(i = 0; i < demo_frame; ++i) dets[i] = make_network_boxes(net);

buff[0] = get_image_from_stream(cap);
buff[1] = copy_image(buff[0]);
Expand Down
Loading

0 comments on commit 0f11083

Please sign in to comment.