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 |  721 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 620 insertions(+), 101 deletions(-)

diff --git a/src/classifier.c b/src/classifier.c
index 24b28b5..b38f2fe 100644
--- a/src/classifier.c
+++ b/src/classifier.c
@@ -3,56 +3,74 @@
 #include "parser.h"
 #include "option_list.h"
 #include "blas.h"
+#include "assert.h"
 #include "classifier.h"
+#include "cuda.h"
+#ifdef WIN32
+#include <time.h>
+#include <winsock.h>
+#include "gettimeofday.h"
+#else
 #include <sys/time.h>
+#endif
 
 #ifdef OPENCV
 #include "opencv2/highgui/highgui_c.h"
+#include "opencv2/core/version.hpp"
+#ifndef CV_VERSION_EPOCH
+#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
 
-list *read_data_cfg(char *filename)
+float *get_regression_values(char **labels, int n)
 {
-    FILE *file = fopen(filename, "r");
-    if(file == 0) file_error(filename);
-    char *line;
-    int nu = 0;
-    list *options = make_list();
-    while((line=fgetl(file)) != 0){
-        ++ nu;
-        strip(line);
-        switch(line[0]){
-            case '\0':
-            case '#':
-            case ';':
-                free(line);
-                break;
-            default:
-                if(!read_option(line, options)){
-                    fprintf(stderr, "Config file error line %d, could parse: %s\n", nu, line);
-                    free(line);
-                }
-                break;
-        }
+    float *v = calloc(n, sizeof(float));
+    int i;
+    for(i = 0; i < n; ++i){
+        char *p = strchr(labels[i], ' ');
+        *p = 0;
+        v[i] = atof(p+1);
     }
-    fclose(file);
-    return options;
+    return v;
 }
 
-void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int clear)
+void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int dont_show)
 {
-    data_seed = time(0);
-    srand(time(0));
+    int i;
+
     float avg_loss = -1;
     char *base = basecfg(cfgfile);
     printf("%s\n", base);
-    network net = parse_network_cfg(cfgfile);
-    if(weightfile){
-        load_weights(&net, weightfile);
-    }
-    if(clear) *net.seen = 0;
-    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
-    int imgs = net.batch;
+    printf("%d\n", ngpus);
+    network *nets = calloc(ngpus, sizeof(network));
 
+    srand(time(0));
+    int seed = rand();
+    for(i = 0; i < ngpus; ++i){
+        srand(seed);
+#ifdef GPU
+        cuda_set_device(gpus[i]);
+#endif
+        nets[i] = parse_network_cfg(cfgfile);
+        if(weightfile){
+            load_weights(&nets[i], weightfile);
+        }
+        if(clear) *nets[i].seen = 0;
+        nets[i].learning_rate *= ngpus;
+    }
+    srand(time(0));
+    network net = nets[0];
+
+    int imgs = net.batch * net.subdivisions * ngpus;
+
+    printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
     list *options = read_data_cfg(datacfg);
 
     char *backup_directory = option_find_str(options, "backup", "/backup/");
@@ -66,16 +84,21 @@
     printf("%d\n", plist->size);
     int N = plist->size;
     clock_t time;
-    pthread_t load_thread;
-    data train;
-    data buffer;
 
     load_args args = {0};
     args.w = net.w;
     args.h = net.h;
+    args.threads = 32;
+    args.hierarchy = net.hierarchy;
 
     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;
+    args.saturation = net.saturation;
+    args.hue = net.hue;
     args.size = net.w;
 
     args.paths = paths;
@@ -83,52 +106,79 @@
     args.n = imgs;
     args.m = N;
     args.labels = labels;
-    args.d = &buffer;
     args.type = CLASSIFICATION_DATA;
 
-    load_thread = load_data_in_thread(args);
-    int epoch = (*net.seen)/N;
+#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 iter_save = get_current_batch(net);
     while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
         time=clock();
+
         pthread_join(load_thread, 0);
         train = buffer;
+        load_thread = load_data(args);
 
-        load_thread = load_data_in_thread(args);
         printf("Loaded: %lf seconds\n", sec(clock()-time));
         time=clock();
 
-        /*
-           int u;
-           for(u = 0; u < net.batch; ++u){
-           image im = float_to_image(net.w, net.h, 3, train.X.vals[u]);
-           show_image(im, "loaded");
-           cvWaitKey(0);
-           }
-         */
-
-        float loss = train_network(net, train);
+        float loss = 0;
+#ifdef GPU
+        if(ngpus == 1){
+            loss = train_network(net, train);
+        } else {
+            loss = train_networks(nets, ngpus, train, 4);
+        }
+#else
+        loss = train_network(net, train);
+#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);
 
-    pthread_join(load_thread, 0);
-    free_data(buffer);
+#ifdef OPENCV
+    cvReleaseImage(&img);
+    cvDestroyAllWindows();
+#endif
+
     free_network(net);
     free_ptrs((void**)labels, classes);
     free_ptrs((void**)paths, plist->size);
@@ -136,7 +186,119 @@
     free(base);
 }
 
