Joseph Redmon
2016-11-05 c7a700dc2249e8bd3a2c9120dfd09240e413c8bd
new font strategy
18 files modified
95 files deleted
539 ■■■■■ changed files
data/labels/100.png patch | view | raw | blame | history
data/labels/101.png patch | view | raw | blame | history
data/labels/102.png patch | view | raw | blame | history
data/labels/103.png patch | view | raw | blame | history
data/labels/104.png patch | view | raw | blame | history
data/labels/105.png patch | view | raw | blame | history
data/labels/106.png patch | view | raw | blame | history
data/labels/107.png patch | view | raw | blame | history
data/labels/108.png patch | view | raw | blame | history
data/labels/109.png patch | view | raw | blame | history
data/labels/110.png patch | view | raw | blame | history
data/labels/111.png patch | view | raw | blame | history
data/labels/112.png patch | view | raw | blame | history
data/labels/113.png patch | view | raw | blame | history
data/labels/114.png patch | view | raw | blame | history
data/labels/115.png patch | view | raw | blame | history
data/labels/116.png patch | view | raw | blame | history
data/labels/117.png patch | view | raw | blame | history
data/labels/118.png patch | view | raw | blame | history
data/labels/119.png patch | view | raw | blame | history
data/labels/120.png patch | view | raw | blame | history
data/labels/121.png patch | view | raw | blame | history
data/labels/122.png patch | view | raw | blame | history
data/labels/123.png patch | view | raw | blame | history
data/labels/124.png patch | view | raw | blame | history
data/labels/125.png patch | view | raw | blame | history
data/labels/126.png patch | view | raw | blame | history
data/labels/32.png patch | view | raw | blame | history
data/labels/33.png patch | view | raw | blame | history
data/labels/34.png patch | view | raw | blame | history
data/labels/35.png patch | view | raw | blame | history
data/labels/36.png patch | view | raw | blame | history
data/labels/37.png patch | view | raw | blame | history
data/labels/38.png patch | view | raw | blame | history
data/labels/39.png patch | view | raw | blame | history
data/labels/40.png patch | view | raw | blame | history
data/labels/41.png patch | view | raw | blame | history
data/labels/42.png patch | view | raw | blame | history
data/labels/43.png patch | view | raw | blame | history
data/labels/44.png patch | view | raw | blame | history
data/labels/45.png patch | view | raw | blame | history
data/labels/46.png patch | view | raw | blame | history
data/labels/47.png patch | view | raw | blame | history
data/labels/48.png patch | view | raw | blame | history
data/labels/49.png patch | view | raw | blame | history
data/labels/50.png patch | view | raw | blame | history
data/labels/51.png patch | view | raw | blame | history
data/labels/52.png patch | view | raw | blame | history
data/labels/53.png patch | view | raw | blame | history
data/labels/54.png patch | view | raw | blame | history
data/labels/55.png patch | view | raw | blame | history
data/labels/56.png patch | view | raw | blame | history
data/labels/57.png patch | view | raw | blame | history
data/labels/58.png patch | view | raw | blame | history
data/labels/59.png patch | view | raw | blame | history
data/labels/60.png patch | view | raw | blame | history
data/labels/61.png patch | view | raw | blame | history
data/labels/62.png patch | view | raw | blame | history
data/labels/63.png patch | view | raw | blame | history
data/labels/64.png patch | view | raw | blame | history
data/labels/65.png patch | view | raw | blame | history
data/labels/66.png patch | view | raw | blame | history
data/labels/67.png patch | view | raw | blame | history
data/labels/68.png patch | view | raw | blame | history
data/labels/69.png patch | view | raw | blame | history
data/labels/70.png patch | view | raw | blame | history
data/labels/71.png patch | view | raw | blame | history
data/labels/72.png patch | view | raw | blame | history
data/labels/73.png patch | view | raw | blame | history
data/labels/74.png patch | view | raw | blame | history
data/labels/75.png patch | view | raw | blame | history
data/labels/76.png patch | view | raw | blame | history
data/labels/77.png patch | view | raw | blame | history
data/labels/78.png patch | view | raw | blame | history
data/labels/79.png patch | view | raw | blame | history
data/labels/80.png patch | view | raw | blame | history
data/labels/81.png patch | view | raw | blame | history
data/labels/82.png patch | view | raw | blame | history
data/labels/83.png patch | view | raw | blame | history
data/labels/84.png patch | view | raw | blame | history
data/labels/85.png patch | view | raw | blame | history
data/labels/86.png patch | view | raw | blame | history
data/labels/87.png patch | view | raw | blame | history
data/labels/88.png patch | view | raw | blame | history
data/labels/89.png patch | view | raw | blame | history
data/labels/90.png patch | view | raw | blame | history
data/labels/91.png patch | view | raw | blame | history
data/labels/92.png patch | view | raw | blame | history
data/labels/93.png patch | view | raw | blame | history
data/labels/94.png patch | view | raw | blame | history
data/labels/95.png patch | view | raw | blame | history
data/labels/96.png patch | view | raw | blame | history
data/labels/97.png patch | view | raw | blame | history
data/labels/98.png patch | view | raw | blame | history
data/labels/99.png patch | view | raw | blame | history
data/labels/make_labels.py 28 ●●●●● patch | view | raw | blame | history
src/classifier.c 64 ●●●●● patch | view | raw | blame | history
src/coco.c 2 ●●● patch | view | raw | blame | history
src/convolutional_kernels.cu 8 ●●●●● patch | view | raw | blame | history
src/convolutional_layer.c 10 ●●●● patch | view | raw | blame | history
src/demo.c 4 ●●●● patch | view | raw | blame | history
src/detector.c 65 ●●●●● patch | view | raw | blame | history
src/image.c 43 ●●●● patch | view | raw | blame | history
src/image.h 4 ●●●● patch | view | raw | blame | history
src/layer.h 4 ●●●● patch | view | raw | blame | history
src/option_list.c 29 ●●●●● patch | view | raw | blame | history
src/option_list.h 1 ●●●● patch | view | raw | blame | history
src/parser.c 66 ●●●●● patch | view | raw | blame | history
src/region_layer.c 137 ●●●●● patch | view | raw | blame | history
src/softmax_layer.c 24 ●●●●● patch | view | raw | blame | history
src/tree.c 44 ●●●●● patch | view | raw | blame | history
src/tree.h 4 ●●●● patch | view | raw | blame | history
src/yolo.c 2 ●●● patch | view | raw | blame | history
data/labels/100.png
Binary files differ
data/labels/101.png
Binary files differ
data/labels/102.png
Binary files differ
data/labels/103.png
Binary files differ
data/labels/104.png
Binary files differ
data/labels/105.png
Binary files differ
data/labels/106.png
Binary files differ
data/labels/107.png
Binary files differ
data/labels/108.png
Binary files differ
data/labels/109.png
Binary files differ
data/labels/110.png
Binary files differ
data/labels/111.png
Binary files differ
data/labels/112.png
Binary files differ
data/labels/113.png
Binary files differ
data/labels/114.png
Binary files differ
data/labels/115.png
Binary files differ
data/labels/116.png
Binary files differ
data/labels/117.png
Binary files differ
data/labels/118.png
Binary files differ
data/labels/119.png
Binary files differ
data/labels/120.png
Binary files differ
data/labels/121.png
Binary files differ
data/labels/122.png
Binary files differ
data/labels/123.png
Binary files differ
data/labels/124.png
Binary files differ
data/labels/125.png
Binary files differ
data/labels/126.png
Binary files differ
data/labels/32.png
Binary files differ
data/labels/33.png
Binary files differ
data/labels/34.png
Binary files differ
data/labels/35.png
Binary files differ
data/labels/36.png
Binary files differ
data/labels/37.png
Binary files differ
data/labels/38.png
Binary files differ
data/labels/39.png
Binary files differ
data/labels/40.png
Binary files differ
data/labels/41.png
Binary files differ
data/labels/42.png
Binary files differ
data/labels/43.png
Binary files differ
data/labels/44.png
Binary files differ
data/labels/45.png
Binary files differ
data/labels/46.png
Binary files differ
data/labels/47.png
Binary files differ
data/labels/48.png
Binary files differ
data/labels/49.png
Binary files differ
data/labels/50.png
Binary files differ
data/labels/51.png
Binary files differ
data/labels/52.png
Binary files differ
data/labels/53.png
Binary files differ
data/labels/54.png
Binary files differ
data/labels/55.png
Binary files differ
data/labels/56.png
Binary files differ
data/labels/57.png
Binary files differ
data/labels/58.png
Binary files differ
data/labels/59.png
Binary files differ
data/labels/60.png
Binary files differ
data/labels/61.png
Binary files differ
data/labels/62.png
Binary files differ
data/labels/63.png
Binary files differ
data/labels/64.png
Binary files differ
data/labels/65.png
Binary files differ
data/labels/66.png
Binary files differ
data/labels/67.png
Binary files differ
data/labels/68.png
Binary files differ
data/labels/69.png
Binary files differ
data/labels/70.png
Binary files differ
data/labels/71.png
Binary files differ
data/labels/72.png
Binary files differ
data/labels/73.png
Binary files differ
data/labels/74.png
Binary files differ
data/labels/75.png
Binary files differ
data/labels/76.png
Binary files differ
data/labels/77.png
Binary files differ
data/labels/78.png
Binary files differ
data/labels/79.png
Binary files differ
data/labels/80.png
Binary files differ
data/labels/81.png
Binary files differ
data/labels/82.png
Binary files differ
data/labels/83.png
Binary files differ
data/labels/84.png
Binary files differ
data/labels/85.png
Binary files differ
data/labels/86.png
Binary files differ
data/labels/87.png
Binary files differ
data/labels/88.png
Binary files differ
data/labels/89.png
Binary files differ
data/labels/90.png
Binary files differ
data/labels/91.png
Binary files differ
data/labels/92.png
Binary files differ
data/labels/93.png
Binary files differ
data/labels/94.png
Binary files differ
data/labels/95.png
Binary files differ
data/labels/96.png
Binary files differ
data/labels/97.png
Binary files differ
data/labels/98.png
Binary files differ
data/labels/99.png
Binary files differ
data/labels/make_labels.py
@@ -2,18 +2,22 @@
import string
import pipes
#l = ["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
font = 'futura-normal'
l = string.printable
def make_labels(s):
    l = string.printable
    for word in l:
        if word == ' ':
            os.system('convert -fill black -background white -bordercolor white -font %s -pointsize %d label:"\ " 32_%d.png'%(font,s,s/12-1))
        if word == '@':
            os.system('convert -fill black -background white -bordercolor white -font %s -pointsize %d label:"\@" 64_%d.png'%(font,s,s/12-1))
        elif word == '\\':
            os.system('convert -fill black -background white -bordercolor white -font %s -pointsize %d label:"\\\\\\\\" 92_%d.png'%(font,s,s/12-1))
        elif ord(word) in [9,10,11,12,13,14]:
            pass
        else:
            os.system("convert -fill black -background white -bordercolor white -font %s -pointsize %d label:%s \"%d_%d.png\""%(font,s,pipes.quote(word), ord(word),s/12-1))
