fixed demo thing

This commit is contained in:
Joseph Redmon 2018-03-25 05:48:43 -07:00
parent d3828827e7
commit f444f25e91

View File

@ -30,13 +30,59 @@ static int running = 0;
static int demo_frame = 3; static int demo_frame = 3;
static int demo_index = 0; static int demo_index = 0;
//static float **predictions; static float **predictions;
//static float *avg; static float *avg;
static int demo_done = 0; static int demo_done = 0;
static int demo_total = 0;
double demo_time; double demo_time;
detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, int *num); detection *get_network_boxes(network *net, int w, int h, float thresh, float hier, int *map, int relative, int *num);
int size_network(network *net)
{
int i;
int count = 0;
for(i = 0; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
count += l.outputs;
}
}
return count;
}
void remember_network(network *net)
{
int i;
int count = 0;
for(i = 0; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
memcpy(predictions[demo_index] + count, net->layers[i].output, sizeof(float) * l.outputs);
count += l.outputs;
}
}
}
detection *avg_predictions(network *net, int *nboxes)
{
int i, j;
int count = 0;
fill_cpu(demo_total, 0, avg, 1);
for(j = 0; j < demo_frame; ++j){
axpy_cpu(demo_total, 1./demo_frame, predictions[j], 1, avg, 1);
}
for(i = 0; i < net->n; ++i){
layer l = net->layers[i];
if(l.type == YOLO || l.type == REGION || l.type == DETECTION){
memcpy(l.output, avg + count, sizeof(float) * l.outputs);
count += l.outputs;
}
}
detection *dets = get_network_boxes(net, buff[0].w, buff[0].h, demo_thresh, demo_hier, 0, 1, nboxes);
return dets;
}
void *detect_in_thread(void *ptr) void *detect_in_thread(void *ptr)
{ {
running = 1; running = 1;
@ -47,37 +93,35 @@ void *detect_in_thread(void *ptr)
network_predict(net, X); network_predict(net, X);
/* /*
if(l.type == DETECTION){ if(l.type == DETECTION){
get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0); get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
} else */ } else */
remember_network(net);
detection *dets = 0; detection *dets = 0;
int nboxes = 0; int nboxes = 0;
if (l.type == REGION){ dets = avg_predictions(net, &nboxes);
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 i,j;
int classes = l.classes; box zero = {0};
for(i = 0; i < demo_detections; ++i){ int classes = l.classes;
avg[i].objectness = 0; for(i = 0; i < demo_detections; ++i){
avg[i].bbox = zero; avg[i].objectness = 0;
memset(avg[i].prob, 0, classes*sizeof(float)); avg[i].bbox = zero;
for(j = 0; j < demo_frame; ++j){ memset(avg[i].prob, 0, classes*sizeof(float));
axpy_cpu(classes, 1./demo_frame, dets[j][i].prob, 1, avg[i].prob, 1); for(j = 0; j < demo_frame; ++j){
avg[i].objectness += dets[j][i].objectness * 1./demo_frame; axpy_cpu(classes, 1./demo_frame, dets[j][i].prob, 1, avg[i].prob, 1);
avg[i].bbox.x += dets[j][i].bbox.x * 1./demo_frame; avg[i].objectness += dets[j][i].objectness * 1./demo_frame;
avg[i].bbox.y += dets[j][i].bbox.y * 1./demo_frame; avg[i].bbox.x += dets[j][i].bbox.x * 1./demo_frame;
avg[i].bbox.w += dets[j][i].bbox.w * 1./demo_frame; avg[i].bbox.y += dets[j][i].bbox.y * 1./demo_frame;
avg[i].bbox.h += dets[j][i].bbox.h * 1./demo_frame; avg[i].bbox.w += dets[j][i].bbox.w * 1./demo_frame;
} avg[i].bbox.h += dets[j][i].bbox.h * 1./demo_frame;
//copy_cpu(classes, dets[0][i].prob, 1, avg[i].prob, 1); }
//avg[i].objectness = dets[0][i].objectness; //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(dets, nboxes, l.classes, nms); if (nms > 0) do_nms_obj(dets, nboxes, l.classes, nms);
@ -155,6 +199,14 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch
srand(2222222); srand(2222222);
int i;
demo_total = size_network(net);
predictions = calloc(demo_frame, sizeof(float*));
for (i = 0; i < demo_frame; ++i){
predictions[i] = calloc(demo_total, sizeof(float));
}
avg = calloc(demo_total, sizeof(float));
if(filename){ if(filename){
printf("video file: %s\n", filename); printf("video file: %s\n", filename);
cap = cvCaptureFromFile(filename); cap = cvCaptureFromFile(filename);
@ -215,92 +267,92 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch
} }
/* /*
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) 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; demo_frame = avg_frames;
predictions = calloc(demo_frame, sizeof(float*)); predictions = calloc(demo_frame, sizeof(float*));
image **alphabet = load_alphabet(); image **alphabet = load_alphabet();
demo_names = names; demo_names = names;
demo_alphabet = alphabet; demo_alphabet = alphabet;
demo_classes = classes; demo_classes = classes;
demo_thresh = thresh; demo_thresh = thresh;
demo_hier = hier; demo_hier = hier;
printf("Demo\n"); printf("Demo\n");
net = load_network(cfg1, weight1, 0); net = load_network(cfg1, weight1, 0);
set_batch_network(net, 1); set_batch_network(net, 1);
pthread_t detect_thread; pthread_t detect_thread;
pthread_t fetch_thread; pthread_t fetch_thread;
srand(2222222); srand(2222222);
if(filename){ if(filename){
printf("video file: %s\n", filename); printf("video file: %s\n", filename);
cap = cvCaptureFromFile(filename); cap = cvCaptureFromFile(filename);
}else{ }else{
cap = cvCaptureFromCAM(cam_index); cap = cvCaptureFromCAM(cam_index);
if(w){ if(w){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w); cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
} }
if(h){ if(h){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h); cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
} }
if(frames){ if(frames){
cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, frames); cvSetCaptureProperty(cap, CV_CAP_PROP_FPS, frames);
} }
} }
if(!cap) error("Couldn't connect to webcam.\n"); if(!cap) error("Couldn't connect to webcam.\n");
layer l = net->layers[net->n-1]; layer l = net->layers[net->n-1];
demo_detections = l.n*l.w*l.h; demo_detections = l.n*l.w*l.h;
int j; int j;
avg = (float *) calloc(l.outputs, sizeof(float)); avg = (float *) calloc(l.outputs, sizeof(float));
for(j = 0; j < demo_frame; ++j) predictions[j] = (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)); boxes = (box *)calloc(l.w*l.h*l.n, sizeof(box));
probs = (float **)calloc(l.w*l.h*l.n, sizeof(float *)); 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)); 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[0] = get_image_from_stream(cap);
buff[1] = copy_image(buff[0]); buff[1] = copy_image(buff[0]);
buff[2] = copy_image(buff[0]); buff[2] = copy_image(buff[0]);
buff_letter[0] = letterbox_image(buff[0], net->w, net->h); buff_letter[0] = letterbox_image(buff[0], net->w, net->h);
buff_letter[1] = 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); 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); ipl = cvCreateImage(cvSize(buff[0].w,buff[0].h), IPL_DEPTH_8U, buff[0].c);
int count = 0; int count = 0;
if(!prefix){ if(!prefix){
cvNamedWindow("Demo", CV_WINDOW_NORMAL); cvNamedWindow("Demo", CV_WINDOW_NORMAL);
if(fullscreen){ if(fullscreen){
cvSetWindowProperty("Demo", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); cvSetWindowProperty("Demo", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
} else { } else {
cvMoveWindow("Demo", 0, 0); cvMoveWindow("Demo", 0, 0);
cvResizeWindow("Demo", 1352, 1013); cvResizeWindow("Demo", 1352, 1013);
} }
} }
demo_time = what_time_is_it_now();
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./(what_time_is_it_now() - demo_time);
demo_time = what_time_is_it_now(); demo_time = what_time_is_it_now();
display_in_thread(0);
while(!demo_done){ }else{
buff_index = (buff_index + 1) %3; char name[256];
if(pthread_create(&fetch_thread, 0, fetch_in_thread, 0)) error("Thread creation failed"); sprintf(name, "%s_%08d", prefix, count);
if(pthread_create(&detect_thread, 0, detect_in_thread, 0)) error("Thread creation failed"); save_image(buff[(buff_index + 1)%3], name);
if(!prefix){ }
fps = 1./(what_time_is_it_now() - demo_time); pthread_join(fetch_thread, 0);
demo_time = what_time_is_it_now(); pthread_join(detect_thread, 0);
display_in_thread(0); ++count;
}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;
}
} }
*/ */
#else #else