AlexeyAB
2018-03-01 35a66b73a25ab93f2a3ceb85993d4801240046a4
Changed class to class_id
9 files modified
172 ■■■■ changed files
src/box.c 8 ●●●● patch | view | raw | blame | history
src/cifar.c 12 ●●●● patch | view | raw | blame | history
src/classifier.c 32 ●●●● patch | view | raw | blame | history
src/compare.c 40 ●●●● patch | view | raw | blame | history
src/data.c 8 ●●●● patch | view | raw | blame | history
src/detector.c 6 ●●●● patch | view | raw | blame | history
src/image.c 22 ●●●● patch | view | raw | blame | history
src/matrix.c 4 ●●●● patch | view | raw | blame | history
src/region_layer.c 40 ●●●● patch | view | raw | blame | history
src/box.c
@@ -232,7 +232,7 @@
typedef struct{
    int index;
    int class;
    int class_id;
    float **probs;
} sortable_bbox;
@@ -240,7 +240,7 @@
{
    sortable_bbox a = *(sortable_bbox *)pa;
    sortable_bbox b = *(sortable_bbox *)pb;
    float diff = a.probs[a.index][b.class] - b.probs[b.index][b.class];
    float diff = a.probs[a.index][b.class_id] - b.probs[b.index][b.class_id];
    if(diff < 0) return 1;
    else if(diff > 0) return -1;
    return 0;
@@ -253,13 +253,13 @@
    for(i = 0; i < total; ++i){
        s[i].index = i;       
        s[i].class = 0;
        s[i].class_id = 0;
        s[i].probs = probs;
    }
    for(k = 0; k < classes; ++k){
        for(i = 0; i < total; ++i){
            s[i].class = k;
            s[i].class_id = k;
        }
        qsort(s, total, sizeof(sortable_bbox), nms_comparator);
        for(i = 0; i < total; ++i){
src/cifar.c
@@ -137,8 +137,8 @@
        axpy_cpu(10, 1, p, 1, pred, 1);
        int index = max_index(pred, 10);
        int class = max_index(test.y.vals[i], 10);
        if(index == class) avg_acc += 1;
        int class_id = max_index(test.y.vals[i], 10);
        if(index == class_id) avg_acc += 1;
        free_image(im);
        printf("%4d: %.2f%%\n", i, 100.*avg_acc/(i+1));
    }
@@ -174,16 +174,16 @@
    data test = load_cifar10_data("data/cifar/cifar-10-batches-bin/test_batch.bin");
    for(i = 0; i < train.X.rows; ++i){
        image im = float_to_image(32, 32, 3, train.X.vals[i]);
        int class = max_index(train.y.vals[i], 10);
        int class_id = max_index(train.y.vals[i], 10);
        char buff[256];
        sprintf(buff, "data/cifar/train/%d_%s",i,labels[class]);
        sprintf(buff, "data/cifar/train/%d_%s",i,labels[class_id]);
        save_image_png(im, buff);
    }
    for(i = 0; i < test.X.rows; ++i){
        image im = float_to_image(32, 32, 3, test.X.vals[i]);
        int class = max_index(test.y.vals[i], 10);
        int class_id = max_index(test.y.vals[i], 10);
        char buff[256];
        sprintf(buff, "data/cifar/test/%d_%s",i,labels[class]);
        sprintf(buff, "data/cifar/test/%d_%s",i,labels[class_id]);
        save_image_png(im, buff);
    }
}
src/classifier.c
@@ -362,11 +362,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 +396,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));
@@ -435,11 +435,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 +456,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));
@@ -497,11 +497,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 +519,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));
@@ -559,11 +559,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 +583,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));
src/compare.c
@@ -148,21 +148,21 @@
typedef struct {
    network net;
    char *filename;
    int class;
    int class_id;
    int classes;
    float elo;
    float *elos;
} sortable_bbox;
int total_compares = 0;
int current_class = 0;
int current_class_id = 0;
int elo_comparator(const void*a, const void *b)
{
    sortable_bbox box1 = *(sortable_bbox*)a;
    sortable_bbox box2 = *(sortable_bbox*)b;
    if(box1.elos[current_class] == box2.elos[current_class]) return 0;
    if(box1.elos[current_class] >  box2.elos[current_class]) return -1;
    if(box1.elos[current_class_id] == box2.elos[current_class_id]) return 0;
    if(box1.elos[current_class_id] >  box2.elos[current_class_id]) return -1;
    return 1;
}
@@ -172,7 +172,7 @@
    sortable_bbox box1 = *(sortable_bbox*)a;
    sortable_bbox box2 = *(sortable_bbox*)b;
    network net = box1.net;
    int class   = box1.class;
    int class_id   = box1.class_id;
    image im1 = load_image_color(box1.filename, net.w, net.h);
    image im2 = load_image_color(box2.filename, net.w, net.h);