-void validate_classifier(char *datacfg, char *filename, char *weightfile)
+
+/*
+   void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int clear)
+   {
+   srand(time(0));
+   float avg_loss = -1;
+   char *base = basecfg(cfgfile);
+   printf("%s\n", base);
+   network net = parse_network_cfg(cfgfile);
+   if(weightfile){
+   load_weights(&net, weightfile);
+   }
+   if(clear) *net.seen = 0;
+
+   int imgs = net.batch * net.subdivisions;
+
+   printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
+   list *options = read_data_cfg(datacfg);
+
+   char *backup_directory = option_find_str(options, "backup", "/backup/");
+   char *label_list = option_find_str(options, "labels", "data/labels.list");
+   char *train_list = option_find_str(options, "train", "data/train.list");
+   int classes = option_find_int(options, "classes", 2);
+
+   char **labels = get_labels(label_list);
+   list *plist = get_paths(train_list);
+   char **paths = (char **)list_to_array(plist);
+   printf("%d\n", plist->size);
+   int N = plist->size;
+   clock_t time;
+
+   load_args args = {0};
+   args.w = net.w;
+   args.h = net.h;
+   args.threads = 8;
+
+   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;
+   args.saturation = net.saturation;
+   args.hue = net.hue;
+   args.size = net.w;
+   args.hierarchy = net.hierarchy;
+
+   args.paths = paths;
+   args.classes = classes;
+   args.n = imgs;
+   args.m = N;
+   args.labels = labels;
+   args.type = CLASSIFICATION_DATA;
+
+   data train;
+   data buffer;
+   pthread_t load_thread;
+   args.d = &buffer;
+   load_thread = load_data(args);
+
+   int epoch = (*net.seen)/N;
+   while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
+   time=clock();
+
+   pthread_join(load_thread, 0);
+   train = buffer;
+   load_thread = load_data(args);
+
+   printf("Loaded: %lf seconds\n", sec(clock()-time));
+   time=clock();
+
+#ifdef OPENCV
+if(0){
+int u;
+for(u = 0; u < imgs; ++u){
+    image im = float_to_image(net.w, net.h, 3, train.X.vals[u]);
+    show_image(im, "loaded");
+    cvWaitKey(0);
+}
+}
+#endif
+
+float loss = train_network(net, train);
+free_data(train);
+
+if(avg_loss == -1) avg_loss = loss;
+avg_loss = avg_loss*.9 + loss*.1;
+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);
+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);
+}
+}
+char buff[256];
+sprintf(buff, "%s/%s.weights", backup_directory, base);
+save_weights(net, buff);
+
+free_network(net);
+free_ptrs((void**)labels, classes);
+free_ptrs((void**)paths, plist->size);
+free_list(plist);
+free(base);
+}
+*/
+
+void validate_classifier_crop(char *datacfg, char *filename, char *weightfile)
 {
     int i = 0;
     network net = parse_network_cfg(filename);
@@ -151,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);
@@ -219,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);
@@ -232,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;
             }
         }