for word in l:
    #os.system("convert -fill black -background white -bordercolor white -border 4 -font futura-normal -pointsize 18 label:\"%s\" \"%s.png\""%(word, word))
    if word == ' ':
        os.system('convert -fill black -background white -bordercolor white -font futura-normal -pointsize 64 label:"\ " 32.png')
    elif word == '\\':
        os.system('convert -fill black -background white -bordercolor white -font futura-normal -pointsize 64 label:"\\\\\\\\" 92.png')
    elif ord(word) in [9,10,11,12,13,14]:
        pass
    else:
        os.system("convert -fill black -background white -bordercolor white -font futura-normal -pointsize 64 label:%s \"%d.png\""%(pipes.quote(word), ord(word)))
for i in [12,24,36,48,60,72,84,96]:
    make_labels(i)
src/classifier.c
@@ -13,50 +13,6 @@
image get_image_from_stream(CvCapture *cap);
#endif
list *read_data_cfg(char *filename)
{
    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;
        }
    }
    fclose(file);
    return options;
}
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves)
{
    int j;
    for(j = 0; j < n; ++j){
        int parent = hier->parent[j];
        if(parent >= 0){
            predictions[j] *= predictions[parent];
        }
    }
    if(only_leaves){
        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));
@@ -488,26 +444,6 @@
    }
}
void change_leaves(tree *t, char *leaf_list)
{
    list *llist = get_paths(leaf_list);
    char **leaves = (char **)list_to_array(llist);
    int n = llist->size;
    int i,j;
    int found = 0;
    for(i = 0; i < t->n; ++i){
        t->leaf[i] = 0;
        for(j = 0; j < n; ++j){
            if (0==strcmp(t->name[i], leaves[j])){
                t->leaf[i] = 1;
                ++found;
                break;
            }
        }
    }
    fprintf(stderr, "Found %d leaves.\n", found);
}
void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
{
src/coco.c
@@ -318,7 +318,7 @@
void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
{
    image *alphabet = load_alphabet();
    image **alphabet = load_alphabet();
    network net = parse_network_cfg(cfgfile);
    if(weightfile){
        load_weights(&net, weightfile);
src/convolutional_kernels.cu
@@ -215,6 +215,10 @@
        cuda_pull_array(layer.rolling_mean_gpu, layer.rolling_mean, layer.n);
        cuda_pull_array(layer.rolling_variance_gpu, layer.rolling_variance, layer.n);
    }
    if (layer.adam){
        cuda_pull_array(layer.m_gpu, layer.m, layer.c*layer.n*layer.size*layer.size);
        cuda_pull_array(layer.v_gpu, layer.v, layer.c*layer.n*layer.size*layer.size);
    }
}
void push_convolutional_layer(convolutional_layer layer)
@@ -228,6 +232,10 @@
        cuda_push_array(layer.rolling_mean_gpu, layer.rolling_mean, layer.n);
        cuda_push_array(layer.rolling_variance_gpu, layer.rolling_variance, layer.n);
    }
    if (layer.adam){
        cuda_push_array(layer.m_gpu, layer.m, layer.c*layer.n*layer.size*layer.size);
        cuda_push_array(layer.v_gpu, layer.v, layer.c*layer.n*layer.size*layer.size);
    }
}
void update_convolutional_layer_gpu(convolutional_layer layer, int batch, float learning_rate, float momentum, float decay)
src/convolutional_layer.c
@@ -235,6 +235,11 @@
        l.rolling_mean = calloc(n, sizeof(float));
        l.rolling_variance = calloc(n, sizeof(float));
    }
    if(adam){
        l.adam = 1;
        l.m = calloc(c*n*size*size, sizeof(float));
        l.v = calloc(c*n*size*size, sizeof(float));
    }
