yolo.c
Go to the documentation of this file.
00001 #include "network.h"
00002 #include "detection_layer.h"
00003 #include "cost_layer.h"
00004 #include "utils.h"
00005 #include "parser.h"
00006 #include "box.h"
00007 #include "demo.h"
00008 
00009 #ifdef OPENCV
00010 #include "opencv2/highgui/highgui_c.h"
00011 #endif
00012 
00013 char *voc_names[] = {"aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
00014 
00015 void train_yolo(char *cfgfile, char *weightfile)
00016 {
00017     char *train_images = "/data/voc/train.txt";
00018     char *backup_directory = "/home/pjreddie/backup/";
00019     srand(time(0));
00020     char *base = basecfg(cfgfile);
00021     printf("%s\n", base);
00022     float avg_loss = -1;
00023     network net = parse_network_cfg(cfgfile);
00024     if(weightfile){
00025         load_weights(&net, weightfile);
00026     }
00027     printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00028     int imgs = net.batch*net.subdivisions;
00029     int i = *net.seen/imgs;
00030     data train, buffer;
00031 
00032 
00033     layer l = net.layers[net.n - 1];
00034 
00035     int side = l.side;
00036     int classes = l.classes;
00037     float jitter = l.jitter;
00038 
00039     list *plist = get_paths(train_images);
00040     //int N = plist->size;
00041     char **paths = (char **)list_to_array(plist);
00042 
00043     load_args args = {0};
00044     args.w = net.w;
00045     args.h = net.h;
00046     args.paths = paths;
00047     args.n = imgs;
00048     args.m = plist->size;
00049     args.classes = classes;
00050     args.jitter = jitter;
00051     args.num_boxes = side;
00052     args.d = &buffer;
00053     args.type = REGION_DATA;
00054 
00055     args.angle = net.angle;
00056     args.exposure = net.exposure;
00057     args.saturation = net.saturation;
00058     args.hue = net.hue;
00059 
00060     pthread_t load_thread = load_data_in_thread(args);
00061     clock_t time;
00062     //while(i*imgs < N*120){
00063     while(get_current_batch(net) < net.max_batches){
00064         i += 1;
00065         time=clock();
00066         pthread_join(load_thread, 0);
00067         train = buffer;
00068         load_thread = load_data_in_thread(args);
00069 
00070         printf("Loaded: %lf seconds\n", sec(clock()-time));
00071 
00072         time=clock();
00073         float loss = train_network(net, train);
00074         if (avg_loss < 0) avg_loss = loss;
00075         avg_loss = avg_loss*.9 + loss*.1;
00076 
00077         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);
00078         if(i%1000==0 || (i < 1000 && i%100 == 0)){
00079             char buff[256];
00080             sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
00081             save_weights(net, buff);
00082         }
00083         free_data(train);
00084     }
00085     char buff[256];
00086     sprintf(buff, "%s/%s_final.weights", backup_directory, base);
00087     save_weights(net, buff);
00088 }
00089 
00090 void print_yolo_detections(FILE **fps, char *id, box *boxes, float **probs, int total, int classes, int w, int h)
00091 {
00092     int i, j;
00093     for(i = 0; i < total; ++i){
00094         float xmin = boxes[i].x - boxes[i].w/2.;
00095         float xmax = boxes[i].x + boxes[i].w/2.;
00096         float ymin = boxes[i].y - boxes[i].h/2.;
00097         float ymax = boxes[i].y + boxes[i].h/2.;
00098 
00099         if (xmin < 0) xmin = 0;
00100         if (ymin < 0) ymin = 0;
00101         if (xmax > w) xmax = w;
00102         if (ymax > h) ymax = h;
00103 
00104         for(j = 0; j < classes; ++j){
00105             if (probs[i][j]) fprintf(fps[j], "%s %f %f %f %f %f\n", id, probs[i][j],
00106                     xmin, ymin, xmax, ymax);
00107         }
00108     }
00109 }
00110 
00111 void validate_yolo(char *cfgfile, char *weightfile)
00112 {
00113     network net = parse_network_cfg(cfgfile);
00114     if(weightfile){
00115         load_weights(&net, weightfile);
00116     }
00117     set_batch_network(&net, 1);
00118     fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00119     srand(time(0));
00120 
00121     char *base = "results/comp4_det_test_";
00122     //list *plist = get_paths("data/voc.2007.test");
00123     list *plist = get_paths("/home/pjreddie/data/voc/2007_test.txt");
00124     //list *plist = get_paths("data/voc.2012.test");
00125     char **paths = (char **)list_to_array(plist);
00126 
00127     layer l = net.layers[net.n-1];
00128     int classes = l.classes;
00129 
00130     int j;
00131     FILE **fps = calloc(classes, sizeof(FILE *));
00132     for(j = 0; j < classes; ++j){
00133         char buff[1024];
00134         snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
00135         fps[j] = fopen(buff, "w");
00136     }
00137     box *boxes = calloc(l.side*l.side*l.n, sizeof(box));
00138     float **probs = calloc(l.side*l.side*l.n, sizeof(float *));
00139     for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));
00140 
00141     int m = plist->size;
00142     int i=0;
00143     int t;
00144 
00145     float thresh = .001;
00146     int nms = 1;
00147     float iou_thresh = .5;
00148 
00149     int nthreads = 8;
00150     image *val = calloc(nthreads, sizeof(image));
00151     image *val_resized = calloc(nthreads, sizeof(image));
00152     image *buf = calloc(nthreads, sizeof(image));
00153     image *buf_resized = calloc(nthreads, sizeof(image));
00154     pthread_t *thr = calloc(nthreads, sizeof(pthread_t));
00155 
00156     load_args args = {0};
00157     args.w = net.w;
00158     args.h = net.h;
00159     args.type = IMAGE_DATA;
00160 
00161     for(t = 0; t < nthreads; ++t){
00162         args.path = paths[i+t];
00163         args.im = &buf[t];
00164         args.resized = &buf_resized[t];
00165         thr[t] = load_data_in_thread(args);
00166     }
00167     time_t start = time(0);
00168     for(i = nthreads; i < m+nthreads; i += nthreads){
00169         fprintf(stderr, "%d\n", i);
00170         for(t = 0; t < nthreads && i+t-nthreads < m; ++t){
00171             pthread_join(thr[t], 0);
00172             val[t] = buf[t];
00173             val_resized[t] = buf_resized[t];
00174         }
00175         for(t = 0; t < nthreads && i+t < m; ++t){
00176             args.path = paths[i+t];
00177             args.im = &buf[t];
00178             args.resized = &buf_resized[t];
00179             thr[t] = load_data_in_thread(args);
00180         }
00181         for(t = 0; t < nthreads && i+t-nthreads < m; ++t){
00182             char *path = paths[i+t-nthreads];
00183             char *id = basecfg(path);
00184             float *X = val_resized[t].data;
00185             network_predict(net, X);
00186             int w = val[t].w;
00187             int h = val[t].h;
00188             get_detection_boxes(l, w, h, thresh, probs, boxes, 0);
00189             if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, classes, iou_thresh);
00190             print_yolo_detections(fps, id, boxes, probs, l.side*l.side*l.n, classes, w, h);
00191             free(id);
00192             free_image(val[t]);
00193             free_image(val_resized[t]);
00194         }
00195     }
00196     fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
00197 }
00198 
00199 void validate_yolo_recall(char *cfgfile, char *weightfile)
00200 {
00201     network net = parse_network_cfg(cfgfile);
00202     if(weightfile){
00203         load_weights(&net, weightfile);
00204     }
00205     set_batch_network(&net, 1);
00206     fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00207     srand(time(0));
00208 
00209     char *base = "results/comp4_det_test_";
00210     list *plist = get_paths("data/voc.2007.test");
00211     char **paths = (char **)list_to_array(plist);
00212 
00213     layer l = net.layers[net.n-1];
00214     int classes = l.classes;
00215     int side = l.side;
00216 
00217     int j, k;
00218     FILE **fps = calloc(classes, sizeof(FILE *));
00219     for(j = 0; j < classes; ++j){
00220         char buff[1024];
00221         snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
00222         fps[j] = fopen(buff, "w");
00223     }
00224     box *boxes = calloc(side*side*l.n, sizeof(box));
00225     float **probs = calloc(side*side*l.n, sizeof(float *));
00226     for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));
00227 
00228     int m = plist->size;
00229     int i=0;
00230 
00231     float thresh = .001;
00232     float iou_thresh = .5;
00233     float nms = 0;
00234 
00235     int total = 0;
00236     int correct = 0;
00237     int proposals = 0;
00238     float avg_iou = 0;
00239 
00240     for(i = 0; i < m; ++i){
00241         char *path = paths[i];
00242         image orig = load_image_color(path, 0, 0);
00243         image sized = resize_image(orig, net.w, net.h);
00244         char *id = basecfg(path);
00245         network_predict(net, sized.data);
00246         get_detection_boxes(l, orig.w, orig.h, thresh, probs, boxes, 1);
00247         if (nms) do_nms(boxes, probs, side*side*l.n, 1, nms);
00248 
00249         char labelpath[4096];
00250         find_replace(path, "images", "labels", labelpath);
00251         find_replace(labelpath, "JPEGImages", "labels", labelpath);
00252         find_replace(labelpath, ".jpg", ".txt", labelpath);
00253         find_replace(labelpath, ".JPEG", ".txt", labelpath);
00254 
00255         int num_labels = 0;
00256         box_label *truth = read_boxes(labelpath, &num_labels);
00257         for(k = 0; k < side*side*l.n; ++k){
00258             if(probs[k][0] > thresh){
00259                 ++proposals;
00260             }
00261         }
00262         for (j = 0; j < num_labels; ++j) {
00263             ++total;
00264             box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h};
00265             float best_iou = 0;
00266             for(k = 0; k < side*side*l.n; ++k){
00267                 float iou = box_iou(boxes[k], t);
00268                 if(probs[k][0] > thresh && iou > best_iou){
00269                     best_iou = iou;
00270                 }
00271             }
00272             avg_iou += best_iou;
00273             if(best_iou > iou_thresh){
00274                 ++correct;
00275             }
00276         }
00277 
00278         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);
00279         free(id);
00280         free_image(orig);
00281         free_image(sized);
00282     }
00283 }
00284 
00285 void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh)
00286 {
00287     image **alphabet = load_alphabet();
00288     network net = parse_network_cfg(cfgfile);
00289     if(weightfile){
00290         load_weights(&net, weightfile);
00291     }
00292     detection_layer l = net.layers[net.n-1];
00293     set_batch_network(&net, 1);
00294     srand(2222222);
00295     clock_t time;
00296     char buff[256];
00297     char *input = buff;
00298     int j;
00299     float nms=.4;
00300     box *boxes = calloc(l.side*l.side*l.n, sizeof(box));
00301     float **probs = calloc(l.side*l.side*l.n, sizeof(float *));
00302     for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(l.classes, sizeof(float *));
00303     while(1){
00304         if(filename){
00305             strncpy(input, filename, 256);
00306         } else {
00307             printf("Enter Image Path: ");
00308             fflush(stdout);
00309             input = fgets(input, 256, stdin);
00310             if(!input) return;
00311             strtok(input, "\n");
00312         }
00313         image im = load_image_color(input,0,0);
00314         image sized = resize_image(im, net.w, net.h);
00315         float *X = sized.data;
00316         time=clock();
00317         network_predict(net, X);
00318         printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
00319         get_detection_boxes(l, 1, 1, thresh, probs, boxes, 0);
00320         if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms);
00321         //draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, alphabet, 20);
00322         draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, alphabet, 20);
00323         save_image(im, "predictions");
00324         show_image(im, "predictions");
00325 
00326         free_image(im);
00327         free_image(sized);
00328 #ifdef OPENCV
00329         cvWaitKey(0);
00330         cvDestroyAllWindows();
00331 #endif
00332         if (filename) break;
00333     }
00334 }
00335 
00336 void run_yolo(int argc, char **argv)
00337 {
00338     char *prefix = find_char_arg(argc, argv, "-prefix", 0);
00339     float thresh = find_float_arg(argc, argv, "-thresh", .2);
00340     int cam_index = find_int_arg(argc, argv, "-c", 0);
00341     int frame_skip = find_int_arg(argc, argv, "-s", 0);
00342     if(argc < 4){
00343         fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
00344         return;
00345     }
00346 
00347     char *cfg = argv[3];
00348     char *weights = (argc > 4) ? argv[4] : 0;
00349     char *filename = (argc > 5) ? argv[5]: 0;
00350     if(0==strcmp(argv[2], "test")) test_yolo(cfg, weights, filename, thresh);
00351     else if(0==strcmp(argv[2], "train")) train_yolo(cfg, weights);
00352     else if(0==strcmp(argv[2], "valid")) validate_yolo(cfg, weights);
00353     else if(0==strcmp(argv[2], "recall")) validate_yolo_recall(cfg, weights);
00354     else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, cam_index, filename, voc_names, 20, frame_skip, prefix);
00355 }


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