Joseph Redmon
2016-10-21 d8adaf8ea6a31a380f6bf1fe65e88b661d3bb51e
src/classifier.c
@@ -10,6 +10,7 @@
#ifdef OPENCV
#include "opencv2/highgui/highgui_c.h"
image get_image_from_stream(CvCapture *cap);
#endif
list *read_data_cfg(char *filename)
@@ -40,6 +41,20 @@
    return options;
}
void hierarchy_predictions(float *predictions, int n, tree *hier)
{
    int j;
    for(j = 0; j < n; ++j){
        int parent = hier->parent[j];
        if(parent >= 0){
            predictions[j] *= predictions[parent];
        }
    }
    for(j = 0; j < n; ++j){
        if(!hier->leaf[j]) predictions[j] = 0;
    }
}
float *get_regression_values(char **labels, int n)
{
    float *v = calloc(n, sizeof(float));
@@ -57,25 +72,26 @@
#ifdef GPU
    int i;
    srand(time(0));
    float avg_loss = -1;
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
    printf("%d\n", ngpus);
    network *nets = calloc(ngpus, sizeof(network));
    srand(time(0));
    int seed = rand();
    for(i = 0; i < ngpus; ++i){
        srand(seed);
        cuda_set_device(gpus[i]);
        nets[i] = parse_network_cfg(cfgfile);
        if(clear) *nets[i].seen = 0;
        if(weightfile){
            load_weights(&nets[i], weightfile);
        }
    }
    network net = nets[0];
    for(i = 0; i < ngpus; ++i){
        *nets[i].seen = *net.seen;
        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;
@@ -97,7 +113,8 @@
    load_args args = {0};
    args.w = net.w;
    args.h = net.h;
    args.threads = 16;
    args.threads = 32;
    args.hierarchy = net.hierarchy;
    args.min = net.min_crop;
    args.max = net.max_crop;
@@ -204,6 +221,7 @@
    args.saturation = net.saturation;
    args.hue = net.hue;
    args.size = net.w;
    args.hierarchy = net.hierarchy;
    args.paths = paths;
    args.classes = classes;
@@ -392,6 +410,7 @@
        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);
            axpy_cpu(classes, 1, p, 1, pred, 1);
            free_image(images[j]);
        }
@@ -452,6 +471,7 @@
        //show_image(crop, "cropped");
        //cvWaitKey(0);
        float *pred = network_predict(net, resized.data);
        if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy);
        free_image(im);
        free_image(resized);
@@ -511,6 +531,7 @@
        //show_image(crop, "cropped");
        //cvWaitKey(0);
        float *pred = network_predict(net, crop.data);
        if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy);
        if(resized.data != im.data) free_image(resized);
        free_image(im);
@@ -571,6 +592,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);
            axpy_cpu(classes, 1, p, 1, pred, 1);
            flip_image(r);
            p = network_predict(net, r.data);
@@ -670,7 +692,6 @@
    }
}
void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename)
{
    network net = parse_network_cfg(cfgfile);
@@ -711,11 +732,13 @@
        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);
        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);
@@ -897,15 +920,15 @@
        float curr_threat = 0;
        if(1){
            curr_threat = predictions[0] * 0 + 
                            predictions[1] * .6 +
                            predictions[2];
                predictions[1] * .6 +
                predictions[2];
        } else {
            curr_threat = predictions[218] +
                        predictions[539] +
                        predictions[540] +
                        predictions[368] +
                        predictions[369] +
                        predictions[370];
                predictions[539] +
                predictions[540] +
                predictions[368] +
                predictions[369] +
                predictions[370];
        }
        threat = roll * curr_threat + (1-roll) * threat;
@@ -1090,6 +1113,7 @@
        show_image(in, "Classifier");
        float *predictions = network_predict(net, in_s.data);
        if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy);
        top_predictions(net, top, indexes);
        printf("\033[2J");