From 7a223d8591e0a497889b9fce9bc43ac4bd3969fd Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Fri, 21 Jul 2017 19:33:49 -0700 Subject: [PATCH] gotta make a CVPR demo --- examples/classifier.c | 10 ++-- examples/detector.c | 2 +- src/demo.c | 118 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 102 insertions(+), 28 deletions(-) diff --git a/examples/classifier.c b/examples/classifier.c index 75786237..593b34e5 100644 --- a/examples/classifier.c +++ b/examples/classifier.c @@ -53,7 +53,7 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, char **paths = (char **)list_to_array(plist); printf("%d\n", plist->size); int N = plist->size; - clock_t time; + double time; load_args args = {0}; args.w = net.w; @@ -85,14 +85,14 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int epoch = (*net.seen)/N; while(get_current_batch(net) < net.max_batches || net.max_batches == 0){ - time=clock(); + time = what_time_is_it_now(); pthread_join(load_thread, 0); train = buffer; load_thread = load_data(args); - printf("Loaded: %lf seconds\n", sec(clock()-time)); - time=clock(); + printf("Loaded: %lf seconds\n", what_time_is_it_now()-time); + time = what_time_is_it_now(); float loss = 0; #ifdef GPU @@ -106,7 +106,7 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, #endif if(avg_loss == -1) avg_loss = loss; avg_loss = avg_loss*.9 + loss*.1; - printf("%ld, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen); + printf("%ld, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), what_time_is_it_now()-time, *net.seen); free_data(train); if(*net.seen/N > epoch){ epoch = *net.seen/N; diff --git a/examples/detector.c b/examples/detector.c index b482cb62..0537588c 100644 --- a/examples/detector.c +++ b/examples/detector.c @@ -503,7 +503,7 @@ void validate_detector_recall(char *cfgfile, char *weightfile) fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); srand(time(0)); - list *plist = get_paths("data/voc.2007.test"); + list *plist = get_paths("data/coco_val_5k.list"); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n-1]; diff --git a/src/demo.c b/src/demo.c index a84c2a3f..a60c456d 100644 --- a/src/demo.c +++ b/src/demo.c @@ -20,6 +20,10 @@ static int demo_classes; static float **probs; static box *boxes; static network net; +static network net2; +static float **probs2; +static box *boxes2; +static float **predictions2; static image buff [3]; static image buff_letter[3]; static int buff_index = 0; @@ -30,14 +34,11 @@ static float demo_thresh = 0; static float demo_hier = .5; static int running = 0; -static int demo_delay = 0; static int demo_frame = 3; static int demo_detections = 0; static float **predictions; static int demo_index = 0; static int demo_done = 0; -static float *last_avg2; -static float *last_avg; static float *avg; double demo_time; @@ -61,8 +62,7 @@ void *detect_in_thread(void *ptr) memcpy(predictions[demo_index], prediction, l.outputs*sizeof(float)); mean_arrays(predictions, demo_frame, l.outputs, avg); - l.output = last_avg2; - if(demo_delay == 0) l.output = avg; + l.output = avg; if(l.type == DETECTION){ get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0); } else if (l.type == REGION){ @@ -97,12 +97,7 @@ void *display_in_thread(void *ptr) show_image_cv(buff[(buff_index + 1)%3], "Demo", ipl); int c = cvWaitKey(1); if (c != -1) c = c%256; - if (c == 10){ - if(demo_delay == 0) demo_delay = 60; - else if(demo_delay == 5) demo_delay = 0; - else if(demo_delay == 60) demo_delay = 5; - else demo_delay = 0; - } else if (c == 27) { + if (c == 27) { demo_done = 1; return 0; } else if (c == 82) { @@ -135,7 +130,6 @@ void *detect_loop(void *ptr) void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen) { - demo_delay = delay; demo_frame = avg_frames; predictions = calloc(demo_frame, sizeof(float*)); image **alphabet = load_alphabet(); @@ -179,8 +173,6 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch int j; avg = (float *) calloc(l.outputs, sizeof(float)); - last_avg = (float *) calloc(l.outputs, sizeof(float)); - last_avg2 = (float *) calloc(l.outputs, sizeof(float)); for(j = 0; j < demo_frame; ++j) predictions[j] = (float *) calloc(l.outputs, sizeof(float)); boxes = (box *)calloc(l.w*l.h*l.n, sizeof(box)); @@ -213,14 +205,96 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed"); if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed"); if(!prefix){ - if(count % (demo_delay+1) == 0){ - fps = 1./(get_wall_time() - demo_time); - demo_time = get_wall_time(); - float *swap = last_avg; - last_avg = last_avg2; - last_avg2 = swap; - memcpy(last_avg, avg, l.outputs*sizeof(float)); - } + fps = 1./(get_wall_time() - demo_time); + demo_time = get_wall_time(); + display_in_thread(0); + }else{ + char name[256]; + sprintf(name, "%s_%08d", prefix, count); + save_image(buff[(buff_index + 1)%3], name); + } + pthread_join(fetch_thread, 0); + pthread_join(detect_thread, 0); + ++count; + } +} + +void demo_compare(char *cfg1, char *weight1, char *cfg2, char *weight2, float thresh, int cam_index, const char *filename, char **names, int classes, int delay, char *prefix, int avg_frames, float hier, int w, int h, int frames, int fullscreen) +{ + demo_frame = avg_frames; + predictions = calloc(demo_frame, sizeof(float*)); + image **alphabet = load_alphabet(); + demo_names = names; + demo_alphabet = alphabet; + demo_classes = classes; + demo_thresh = thresh; + demo_hier = hier; + printf("Demo\n"); + net = load_network(cfg1, weight1, 0); + set_batch_network(&net, 1); + pthread_t detect_thread; + pthread_t fetch_thread; + + srand(2222222); + + if(filename){ + printf("video file: %s\n", filename); + cap = cvCaptureFromFile(filename); + }else{ + cap = cvCaptureFromCAM(cam_index); + + if(w){ + cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w); + } + if(h){ + cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h); + } + if(frames){ + cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, frames); + } + } + + if(!cap) error("Couldn't connect to webcam.\n"); + + layer l = net.layers[net.n-1]; + demo_detections = l.n*l.w*l.h; + int j; + + avg = (float *) calloc(l.outputs, sizeof(float)); + for(j = 0; j < demo_frame; ++j) predictions[j] = (float *) calloc(l.outputs, sizeof(float)); + + boxes = (box *)calloc(l.w*l.h*l.n, sizeof(box)); + probs = (float **)calloc(l.w*l.h*l.n, sizeof(float *)); + for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float *)calloc(l.classes+1, sizeof(float)); + + buff[0] = get_image_from_stream(cap); + buff[1] = copy_image(buff[0]); + buff[2] = copy_image(buff[0]); + buff_letter[0] = letterbox_image(buff[0], net.w, net.h); + buff_letter[1] = letterbox_image(buff[0], net.w, net.h); + buff_letter[2] = letterbox_image(buff[0], net.w, net.h); + ipl = cvCreateImage(cvSize(buff[0].w,buff[0].h), IPL_DEPTH_8U, buff[0].c); + + int count = 0; + if(!prefix){ + cvNamedWindow("Demo", CV_WINDOW_NORMAL); + if(fullscreen){ + cvSetWindowProperty("Demo", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); + } else { + cvMoveWindow("Demo", 0, 0); + cvResizeWindow("Demo", 1352, 1013); + } + } + + demo_time = get_wall_time(); + + while(!demo_done){ + buff_index = (buff_index + 1) %3; + if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed"); + if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed"); + if(!prefix){ + fps = 1./(get_wall_time() - demo_time); + demo_time = get_wall_time(); display_in_thread(0); }else{ char name[256];