AlexeyAB
2018-04-12 fc496d52bf22a0bb257300d3c79be9cd80e722cb
src/detector.c
@@ -88,7 +88,7 @@
    args.classes = classes;
    args.jitter = jitter;
    args.num_boxes = l.max_boxes;
   args.small_object = l.small_object;
   args.small_object = net.small_object;
    args.d = &buffer;
    args.type = DETECTION_DATA;
   args.threads = 64;   // 8
@@ -385,7 +385,7 @@
         int nboxes = 0;
         int letterbox = (args.type == LETTERBOX_DATA);
         detection *dets = get_network_boxes(&net, w, h, thresh, .5, map, 0, &nboxes, letterbox);
         if (nms) do_nms_sort_v3(dets, nboxes, classes, nms);
         if (nms) do_nms_sort(dets, nboxes, classes, nms);
         if (coco) {
            print_cocos(fp, path, dets, nboxes, classes, w, h);
         }
@@ -419,6 +419,7 @@
      load_weights(&net, weightfile);
   }
   set_batch_network(&net, 1);
   fuse_conv_batchnorm(net);
   srand(time(0));
   //list *plist = get_paths("data/coco_val_5k.list");
@@ -452,7 +453,7 @@
      int nboxes = 0;
      int letterbox = 0;
      detection *dets = get_network_boxes(&net, sized.w, sized.h, thresh, .5, 0, 1, &nboxes, letterbox);
      if (nms) do_nms_obj_v3(dets, nboxes, 1, nms);
      if (nms) do_nms_obj(dets, nboxes, 1, nms);
      char labelpath[4096];
      find_replace(path, "images", "labels", labelpath);
@@ -471,7 +472,7 @@
         ++total;
         box t = { truth[j].x, truth[j].y, truth[j].w, truth[j].h };
         float best_iou = 0;
         for (k = 0; k < l.w*l.h*l.n; ++k) {
         for (k = 0; k < nboxes; ++k) {
            float iou = box_iou(dets[k].bbox, t);
            if (dets[k].objectness > thresh && iou > best_iou) {
               best_iou = iou;
@@ -526,6 +527,7 @@
      load_weights(&net, weightfile);
   }
   set_batch_network(&net, 1);
   fuse_conv_batchnorm(net);
   srand(time(0));
   list *plist = get_paths(valid_images);
@@ -603,8 +605,9 @@
         int nboxes = 0;
         int letterbox = (args.type == LETTERBOX_DATA);
         float hier_thresh = 0;
         detection *dets = get_network_boxes(&net, 1, 1, thresh, hier_thresh, 0, 1, &nboxes, letterbox);
         if (nms) do_nms_sort_v3(dets, nboxes, l.classes, nms);
         detection *dets = get_network_boxes(&net, 1, 1, thresh, hier_thresh, 0, 0, &nboxes, letterbox);
         //detection *dets = get_network_boxes(&net, val[t].w, val[t].h, thresh, hier_thresh, 0, 1, &nboxes, letterbox); // for letterbox=1
         if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
         char labelpath[4096];
         find_replace(path, "images", "labels", labelpath);
@@ -820,6 +823,20 @@
}
#ifdef OPENCV
typedef struct {
   float w, h;
} anchors_t;
int anchors_comparator(const void *pa, const void *pb)
{
   anchors_t a = *(anchors_t *)pa;
   anchors_t b = *(anchors_t *)pb;
   float diff = b.w*b.h - a.w*a.h;
   if (diff < 0) return 1;
   else if (diff > 0) return -1;
   return 0;
}
void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int show)
{
   printf("\n num_of_clusters = %d, width = %d, height = %d \n", num_of_clusters, width, height);
@@ -886,7 +903,10 @@
      cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10000, 0), attemps, 
      0, KMEANS_PP_CENTERS,
      centers, &compactness);
   // sort anchors
   qsort(centers->data.fl, num_of_clusters, 2*sizeof(float), anchors_comparator);
   //orig 2.0 anchors = 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52
   //float orig_anch[] = { 1.08,1.19,  3.42,4.41,  6.63,11.38,  9.42,5.11,  16.62,10.52 };
   // worse than ours (even for 19x19 final size - for input size 608x608)
@@ -943,9 +963,12 @@
   printf("anchors = ");
   for (i = 0; i < num_of_clusters; ++i) {
      sprintf(buff, "%2.4f,%2.4f", centers->data.fl[i * 2], centers->data.fl[i * 2 + 1]);
      printf("%s, ", buff);
      printf("%s", buff);
      fwrite(buff, sizeof(char), strlen(buff), fw);
      if (i + 1 < num_of_clusters) fwrite(", ", sizeof(char), 2, fw);;
      if (i + 1 < num_of_clusters) {
         fwrite(", ", sizeof(char), 2, fw);
         printf(", ");
      }
   }
   printf("\n");
   fclose(fw);
@@ -1002,6 +1025,7 @@
        load_weights(&net, weightfile);
    }
    set_batch_network(&net, 1);
   fuse_conv_batchnorm(net);
    srand(2222222);
    clock_t time;
    char buff[256];
@@ -1032,13 +1056,14 @@
        float *X = sized.data;
        time=clock();
        network_predict(net, X);
      //network_predict_image(&net, im);
        printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
        //get_region_boxes(l, 1, 1, thresh, probs, boxes, 0, 0);
      // if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms);
      // if (nms) do_nms_sort_v2(boxes, probs, l.w*l.h*l.n, l.classes, nms);
      //draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, names, alphabet, l.classes);
      int nboxes = 0;
      detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox);
      if (nms) do_nms_sort_v3(dets, nboxes, l.classes, nms);
      if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
      draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes);
      free_detections(dets, nboxes);
        save_image(im, "predictions");
@@ -1126,4 +1151,5 @@
        demo(cfg, weights, thresh, hier_thresh, cam_index, filename, names, classes, frame_skip, prefix, out_filename,
         http_stream_port, dont_show);
    }
   else printf(" There isn't such command: %s", argv[2]);
}