@@ -259,15 +423,16 @@
         float *pred = calloc(classes, sizeof(float));
         for(j = 0; j < 10; ++j){
             float *p = network_predict(net, images[j].data);
+            if(net.hierarchy) hierarchy_predictions(p, net.outputs, net.hierarchy, 1);
             axpy_cpu(classes, 1, p, 1, pred, 1);
             free_image(images[j]);
         }
         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));
@@ -290,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);
@@ -304,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;
             }
         }
@@ -319,14 +485,15 @@
         //show_image(crop, "cropped");
         //cvWaitKey(0);
         float *pred = network_predict(net, resized.data);
+        if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1);
 
         free_image(im);
         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));
@@ -338,18 +505,21 @@
 {
     int i, j;
     network net = parse_network_cfg(filename);
-    set_batch_network(&net, 1);
     if(weightfile){
         load_weights(&net, weightfile);
     }
+    set_batch_network(&net, 1);
     srand(time(0));
 
     list *options = read_data_cfg(datacfg);
 
     char *label_list = option_find_str(options, "labels", "data/labels.list");
+    char *leaf_list = option_find_str(options, "leaves", 0);
+    if(leaf_list) change_leaves(net.hierarchy, leaf_list);
     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);
@@ -363,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;
             }
         }
@@ -378,15 +548,16 @@
         //show_image(crop, "cropped");
         //cvWaitKey(0);
         float *pred = network_predict(net, crop.data);
+        if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1);
 
         if(resized.data != im.data) free_image(resized);
         free_image(im);
         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));
@@ -409,10 +580,11 @@
     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);
-    int scales[] = {192, 224, 288, 320, 352};
+    int scales[] = {224, 288, 320, 352, 384};
     int nscales = sizeof(scales)/sizeof(scales[0]);
 
     char **paths = (char **)list_to_array(plist);
@@ -424,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;
             }
         }
@@ -438,6 +610,7 @@
             image r = resize_min(im, scales[j]);
             resize_network(&net, r.w, r.h);
             float *p = network_predict(net, r.data);
+            if(net.hierarchy) hierarchy_predictions(p, net.outputs, net.hierarchy, 1);
             axpy_cpu(classes, 1, p, 1, pred, 1);
             flip_image(r);
             p = network_predict(net, r.data);
@@ -447,18 +620,18 @@
         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));
     }
 }
 
-void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename)
+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);
     }
@@ -469,7 +642,92 @@
 
     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);
