3 static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};
6 void train_detector(
char *datacfg,
char *cfgfile,
char *weightfile,
int *gpus,
int ngpus,
int clear)
9 char *train_images =
option_find_str(options,
"train",
"data/train.list");
10 char *backup_directory =
option_find_str(options,
"backup",
"/backup/");
21 for(i = 0; i < ngpus; ++i){
63 if(l.
random && count++%10 == 0){
65 int dim = (rand() % 10 + 10) * 32;
72 pthread_join(load_thread, 0);
77 #pragma omp parallel for 78 for(i = 0; i < ngpus; ++i){
84 pthread_join(load_thread, 0);
120 loss = train_networks(nets, ngpus, train, 4);
125 if (avg_loss < 0) avg_loss = loss;
126 avg_loss = avg_loss*.9 + loss*.1;
129 printf(
"%ld: %f, %f avg, %f rate, %lf seconds, %d images\n",
get_current_batch(net), loss, avg_loss,
get_current_rate(net),
what_time_is_it_now()-time, i*imgs);
132 if(ngpus != 1) sync_nets(nets, ngpus, 0);
135 sprintf(buff,
"%s/%s.backup", backup_directory, base);
138 if(i%10000==0 || (i < 1000 && i%100 == 0)){
140 if(ngpus != 1) sync_nets(nets, ngpus, 0);
143 sprintf(buff,
"%s/%s_%d.weights", backup_directory, base, i);
149 if(ngpus != 1) sync_nets(nets, ngpus, 0);
152 sprintf(buff,
"%s/%s_final.weights", backup_directory, base);
159 char *p = strrchr(filename,
'/');
160 char *c = strrchr(filename,
'_');
165 static void print_cocos(FILE *fp,
char *image_path,
detection *dets,
int num_boxes,
int classes,
int w,
int h)
169 for(i = 0; i < num_boxes; ++i){
170 float xmin = dets[i].
bbox.
x - dets[i].
bbox.
w/2.;
171 float xmax = dets[i].
bbox.
x + dets[i].
bbox.
w/2.;
172 float ymin = dets[i].
bbox.
y - dets[i].
bbox.
h/2.;
173 float ymax = dets[i].
bbox.
y + dets[i].
bbox.
h/2.;
175 if (xmin < 0) xmin = 0;
176 if (ymin < 0) ymin = 0;
177 if (xmax > w) xmax = w;
178 if (ymax > h) ymax = h;
182 float bw = xmax - xmin;
183 float bh = ymax - ymin;
186 if (dets[i].prob[j]) fprintf(fp,
"{\"image_id\":%d, \"category_id\":%d, \"bbox\":[%f, %f, %f, %f], \"score\":%f},\n", image_id,
coco_ids[j], bx, by, bw, bh, dets[i].prob[j]);
194 for(i = 0; i < total; ++i){
195 float xmin = dets[i].
bbox.
x - dets[i].
bbox.
w/2. + 1;
196 float xmax = dets[i].
bbox.
x + dets[i].
bbox.
w/2. + 1;
197 float ymin = dets[i].
bbox.
y - dets[i].
bbox.
h/2. + 1;
198 float ymax = dets[i].
bbox.
y + dets[i].
bbox.
h/2. + 1;
200 if (xmin < 1) xmin = 1;
201 if (ymin < 1) ymin = 1;
202 if (xmax > w) xmax = w;
203 if (ymax > h) ymax = h;
206 if (dets[i].prob[j]) fprintf(fps[j],
"%s %f %f %f %f %f\n",
id, dets[i].prob[j],
207 xmin, ymin, xmax, ymax);
215 for(i = 0; i < total; ++i){
216 float xmin = dets[i].
bbox.
x - dets[i].
bbox.
w/2.;
217 float xmax = dets[i].
bbox.
x + dets[i].
bbox.
w/2.;
218 float ymin = dets[i].
bbox.
y - dets[i].
bbox.
h/2.;
219 float ymax = dets[i].
bbox.
y + dets[i].
bbox.
h/2.;
221 if (xmin < 0) xmin = 0;
222 if (ymin < 0) ymin = 0;
223 if (xmax > w) xmax = w;
224 if (ymax > h) ymax = h;
228 if (dets[i].prob[
class]) fprintf(fp,
"%d %d %f %f %f %f %f\n",
id, j+1, dets[i].prob[
class],
229 xmin, ymin, xmax, ymax);
238 char *valid_images =
option_find_str(options,
"valid",
"data/train.list");
239 char *name_list =
option_find_str(options,
"names",
"data/names.list");
263 if(0==strcmp(type,
"coco")){
264 if(!outfile) outfile =
"coco_results";
265 snprintf(buff, 1024,
"%s/%s.json", prefix, outfile);
266 fp = fopen(buff,
"w");
269 }
else if(0==strcmp(type,
"imagenet")){
270 if(!outfile) outfile =
"imagenet-detection";
271 snprintf(buff, 1024,
"%s/%s.txt", prefix, outfile);
272 fp = fopen(buff,
"w");
276 if(!outfile) outfile =
"comp4_det_test_";
277 fps = calloc(classes,
sizeof(FILE *));
279 snprintf(buff, 1024,
"%s/%s%s.txt", prefix, outfile, names[j]);
280 fps[j] = fopen(buff,
"w");
293 image *val_resized = calloc(nthreads,
sizeof(
image));
295 image *buf_resized = calloc(nthreads,
sizeof(
image));
296 pthread_t *thr = calloc(nthreads,
sizeof(pthread_t));
306 for(t = 0; t < nthreads; ++
t){
307 args.
path = paths[i+
t];
313 for(i = nthreads; i < m+nthreads; i += nthreads){
314 fprintf(stderr,
"%d\n", i);
315 for(t = 0; t < nthreads && i+t-nthreads < m; ++
t){
316 pthread_join(thr[t], 0);
318 val_resized[
t] = buf_resized[
t];
320 for(t = 0; t < nthreads && i+t < m; ++
t){
321 args.
path = paths[i+
t];
326 for(t = 0; t < nthreads && i+t-nthreads < m; ++
t){
327 char *path = paths[i+t-nthreads];
340 print_cocos(fp, path, dets, num, classes, w, h);
341 }
else if (imagenet){
353 if(fps) fclose(fps[j]);
356 fseek(fp, -2, SEEK_CUR);
357 fprintf(fp,
"\n]\n");
368 char *valid_images =
option_find_str(options,
"valid",
"data/train.list");
369 char *name_list =
option_find_str(options,
"names",
"data/names.list");
393 if(0==strcmp(type,
"coco")){
394 if(!outfile) outfile =
"coco_results";
395 snprintf(buff, 1024,
"%s/%s.json", prefix, outfile);
396 fp = fopen(buff,
"w");
399 }
else if(0==strcmp(type,
"imagenet")){
400 if(!outfile) outfile =
"imagenet-detection";
401 snprintf(buff, 1024,
"%s/%s.txt", prefix, outfile);
402 fp = fopen(buff,
"w");
406 if(!outfile) outfile =
"comp4_det_test_";
407 fps = calloc(classes,
sizeof(FILE *));
409 snprintf(buff, 1024,
"%s/%s%s.txt", prefix, outfile, names[j]);
410 fps[j] = fopen(buff,
"w");
424 image *val_resized = calloc(nthreads,
sizeof(
image));
426 image *buf_resized = calloc(nthreads,
sizeof(
image));
427 pthread_t *thr = calloc(nthreads,
sizeof(pthread_t));
435 for(t = 0; t < nthreads; ++
t){
436 args.
path = paths[i+
t];
442 for(i = nthreads; i < m+nthreads; i += nthreads){
443 fprintf(stderr,
"%d\n", i);
444 for(t = 0; t < nthreads && i+t-nthreads < m; ++
t){
445 pthread_join(thr[t], 0);
447 val_resized[
t] = buf_resized[
t];
449 for(t = 0; t < nthreads && i+t < m; ++
t){
450 args.
path = paths[i+
t];
455 for(t = 0; t < nthreads && i+t-nthreads < m; ++
t){
456 char *path = paths[i+t-nthreads];
458 float *X = val_resized[
t].
data;
466 print_cocos(fp, path, dets, nboxes, classes, w, h);
467 }
else if (imagenet){
479 if(fps) fclose(fps[j]);
482 fseek(fp, -2, SEEK_CUR);
483 fprintf(fp,
"\n]\n");
507 float iou_thresh = .5;
515 for(i = 0; i < m; ++i){
516 char *path = paths[i];
525 char labelpath[4096];
527 find_replace(labelpath,
"JPEGImages",
"labels", labelpath);
533 for(k = 0; k < nboxes; ++k){
534 if(dets[k].objectness > thresh){
538 for (j = 0; j < num_labels; ++j) {
540 box t = {truth[j].
x, truth[j].
y, truth[j].
w, truth[j].
h};
542 for(k = 0; k < l.
w*l.
h*l.
n; ++k){
543 float iou =
box_iou(dets[k].bbox, t);
544 if(dets[k].objectness > thresh && iou > best_iou){
549 if(best_iou > iou_thresh){
554 fprintf(stderr,
"%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (
float)proposals/(i+1), avg_iou*100/total, 100.*correct/total);
562 void test_detector(
char *datacfg,
char *cfgfile,
char *weightfile,
char *filename,
float thresh,
float hier_thresh,
char *outfile,
int fullscreen)
565 char *name_list =
option_find_str(options,
"names",
"data/names.list");
578 strncpy(input, filename, 256);
580 printf(
"Enter Image Path: ");
582 input = fgets(input, 256, stdin);
595 float *X = sized.
data;
612 cvNamedWindow(
"predictions", CV_WINDOW_NORMAL);
614 cvSetWindowProperty(
"predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
801 fprintf(stderr,
"usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
810 printf(
"%s\n", gpu_list);
811 int len = strlen(gpu_list);
814 for(i = 0; i < len; ++i){
815 if (gpu_list[i] ==
',') ++ngpus;
817 gpus = calloc(ngpus,
sizeof(
int));
818 for(i = 0; i < ngpus; ++i){
819 gpus[i] = atoi(gpu_list);
820 gpu_list = strchr(gpu_list,
',')+1;
828 int clear =
find_arg(argc, argv,
"-clear");
829 int fullscreen =
find_arg(argc, argv,
"-fullscreen");
835 char *datacfg = argv[3];
837 char *weights = (argc > 5) ? argv[5] : 0;
838 char *filename = (argc > 6) ? argv[6]: 0;
839 if(0==strcmp(argv[2],
"test"))
test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, outfile, fullscreen);
840 else if(0==strcmp(argv[2],
"train"))
train_detector(datacfg, cfg, weights, gpus, ngpus, clear);
841 else if(0==strcmp(argv[2],
"valid"))
validate_detector(datacfg, cfg, weights, outfile);
844 else if(0==strcmp(argv[2],
"demo")) {
847 char *name_list =
option_find_str(options,
"names",
"data/names.list");
849 demo(cfg, weights, thresh, cam_index, filename, names, classes, frame_skip, prefix, avg, hier_thresh, width, height, fps, fullscreen);
void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
image resize_image(image im, int w, int h)
pthread_t load_data_in_thread(load_args args)
int find_arg(int argc, char *argv[], char *arg)
load_args get_base_args(network *net)
void set_batch_network(network *net, int b)
box_label * read_boxes(char *filename, int *n)
void validate_detector_recall(char *cfgfile, char *weightfile)
void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix, int avg, float hier_thresh, int w, int h, int fps, int fullscreen)
int show_image(image p, const char *name, int ms)
char * find_char_arg(int argc, char **argv, char *arg, char *def)
char * basecfg(char *cfgfile)
void ** list_to_array(list *l)
char * option_find_str(list *l, char *key, char *def)
float train_network(network *net, data d)
void save_image(image p, const char *name)
void run_detector(int argc, char **argv)
int get_coco_image_id(char *filename)
void save_weights(network *net, char *filename)
int resize_network(network *net, int w, int h)
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
image load_image_color(char *filename, int w, int h)
float find_float_arg(int argc, char **argv, char *arg, float def)
float get_current_rate(network *net)
int find_int_arg(int argc, char **argv, char *arg, int def)
network * load_network(char *cfg, char *weights, int clear)
char ** get_labels(char *filename)
void print_detector_detections(FILE **fps, char *id, detection *dets, int total, int classes, int w, int h)
void * load_thread(void *ptr)
float box_iou(box a, box b)
void cuda_set_device(int n)
list * read_data_cfg(char *filename)
void copy_cpu(int N, float *X, int INCX, float *Y, int INCY)
int * read_map(char *filename)
size_t get_current_batch(network *net)
void draw_detections(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes)
void find_replace(char *str, char *orig, char *rep, char *output)
void validate_detector_flip(char *datacfg, char *cfgfile, char *weightfile, char *outfile)
void print_imagenet_detections(FILE *fp, int id, detection *dets, int total, int classes, int w, int h)
list * get_paths(char *filename)
void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *outfile)
pthread_t load_data(load_args args)
int option_find_int(list *l, char *key, int def)
double what_time_is_it_now()