#ifdef GPU
    l.forward_gpu = forward_convolutional_layer_gpu;
@@ -243,9 +248,8 @@
    if(gpu_index >= 0){
        if (adam) {
            l.adam = 1;
            l.m_gpu = cuda_make_array(l.weight_updates, c*n*size*size);
            l.v_gpu = cuda_make_array(l.weight_updates, c*n*size*size);
            l.m_gpu = cuda_make_array(l.m, c*n*size*size);
            l.v_gpu = cuda_make_array(l.v, c*n*size*size);
        }
        l.weights_gpu = cuda_make_array(l.weights, c*n*size*size);
src/demo.c
@@ -17,7 +17,7 @@
image get_image_from_stream(CvCapture *cap);
static char **demo_names;
static image *demo_alphabet;
static image **demo_alphabet;
static int demo_classes;
static float **probs;
@@ -94,7 +94,7 @@
void demo(char *cfgfile, char *weightfile, float thresh, int cam_index, const char *filename, char **names, int classes, int frame_skip, char *prefix)
{
    //skip = frame_skip;
    image *alphabet = load_alphabet();
    image **alphabet = load_alphabet();
    int delay = frame_skip;
    demo_names = names;
    demo_alphabet = alphabet;
src/detector.c
@@ -5,17 +5,18 @@
#include "parser.h"
#include "box.h"
#include "demo.h"
#include "option_list.h"
#ifdef OPENCV
#include "opencv2/highgui/highgui_c.h"
#endif
static char *voc_names[] = {"aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"};
void train_detector(char *cfgfile, char *weightfile)
void train_detector(char *datacfg, char *cfgfile, char *weightfile, int clear)
{
    char *train_images = "/data/voc/train.txt";
    char *backup_directory = "/home/pjreddie/backup/";
    list *options = read_data_cfg(datacfg);
    char *train_images = option_find_str(options, "train", "data/train.list");
    char *backup_directory = option_find_str(options, "backup", "/backup/");
    srand(time(0));
    char *base = basecfg(cfgfile);
    printf("%s\n", base);
@@ -24,6 +25,7 @@
    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*net.subdivisions;
    int i = *net.seen/imgs;
@@ -124,8 +126,13 @@
    }
}
void validate_detector(char *cfgfile, char *weightfile)
void validate_detector(char *datacfg, char *cfgfile, char *weightfile)
{
    list *options = read_data_cfg(datacfg);
    char *valid_images = option_find_str(options, "valid", "data/train.list");
    char *name_list = option_find_str(options, "names", "data/names.list");
    char **names = get_labels(name_list);
    network net = parse_network_cfg(cfgfile);
    if(weightfile){
        load_weights(&net, weightfile);
@@ -135,9 +142,7 @@
    srand(time(0));
    char *base = "results/comp4_det_test_";
    //list *plist = get_paths("data/voc.2007.test");
    list *plist = get_paths("/home/pjreddie/data/voc/2007_test.txt");
    //list *plist = get_paths("data/voc.2012.test");
    list *plist = get_paths(valid_images);
    char **paths = (char **)list_to_array(plist);
    layer l = net.layers[net.n-1];
@@ -147,7 +152,7 @@
    FILE **fps = calloc(classes, sizeof(FILE *));
    for(j = 0; j < classes; ++j){
        char buff[1024];
        snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
        snprintf(buff, 1024, "%s%s.txt", base, names[j]);
        fps[j] = fopen(buff, "w");
    }
    box *boxes = calloc(l.w*l.h*l.n, sizeof(box));
@@ -224,7 +229,6 @@
    fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
    srand(time(0));
    char *base = "results/comp4_det_test_";
    list *plist = get_paths("data/voc.2007.test");
    char **paths = (char **)list_to_array(plist);
@@ -232,12 +236,6 @@
    int classes = l.classes;
    int j, k;
    FILE **fps = calloc(classes, sizeof(FILE *));
    for(j = 0; j < classes; ++j){
        char buff[1024];
        snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
        fps[j] = fopen(buff, "w");
    }
    box *boxes = calloc(l.w*l.h*l.n, sizeof(box));
    float **probs = calloc(l.w*l.h*l.n, sizeof(float *));
    for(j = 0; j < l.w*l.h*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));
@@ -299,9 +297,13 @@
    }
}
void test_detector(char *cfgfile, char *weightfile, char *filename, float thresh)
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh)
{
    image *alphabet = load_alphabet();
    list *options = read_data_cfg(datacfg);
        char *name_list = option_find_str(options, "names", "data/names.list");
        char **names = get_labels(name_list);
    image **alphabet = load_alphabet();
    network net = parse_network_cfg(cfgfile);
    if(weightfile){
        load_weights(&net, weightfile);
@@ -335,8 +337,7 @@
        printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
        get_region_boxes(l, 1, 1, thresh, probs, boxes, 0);
        if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms);
        //draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, voc_names, voc_labels, 20);
        draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, voc_names, alphabet, 20);
        draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, names, alphabet, l.classes);
        save_image(im, "predictions");
        show_image(im, "predictions");