+    clock_t time;
+    int *indexes = calloc(top, sizeof(int));
+    char buff[256];
+    char *input = buff;
+    while(1){
+        if(filename){
+            strncpy(input, filename, 256);
+        }else{
+            printf("Enter Image Path: ");
+            fflush(stdout);
+            input = fgets(input, 256, stdin);
+            if(!input) return;
+            strtok(input, "\n");
+        }
+        image orig = load_image_color(input, 0, 0);
+        image r = resize_min(orig, 256);
+        image im = crop_image(r, (r.w - 224 - 1)/2 + 1, (r.h - 224 - 1)/2 + 1, 224, 224);
+        float mean[] = {0.48263312050943, 0.45230225481413, 0.40099074308742};
+        float std[] = {0.22590347483426, 0.22120921437787, 0.22103996251583};
+        float var[3];
+        var[0] = std[0]*std[0];
+        var[1] = std[1]*std[1];
+        var[2] = std[2]*std[2];
+
+        normalize_cpu(im.data, mean, var, 1, 3, im.w*im.h);
+
+        float *X = im.data;
+        time=clock();
+        float *predictions = network_predict(net, X);
+
+        layer l = net.layers[layer_num];
+        for(i = 0; i < l.c; ++i){
+            if(l.rolling_mean) printf("%f %f %f\n", l.rolling_mean[i], l.rolling_variance[i], l.scales[i]);
+        }
+#ifdef GPU
+        cuda_pull_array(l.output_gpu, l.output, l.outputs);
+#endif
+        for(i = 0; i < l.outputs; ++i){
+            printf("%f\n", l.output[i]);
+        }
+        /*
+
+           printf("\n\nWeights\n");
+           for(i = 0; i < l.n*l.size*l.size*l.c; ++i){
+           printf("%f\n", l.filters[i]);
+           }
+
+           printf("\n\nBiases\n");
+           for(i = 0; i < l.n; ++i){
+           printf("%f\n", l.biases[i]);
+           }
+         */
+
+        top_predictions(net, top, indexes);
+        printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
+        for(i = 0; i < top; ++i){
+            int index = indexes[i];
+            printf("%s: %f\n", names[index], predictions[index]);
+        }
+        free_image(im);
+        if (filename) break;
+    }
+}
+
+void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top)
+{
+    network net = parse_network_cfg_custom(cfgfile, 1);
+    if(weightfile){
+        load_weights(&net, weightfile);
+    }
+    set_batch_network(&net, 1);
+    srand(2222222);
+
+    list *options = read_data_cfg(datacfg);
+
+    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);
+    if (top == 0) top = option_find_int(options, "top", 1);
+    if (top > classes) top = classes;
 
     int i = 0;
     char **names = get_labels(name_list);
@@ -489,18 +747,21 @@
             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;
         time=clock();
         float *predictions = network_predict(net, X);
-        top_predictions(net, top, indexes);
+        if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 0);
+        top_k(predictions, net.outputs, top, indexes);
         printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
         for(i = 0; i < top; ++i){
             int index = indexes[i];
-            printf("%s: %f\n", names[index], predictions[index]);
+            if(net.hierarchy) printf("%d, %s: %f, parent: %s \n",index, names[index], predictions[index], (net.hierarchy->parent[index] >= 0) ? names[net.hierarchy->parent[index]] : "Root");
+            else printf("%s: %f\n",names[index], predictions[index]);
         }
         if(r.data != im.data) free_image(r);
         free_image(im);
@@ -621,9 +882,12 @@
 }
 
 
