From ec766fc3f114dd4faaa3add500776a7bae957ba5 Mon Sep 17 00:00:00 2001
From: AlexeyAB <alexeyab84@gmail.com>
Date: Tue, 29 May 2018 11:35:32 +0000
Subject: [PATCH] Ignore empty extra lines in train.txt

---
 src/classifier.c |  160 ++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 110 insertions(+), 50 deletions(-)

diff --git a/src/classifier.c b/src/classifier.c
index 5e718c5..b38f2fe 100644
--- a/src/classifier.c
+++ b/src/classifier.c
@@ -21,6 +21,12 @@
 #include "opencv2/videoio/videoio_c.h"
 #endif
 image get_image_from_stream(CvCapture *cap);
+image get_image_from_stream_cpp(CvCapture *cap);
+#include "http_stream.h"
+
+IplImage* draw_train_chart(float max_img_loss, int max_batches, int number_of_lines, int img_size);
+void draw_train_loss(IplImage* img, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches);
+
 #endif
 
 float *get_regression_values(char **labels, int n)
@@ -35,7 +41,7 @@
     return v;
 }
 
-void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
+void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int dont_show)
 {
     int i;
 
@@ -87,6 +93,7 @@
 
     args.min = net.min_crop;
     args.max = net.max_crop;
+    args.flip = net.flip;
     args.angle = net.angle;
     args.aspect = net.aspect;
     args.exposure = net.exposure;
@@ -101,13 +108,23 @@
     args.labels = labels;
     args.type = CLASSIFICATION_DATA;
 
+#ifdef OPENCV
+    args.threads = 3;
+    IplImage* img = NULL;
+    float max_img_loss = 5;
+    int number_of_lines = 100;
+    int img_size = 1000;
+    if (!dont_show)
+        img = draw_train_chart(max_img_loss, net.max_batches, number_of_lines, img_size);
+#endif  //OPENCV
+
     data train;
     data buffer;
     pthread_t load_thread;
     args.d = &buffer;
     load_thread = load_data(args);
 
-    int epoch = (*net.seen)/N;
+    int iter_save = get_current_batch(net);
     while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
         time=clock();
 
@@ -130,24 +147,38 @@
 #endif
         if(avg_loss == -1) avg_loss = loss;
         avg_loss = avg_loss*.9 + loss*.1;
+
+        i = get_current_batch(net);
+
         printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen);
+#ifdef OPENCV
+        if(!dont_show)
+            draw_train_loss(img, img_size, avg_loss, max_img_loss, i, net.max_batches);
+#endif  // OPENCV
+
+        if (i >= (iter_save + 100)) {
+            iter_save = i;
+#ifdef GPU
+            if (ngpus != 1) sync_nets(nets, ngpus, 0);
+#endif            
+            char buff[256];
+            sprintf(buff, "%s/%s_%d.weights",backup_directory,base, i);
+            save_weights(net, buff);
+        }
         free_data(train);
-        if(*net.seen/N > epoch){
-            epoch = *net.seen/N;
-            char buff[256];
-            sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch);
-            save_weights(net, buff);
-        }
-        if(get_current_batch(net)%100 == 0){
-            char buff[256];
-            sprintf(buff, "%s/%s.backup",backup_directory,base);
-            save_weights(net, buff);
-        }
     }
+#ifdef GPU
+    if (ngpus != 1) sync_nets(nets, ngpus, 0);
+#endif    
     char buff[256];
-    sprintf(buff, "%s/%s.weights", backup_directory, base);
+    sprintf(buff, "%s/%s_final.weights", backup_directory, base);
     save_weights(net, buff);
 
+#ifdef OPENCV
+    cvReleaseImage(&img);
+    cvDestroyAllWindows();
+#endif
+
     free_network(net);
     free_ptrs((void**)labels, classes);
     free_ptrs((void**)paths, plist->size);
@@ -193,6 +224,7 @@
 
    args.min = net.min_crop;
    args.max = net.max_crop;
+   args.flip = net.flip;
    args.angle = net.angle;
    args.aspect = net.aspect;
    args.exposure = net.exposure;
@@ -281,6 +313,7 @@
     char *valid_list = option_find_str(options, "valid", "data/train.list");
     int classes = option_find_int(options, "classes", 2);
     int topk = option_find_int(options, "top", 1);
+    if (topk > classes) topk = classes;
 
     char **labels = get_labels(label_list);
     list *plist = get_paths(valid_list);
@@ -349,6 +382,7 @@
     char *valid_list = option_find_str(options, "valid", "data/train.list");
     int classes = option_find_int(options, "classes", 2);
     int topk = option_find_int(options, "top", 1);
+    if (topk > classes) topk = classes;
 
     char **labels = get_labels(label_list);
     list *plist = get_paths(valid_list);