@@ -360,13 +361,21 @@
        fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
        return;
    }
    int clear = find_arg(argc, argv, "-clear");
    char *cfg = argv[3];
    char *weights = (argc > 4) ? argv[4] : 0;
    char *filename = (argc > 5) ? argv[5]: 0;
    if(0==strcmp(argv[2], "test")) test_detector(cfg, weights, filename, thresh);
    else if(0==strcmp(argv[2], "train")) train_detector(cfg, weights);
    else if(0==strcmp(argv[2], "valid")) validate_detector(cfg, weights);
    char *datacfg = argv[3];
    char *cfg = argv[4];
    char *weights = (argc > 5) ? argv[5] : 0;
    char *filename = (argc > 6) ? argv[6]: 0;
    if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh);
    else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, clear);
    else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights);
    else if(0==strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights);
    else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, cam_index, filename, voc_names, 20, frame_skip, prefix);
    else if(0==strcmp(argv[2], "demo")) {
        list *options = read_data_cfg(datacfg);
        int classes = option_find_int(options, "classes", 20);
        char *name_list = option_find_str(options, "names", "data/names.list");
        char **names = get_labels(name_list);
        demo(cfg, weights, thresh, cam_index, filename, names, classes, frame_skip, prefix);
    }
}
src/image.c
@@ -53,6 +53,7 @@
        for(y = 0; y < b.h; ++y){
            for(x = 0; x < b.w; ++x){
                float val = get_pixel_extend(a, x - border, y - border, k);
                if(x - border < 0 || x - border >= a.w || y - border < 0 || y - border >= a.h) val = 1;
                set_pixel(b, x, y, k, val);
            }
        }