@@ -184,24 +184,24 @@
    free_image(im1);
    free_image(im2);
    free(X);
    if (predictions[class*2] > predictions[class*2+1]){
    if (predictions[class_id*2] > predictions[class_id*2+1]){
        return 1;
    }
    return -1;
}
void bbox_update(sortable_bbox *a, sortable_bbox *b, int class, int result)
void bbox_update(sortable_bbox *a, sortable_bbox *b, int class_id, int result)
{
    int k = 32;
    float EA = 1./(1+pow(10, (b->elos[class] - a->elos[class])/400.));
    float EB = 1./(1+pow(10, (a->elos[class] - b->elos[class])/400.));
    float EA = 1./(1+pow(10, (b->elos[class_id] - a->elos[class_id])/400.));
    float EB = 1./(1+pow(10, (a->elos[class_id] - b->elos[class_id])/400.));
    float SA = result ? 1 : 0;
    float SB = result ? 0 : 1;
    a->elos[class] += k*(SA - EA);
    b->elos[class] += k*(SB - EB);
    a->elos[class_id] += k*(SA - EA);
    b->elos[class_id] += k*(SB - EB);
}
void bbox_fight(network net, sortable_bbox *a, sortable_bbox *b, int classes, int class)
void bbox_fight(network net, sortable_bbox *a, sortable_bbox *b, int classes, int class_id)
{
    image im1 = load_image_color(a->filename, net.w, net.h);
    image im2 = load_image_color(b->filename, net.w, net.h);
@@ -213,7 +213,7 @@
    int i;
    for(i = 0; i < classes; ++i){
        if(class < 0 || class == i){
        if(class_id < 0 || class_id == i){
            int result = predictions[i*2] > predictions[i*2+1];
            bbox_update(a, b, i, result);
        }
@@ -244,7 +244,7 @@
    for(i = 0; i < N; ++i){
        boxes[i].filename = paths[i];
        boxes[i].net = net;
        boxes[i].class = 7;
        boxes[i].class_id = 7;
        boxes[i].elo = 1500;
    }
    clock_t time=clock();
@@ -297,12 +297,12 @@
        printf("Round: %f secs, %d remaining\n", sec(clock()-round_time), N);
    }
    int class;
    int class_id;
    for (class = 0; class < classes; ++class){
    for (class_id = 0; class_id < classes; ++class_id){
        N = total;
        current_class = class;
        current_class_id = class_id;
        qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
        N /= 2;
@@ -312,7 +312,7 @@
            sorta_shuffle(boxes, N, sizeof(sortable_bbox), 10);
            for(i = 0; i < N/2; ++i){
                bbox_fight(net, boxes+i*2, boxes+i*2+1, classes, class);
                bbox_fight(net, boxes+i*2, boxes+i*2+1, classes, class_id);
            }
            qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
            if(round <= 20) N = (N*9/10)/2*2;
@@ -320,10 +320,10 @@
            printf("Round: %f secs, %d remaining\n", sec(clock()-round_time), N);
        }
        char buff[256];
        sprintf(buff, "results/battle_%d.log", class);
        sprintf(buff, "results/battle_%d.log", class_id);
        FILE *outfp = fopen(buff, "w");
        for(i = 0; i < N; ++i){
            fprintf(outfp, "%s %f\n", boxes[i].filename, boxes[i].elos[class]);
            fprintf(outfp, "%s %f\n", boxes[i].filename, boxes[i].elos[class_id]);
        }
        fclose(outfp);
    }
src/data.c
@@ -967,8 +967,8 @@
    for(i = 0; i < 10000; ++i){
        unsigned char bytes[3073];
        fread(bytes, 1, 3073, fp);
        int class = bytes[0];
        y.vals[i][class] = 1;
        int class_id = bytes[0];
        y.vals[i][class_id] = 1;
        for(j = 0; j < X.cols; ++j){
            X.vals[i][j] = (double)bytes[j+1];
        }
@@ -1031,8 +1031,8 @@
        for(i = 0; i < 10000; ++i){
            unsigned char bytes[3073];
            fread(bytes, 1, 3073, fp);
            int class = bytes[0];
            y.vals[i+b*10000][class] = 1;
            int class_id = bytes[0];
            y.vals[i+b*10000][class_id] = 1;
            for(j = 0; j < X.cols; ++j){
                X.vals[i+b*10000][j] = (double)bytes[j+1];
            }
src/detector.c
@@ -245,8 +245,8 @@
        if (ymax > h) ymax = h;
        for(j = 0; j < classes; ++j){
            int class = j;
            if (probs[i][class]) fprintf(fp, "%d %d %f %f %f %f %f\n", id, j+1, probs[i][class],
            int class_id = j;
            if (probs[i][class_id]) fprintf(fp, "%d %d %f %f %f %f %f\n", id, j+1, probs[i][class_id],
                    xmin, ymin, xmax, ymax);
        }
    }
@@ -777,7 +777,7 @@
            avg_precision += cur_precision;
        }
        avg_precision = avg_precision / 11;
        printf("class = %d, name = %s, \t ap = %2.2f %% \n", i, names[i], avg_precision*100);
        printf("class_id = %d, name = %s, \t ap = %2.2f %% \n", i, names[i], avg_precision*100);
        mean_average_precision += avg_precision;
    }
    
src/image.c
@@ -188,8 +188,8 @@
    int i;
    for(i = 0; i < num; ++i){
        int class = max_index(probs[i], classes);
        float prob = probs[i][class];
        int class_id = max_index(probs[i], classes);
        float prob = probs[i][class_id];
        if(prob > thresh){
            //// for comparison with OpenCV version of DNN Darknet Yolo v2
@@ -207,8 +207,8 @@
                alphabet = 0;
            }
            printf("%s: %.0f%%\n", names[class], prob*100);
            int offset = class*123457 % classes;
            printf("%s: %.0f%%\n", names[class_id], prob*100);
            int offset = class_id*123457 % classes;
            float red = get_color(2,offset,classes);
            float green = get_color(1,offset,classes);
            float blue = get_color(0,offset,classes);
@@ -233,7 +233,7 @@
            draw_box_width(im, left, top, right, bot, width, red, green, blue);
            if (alphabet) {
                image label = get_label(alphabet, names[class], (im.h*.03)/10);
                image label = get_label(alphabet, names[class_id], (im.h*.03)/10);
                draw_label(im, top + width, left, label, rgb);
            }
        }
@@ -246,8 +246,8 @@
    int i;
    for (i = 0; i < num; ++i) {
        int class = max_index(probs[i], classes);
        float prob = probs[i][class];
        int class_id = max_index(probs[i], classes);
        float prob = probs[i][class_id];
        if (prob > thresh) {
            int width = show_img->height * .012;
@@ -257,8 +257,8 @@
                alphabet = 0;
            }
            printf("%s: %.0f%%\n", names[class], prob * 100);
            int offset = class * 123457 % classes;
            printf("%s: %.0f%%\n", names[class_id], prob * 100);
            int offset = class_id * 123457 % classes;
            float red = get_color(2, offset, classes);
            float green = get_color(1, offset, classes);
            float blue = get_color(0, offset, classes);
@@ -299,14 +299,14 @@
            color.val[2] = blue * 256;
            cvRectangle(show_img, pt1, pt2, color, width, 8, 0);
            //printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class, names[class]);
            //printf("left=%d, right=%d, top=%d, bottom=%d, obj_id=%d, obj=%s \n", left, right, top, bot, class_id, names[class_id]);
            cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, width, 8, 0);
            cvRectangle(show_img, pt_text_bg1, pt_text_bg2, color, CV_FILLED, 8, 0);    // filled
            CvScalar black_color;
            black_color.val[0] = 0;
            CvFont font;
            cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, font_size, font_size, 0, font_size * 3, 8);  
            cvPutText(show_img, names[class], pt_text, &font, black_color);
            cvPutText(show_img, names[class_id], pt_text, &font, black_color);
        }
    }
}
src/matrix.c
@@ -22,8 +22,8 @@
    for(i = 0; i < truth.rows; ++i){
        top_k(guess.vals[i], n, k, indexes);
        for(j = 0; j < k; ++j){
            int class = indexes[j];
            if(truth.vals[i][class]){
            int class_id = indexes[j];
            if(truth.vals[i][class_id]){
                ++correct;
                break;
            }
src/region_layer.c
@@ -110,27 +110,27 @@
    return iou;
}
void delta_region_class(float *output, float *delta, int index, int class, int classes, tree *hier, float scale, float *avg_cat)
void delta_region_class(float *output, float *delta, int index, int class_id, int classes, tree *hier, float scale, float *avg_cat)
{
    int i, n;
    if(hier){
        float pred = 1;
        while(class >= 0){
            pred *= output[index + class];
            int g = hier->group[class];
        while(class_id >= 0){
            pred *= output[index + class_id];
            int g = hier->group[class_id];
            int offset = hier->group_offset[g];
            for(i = 0; i < hier->group_size[g]; ++i){
                delta[index + offset + i] = scale * (0 - output[index + offset + i]);
            }
            delta[index + class] = scale * (1 - output[index + class]);
            delta[index + class_id] = scale * (1 - output[index + class_id]);
            class = hier->parent[class];
            class_id = hier->parent[class_id];
        }
        *avg_cat += pred;
    } else {
        for(n = 0; n < classes; ++n){
            delta[index + n] = scale * (((n == class)?1 : 0) - output[index + n]);
            if(n == class) *avg_cat += output[index + n];
            delta[index + n] = scale * (((n == class_id)?1 : 0) - output[index + n]);
            if(n == class_id) *avg_cat += output[index + n];
        }
    }
}
@@ -191,31 +191,31 @@
    *(l.cost) = 0;
    for (b = 0; b < l.batch; ++b) {
        if(l.softmax_tree){
            int onlyclass = 0;
            int onlyclass_id = 0;
            for(t = 0; t < l.max_boxes; ++t){
                box truth = float_to_box(state.truth + t*5 + b*l.truths);
                if(!truth.x) break;
                int class = state.truth[t*5 + b*l.truths + 4];
                int class_id = state.truth[t*5 + b*l.truths + 4];
                float maxp = 0;
                int maxi = 0;
                if(truth.x > 100000 && truth.y > 100000){
                    for(n = 0; n < l.n*l.w*l.h; ++n){
                        int index = size*n + b*l.outputs + 5;
                        float scale =  l.output[index-1];
                        float p = scale*get_hierarchy_probability(l.output + index, l.softmax_tree, class);
                        float p = scale*get_hierarchy_probability(l.output + index, l.softmax_tree, class_id);
                        if(p > maxp){
                            maxp = p;
                            maxi = n;
                        }
                    }
                    int index = size*maxi + b*l.outputs + 5;
                    delta_region_class(l.output, l.delta, index, class, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
                    delta_region_class(l.output, l.delta, index, class_id, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
                    ++class_count;
                    onlyclass = 1;
                    onlyclass_id = 1;
                    break;
                }
            }
            if(onlyclass) continue;
            if(onlyclass_id) continue;
        }
        for (j = 0; j < l.h; ++j) {
            for (i = 0; i < l.w; ++i) {
@@ -223,13 +223,13 @@
                    int index = size*(j*l.w*l.n + i*l.n + n) + b*l.outputs;
                    box pred = get_region_box(l.output, l.biases, n, index, i, j, l.w, l.h);
                    float best_iou = 0;
                    int best_class = -1;
                    int best_class_id = -1;
                    for(t = 0; t < l.max_boxes; ++t){
                        box truth = float_to_box(state.truth + t*5 + b*l.truths);
                        if(!truth.x) break;
                        float iou = box_iou(pred, truth);
                        if (iou > best_iou) {
                            best_class = state.truth[t*5 + b*l.truths + 4];
                            best_class_id = state.truth[t*5 + b*l.truths + 4];
                            best_iou = iou;
                        }
                    }
@@ -240,7 +240,7 @@
                        if (best_iou > l.thresh) {
                            l.delta[index + 4] = 0;
                            if(l.classfix > 0){
                                delta_region_class(l.output, l.delta, index + 5, best_class, l.classes, l.softmax_tree, l.class_scale*(l.classfix == 2 ? l.output[index + 4] : 1), &avg_cat);
                                delta_region_class(l.output, l.delta, index + 5, best_class_id, l.classes, l.softmax_tree, l.class_scale*(l.classfix == 2 ? l.output[index + 4] : 1), &avg_cat);
                                ++class_count;
                            }
                        }
@@ -310,9 +310,9 @@
            }
            int class = state.truth[t*5 + b*l.truths + 4];
            if (l.map) class = l.map[class];
            delta_region_class(l.output, l.delta, best_index + 5, class, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
            int class_id = state.truth[t*5 + b*l.truths + 4];
            if (l.map) class_id = l.map[class_id];
            delta_region_class(l.output, l.delta, best_index + 5, class_id, l.classes, l.softmax_tree, l.class_scale, &avg_cat);
            ++count;
            ++class_count;
        }