-void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
+void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
 {
 #ifdef OPENCV
+    float threat = 0;
+    float roll = .2;
+
     printf("Classifier Demo\n");
     network net = parse_network_cfg(cfgfile);
     if(weightfile){
@@ -635,13 +899,238 @@
     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);
+
+    int *indexes = calloc(top, sizeof(int));
+
+    if(!cap) error("Couldn't connect to webcam.\n");
+    //cvNamedWindow("Threat", CV_WINDOW_NORMAL); 
+    //cvResizeWindow("Threat", 512, 512);
+    float fps = 0;
+    int i;
+
+    int count = 0;
+
+    while(1){
+        ++count;
+        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_cpp(cap);
+        if(!in.data) break;
+        image in_s = resize_image(in, net.w, net.h);
+
+        image out = in;
+        int x1 = out.w / 20;
+        int y1 = out.h / 20;
+        int x2 = 2*x1;
+        int y2 = out.h - out.h/20;
+
+        int border = .01*out.h;
+        int h = y2 - y1 - 2*border;
+        int w = x2 - x1 - 2*border;
+
+        float *predictions = network_predict(net, in_s.data);
+        float curr_threat = 0;
+        if(1){
+            curr_threat = predictions[0] * 0 + 
+                predictions[1] * .6 + 
+                predictions[2];
+        } else {
+            curr_threat = predictions[218] +
+                predictions[539] + 
+                predictions[540] + 
+                predictions[368] + 
+                predictions[369] + 
+                predictions[370];
+        }
+        threat = roll * curr_threat + (1-roll) * threat;
+
+        draw_box_width(out, x2 + border, y1 + .02*h, x2 + .5 * w, y1 + .02*h + border, border, 0,0,0);
+        if(threat > .97) {
+            draw_box_width(out,  x2 + .5 * w + border,
+                    y1 + .02*h - 2*border, 
+                    x2 + .5 * w + 6*border, 
+                    y1 + .02*h + 3*border, 3*border, 1,0,0);
+        }
+        draw_box_width(out,  x2 + .5 * w + border,
+                y1 + .02*h - 2*border, 
+                x2 + .5 * w + 6*border, 
+                y1 + .02*h + 3*border, .5*border, 0,0,0);
+        draw_box_width(out, x2 + border, y1 + .42*h, x2 + .5 * w, y1 + .42*h + border, border, 0,0,0);
+        if(threat > .57) {
+            draw_box_width(out,  x2 + .5 * w + border,
+                    y1 + .42*h - 2*border, 
+                    x2 + .5 * w + 6*border, 
+                    y1 + .42*h + 3*border, 3*border, 1,1,0);
+        }
+        draw_box_width(out,  x2 + .5 * w + border,
+                y1 + .42*h - 2*border, 
+                x2 + .5 * w + 6*border, 
+                y1 + .42*h + 3*border, .5*border, 0,0,0);
+
+        draw_box_width(out, x1, y1, x2, y2, border, 0,0,0);
+        for(i = 0; i < threat * h ; ++i){
+            float ratio = (float) i / h;
+            float r = (ratio < .5) ? (2*(ratio)) : 1;
+            float g = (ratio < .5) ? 1 : 1 - 2*(ratio - .5);
+            draw_box_width(out, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0);
+        }
+        top_predictions(net, top, indexes);
+        char buff[256];
+        sprintf(buff, "/home/pjreddie/tmp/threat_%06d", count);
+        //save_image(out, buff);
+
+        printf("\033[2J");
+        printf("\033[1;1H");
+        printf("\nFPS:%.0f\n",fps);
+
+        for(i = 0; i < top; ++i){
+            int index = indexes[i];
+            printf("%.1f%%: %s\n", predictions[index]*100, names[index]);
+        }
+
+        if(1){
+            show_image(out, "Threat");
+            cvWaitKey(10);
+        }
+        free_image(in_s);
+        free_image(in);
+
+        gettimeofday(&tval_after, NULL);
+        timersub(&tval_after, &tval_before, &tval_result);
+        float curr = 1000000.f/((long int)tval_result.tv_usec);
+        fps = .9*fps + .1*curr;
+    }
+#endif
+}
+
+
+void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
+{
+#ifdef OPENCV
+    int bad_cats[] = {218, 539, 540, 1213, 1501, 1742, 1911, 2415, 4348, 19223, 368, 369, 370, 1133, 1200, 1306, 2122, 2301, 2537, 2823, 3179, 3596, 3639, 4489, 5107, 5140, 5289, 6240, 6631, 6762, 7048, 7171, 7969, 7984, 7989, 8824, 8927, 9915, 10270, 10448, 13401, 15205, 18358, 18894, 18895, 19249, 19697};
+
+    printf("Classifier Demo\n");
+    network net = parse_network_cfg(cfgfile);
+    if(weightfile){
+        load_weights(&net, weightfile);
+    }
+    set_batch_network(&net, 1);
+    list *options = read_data_cfg(datacfg);
+
+    srand(2222222);
+    CvCapture * cap;
+
+    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);
+
+    int *indexes = calloc(top, sizeof(int));
+
+    if(!cap) error("Couldn't connect to webcam.\n");
+    cvNamedWindow("Threat Detection", CV_WINDOW_NORMAL); 
+    cvResizeWindow("Threat Detection", 512, 512);
+    float fps = 0;
+    int i;
+
+    while(1){
+        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_cpp(cap);
+        image in_s = resize_image(in, net.w, net.h);
+        show_image(in, "Threat Detection");
+
+        float *predictions = network_predict(net, in_s.data);
+        top_predictions(net, top, indexes);
+
+        printf("\033[2J");
+        printf("\033[1;1H");
+
+        int threat = 0;
+        for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){
+            int index = bad_cats[i];
+            if(predictions[index] > .01){
+                printf("Threat Detected!\n");
+                threat = 1;
+                break;
+            }
+        }
+        if(!threat) printf("Scanning...\n");
+        for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){
+            int index = bad_cats[i];
+            if(predictions[index] > .01){
+                printf("%s\n", names[index]);
+            }
+        }
+
+        free_image(in_s);
+        free_image(in);
+
+        cvWaitKey(10);
+
+        gettimeofday(&tval_after, NULL);
+        timersub(&tval_after, &tval_before, &tval_result);
+        float curr = 1000000.f/((long int)tval_result.tv_usec);
+        fps = .9*fps + .1*curr;
+    }
+#endif
+}
+
+void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
+{
+#ifdef OPENCV
+    printf("Classifier Demo\n");
+    network net = parse_network_cfg_custom(cfgfile, 1);
+    if(weightfile){
+        load_weights(&net, weightfile);
+    }
+    set_batch_network(&net, 1);
+    list *options = read_data_cfg(datacfg);
+
+    srand(2222222);
+    CvCapture * cap;
+
+    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);
@@ -658,11 +1147,13 @@
         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");
 
         float *predictions = network_predict(net, in_s.data);
