From 532c6e1481e78ba0e27c56f4492a7e8d3cc36597 Mon Sep 17 00:00:00 2001 From: Joseph Redmon Date: Tue, 10 Oct 2017 13:35:28 -0700 Subject: [PATCH] hm...... --- examples/detector.c | 2 +- examples/rnn.c | 27 ++++++++++++++++++--------- include/darknet.h | 1 + src/demo.c | 21 ++++++--------------- src/image.c | 33 +++++++++++++++++++++++---------- src/region_layer.c | 8 ++++++++ src/utils.c | 28 ++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 35 deletions(-) diff --git a/examples/detector.c b/examples/detector.c index 4709b898..b414546f 100644 --- a/examples/detector.c +++ b/examples/detector.c @@ -621,7 +621,7 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now()-time); get_region_boxes(l, im.w, im.h, net.w, net.h, thresh, probs, boxes, masks, 0, 0, hier_thresh, 1); - if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms); + if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms); //else if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms); draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, masks, names, alphabet, l.classes); if(outfile){ diff --git a/examples/rnn.c b/examples/rnn.c index 8169f510..994efb3d 100644 --- a/examples/rnn.c +++ b/examples/rnn.c @@ -7,6 +7,20 @@ typedef struct { float *y; } float_pair; +unsigned char **load_files(char *filename, int *n) +{ + list *paths = get_paths(filename); + *n = paths->size; + unsigned char **contents = calloc(*n, sizeof(char *)); + int i; + node *x = paths->front; + for(i = 0; i < *n; ++i){ + contents[i] = read_file((char *)x->val); + x = x->next; + } + return contents; +} + int *read_tokenized_data(char *filename, size_t *read) { size_t size = 512; @@ -86,6 +100,8 @@ float_pair get_seq2seq_data(char **source, char **dest, int n, int characters, s float *y = calloc(batch * steps * characters, sizeof(float)); for(i = 0; i < batch; ++i){ int index = rand()%n; + int slen = strlen(source[index]); + int dlen = strlen(dest[index]); for(j = 0; j < steps; ++j){ unsigned char curr = source[index][j]; unsigned char next = dest[index][j]; @@ -147,15 +163,8 @@ void train_char_rnn(char *cfgfile, char *weightfile, char *filename, int clear, if(tokenized){ tokens = read_tokenized_data(filename, &size); } else { - FILE *fp = fopen(filename, "rb"); - - fseek(fp, 0, SEEK_END); - size = ftell(fp); - fseek(fp, 0, SEEK_SET); - - text = calloc(size+1, sizeof(char)); - fread(text, 1, size, fp); - fclose(fp); + text = read_file(filename); + size = strlen((const char*)text); } char *backup_directory = "/home/pjreddie/backup/"; diff --git a/include/darknet.h b/include/darknet.h index 336ed6f6..0b6eeb06 100644 --- a/include/darknet.h +++ b/include/darknet.h @@ -574,6 +574,7 @@ typedef struct list{ pthread_t load_data(load_args args); list *read_data_cfg(char *filename); list *read_cfg(char *filename); +unsigned char *read_file(char *filename); void forward_network(network net); void backward_network(network net); diff --git a/src/demo.c b/src/demo.c index 28a6ddc6..c8ff8cbc 100644 --- a/src/demo.c +++ b/src/demo.c @@ -38,15 +38,6 @@ static int demo_done = 0; static float *avg; double demo_time; -double get_wall_time() -{ - struct timeval time; - if (gettimeofday(&time,NULL)){ - return 0; - } - return (double)time.tv_sec + (double)time.tv_usec * .000001; -} - void *detect_in_thread(void *ptr) { running = 1; @@ -194,15 +185,15 @@ void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const ch } } - demo_time = get_wall_time(); + 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./(get_wall_time() - demo_time); - demo_time = get_wall_time(); + fps = 1./(what_time_is_it_now() - demo_time); + demo_time = what_time_is_it_now(); display_in_thread(0); }else{ char name[256]; @@ -282,15 +273,15 @@ void demo_compare(char *cfg1, char *weight1, char *cfg2, char *weight2, float th } } - demo_time = get_wall_time(); + 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./(get_wall_time() - demo_time); - demo_time = get_wall_time(); + fps = 1./(what_time_is_it_now() - demo_time); + demo_time = what_time_is_it_now(); display_in_thread(0); }else{ char name[256]; diff --git a/src/image.c b/src/image.c index fc8d08d1..ac1b6296 100644 --- a/src/image.c +++ b/src/image.c @@ -237,21 +237,34 @@ image **load_alphabet() void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes) { - int i; + int i,j; for(i = 0; i < num; ++i){ - int class = max_index(probs[i], classes); - float prob = probs[i][class]; - if(prob > thresh){ + char labelstr[4096] = {0}; + int class = -1; + for(j = 0; j < classes; ++j){ + if (probs[i][j] > thresh){ + if (class < 0) { + strcat(labelstr, names[j]); + class = j; + } else { + strcat(labelstr, ", "); + strcat(labelstr, names[j]); + } + printf("%s: %.0f%%\n", names[j], probs[i][j]*100); + } + } + if(class >= 0){ int width = im.h * .006; - if(0){ - width = pow(prob, 1./2.)*10+1; - alphabet = 0; - } + /* + if(0){ + width = pow(prob, 1./2.)*10+1; + alphabet = 0; + } + */ //printf("%d %s: %.0f%%\n", i, names[class], prob*100); - printf("%s: %.0f%%\n", names[class], prob*100); int offset = class*123457 % classes; float red = get_color(2,offset,classes); float green = get_color(1,offset,classes); @@ -277,7 +290,7 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs, draw_box_width(im, left, top, right, bot, width, red, green, blue); if (alphabet) { - image label = get_label(alphabet, names[class], (im.h*.03)/10); + image label = get_label(alphabet, labelstr, (im.h*.03)/10); draw_label(im, top + width, left, label, rgb); free_image(label); } diff --git a/src/region_layer.c b/src/region_layer.c index ba4cc336..f6f4828c 100644 --- a/src/region_layer.c +++ b/src/region_layer.c @@ -127,6 +127,10 @@ void delta_region_class(float *output, float *delta, int index, int class, int c } *avg_cat += pred; } else { + if (delta[index]){ + delta[index + stride*class] = scale * (1 - output[index + stride*class]); + return; + } for(n = 0; n < classes; ++n){ delta[index + stride*n] = scale * (((n == class)?1 : 0) - output[index + stride*n]); if(n == class) *avg_cat += output[index + stride*n]; @@ -163,6 +167,8 @@ void forward_region_layer(const layer l, network net) activate_array(l.output + index, 2*l.w*l.h, LOGISTIC); index = entry_index(l, b, n*l.w*l.h, l.coords); if(!l.background) activate_array(l.output + index, l.w*l.h, LOGISTIC); + index = entry_index(l, b, n*l.w*l.h, l.coords + 1); + if(!l.softmax && !l.softmax_tree) activate_array(l.output + index, l.classes*l.w*l.h, LOGISTIC); } } if (l.softmax_tree){ @@ -466,6 +472,8 @@ void forward_region_layer_gpu(const layer l, network net) } index = entry_index(l, b, n*l.w*l.h, l.coords); if(!l.background) activate_array_gpu(l.output_gpu + index, l.w*l.h, LOGISTIC); + index = entry_index(l, b, n*l.w*l.h, l.coords + 1); + if(!l.softmax && !l.softmax_tree) activate_array_gpu(l.output_gpu + index, l.classes*l.w*l.h, LOGISTIC); } } if (l.softmax_tree){ diff --git a/src/utils.c b/src/utils.c index 9c06e1a3..b469ec55 100644 --- a/src/utils.c +++ b/src/utils.c @@ -10,6 +10,19 @@ #include "utils.h" + +/* +// old timing. is it better? who knows!! +double get_wall_time() +{ + struct timeval time; + if (gettimeofday(&time,NULL)){ + return 0; + } + return (double)time.tv_sec + (double)time.tv_usec * .000001; +} +*/ + double what_time_is_it_now() { struct timespec now; @@ -225,6 +238,21 @@ void error(const char *s) exit(-1); } +unsigned char *read_file(char *filename) +{ + FILE *fp = fopen(filename, "rb"); + size_t size; + + fseek(fp, 0, SEEK_END); + size = ftell(fp); + fseek(fp, 0, SEEK_SET); + + unsigned char *text = calloc(size+1, sizeof(char)); + fread(text, 1, size, fp); + fclose(fp); + return text; +} + void malloc_error() { fprintf(stderr, "Malloc error\n");