@@ -70,12 +71,13 @@
    return c;
}
image get_label(image *characters, char *string)
image get_label(image **characters, char *string, int size)
{
    if(size > 7) size = 7;
    image label = make_empty_image(0,0,0);
    while(*string){
        image l = characters[(int)*string];
        image n = tile_images(label, l, -4);
        image l = characters[size][(int)*string];
        image n = tile_images(label, l, -size - 1 + (size+1)/2);
        free_image(label);
        label = n;
        ++string;
@@ -87,24 +89,19 @@
void draw_label(image a, int r, int c, image label, const float *rgb)
{
    float ratio = (float) label.w / label.h;
    int h = a.h * .04;
    h = label.h;
    h = a.h * .06;
    int w = ratio * h;
    image rl = resize_image(label, w, h);
    int w = label.w;
    int h = label.h;
    if (r - h >= 0) r = r - h;
    int i, j, k;
    for(j = 0; j < h && j + r < a.h; ++j){
        for(i = 0; i < w && i + c < a.w; ++i){
            for(k = 0; k < label.c; ++k){
                float val = get_pixel(rl, i, j, k);
                float val = get_pixel(label, i, j, k);
                set_pixel(a, i+c, j+r, k, rgb[k] * val);
            }
        }
    }
    free_image(rl);
}
void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b)
@@ -164,19 +161,23 @@
    }
}
image *load_alphabet()
image **load_alphabet()
{
    int i;
    image *alphabet = calloc(128, sizeof(image));
    for(i = 32; i < 127; ++i){
        char buff[256];
        sprintf(buff, "data/labels/%d.png", i);
        alphabet[i] = load_image_color(buff, 0, 0);
    int i, j;
    const int nsize = 8;
    image **alphabets = calloc(nsize, sizeof(image));
    for(j = 0; j < nsize; ++j){
        alphabets[j] = calloc(128, sizeof(image));
        for(i = 32; i < 127; ++i){
            char buff[256];
            sprintf(buff, "data/labels/%d_%d.png", i, j);
            alphabets[j][i] = load_image_color(buff, 0, 0);
        }
    }
    return alphabet;
    return alphabets;
}
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image *alphabet, int classes)
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
    int i;
@@ -212,7 +213,7 @@
            draw_box_width(im, left, top, right, bot, width, red, green, blue);
            if (alphabet) {
                image label = get_label(alphabet, names[class]);
                image label = get_label(alphabet, names[class], (im.h*.03)/10);
                draw_label(im, top + width, left, label, rgb);
            }
        }
src/image.h
@@ -22,7 +22,7 @@
void draw_bbox(image a, box bbox, int w, float r, float g, float b);
void draw_label(image a, int r, int c, image label, const float *rgb);
void write_label(image a, int r, int c, image *characters, char *string, float *rgb);
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image *labels, int classes);
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **labels, int classes);
image image_distance(image a, image b);
void scale_image(image m, float s);
image crop_image(image im, int dx, int dy, int w, int h);
@@ -69,7 +69,7 @@
image copy_image(image p);
image load_image(char *filename, int w, int h, int c);
image load_image_color(char *filename, int w, int h);
image *load_alphabet();
image **load_alphabet();
float get_pixel(image m, int x, int y, int c);
float get_pixel_extend(image m, int x, int y, int c);
src/layer.h
@@ -101,8 +101,11 @@
    float *m_gpu;
    float *v_gpu;
    int t;
    float *m;
    float *v;
    tree *softmax_tree;
    int  *map;
    float alpha;
    float beta;
@@ -112,6 +115,7 @@
    float object_scale;
    float noobject_scale;
    float class_scale;
    int bias_match;
    int random;
    int dontload;