+        if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 1);
         top_predictions(net, top, indexes);
 
         printf("\033[2J");
@@ -695,7 +1186,32 @@
         return;
     }
 
+    char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
+    int *gpus = 0;
+    int gpu = 0;
+    int ngpus = 0;
+    if(gpu_list){
+        printf("%s\n", gpu_list);
+        int len = strlen(gpu_list);
+        ngpus = 1;
+        int i;
+        for(i = 0; i < len; ++i){
+            if (gpu_list[i] == ',') ++ngpus;
+        }
+        gpus = calloc(ngpus, sizeof(int));
+        for(i = 0; i < ngpus; ++i){
+            gpus[i] = atoi(gpu_list);
+            gpu_list = strchr(gpu_list, ',')+1;
+        }
+    } else {
+        gpu = gpu_index;
+        gpus = &gpu;
+        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");
     char *data = argv[3];
     char *cfg = argv[4];
@@ -703,15 +1219,18 @@
     char *filename = (argc > 6) ? argv[6]: 0;
     char *layer_s = (argc > 7) ? argv[7]: 0;
     int layer = layer_s ? atoi(layer_s) : -1;
-    if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename);
-    else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights, clear);
+    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, 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);
     else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights, layer);
     else if(0==strcmp(argv[2], "label")) label_classifier(data, cfg, weights);
-    else if(0==strcmp(argv[2], "valid")) validate_classifier(data, cfg, weights);
-    else if(0==strcmp(argv[2], "valid10")) validate_classifier_10(data, cfg, weights);
+    else if(0==strcmp(argv[2], "valid")) validate_classifier_single(data, cfg, weights);
     else if(0==strcmp(argv[2], "validmulti")) validate_classifier_multi(data, cfg, weights);
-    else if(0==strcmp(argv[2], "validsingle")) validate_classifier_single(data, cfg, weights);
+    else if(0==strcmp(argv[2], "valid10")) validate_classifier_10(data, cfg, weights);
+    else if(0==strcmp(argv[2], "validcrop")) validate_classifier_crop(data, cfg, weights);
     else if(0==strcmp(argv[2], "validfull")) validate_classifier_full(data, cfg, weights);
 }
 

--
Gitblit v1.10.0