diff --git a/data/labels/100.png b/data/labels/100.png deleted file mode 100644 index 6f9f2f59..00000000 Binary files a/data/labels/100.png and /dev/null differ diff --git a/data/labels/101.png b/data/labels/101.png deleted file mode 100644 index 780ac5fc..00000000 Binary files a/data/labels/101.png and /dev/null differ diff --git a/data/labels/102.png b/data/labels/102.png deleted file mode 100644 index 7500d0d1..00000000 Binary files a/data/labels/102.png and /dev/null differ diff --git a/data/labels/103.png b/data/labels/103.png deleted file mode 100644 index 4b2f7861..00000000 Binary files a/data/labels/103.png and /dev/null differ diff --git a/data/labels/104.png b/data/labels/104.png deleted file mode 100644 index 50b58ee0..00000000 Binary files a/data/labels/104.png and /dev/null differ diff --git a/data/labels/105.png b/data/labels/105.png deleted file mode 100644 index f45299ab..00000000 Binary files a/data/labels/105.png and /dev/null differ diff --git a/data/labels/106.png b/data/labels/106.png deleted file mode 100644 index 5efaa6d3..00000000 Binary files a/data/labels/106.png and /dev/null differ diff --git a/data/labels/107.png b/data/labels/107.png deleted file mode 100644 index ba3793ee..00000000 Binary files a/data/labels/107.png and /dev/null differ diff --git a/data/labels/108.png b/data/labels/108.png deleted file mode 100644 index eecb2168..00000000 Binary files a/data/labels/108.png and /dev/null differ diff --git a/data/labels/109.png b/data/labels/109.png deleted file mode 100644 index cc2b35fd..00000000 Binary files a/data/labels/109.png and /dev/null differ diff --git a/data/labels/110.png b/data/labels/110.png deleted file mode 100644 index 9e712a8c..00000000 Binary files a/data/labels/110.png and /dev/null differ diff --git a/data/labels/111.png b/data/labels/111.png deleted file mode 100644 index ef920c5b..00000000 Binary files a/data/labels/111.png and /dev/null differ diff --git a/data/labels/112.png b/data/labels/112.png deleted file mode 100644 index 8b45c89d..00000000 Binary files a/data/labels/112.png and /dev/null differ diff --git a/data/labels/113.png b/data/labels/113.png deleted file mode 100644 index c6432d86..00000000 Binary files a/data/labels/113.png and /dev/null differ diff --git a/data/labels/114.png b/data/labels/114.png deleted file mode 100644 index c99d94a5..00000000 Binary files a/data/labels/114.png and /dev/null differ diff --git a/data/labels/115.png b/data/labels/115.png deleted file mode 100644 index 567c48a2..00000000 Binary files a/data/labels/115.png and /dev/null differ diff --git a/data/labels/116.png b/data/labels/116.png deleted file mode 100644 index 5827df5c..00000000 Binary files a/data/labels/116.png and /dev/null differ diff --git a/data/labels/117.png b/data/labels/117.png deleted file mode 100644 index 65f05e77..00000000 Binary files a/data/labels/117.png and /dev/null differ diff --git a/data/labels/118.png b/data/labels/118.png deleted file mode 100644 index a84cd13c..00000000 Binary files a/data/labels/118.png and /dev/null differ diff --git a/data/labels/119.png b/data/labels/119.png deleted file mode 100644 index d4fb1e95..00000000 Binary files a/data/labels/119.png and /dev/null differ diff --git a/data/labels/120.png b/data/labels/120.png deleted file mode 100644 index e2fb2ca1..00000000 Binary files a/data/labels/120.png and /dev/null differ diff --git a/data/labels/121.png b/data/labels/121.png deleted file mode 100644 index 11901950..00000000 Binary files a/data/labels/121.png and /dev/null differ diff --git a/data/labels/122.png b/data/labels/122.png deleted file mode 100644 index 1a33dac7..00000000 Binary files a/data/labels/122.png and /dev/null differ diff --git a/data/labels/123.png b/data/labels/123.png deleted file mode 100644 index 63c1532a..00000000 Binary files a/data/labels/123.png and /dev/null differ diff --git a/data/labels/124.png b/data/labels/124.png deleted file mode 100644 index f595ac8a..00000000 Binary files a/data/labels/124.png and /dev/null differ diff --git a/data/labels/125.png b/data/labels/125.png deleted file mode 100644 index baf5da24..00000000 Binary files a/data/labels/125.png and /dev/null differ diff --git a/data/labels/126.png b/data/labels/126.png deleted file mode 100644 index 18fe078c..00000000 Binary files a/data/labels/126.png and /dev/null differ diff --git a/data/labels/32.png b/data/labels/32.png deleted file mode 100644 index 03f56cbf..00000000 Binary files a/data/labels/32.png and /dev/null differ diff --git a/data/labels/33.png b/data/labels/33.png deleted file mode 100644 index dbdc66f0..00000000 Binary files a/data/labels/33.png and /dev/null differ diff --git a/data/labels/34.png b/data/labels/34.png deleted file mode 100644 index fafd8417..00000000 Binary files a/data/labels/34.png and /dev/null differ diff --git a/data/labels/35.png b/data/labels/35.png deleted file mode 100644 index 1a5cf1bd..00000000 Binary files a/data/labels/35.png and /dev/null differ diff --git a/data/labels/36.png b/data/labels/36.png deleted file mode 100644 index 0679d7d0..00000000 Binary files a/data/labels/36.png and /dev/null differ diff --git a/data/labels/37.png b/data/labels/37.png deleted file mode 100644 index 09035f02..00000000 Binary files a/data/labels/37.png and /dev/null differ diff --git a/data/labels/38.png b/data/labels/38.png deleted file mode 100644 index 6720b274..00000000 Binary files a/data/labels/38.png and /dev/null differ diff --git a/data/labels/39.png b/data/labels/39.png deleted file mode 100644 index 1483788a..00000000 Binary files a/data/labels/39.png and /dev/null differ diff --git a/data/labels/40.png b/data/labels/40.png deleted file mode 100644 index bc3193c4..00000000 Binary files a/data/labels/40.png and /dev/null differ diff --git a/data/labels/41.png b/data/labels/41.png deleted file mode 100644 index 83fa209e..00000000 Binary files a/data/labels/41.png and /dev/null differ diff --git a/data/labels/42.png b/data/labels/42.png deleted file mode 100644 index c626053c..00000000 Binary files a/data/labels/42.png and /dev/null differ diff --git a/data/labels/43.png b/data/labels/43.png deleted file mode 100644 index 1a820153..00000000 Binary files a/data/labels/43.png and /dev/null differ diff --git a/data/labels/44.png b/data/labels/44.png deleted file mode 100644 index 02c5f307..00000000 Binary files a/data/labels/44.png and /dev/null differ diff --git a/data/labels/45.png b/data/labels/45.png deleted file mode 100644 index 3f23ed86..00000000 Binary files a/data/labels/45.png and /dev/null differ diff --git a/data/labels/46.png b/data/labels/46.png deleted file mode 100644 index fb834e6b..00000000 Binary files a/data/labels/46.png and /dev/null differ diff --git a/data/labels/47.png b/data/labels/47.png deleted file mode 100644 index facc7c7b..00000000 Binary files a/data/labels/47.png and /dev/null differ diff --git a/data/labels/48.png b/data/labels/48.png deleted file mode 100644 index bad34852..00000000 Binary files a/data/labels/48.png and /dev/null differ diff --git a/data/labels/49.png b/data/labels/49.png deleted file mode 100644 index 14d91ded..00000000 Binary files a/data/labels/49.png and /dev/null differ diff --git a/data/labels/50.png b/data/labels/50.png deleted file mode 100644 index 6360e06a..00000000 Binary files a/data/labels/50.png and /dev/null differ diff --git a/data/labels/51.png b/data/labels/51.png deleted file mode 100644 index 97c57301..00000000 Binary files a/data/labels/51.png and /dev/null differ diff --git a/data/labels/52.png b/data/labels/52.png deleted file mode 100644 index c69eaf63..00000000 Binary files a/data/labels/52.png and /dev/null differ diff --git a/data/labels/53.png b/data/labels/53.png deleted file mode 100644 index db833ad0..00000000 Binary files a/data/labels/53.png and /dev/null differ diff --git a/data/labels/54.png b/data/labels/54.png deleted file mode 100644 index 9d6134dc..00000000 Binary files a/data/labels/54.png and /dev/null differ diff --git a/data/labels/55.png b/data/labels/55.png deleted file mode 100644 index c74c1723..00000000 Binary files a/data/labels/55.png and /dev/null differ diff --git a/data/labels/56.png b/data/labels/56.png deleted file mode 100644 index eaa59ae6..00000000 Binary files a/data/labels/56.png and /dev/null differ diff --git a/data/labels/57.png b/data/labels/57.png deleted file mode 100644 index 9c849ce3..00000000 Binary files a/data/labels/57.png and /dev/null differ diff --git a/data/labels/58.png b/data/labels/58.png deleted file mode 100644 index 43513433..00000000 Binary files a/data/labels/58.png and /dev/null differ diff --git a/data/labels/59.png b/data/labels/59.png deleted file mode 100644 index 4d3261a6..00000000 Binary files a/data/labels/59.png and /dev/null differ diff --git a/data/labels/60.png b/data/labels/60.png deleted file mode 100644 index a18cd000..00000000 Binary files a/data/labels/60.png and /dev/null differ diff --git a/data/labels/61.png b/data/labels/61.png deleted file mode 100644 index f40d01ef..00000000 Binary files a/data/labels/61.png and /dev/null differ diff --git a/data/labels/62.png b/data/labels/62.png deleted file mode 100644 index 09331955..00000000 Binary files a/data/labels/62.png and /dev/null differ diff --git a/data/labels/63.png b/data/labels/63.png deleted file mode 100644 index 9d79c47a..00000000 Binary files a/data/labels/63.png and /dev/null differ diff --git a/data/labels/64.png b/data/labels/64.png deleted file mode 100644 index 699d5661..00000000 Binary files a/data/labels/64.png and /dev/null differ diff --git a/data/labels/65.png b/data/labels/65.png deleted file mode 100644 index 27515212..00000000 Binary files a/data/labels/65.png and /dev/null differ diff --git a/data/labels/66.png b/data/labels/66.png deleted file mode 100644 index af54954d..00000000 Binary files a/data/labels/66.png and /dev/null differ diff --git a/data/labels/67.png b/data/labels/67.png deleted file mode 100644 index 256d68e0..00000000 Binary files a/data/labels/67.png and /dev/null differ diff --git a/data/labels/68.png b/data/labels/68.png deleted file mode 100644 index f824c02c..00000000 Binary files a/data/labels/68.png and /dev/null differ diff --git a/data/labels/69.png b/data/labels/69.png deleted file mode 100644 index 8d8d290a..00000000 Binary files a/data/labels/69.png and /dev/null differ diff --git a/data/labels/70.png b/data/labels/70.png deleted file mode 100644 index 4c843a14..00000000 Binary files a/data/labels/70.png and /dev/null differ diff --git a/data/labels/71.png b/data/labels/71.png deleted file mode 100644 index 6bf073c8..00000000 Binary files a/data/labels/71.png and /dev/null differ diff --git a/data/labels/72.png b/data/labels/72.png deleted file mode 100644 index 1f665557..00000000 Binary files a/data/labels/72.png and /dev/null differ diff --git a/data/labels/73.png b/data/labels/73.png deleted file mode 100644 index 3bf27833..00000000 Binary files a/data/labels/73.png and /dev/null differ diff --git a/data/labels/74.png b/data/labels/74.png deleted file mode 100644 index fdca39da..00000000 Binary files a/data/labels/74.png and /dev/null differ diff --git a/data/labels/75.png b/data/labels/75.png deleted file mode 100644 index 15533941..00000000 Binary files a/data/labels/75.png and /dev/null differ diff --git a/data/labels/76.png b/data/labels/76.png deleted file mode 100644 index 06d192b9..00000000 Binary files a/data/labels/76.png and /dev/null differ diff --git a/data/labels/77.png b/data/labels/77.png deleted file mode 100644 index 5ef2e73f..00000000 Binary files a/data/labels/77.png and /dev/null differ diff --git a/data/labels/78.png b/data/labels/78.png deleted file mode 100644 index c4dcbd9f..00000000 Binary files a/data/labels/78.png and /dev/null differ diff --git a/data/labels/79.png b/data/labels/79.png deleted file mode 100644 index 1e74b73c..00000000 Binary files a/data/labels/79.png and /dev/null differ diff --git a/data/labels/80.png b/data/labels/80.png deleted file mode 100644 index 1a19fa11..00000000 Binary files a/data/labels/80.png and /dev/null differ diff --git a/data/labels/81.png b/data/labels/81.png deleted file mode 100644 index b09d3a7a..00000000 Binary files a/data/labels/81.png and /dev/null differ diff --git a/data/labels/82.png b/data/labels/82.png deleted file mode 100644 index 4cef220e..00000000 Binary files a/data/labels/82.png and /dev/null differ diff --git a/data/labels/83.png b/data/labels/83.png deleted file mode 100644 index 5ab68a87..00000000 Binary files a/data/labels/83.png and /dev/null differ diff --git a/data/labels/84.png b/data/labels/84.png deleted file mode 100644 index 6d5b72b4..00000000 Binary files a/data/labels/84.png and /dev/null differ diff --git a/data/labels/85.png b/data/labels/85.png deleted file mode 100644 index 734e0431..00000000 Binary files a/data/labels/85.png and /dev/null differ diff --git a/data/labels/86.png b/data/labels/86.png deleted file mode 100644 index 5d090df2..00000000 Binary files a/data/labels/86.png and /dev/null differ diff --git a/data/labels/87.png b/data/labels/87.png deleted file mode 100644 index c9994b43..00000000 Binary files a/data/labels/87.png and /dev/null differ diff --git a/data/labels/88.png b/data/labels/88.png deleted file mode 100644 index e6d2cb5b..00000000 Binary files a/data/labels/88.png and /dev/null differ diff --git a/data/labels/89.png b/data/labels/89.png deleted file mode 100644 index a470d9ae..00000000 Binary files a/data/labels/89.png and /dev/null differ diff --git a/data/labels/90.png b/data/labels/90.png deleted file mode 100644 index 50c7b780..00000000 Binary files a/data/labels/90.png and /dev/null differ diff --git a/data/labels/91.png b/data/labels/91.png deleted file mode 100644 index 2a9affd5..00000000 Binary files a/data/labels/91.png and /dev/null differ diff --git a/data/labels/92.png b/data/labels/92.png deleted file mode 100644 index fbd8ef9f..00000000 Binary files a/data/labels/92.png and /dev/null differ diff --git a/data/labels/93.png b/data/labels/93.png deleted file mode 100644 index e2084aab..00000000 Binary files a/data/labels/93.png and /dev/null differ diff --git a/data/labels/94.png b/data/labels/94.png deleted file mode 100644 index 5c6f7af1..00000000 Binary files a/data/labels/94.png and /dev/null differ diff --git a/data/labels/95.png b/data/labels/95.png deleted file mode 100644 index 2c94ebd5..00000000 Binary files a/data/labels/95.png and /dev/null differ diff --git a/data/labels/96.png b/data/labels/96.png deleted file mode 100644 index 1bab8101..00000000 Binary files a/data/labels/96.png and /dev/null differ diff --git a/data/labels/97.png b/data/labels/97.png deleted file mode 100644 index 2478e62a..00000000 Binary files a/data/labels/97.png and /dev/null differ diff --git a/data/labels/98.png b/data/labels/98.png deleted file mode 100644 index 29d3473a..00000000 Binary files a/data/labels/98.png and /dev/null differ diff --git a/data/labels/99.png b/data/labels/99.png deleted file mode 100644 index ef3f4530..00000000 Binary files a/data/labels/99.png and /dev/null differ diff --git a/data/labels/make_labels.py b/data/labels/make_labels.py index 1dacdc37..c8146f6d 100644 --- a/data/labels/make_labels.py +++ b/data/labels/make_labels.py @@ -2,18 +2,22 @@ import os 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) diff --git a/src/classifier.c b/src/classifier.c index a77f9df8..2ce62078 100644 --- a/src/classifier.c +++ b/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 validate_classifier_full(char *datacfg, char *filename, char *weightfile) } } -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) { diff --git a/src/coco.c b/src/coco.c index 939a08d5..f684feea 100644 --- a/src/coco.c +++ b/src/coco.c @@ -318,7 +318,7 @@ void validate_coco_recall(char *cfgfile, char *weightfile) 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); diff --git a/src/convolutional_kernels.cu b/src/convolutional_kernels.cu index 76a3fb31..709f77fb 100644 --- a/src/convolutional_kernels.cu +++ b/src/convolutional_kernels.cu @@ -215,6 +215,10 @@ void pull_convolutional_layer(convolutional_layer layer) 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 @@ void push_convolutional_layer(convolutional_layer layer) 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) diff --git a/src/convolutional_layer.c b/src/convolutional_layer.c index 888eca33..1d93b3fc 100644 --- a/src/convolutional_layer.c +++ b/src/convolutional_layer.c @@ -235,6 +235,11 @@ convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int 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 @@ convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int 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); diff --git a/src/demo.c b/src/demo.c index 6c653a9f..91562c04 100644 --- a/src/demo.c +++ b/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 @@ double get_wall_time() 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; diff --git a/src/detector.c b/src/detector.c index 1f48c618..e020be5d 100644 --- a/src/detector.c +++ b/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 @@ void train_detector(char *cfgfile, char *weightfile) 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 print_detector_detections(FILE **fps, char *id, box *boxes, float **probs, } } -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 @@ void validate_detector(char *cfgfile, char *weightfile) 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 @@ void validate_detector(char *cfgfile, char *weightfile) 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 @@ void validate_detector_recall(char *cfgfile, char *weightfile) 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 @@ void validate_detector_recall(char *cfgfile, char *weightfile) 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 validate_detector_recall(char *cfgfile, char *weightfile) } } -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 @@ void test_detector(char *cfgfile, char *weightfile, char *filename, float thresh 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 @@ void run_detector(int argc, char **argv) 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); + } } diff --git a/src/image.c b/src/image.c index 52869796..ad7c2d62 100644 --- a/src/image.c +++ b/src/image.c @@ -53,6 +53,7 @@ image border_image(image a, int border) 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 @@ image tile_images(image a, image b, int dx) 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 @@ image get_label(image *characters, char *string) 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 @@ void draw_bbox(image a, box bbox, int w, float r, float g, float b) } } -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 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs, 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); } } diff --git a/src/image.h b/src/image.h index 7e7ecf6d..6e80ac2e 100644 --- a/src/image.h +++ b/src/image.h @@ -22,7 +22,7 @@ void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, flo 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 float_to_image(int w, int h, int c, float *data); 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); diff --git a/src/layer.h b/src/layer.h index e07ea42f..e461fbc6 100644 --- a/src/layer.h +++ b/src/layer.h @@ -101,8 +101,11 @@ struct layer{ 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 @@ struct layer{ float object_scale; float noobject_scale; float class_scale; + int bias_match; int random; int dontload; diff --git a/src/option_list.c b/src/option_list.c index 7d68ead2..f935af30 100644 --- a/src/option_list.c +++ b/src/option_list.c @@ -2,6 +2,35 @@ #include #include #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) { diff --git a/src/option_list.h b/src/option_list.h index d0417aa9..054b3fdd 100644 --- a/src/option_list.h +++ b/src/option_list.h @@ -9,6 +9,7 @@ typedef struct{ } 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); diff --git a/src/parser.c b/src/parser.c index 44ba1c45..09eef42c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -232,6 +232,21 @@ softmax_layer parse_softmax(list *options, size_params params) 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 @@ layer parse_region(list *options, size_params params) 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 @@ void save_convolutional_weights(layer l, FILE *fp) 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 @@ void load_convolutional_weights(layer l, FILE *fp) //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); + } + if(l.adam){ + fread(l.m, sizeof(float), num, fp); + fread(l.v, sizeof(float), num, fp); } - fread(l.weights, 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); diff --git a/src/region_layer.c b/src/region_layer.c index 5f8b3cc9..c3935cad 100644 --- a/src/region_layer.c +++ b/src/region_layer.c @@ -48,11 +48,17 @@ region_layer make_region_layer(int batch, int w, int h, int n, int classes, int 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 delta_region_box(box truth, float *x, float *biases, int n, int index, int 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 @@ float tisnan(float x) 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 @@ void forward_region_layer(const region_layer l, network_state state) 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 @@ void forward_region_layer(const region_layer l, network_state state) 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 @@ void forward_region_layer(const region_layer l, network_state state) } 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 @@ void forward_region_layer(const region_layer l, network_state state) 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 @@ void forward_region_layer(const region_layer l, network_state state) 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 @@ void forward_region_layer(const region_layer l, network_state state) 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 @@ void get_region_boxes(layer l, int w, int h, float thresh, float **probs, box *b 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; diff --git a/src/softmax_layer.c b/src/softmax_layer.c index 31f3e03f..0aa9047f 100644 --- a/src/softmax_layer.c +++ b/src/softmax_layer.c @@ -32,21 +32,27 @@ softmax_layer make_softmax_layer(int batch, int inputs, int groups) 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); diff --git a/src/tree.c b/src/tree.c index 5a758f78..cd9fcd12 100644 --- a/src/tree.c +++ b/src/tree.c @@ -2,6 +2,43 @@ #include #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 @@ tree *read_tree(char *filename) 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; diff --git a/src/tree.h b/src/tree.h index c713866d..b0b0ecef 100644 --- a/src/tree.h +++ b/src/tree.h @@ -5,12 +5,16 @@ typedef struct{ 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 diff --git a/src/yolo.c b/src/yolo.c index fd2fccc9..05f2be6a 100644 --- a/src/yolo.c +++ b/src/yolo.c @@ -284,7 +284,7 @@ void validate_yolo_recall(char *cfgfile, char *weightfile) 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);