coco.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "network.h"
00004 #include "detection_layer.h"
00005 #include "cost_layer.h"
00006 #include "utils.h"
00007 #include "parser.h"
00008 #include "box.h"
00009 #include "demo.h"
00010 
00011 #ifdef OPENCV
00012 #include "opencv2/highgui/highgui_c.h"
00013 #endif
00014 
00015 char *coco_classes[] = {"person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"};
00016 
00017 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};
00018 
00019 void train_coco(char *cfgfile, char *weightfile)
00020 {
00021     //char *train_images = "/home/pjreddie/data/voc/test/train.txt";
00022     //char *train_images = "/home/pjreddie/data/coco/train.txt";
00023     char *train_images = "data/coco.trainval.txt";
00024     //char *train_images = "data/bags.train.list";
00025     char *backup_directory = "/home/pjreddie/backup/";
00026     srand(time(0));
00027     char *base = basecfg(cfgfile);
00028     printf("%s\n", base);
00029     float avg_loss = -1;
00030     network net = parse_network_cfg(cfgfile);
00031     if(weightfile){
00032         load_weights(&net, weightfile);
00033     }
00034     printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00035     int imgs = net.batch*net.subdivisions;
00036     int i = *net.seen/imgs;
00037     data train, buffer;
00038 
00039 
00040     layer l = net.layers[net.n - 1];
00041 
00042     int side = l.side;
00043     int classes = l.classes;
00044     float jitter = l.jitter;
00045 
00046     list *plist = get_paths(train_images);
00047     //int N = plist->size;
00048     char **paths = (char **)list_to_array(plist);
00049 
00050     load_args args = {0};
00051     args.w = net.w;
00052     args.h = net.h;
00053     args.paths = paths;
00054     args.n = imgs;
00055     args.m = plist->size;
00056     args.classes = classes;
00057     args.jitter = jitter;
00058     args.num_boxes = side;
00059     args.d = &buffer;
00060     args.type = REGION_DATA;
00061 
00062     args.angle = net.angle;
00063     args.exposure = net.exposure;
00064     args.saturation = net.saturation;
00065     args.hue = net.hue;
00066 
00067     pthread_t load_thread = load_data_in_thread(args);
00068     clock_t time;
00069     //while(i*imgs < N*120){
00070     while(get_current_batch(net) < net.max_batches){
00071         i += 1;
00072         time=clock();
00073         pthread_join(load_thread, 0);
00074         train = buffer;
00075         load_thread = load_data_in_thread(args);
00076 
00077         printf("Loaded: %lf seconds\n", sec(clock()-time));
00078 
00079         /*
00080            image im = float_to_image(net.w, net.h, 3, train.X.vals[113]);
00081            image copy = copy_image(im);
00082            draw_coco(copy, train.y.vals[113], 7, "truth");
00083            cvWaitKey(0);
00084            free_image(copy);
00085          */
00086 
00087         time=clock();
00088         float loss = train_network(net, train);
00089         if (avg_loss < 0) avg_loss = loss;
00090         avg_loss = avg_loss*.9 + loss*.1;
00091 
00092         printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs);
00093         if(i%1000==0 || (i < 1000 && i%100 == 0)){
00094             char buff[256];
00095             sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
00096             save_weights(net, buff);
00097         }
00098         if(i%100==0){
00099             char buff[256];
00100             sprintf(buff, "%s/%s.backup", backup_directory, base);
00101             save_weights(net, buff);
00102         }
00103         free_data(train);
00104     }
00105     char buff[256];
00106     sprintf(buff, "%s/%s_final.weights", backup_directory, base);
00107     save_weights(net, buff);
00108 }
00109 
00110 void print_cocos(FILE *fp, int image_id, box *boxes, float **probs, int num_boxes, int classes, int w, int h)
00111 {
00112     int i, j;
00113     for(i = 0; i < num_boxes; ++i){
00114         float xmin = boxes[i].x - boxes[i].w/2.;
00115         float xmax = boxes[i].x + boxes[i].w/2.;
00116         float ymin = boxes[i].y - boxes[i].h/2.;
00117         float ymax = boxes[i].y + boxes[i].h/2.;
00118 
00119         if (xmin < 0) xmin = 0;
00120         if (ymin < 0) ymin = 0;
00121         if (xmax > w) xmax = w;
00122         if (ymax > h) ymax = h;
00123 
00124         float bx = xmin;
00125         float by = ymin;
00126         float bw = xmax - xmin;
00127         float bh = ymax - ymin;
00128 
00129         for(j = 0; j < classes; ++j){
00130             if (probs[i][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, probs[i][j]);
00131         }
00132     }
00133 }
00134 
00135 int get_coco_image_id(char *filename)
00136 {
00137     char *p = strrchr(filename, '_');
00138     return atoi(p+1);
00139 }
00140 
00141 void validate_coco(char *cfgfile, char *weightfile)
00142 {
00143     network net = parse_network_cfg(cfgfile);
00144     if(weightfile){
00145         load_weights(&net, weightfile);
00146     }
00147     set_batch_network(&net, 1);
00148     fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00149     srand(time(0));
00150 
00151     char *base = "results/";
00152     list *plist = get_paths("data/coco_val_5k.list");
00153     //list *plist = get_paths("/home/pjreddie/data/people-art/test.txt");
00154     //list *plist = get_paths("/home/pjreddie/data/voc/test/2007_test.txt");
00155     char **paths = (char **)list_to_array(plist);
00156 
00157     layer l = net.layers[net.n-1];
00158     int classes = l.classes;
00159     int side = l.side;
00160 
00161     int j;
00162     char buff[1024];
00163     snprintf(buff, 1024, "%s/coco_results.json", base);
00164     FILE *fp = fopen(buff, "w");
00165     fprintf(fp, "[\n");
00166 
00167     box *boxes = calloc(side*side*l.n, sizeof(box));
00168     float **probs = calloc(side*side*l.n, sizeof(float *));
00169     for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));
00170 
00171     int m = plist->size;
00172     int i=0;
00173     int t;
00174 
00175     float thresh = .01;
00176     int nms = 1;
00177     float iou_thresh = .5;
00178 
00179     int nthreads = 8;
00180     image *val = calloc(nthreads, sizeof(image));
00181     image *val_resized = calloc(nthreads, sizeof(image));
00182     image *buf = calloc(nthreads, sizeof(image));
00183     image *buf_resized = calloc(nthreads, sizeof(image));
00184     pthread_t *thr = calloc(nthreads, sizeof(pthread_t));
00185 
00186     load_args args = {0};
00187     args.w = net.w;
00188     args.h = net.h;
00189     args.type = IMAGE_DATA;
00190 
00191     for(t = 0; t < nthreads; ++t){
00192         args.path = paths[i+t];
00193         args.im = &buf[t];
00194         args.resized = &buf_resized[t];
00195         thr[t] = load_data_in_thread(args);
00196     }
00197     time_t start = time(0);
00198     for(i = nthreads; i < m+nthreads; i += nthreads){
00199         fprintf(stderr, "%d\n", i);
00200         for(t = 0; t < nthreads && i+t-nthreads < m; ++t){
00201             pthread_join(thr[t], 0);
00202             val[t] = buf[t];
00203             val_resized[t] = buf_resized[t];
00204         }
00205         for(t = 0; t < nthreads && i+t < m; ++t){
00206             args.path = paths[i+t];
00207             args.im = &buf[t];
00208             args.resized = &buf_resized[t];
00209             thr[t] = load_data_in_thread(args);
00210         }
00211         for(t = 0; t < nthreads && i+t-nthreads < m; ++t){
00212             char *path = paths[i+t-nthreads];
00213             int image_id = get_coco_image_id(path);
00214             float *X = val_resized[t].data;
00215             network_predict(net, X);
00216             int w = val[t].w;
00217             int h = val[t].h;
00218             get_detection_boxes(l, w, h, thresh, probs, boxes, 0);
00219             if (nms) do_nms_sort(boxes, probs, side*side*l.n, classes, iou_thresh);
00220             print_cocos(fp, image_id, boxes, probs, side*side*l.n, classes, w, h);
00221             free_image(val[t]);
00222             free_image(val_resized[t]);
00223         }
00224     }
00225     fseek(fp, -2, SEEK_CUR); 
00226     fprintf(fp, "\n]\n");
00227     fclose(fp);
00228 
00229     fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
00230 }
00231 
00232 void validate_coco_recall(char *cfgfile, char *weightfile)
00233 {
00234     network net = parse_network_cfg(cfgfile);
00235     if(weightfile){
00236         load_weights(&net, weightfile);
00237     }
00238     set_batch_network(&net, 1);
00239     fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00240     srand(time(0));
00241 
00242     char *base = "results/comp4_det_test_";
00243     list *plist = get_paths("/home/pjreddie/data/voc/test/2007_test.txt");
00244     char **paths = (char **)list_to_array(plist);
00245 
00246     layer l = net.layers[net.n-1];
00247     int classes = l.classes;
00248     int side = l.side;
00249 
00250     int j, k;
00251     FILE **fps = calloc(classes, sizeof(FILE *));
00252     for(j = 0; j < classes; ++j){
00253         char buff[1024];
00254         snprintf(buff, 1024, "%s%s.txt", base, coco_classes[j]);
00255         fps[j] = fopen(buff, "w");
00256     }
00257     box *boxes = calloc(side*side*l.n, sizeof(box));
00258     float **probs = calloc(side*side*l.n, sizeof(float *));
00259     for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));
00260 
00261     int m = plist->size;
00262     int i=0;
00263 
00264     float thresh = .001;
00265     int nms = 0;
00266     float iou_thresh = .5;
00267     float nms_thresh = .5;
00268 
00269     int total = 0;
00270     int correct = 0;
00271     int proposals = 0;
00272     float avg_iou = 0;
00273 
00274     for(i = 0; i < m; ++i){
00275         char *path = paths[i];
00276         image orig = load_image_color(path, 0, 0);
00277         image sized = resize_image(orig, net.w, net.h);
00278         char *id = basecfg(path);
00279         network_predict(net, sized.data);
00280         get_detection_boxes(l, 1, 1, thresh, probs, boxes, 1);
00281         if (nms) do_nms(boxes, probs, side*side*l.n, 1, nms_thresh);
00282 
00283         char labelpath[4096];
00284         find_replace(path, "images", "labels", labelpath);
00285         find_replace(labelpath, "JPEGImages", "labels", labelpath);
00286         find_replace(labelpath, ".jpg", ".txt", labelpath);
00287         find_replace(labelpath, ".JPEG", ".txt", labelpath);
00288 
00289         int num_labels = 0;
00290         box_label *truth = read_boxes(labelpath, &num_labels);
00291         for(k = 0; k < side*side*l.n; ++k){
00292             if(probs[k][0] > thresh){
00293                 ++proposals;
00294             }
00295         }
00296         for (j = 0; j < num_labels; ++j) {
00297             ++total;
00298             box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h};
00299             float best_iou = 0;
00300             for(k = 0; k < side*side*l.n; ++k){
00301                 float iou = box_iou(boxes[k], t);
00302                 if(probs[k][0] > thresh && iou > best_iou){
00303                     best_iou = iou;
00304                 }
00305             }
00306             avg_iou += best_iou;
00307             if(best_iou > iou_thresh){
00308                 ++correct;
00309             }
00310         }
00311 
00312         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);
00313         free(id);
00314         free_image(orig);
00315         free_image(sized);
00316     }
00317 }
00318 
00319 void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
00320 {
00321     image **alphabet = load_alphabet();
00322     network net = parse_network_cfg(cfgfile);
00323     if(weightfile){
00324         load_weights(&net, weightfile);
00325     }
00326     detection_layer l = net.layers[net.n-1];
00327     set_batch_network(&net, 1);
00328     srand(2222222);
00329     float nms = .4;
00330     clock_t time;
00331     char buff[256];
00332     char *input = buff;
00333     int j;
00334     box *boxes = calloc(l.side*l.side*l.n, sizeof(box));
00335     float **probs = calloc(l.side*l.side*l.n, sizeof(float *));
00336     for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(l.classes, sizeof(float *));
00337     while(1){
00338         if(filename){
00339             strncpy(input, filename, 256);
00340         } else {
00341             printf("Enter Image Path: ");
00342             fflush(stdout);
00343             input = fgets(input, 256, stdin);
00344             if(!input) return;
00345             strtok(input, "\n");
00346         }
00347         image im = load_image_color(input,0,0);
00348         image sized = resize_image(im, net.w, net.h);
00349         float *X = sized.data;
00350         time=clock();
00351         network_predict(net, X);
00352         printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
00353         get_detection_boxes(l, 1, 1, thresh, probs, boxes, 0);
00354         if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms);
00355         draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, coco_classes, alphabet, 80);
00356         save_image(im, "prediction");
00357         show_image(im, "predictions");
00358         free_image(im);
00359         free_image(sized);
00360 #ifdef OPENCV
00361         cvWaitKey(0);
00362         cvDestroyAllWindows();
00363 #endif
00364         if (filename) break;
00365     }
00366 }
00367 
00368 void run_coco(int argc, char **argv)
00369 {
00370     char *prefix = find_char_arg(argc, argv, "-prefix", 0);
00371     float thresh = find_float_arg(argc, argv, "-thresh", .2);
00372     int cam_index = find_int_arg(argc, argv, "-c", 0);
00373     int frame_skip = find_int_arg(argc, argv, "-s", 0);
00374 
00375     if(argc < 4){
00376         fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
00377         return;
00378     }
00379 
00380     char *cfg = argv[3];
00381     char *weights = (argc > 4) ? argv[4] : 0;
00382     char *filename = (argc > 5) ? argv[5]: 0;
00383     if(0==strcmp(argv[2], "test")) test_coco(cfg, weights, filename, thresh);
00384     else if(0==strcmp(argv[2], "train")) train_coco(cfg, weights);
00385     else if(0==strcmp(argv[2], "valid")) validate_coco(cfg, weights);
00386     else if(0==strcmp(argv[2], "recall")) validate_coco_recall(cfg, weights);
00387     else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, cam_index, filename, coco_classes, 80, frame_skip, prefix);
00388 }


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:29