@@ -362,11 +396,11 @@
     int *indexes = calloc(topk, sizeof(int));
 
     for(i = 0; i < m; ++i){
-        int class = -1;
+        int class_id = -1;
         char *path = paths[i];
         for(j = 0; j < classes; ++j){
             if(strstr(path, labels[j])){
-                class = j;
+                class_id = j;
                 break;
             }
         }
@@ -396,9 +430,9 @@
         free_image(im);
         top_k(pred, classes, topk, indexes);
         free(pred);
-        if(indexes[0] == class) avg_acc += 1;
+        if(indexes[0] == class_id) avg_acc += 1;
         for(j = 0; j < topk; ++j){
-            if(indexes[j] == class) avg_topk += 1;
+            if(indexes[j] == class_id) avg_topk += 1;
         }
 
         printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
@@ -421,6 +455,7 @@
     char *valid_list = option_find_str(options, "valid", "data/train.list");
     int classes = option_find_int(options, "classes", 2);
     int topk = option_find_int(options, "top", 1);
+    if (topk > classes) topk = classes;
 
     char **labels = get_labels(label_list);
     list *plist = get_paths(valid_list);
@@ -435,11 +470,11 @@
 
     int size = net.w;
     for(i = 0; i < m; ++i){
-        int class = -1;
+        int class_id = -1;
         char *path = paths[i];
         for(j = 0; j < classes; ++j){
             if(strstr(path, labels[j])){
-                class = j;
+                class_id = j;
                 break;
             }
         }
@@ -456,9 +491,9 @@
         free_image(resized);
         top_k(pred, classes, topk, indexes);
 
-        if(indexes[0] == class) avg_acc += 1;
+        if(indexes[0] == class_id) avg_acc += 1;
         for(j = 0; j < topk; ++j){
-            if(indexes[j] == class) avg_topk += 1;
+            if(indexes[j] == class_id) avg_topk += 1;
         }
 
         printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
@@ -484,6 +519,7 @@
     char *valid_list = option_find_str(options, "valid", "data/train.list");
     int classes = option_find_int(options, "classes", 2);
     int topk = option_find_int(options, "top", 1);
+    if (topk > classes) topk = classes;
 
     char **labels = get_labels(label_list);
     list *plist = get_paths(valid_list);
@@ -497,11 +533,11 @@
     int *indexes = calloc(topk, sizeof(int));
 
     for(i = 0; i < m; ++i){
-        int class = -1;
+        int class_id = -1;
         char *path = paths[i];
         for(j = 0; j < classes; ++j){
             if(strstr(path, labels[j])){
-                class = j;
+                class_id = j;
                 break;
             }
         }
@@ -519,9 +555,9 @@
         free_image(crop);
         top_k(pred, classes, topk, indexes);
 
-        if(indexes[0] == class) avg_acc += 1;
+        if(indexes[0] == class_id) avg_acc += 1;
         for(j = 0; j < topk; ++j){
-            if(indexes[j] == class) avg_topk += 1;
+            if(indexes[j] == class_id) avg_topk += 1;
         }
 
         printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
@@ -544,6 +580,7 @@
     char *valid_list = option_find_str(options, "valid", "data/train.list");
     int classes = option_find_int(options, "classes", 2);
     int topk = option_find_int(options, "top", 1);
+    if (topk > classes) topk = classes;
 
     char **labels = get_labels(label_list);
     list *plist = get_paths(valid_list);
@@ -559,11 +596,11 @@
     int *indexes = calloc(topk, sizeof(int));
 
     for(i = 0; i < m; ++i){
-        int class = -1;
+        int class_id = -1;
         char *path = paths[i];
         for(j = 0; j < classes; ++j){
             if(strstr(path, labels[j])){
-                class = j;
+                class_id = j;
                 break;
             }
         }
@@ -583,9 +620,9 @@
         free_image(im);
         top_k(pred, classes, topk, indexes);
         free(pred);
-        if(indexes[0] == class) avg_acc += 1;
+        if(indexes[0] == class_id) avg_acc += 1;
         for(j = 0; j < topk; ++j){
-            if(indexes[j] == class) avg_topk += 1;
+            if(indexes[j] == class_id) avg_topk += 1;
         }
 
         printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
@@ -594,7 +631,7 @@
 
 void try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int layer_num)
 {
-    network net = parse_network_cfg(cfgfile);
+    network net = parse_network_cfg_custom(cfgfile, 1);
     if(weightfile){
         load_weights(&net, weightfile);
     }
@@ -605,7 +642,9 @@
 
     char *name_list = option_find_str(options, "names", 0);
     if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
+    int classes = option_find_int(options, "classes", 2);
     int top = option_find_int(options, "top", 1);
+    if (top > classes) top = classes;
 
     int i = 0;
     char **names = get_labels(name_list);
@@ -675,7 +714,7 @@
 
 void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top)
 {
-    network net = parse_network_cfg(cfgfile);
+    network net = parse_network_cfg_custom(cfgfile, 1);
     if(weightfile){
         load_weights(&net, weightfile);
     }
@@ -686,7 +725,9 @@
 
     char *name_list = option_find_str(options, "names", 0);
     if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
-    if(top == 0) top = option_find_int(options, "top", 1);
+    int classes = option_find_int(options, "classes", 2);
+    if (top == 0) top = option_find_int(options, "top", 1);
+    if (top > classes) top = classes;
 
     int i = 0;
     char **names = get_labels(name_list);
@@ -706,8 +747,9 @@
             strtok(input, "\n");
         }
         image im = load_image_color(input, 0, 0);
-        image r = resize_min(im, size);
-        resize_network(&net, r.w, r.h);
+        image r = letterbox_image(im, net.w, net.h);
+        //image r = resize_min(im, size);
+        //resize_network(&net, r.w, r.h);
         printf("%d %d\n", r.w, r.h);
 
         float *X = r.data;
@@ -857,13 +899,18 @@
     srand(2222222);
     CvCapture * cap;
 
-    if(filename){
-        cap = cvCaptureFromFile(filename);
-    }else{
-        cap = cvCaptureFromCAM(cam_index);
+    if (filename) {
+        //cap = cvCaptureFromFile(filename);
+        cap = get_capture_video_stream(filename);
+    }
+    else {
+        //cap = cvCaptureFromCAM(cam_index);
+        cap = get_capture_webcam(cam_index);
     }
 
+    int classes = option_find_int(options, "classes", 2);
     int top = option_find_int(options, "top", 1);
+    if (top > classes) top = classes;
 
     char *name_list = option_find_str(options, "names", 0);
     char **names = get_labels(name_list);
@@ -883,7 +930,8 @@
         struct timeval tval_before, tval_after, tval_result;
         gettimeofday(&tval_before, NULL);
 
-        image in = get_image_from_stream(cap);
+        //image in = get_image_from_stream(cap);
+        image in = get_image_from_stream_cpp(cap);
         if(!in.data) break;
         image in_s = resize_image(in, net.w, net.h);
 
@@ -989,13 +1037,18 @@
     srand(2222222);
     CvCapture * cap;
 
-    if(filename){
-        cap = cvCaptureFromFile(filename);
-    }else{
-        cap = cvCaptureFromCAM(cam_index);
+    if (filename) {
+        //cap = cvCaptureFromFile(filename);
+        cap = get_capture_video_stream(filename);
+    }
+    else {
+        //cap = cvCaptureFromCAM(cam_index);
+        cap = get_capture_webcam(cam_index);
     }
 
+    int classes = option_find_int(options, "classes", 2);
     int top = option_find_int(options, "top", 1);
+    if (top > classes) top = classes;
 
     char *name_list = option_find_str(options, "names", 0);
     char **names = get_labels(name_list);
@@ -1012,7 +1065,8 @@
         struct timeval tval_before, tval_after, tval_result;
         gettimeofday(&tval_before, NULL);
 
-        image in = get_image_from_stream(cap);
+        //image in = get_image_from_stream(cap);
+        image in = get_image_from_stream_cpp(cap);
         image in_s = resize_image(in, net.w, net.h);
         show_image(in, "Threat Detection");
 
@@ -1056,7 +1110,7 @@
 {
 #ifdef OPENCV
     printf("Classifier Demo\n");
-    network net = parse_network_cfg(cfgfile);
+    network net = parse_network_cfg_custom(cfgfile, 1);
     if(weightfile){
         load_weights(&net, weightfile);
     }
@@ -1067,12 +1121,16 @@
     CvCapture * cap;
 
     if(filename){
-        cap = cvCaptureFromFile(filename);
+        //cap = cvCaptureFromFile(filename);
+        cap = get_capture_video_stream(filename);
     }else{
-        cap = cvCaptureFromCAM(cam_index);
+        //cap = cvCaptureFromCAM(cam_index);
+        cap = get_capture_webcam(cam_index);
     }
 
+    int classes = option_find_int(options, "classes", 2);
     int top = option_find_int(options, "top", 1);
+    if (top > classes) top = classes;
 
     char *name_list = option_find_str(options, "names", 0);
     char **names = get_labels(name_list);
@@ -1089,7 +1147,8 @@
         struct timeval tval_before, tval_after, tval_result;
         gettimeofday(&tval_before, NULL);
 
-        image in = get_image_from_stream(cap);
+        //image in = get_image_from_stream(cap);
+        image in = get_image_from_stream_cpp(cap);
         image in_s = resize_image(in, net.w, net.h);
         show_image(in, "Classifier");
 
@@ -1150,6 +1209,7 @@
         ngpus = 1;
     }
 
+    int dont_show = find_arg(argc, argv, "-dont_show");
     int cam_index = find_int_arg(argc, argv, "-c", 0);
     int top = find_int_arg(argc, argv, "-t", 0);
     int clear = find_arg(argc, argv, "-clear");
@@ -1161,7 +1221,7 @@
     int layer = layer_s ? atoi(layer_s) : -1;
     if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename, top);
     else if(0==strcmp(argv[2], "try")) try_classifier(data, cfg, weights, filename, atoi(layer_s));
-    else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights, gpus, ngpus, clear);
+    else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights, gpus, ngpus, clear, dont_show);
     else if(0==strcmp(argv[2], "demo")) demo_classifier(data, cfg, weights, cam_index, filename);
     else if(0==strcmp(argv[2], "gun")) gun_classifier(data, cfg, weights, cam_index, filename);
     else if(0==strcmp(argv[2], "threat")) threat_classifier(data, cfg, weights, cam_index, filename);

--
Gitblit v1.10.0