18 files modified
95 files deleted
| | |
| | | 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) |
| | | |
| | |
| | | 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)); |
| | |
| | | } |
| | | } |
| | | |
| | | 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) |
| | | { |
| | |
| | | |
| | | 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); |
| | |
| | | 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) |
| | |
| | | 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) |
| | |
| | | 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; |
| | |
| | | |
| | | 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); |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | #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); |
| | |
| | | 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; |
| | |
| | | } |
| | | } |
| | | |
| | | 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); |
| | |
| | | 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]; |
| | |
| | | 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)); |
| | |
| | | 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); |
| | | |
| | |
| | | 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 *)); |
| | |
| | | } |
| | | } |
| | | |
| | | 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); |
| | |
| | | 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"); |
| | | |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | 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; |
| | |
| | | |
| | | 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) |
| | |
| | | } |
| | | } |
| | | |
| | | 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; |
| | | |
| | |
| | | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | float *m_gpu; |
| | | float *v_gpu; |
| | | int t; |
| | | float *m; |
| | | float *v; |
| | | |
| | | tree *softmax_tree; |
| | | int *map; |
| | | |
| | | float alpha; |
| | | float beta; |
| | |
| | | float object_scale; |
| | | float noobject_scale; |
| | | float class_scale; |
| | | int bias_match; |
| | | int random; |
| | | |
| | | int dontload; |
| | |
| | | #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) |
| | | { |
| | |
| | | } 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); |
| | |
| | | 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); |
| | |
| | | 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) |
| | |
| | | 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) |
| | |
| | | //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); |
| | |
| | | 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; |
| | |
| | | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | 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]); |
| | |
| | | } |
| | | 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; |
| | |
| | | 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); |
| | |
| | | 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; |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | #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) |
| | | { |
| | |
| | | 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; |
| | |
| | | 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 |
| | |
| | | |
| | | 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); |