src/option_list.c
@@ -2,6 +2,35 @@
#include <stdio.h>
#include <string.h>
#include "option_list.h"
#include "utils.h"
list *read_data_cfg(char *filename)
{
    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;
        }
    }
    fclose(file);
    return options;
}
int read_option(char *s, list *options)
{
src/option_list.h
@@ -9,6 +9,7 @@
} kvp;
list *read_data_cfg(char *filename);
int read_option(char *s, list *options);
void option_insert(list *l, char *key, char *val);
char *option_find(list *l, char *key);
src/parser.c
@@ -232,6 +232,21 @@
    return layer;
}
int *read_map(char *filename)
{
    int n = 0;
    int *map = 0;
    char *str;
    FILE *file = fopen(filename, "r");
    if(!file) file_error(filename);
    while((str=fgetl(file))){
        ++n;
        map = realloc(map, n*sizeof(int));
        map[n-1] = atoi(str);
    }
    return map;
}
layer parse_region(list *options, size_params params)
{
    int coords = option_find_int(options, "coords", 4);
@@ -256,6 +271,27 @@
    l.object_scale = option_find_float(options, "object_scale", 1);
    l.noobject_scale = option_find_float(options, "noobject_scale", 1);
    l.class_scale = option_find_float(options, "class_scale", 1);
    l.bias_match = option_find_int_quiet(options, "bias_match",0);
    char *tree_file = option_find_str(options, "tree", 0);
    if (tree_file) l.softmax_tree = read_tree(tree_file);
    char *map_file = option_find_str(options, "map", 0);
    if (map_file) l.map = read_map(map_file);
    char *a = option_find_str(options, "anchors", 0);
    if(a){
        int len = strlen(a);
        int n = 1;
        int i;
        for(i = 0; i < len; ++i){
            if (a[i] == ',') ++n;
        }
        for(i = 0; i < n; ++i){
            float bias = atof(a);
            l.biases[i] = bias;
            a = strchr(a, ',')+1;
        }
    }
    return l;
}
detection_layer parse_detection(list *options, size_params params)
@@ -759,6 +795,10 @@
        fwrite(l.rolling_variance, sizeof(float), l.n, fp);
    }
    fwrite(l.weights, sizeof(float), num, fp);
    if(l.adam){
        fwrite(l.m, sizeof(float), num, fp);
        fwrite(l.v, sizeof(float), num, fp);
    }
}
void save_batchnorm_weights(layer l, FILE *fp)
@@ -937,13 +977,27 @@
        //return;
    }
    int num = l.n*l.c*l.size*l.size;
    fread(l.biases, sizeof(float), l.n, fp);
    if (l.batch_normalize && (!l.dontloadscales)){
        fread(l.scales, sizeof(float), l.n, fp);
        fread(l.rolling_mean, sizeof(float), l.n, fp);
        fread(l.rolling_variance, sizeof(float), l.n, fp);
    if(0){
        fread(l.biases + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
        if (l.batch_normalize && (!l.dontloadscales)){
            fread(l.scales + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
            fread(l.rolling_mean + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
            fread(l.rolling_variance + ((l.n != 1374)?0:5), sizeof(float), l.n, fp);
        }
        fread(l.weights + ((l.n != 1374)?0:5*l.c*l.size*l.size), sizeof(float), num, fp);
    }else{
        fread(l.biases, sizeof(float), l.n, fp);
        if (l.batch_normalize && (!l.dontloadscales)){
            fread(l.scales, sizeof(float), l.n, fp);
            fread(l.rolling_mean, sizeof(float), l.n, fp);
            fread(l.rolling_variance, sizeof(float), l.n, fp);
        }
        fread(l.weights, sizeof(float), num, fp);
    }
    fread(l.weights, sizeof(float), num, fp);
    if(l.adam){
        fread(l.m, sizeof(float), num, fp);
        fread(l.v, sizeof(float), num, fp);
    }
    //if(l.c == 3) scal_cpu(num, 1./256, l.weights, 1);
    if (l.flipped) {
        transpose_matrix(l.weights, l.c*l.size*l.size, l.n);
src/region_layer.c
@@ -48,11 +48,17 @@
    return l;
}
#define LOG 1
box get_region_box(float *x, float *biases, int n, int index, int i, int j, int w, int h)
{
    box b;
    b.x = (i + .5)/w + x[index + 0] * biases[2*n];
    b.y = (j + .5)/h + x[index + 1] * biases[2*n + 1];
    if(LOG){
        b.x = (i + logistic_activate(x[index + 0])) / w;
        b.y = (j + logistic_activate(x[index + 1])) / h;
    }
    b.w = exp(x[index + 2]) * biases[2*n];
    b.h = exp(x[index + 3]) * biases[2*n+1];
    return b;
@@ -65,11 +71,19 @@
    float tx = (truth.x - (i + .5)/w) / biases[2*n];
    float ty = (truth.y - (j + .5)/h) / biases[2*n + 1];
    if(LOG){
        tx = (truth.x*w - i);
        ty = (truth.y*h - j);
    }
    float tw = log(truth.w / biases[2*n]);
    float th = log(truth.h / biases[2*n + 1]);
    delta[index + 0] = scale * (tx - x[index + 0]);
    delta[index + 1] = scale * (ty - x[index + 1]);
    if(LOG){
        delta[index + 0] = scale * (tx - logistic_activate(x[index + 0])) * logistic_gradient(logistic_activate(x[index + 0]));
        delta[index + 1] = scale * (ty - logistic_activate(x[index + 1])) * logistic_gradient(logistic_activate(x[index + 1]));
    }
    delta[index + 2] = scale * (tw - x[index + 2]);
    delta[index + 3] = scale * (th - x[index + 3]);
    return iou;
@@ -85,8 +99,7 @@
    return (x != x);
}
#define LOG 0
void softmax_tree(float *input, int batch, int inputs, float temp, tree *hierarchy, float *output);
void forward_region_layer(const region_layer l, network_state state)
{
    int i,j,b,t,n;
@@ -97,7 +110,9 @@
        for(i = 0; i < l.h*l.w*l.n; ++i){
            int index = size*i + b*l.outputs;
            l.output[index + 4] = logistic_activate(l.output[index + 4]);
            if(l.softmax){
            if(l.softmax_tree){
                softmax_tree(l.output + index + 5, 1, 0, 1, l.softmax_tree, l.output + index + 5);
            } else if(l.softmax){
                softmax(l.output + index + 5, l.classes, 1, l.output + index + 5);
            }
        }
@@ -128,12 +143,12 @@
                    l.delta[index + 4] = l.noobject_scale * ((0 - l.output[index + 4]) * logistic_gradient(l.output[index + 4]));
                    if(best_iou > .5) l.delta[index + 4] = 0;
                    if(*(state.net.seen) < 6400){
                    if(*(state.net.seen) < 12800){
                        box truth = {0};
                        truth.x = (i + .5)/l.w;
                        truth.y = (j + .5)/l.h;
                        truth.w = .5;
                        truth.h = .5;
                        truth.w = l.biases[2*n];
                        truth.h = l.biases[2*n+1];
                        delta_region_box(truth, l.output, l.biases, n, index, i, j, l.w, l.h, l.delta, .01);
                        //l.delta[index + 0] = .1 * (0 - l.output[index + 0]);
                        //l.delta[index + 1] = .1 * (0 - l.output[index + 1]);
@@ -145,7 +160,7 @@
        }
        for(t = 0; t < 30; ++t){
            box truth = float_to_box(state.truth + t*5 + b*l.truths);
            int class = state.truth[t*5 + b*l.truths + 4];
            if(!truth.x) break;
            float best_iou = 0;
            int best_index = 0;
@@ -160,7 +175,11 @@
            for(n = 0; n < l.n; ++n){
                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);
                printf("pred: (%f, %f) %f x %f\n", pred.x*l.w - i - .5, pred.y * l.h - j - .5, pred.w, pred.h);
                if(l.bias_match){
                    pred.w = l.biases[2*n];
                    pred.h = l.biases[2*n+1];
                }
                printf("pred: (%f, %f) %f x %f\n", pred.x, pred.y, pred.w, pred.h);
                pred.x = 0;
                pred.y = 0;
                float iou = box_iou(pred, truth_shift);
@@ -170,7 +189,7 @@
                    best_n = n;
                }
            }
            printf("%d %f (%f, %f) %f x %f\n", best_n, best_iou, truth.x * l.w - i - .5, truth.y*l.h - j - .5, truth.w, truth.h);
            printf("%d %f (%f, %f) %f x %f\n", best_n, best_iou, truth.x, truth.y, truth.w, truth.h);
            float iou = delta_region_box(truth, l.output, l.biases, best_n, best_index, i, j, l.w, l.h, l.delta, l.coord_scale);
            if(iou > .5) recall += 1;
@@ -182,41 +201,32 @@
            if (l.rescore) {
                l.delta[best_index + 4] = l.object_scale * (iou - l.output[best_index + 4]) * logistic_gradient(l.output[best_index + 4]);
            }
            //printf("%f\n", l.delta[best_index+1]);
            /*
               if(isnan(l.delta[best_index+1])){
               printf("%f\n", true_scale);
               printf("%f\n", l.output[best_index + 1]);
               printf("%f\n", truth.w);
               printf("%f\n", truth.h);
               error("bad");
               }
             */
            for(n = 0; n < l.classes; ++n){
                l.delta[best_index + 5 + n] = l.class_scale * (((n == class)?1 : 0) - l.output[best_index + 5 + n]);
                if(n == class) avg_cat += l.output[best_index + 5 + n];
            }
            /*
               if(0){
               printf("truth: %f %f %f %f\n", truth.x, truth.y, truth.w, truth.h);
               printf("pred: %f %f %f %f\n\n", pred.x, pred.y, pred.w, pred.h);
               float aspect = exp(true_aspect);
               float scale  = logistic_activate(true_scale);
               float move_x = true_dx;
               float move_y = true_dy;
               box b;
               b.w = sqrt(scale * aspect);
               b.h = b.w * 1./aspect;
               b.x = move_x * b.w + (i + .5)/l.w;
               b.y = move_y * b.h + (j + .5)/l.h;
               printf("%f %f\n", b.x, truth.x);
               printf("%f %f\n", b.y, truth.y);
               printf("%f %f\n", b.w, truth.w);
               printf("%f %f\n", b.h, truth.h);
            //printf("%f\n", box_iou(b, truth));
            int class = state.truth[t*5 + b*l.truths + 4];
            if (l.map) class = l.map[class];
            if(l.softmax_tree){
                float pred = 1;
                while(class >= 0){
                    pred *= l.output[best_index + 5 + class];
                    int g = l.softmax_tree->group[class];
                    int i;
                    int offset = l.softmax_tree->group_offset[g];
                    for(i = 0; i < l.softmax_tree->group_size[g]; ++i){
                        int index = best_index + 5 + offset + i;
                        l.delta[index] = l.class_scale * (0 - l.output[index]);
                    }
                    l.delta[best_index + 5 + class] = l.class_scale * (1 - l.output[best_index + 5 + class]);
                    class = l.softmax_tree->parent[class];
                }
                avg_cat += pred;
            } else {
                for(n = 0; n < l.classes; ++n){
                    l.delta[best_index + 5 + n] = l.class_scale * (((n == class)?1 : 0) - l.output[best_index + 5 + n]);
                    if(n == class) avg_cat += l.output[best_index + 5 + n];
                }
            }
             */
            ++count;
        }
    }
@@ -244,24 +254,31 @@
            int p_index = index * (l.classes + 5) + 4;
            float scale = predictions[p_index];
            int box_index = index * (l.classes + 5);
            boxes[index].x = (predictions[box_index + 0] + col + .5) / l.w * w;
            boxes[index].y = (predictions[box_index + 1] + row + .5) / l.h * h;
            if(0){
                boxes[index].x = (logistic_activate(predictions[box_index + 0]) + col) / l.w * w;
                boxes[index].y = (logistic_activate(predictions[box_index + 1]) + row) / l.h * h;
            }
            boxes[index].w = pow(logistic_activate(predictions[box_index + 2]), (l.sqrt?2:1)) * w;
            boxes[index].h = pow(logistic_activate(predictions[box_index + 3]), (l.sqrt?2:1)) * h;
            if(1){
                boxes[index].x = ((col + .5)/l.w + predictions[box_index + 0] * .5) * w;
                boxes[index].y = ((row + .5)/l.h + predictions[box_index + 1] * .5) * h;
                boxes[index].w = (exp(predictions[box_index + 2]) * .5) * w;
                boxes[index].h = (exp(predictions[box_index + 3]) * .5) * h;
            }
            for(j = 0; j < l.classes; ++j){
                int class_index = index * (l.classes + 5) + 5;
                float prob = scale*predictions[class_index+j];
                probs[index][j] = (prob > thresh) ? prob : 0;
            boxes[index] = get_region_box(predictions, l.biases, n, box_index, col, row, l.w, l.h);
            boxes[index].x *= w;
            boxes[index].y *= h;
            boxes[index].w *= w;
            boxes[index].h *= h;
            int class_index = index * (l.classes + 5) + 5;
            if(l.softmax_tree){
                hierarchy_predictions(predictions + class_index, l.classes, l.softmax_tree, 0);
                int found = 0;
                for(j = l.classes - 1; j >= 0; --j){
                    if(!found && predictions[class_index + j] > .5){
                        found = 1;
                    } else {
                        predictions[class_index + j] = 0;
                    }
                    float prob = predictions[class_index+j];
                    probs[index][j] = (scale > thresh) ? prob : 0;
                }
            }else{
                for(j = 0; j < l.classes; ++j){
                    float prob = scale*predictions[class_index+j];
                    probs[index][j] = (prob > thresh) ? prob : 0;
                }
            }
            if(only_objectness){
                probs[index][0] = scale;
src/softmax_layer.c
@@ -32,21 +32,27 @@
    return l;
}
void softmax_tree(float *input, int batch, int inputs, float temp, tree *hierarchy, float *output)
{
    int b;
    for(b = 0; b < batch; ++b){
        int i;
        int count = 0;
        for(i = 0; i < hierarchy->groups; ++i){
            int group_size = hierarchy->group_size[i];
            softmax(input+b*inputs + count, group_size, temp, output+b*inputs + count);
            count += group_size;
        }
    }
}
void forward_softmax_layer(const softmax_layer l, network_state state)
{
    int b;
    int inputs = l.inputs / l.groups;
    int batch = l.batch * l.groups;
    if(l.softmax_tree){
        for(b = 0; b < batch; ++b){
            int i;
            int count = 0;
            for(i = 0; i < l.softmax_tree->groups; ++i){
                int group_size = l.softmax_tree->group_size[i];
                softmax(state.input+b*inputs + count, group_size, l.temperature, l.output+b*inputs + count);
                count += group_size;
            }
        }
        softmax_tree(state.input, batch, inputs, l.temperature, l.softmax_tree, l.output);
    } else {
        for(b = 0; b < batch; ++b){
            softmax(state.input+b*inputs, inputs, l.temperature, l.output+b*inputs);
src/tree.c
@@ -2,6 +2,43 @@
#include <stdlib.h>
#include "tree.h"
#include "utils.h"
#include "data.h"
void change_leaves(tree *t, char *leaf_list)
{
    list *llist = get_paths(leaf_list);
    char **leaves = (char **)list_to_array(llist);
    int n = llist->size;
    int i,j;
    int found = 0;
    for(i = 0; i < t->n; ++i){
        t->leaf[i] = 0;
        for(j = 0; j < n; ++j){
            if (0==strcmp(t->name[i], leaves[j])){
                t->leaf[i] = 1;
                ++found;
                break;
            }
        }
    }
    fprintf(stderr, "Found %d leaves.\n", found);
}
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves)
{
    int j;
    for(j = 0; j < n; ++j){
        int parent = hier->parent[j];
        if(parent >= 0){
            predictions[j] *= predictions[parent];
        }
    }
    if(only_leaves){
        for(j = 0; j < n; ++j){
            if(!hier->leaf[j]) predictions[j] = 0;
        }
    }
}
tree *read_tree(char *filename)
{
@@ -19,19 +56,26 @@
        sscanf(line, "%s %d", id, &parent);
        t.parent = realloc(t.parent, (n+1)*sizeof(int));
        t.parent[n] = parent;
        t.name = realloc(t.name, (n+1)*sizeof(char *));
        t.name[n] = id;
        if(parent != last_parent){
            ++groups;
            t.group_offset = realloc(t.group_offset, groups * sizeof(int));
            t.group_offset[groups - 1] = n - group_size;
            t.group_size = realloc(t.group_size, groups * sizeof(int));
            t.group_size[groups - 1] = group_size;
            group_size = 0;
            last_parent = parent;
        }
        t.group = realloc(t.group, (n+1)*sizeof(int));
        t.group[n] = groups;
        ++n;
        ++group_size;
    }
    ++groups;
    t.group_offset = realloc(t.group_offset, groups * sizeof(int));
    t.group_offset[groups - 1] = n - group_size;
    t.group_size = realloc(t.group_size, groups * sizeof(int));
    t.group_size[groups - 1] = group_size;
    t.n = n;
src/tree.h
@@ -5,12 +5,16 @@
    int *leaf;
    int n;
    int *parent;
    int *group;
    char **name;
    int groups;
    int *group_size;
    int *group_offset;
} tree;
tree *read_tree(char *filename);
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves);
void change_leaves(tree *t, char *leaf_list);
#endif
src/yolo.c
@@ -284,7 +284,7 @@
void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh)
{
    image *alphabet = load_alphabet();
    image **alphabet = load_alphabet();
    network net = parse_network_cfg(cfgfile);
    if(weightfile){
        load_weights(&net, weightfile);