void run_coco(int argc, char **argv) { int i; float j; for(i = 0; i < 80; ++i){ char buff[256]; sprintf(buff, "data/labels/%s.png", coco_classes[i]); coco_labels[i] = load_image_color(buff, 0, 0); } for(j = 0.01; j < 0.99; j+=0.01){ char buff[256]; sprintf(buff, "data/probs/%.2f.png", j); probs_labels[(int)(j*100)] = load_image_color(buff, 0, 0); } float thresh = find_float_arg(argc, argv, "-thresh", .2); int cam_index = find_int_arg(argc, argv, "-c", 0); char *file = find_char_arg(argc, argv, "-file", 0); if(argc < 4){ fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); return; } 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_coco(cfg, weights, filename, thresh); else if(0==strcmp(argv[2], "train")) train_coco(cfg, weights); else if(0==strcmp(argv[2], "valid")) validate_coco(cfg, weights); else if(0==strcmp(argv[2], "recall")) validate_coco_recall(cfg, weights); else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, cam_index, file); }
void predict_regressor(char *cfgfile, char *weightfile, char *filename) { network *net = load_network(cfgfile, weightfile, 0); set_batch_network(net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); image sized = letterbox_image(im, net->w, net->h); float *X = sized.data; time=clock(); float *predictions = network_predict(net, X); printf("Predicted: %f\n", predictions[0]); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); free_image(im); free_image(sized); if (filename) break; } free_network(net); }
void label_classifier(char *datacfg, char *filename, char *weightfile) { int i; network *net = load_network(filename, weightfile, 0); set_batch_network(net, 1); srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "names", "data/labels.list"); char *test_list = option_find_str(options, "test", "data/train.list"); int classes = option_find_int(options, "classes", 2); char **labels = get_labels(label_list); list *plist = get_paths(test_list); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); for(i = 0; i < m; ++i){ image im = load_image_color(paths[i], 0, 0); image resized = resize_min(im, net->w); image crop = crop_image(resized, (resized.w - net->w)/2, (resized.h - net->h)/2, net->w, net->h); float *pred = network_predict(net, crop.data); if(resized.data != im.data) free_image(resized); free_image(im); free_image(crop); int ind = max_index(pred, classes); printf("%s\n", labels[ind]); } }
void decode_captcha(char *cfgfile, char *weightfile) { setbuf(stdout, NULL); srand(time(0)); network net = parse_network_cfg(cfgfile); set_batch_network(&net, 1); if(weightfile){ load_weights(&net, weightfile); } char filename[256]; while(1){ printf("Enter filename: "); fgets(filename, 256, stdin); strtok(filename, "\n"); image im = load_image_color(filename, 300, 57); scale_image(im, 1./255.); float *X = im.data; float *predictions = network_predict(net, X); image out = float_to_image(300, 57, 1, predictions); show_image(out, "decoded"); #ifdef OPENCV cvWaitKey(0); #endif free_image(im); } }
void test_captcha(char *cfgfile, char *weightfile) { setbuf(stdout, NULL); srand(time(0)); //char *base = basecfg(cfgfile); //printf("%s\n", base); network net = parse_network_cfg(cfgfile); set_batch_network(&net, 1); if(weightfile){ load_weights(&net, weightfile); } char filename[256]; while(1){ //printf("Enter filename: "); fgets(filename, 256, stdin); strtok(filename, "\n"); image im = load_image_color(filename, 200, 60); translate_image(im, -128); scale_image(im, 1/128.); float *X = im.data; float *predictions = network_predict(net, X); print_letters(predictions, 10); free_image(im); } }
/* * do prediction *@param[in]: yoloctx, context *@param[in]: filename, input picture *@param[in]: thresh, threshold for probability x confidence level *@param[out]: predictions, store detected objects */ void yoloPredict(context_param_yolo_t *yoloctx, char *filename, float thresh, yoloPredictions *predictions) { printf("YOLO predict\n"); int nwidth = yoloctx->_nwidth; int nheight = yoloctx->_nheight; int side = yoloctx->_grid.grids; int classes = yoloctx->_grid.classes; int bbs = yoloctx->_grid.bbs; int sqrt = yoloctx->_sqrt; float nms = yoloctx->_nms; image im = load_image_color(filename, 0, 0); image sized = resize_image(im, nwidth, nheight); resetData(yoloctx); float *x = sized.data; float *fpredictions = network_predict(yoloctx->_net, x); float **probs = yoloctx->_grid.probs; box *boxes = yoloctx->_grid.boxes; convertDetections(fpredictions, classes, bbs, sqrt, side, 1, 1, thresh, probs, boxes, 0); if (nms) do_nms_sort(boxes, probs, side*side*bbs, classes, nms); convertResults(im.w, im.h, side*side*bbs, thresh, boxes, probs, class_names, 20, predictions); //free(predictions); free_image(sized); free_image(im); }
void test_dice(char *cfgfile, char *weightfile, char *filename) { network * net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(net, weightfile); } set_batch_network(net, 1); srand(2222222); int i = 0; char **names = dice_labels; char buff[256]; char *input = buff; int indexes[6]; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, net->w, net->h); float *X = im.data; float *predictions = network_predict(net, X); top_predictions(net, 6, indexes); for(i = 0; i < 6; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } free_image(im); if (filename) break; } }
void run_yolo(int argc, char **argv) { int i; for(i = 0; i < 20; ++i){ char buff[256]; sprintf(buff, "data/labels/%s.png", voc_names[i]); voc_labels[i] = load_image_color(buff, 0, 0); } float thresh = find_float_arg(argc, argv, "-thresh", .2); int cam_index = find_int_arg(argc, argv, "-c", 0); if(argc < 4){ fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]); return; } 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_yolo(cfg, weights, filename, thresh); else if(0==strcmp(argv[2], "train")) train_yolo(cfg, weights); else if(0==strcmp(argv[2], "valid")) validate_yolo(cfg, weights); else if(0==strcmp(argv[2], "recall")) validate_yolo_recall(cfg, weights); else if(0==strcmp(argv[2], "demo")) demo_yolo(cfg, weights, thresh, cam_index, filename); }
void yolo_net_predict(network *pNet, char *imgfilename, char * resfile, float thresh){ detection_layer l = pNet->layers[pNet->n-1]; clock_t time; char buff[256]; // char *input = buff; int j; float nms=.5; box *boxes = calloc(l.side*l.side*l.n, sizeof(box)); float **probs = calloc(l.side*l.side*l.n, sizeof(float *)); for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(l.classes, sizeof(float *)); image im = load_image_color(imgfilename,0,0); image resized = resize_image(im, pNet->w, pNet->h); float *X = resized.data; time=clock(); float *predictions = network_predict(*pNet, X); free_image(im); printf("%s: Predicted in %f seconds.\n", imgfilename, sec(clock()-time)); FILE *ofp = fopen(resfile, "w"); if (ofp == NULL) {fprintf(stderr, "Can't open output file %s!\n",resfile); exit(1);} //convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, demo_thresh, probs, boxes, 0); convert_print_yolo_detections(ofp, predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); //if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); fclose(ofp); }
void validate_classifier_full(char *datacfg, char *filename, char *weightfile) { int i, j; network net = parse_network_cfg(filename); set_batch_network(&net, 1); if(weightfile){ load_weights(&net, weightfile); } srand(time(0)); list *options = read_data_cfg(datacfg); char *label_list = option_find_str(options, "labels", "data/labels.list"); char *valid_list = option_find_str(options, "valid", "data/train.list"); int classes = option_find_int(options, "classes", 2); int topk = option_find_int(options, "top", 1); char **labels = get_labels(label_list); list *plist = get_paths(valid_list); char **paths = (char **)list_to_array(plist); int m = plist->size; free_list(plist); float avg_acc = 0; float avg_topk = 0; int *indexes = calloc(topk, sizeof(int)); int size = net.w; for(i = 0; i < m; ++i){ int class = -1; char *path = paths[i]; for(j = 0; j < classes; ++j){ if(strstr(path, labels[j])){ class = j; break; } } image im = load_image_color(paths[i], 0, 0); image resized = resize_min(im, size); resize_network(&net, resized.w, resized.h); //show_image(im, "orig"); //show_image(crop, "cropped"); //cvWaitKey(0); float *pred = network_predict(net, resized.data); if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1, 1); free_image(im); free_image(resized); top_k(pred, classes, topk, indexes); if(indexes[0] == class) avg_acc += 1; for(j = 0; j < topk; ++j){ if(indexes[j] == class) avg_topk += 1; } printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1)); } }
void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh) { int show_flag = 1; 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); } set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; int j; float nms=.4; while(1){ if(filename){ strncpy(input, filename, 256); } else { printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input,0,0); image sized = resize_image(im, net.w, net.h); layer l = net.layers[net.n-1]; 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(l.classes + 1, sizeof(float *)); float *X = sized.data; time=clock(); network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); get_region_boxes(l, 1, 1, thresh, probs, boxes, 0, 0, hier_thresh); if (l.softmax_tree && nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms); else 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, names, alphabet, l.classes, show_flag); save_image(im, "predictions"); show_image(im, "predictions"); free_image(im); free_image(sized); free(boxes); free_ptrs((void **)probs, l.w*l.h*l.n); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
void test_lsd(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; int i, imlayer = 0; for (i = 0; i < net.n; ++i) { if (net.layers[i].out_c == 3) { imlayer = i; printf("%d\n", i); break; } } while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); image resized = resize_min(im, net.w); image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h); //grayscale_image_3c(crop); float *X = crop.data; time=clock(); network_predict(net, X); image out = get_network_image_layer(net, imlayer); //yuv_to_rgb(out); constrain_image(out); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); show_image(out, "out"); show_image(crop, "crop"); save_image(out, "out"); #ifdef OPENCV cvWaitKey(0); #endif free_image(im); free_image(resized); free_image(crop); if (filename) break; } }
void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); list *options = read_data_cfg(datacfg); char *name_list = option_find_str(options, "names", 0); if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list"); if(top == 0) top = option_find_int(options, "top", 1); int i = 0; char **names = get_labels(name_list); clock_t time; int *indexes = calloc(top, sizeof(int)); char buff[256]; char *input = buff; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); image r = letterbox_image(im, net.w, net.h); //resize_network(&net, r.w, r.h); //printf("%d %d\n", r.w, r.h); float *X = r.data; time=clock(); float *predictions = network_predict(net, X); if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 1, 1); top_k(predictions, net.outputs, top, indexes); fprintf(stderr, "%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < top; ++i){ int index = indexes[i]; //if(net.hierarchy) printf("%d, %s: %f, parent: %s \n",index, names[index], predictions[index], (net.hierarchy->parent[index] >= 0) ? names[net.hierarchy->parent[index]] : "Root"); //else printf("%s: %f\n",names[index], predictions[index]); printf("%5.2f%%: %s\n", predictions[index]*100, names[index]); } if(r.data != im.data) free_image(r); free_image(im); if (filename) break; } }
struct darknet_helper * darknet_init(const char *darknet_root) { struct darknet_helper *dnet = (struct darknet_helper *)malloc(sizeof(struct darknet_helper)); dnet->priv = (struct darknet_priv *)malloc(sizeof(struct darknet_priv)); int i; for (i = 0; i < 20; ++i){ char buff[256]; sprintf(buff, "%s/data/labels/%s.png", darknet_root, voc_names[i]); voc_labels[i] = load_image_color(buff, 0, 0); } return dnet; }
int Darknet::init(const QString &darkNetRoot) { darkNetRootPath = darkNetRoot; for (int i = 0; i < 20; ++i){ char buff[256]; sprintf(buff, "%s/data/labels/%s.png", qPrintable(darkNetRoot), voc_names[i]); voc_labels[i] = load_image_color(buff, 0, 0); } priv = new DarkNetPriv; return 0; }
void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); list *options = read_data_cfg(datacfg); char *name_list = option_find_str(options, "names", 0); if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list"); int top = option_find_int(options, "top", 1); int i = 0; char **names = get_labels(name_list); clock_t time; int *indexes = calloc(top, sizeof(int)); char buff[256]; char *input = buff; int size = net.w; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); image r = resize_min(im, size); resize_network(&net, r.w, r.h); printf("%d %d\n", r.w, r.h); float *X = r.data; time=clock(); float *predictions = network_predict(net, X); top_predictions(net, top, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < top; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } if(r.data != im.data) free_image(r); free_image(im); if (filename) break; } }
void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } detection_layer l = net.layers[net.n-1]; set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; int j; float nms=.5; box *boxes = calloc(l.side*l.side*l.n, sizeof(box)); float **probs = calloc(l.side*l.side*l.n, sizeof(float *)); for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(l.classes, sizeof(float *)); while(1){ if(filename){ strncpy(input, filename, 256); } else { printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input,0,0); image sized = resize_image(im, net.w, net.h); float *X = sized.data; time=clock(); float *predictions = network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); //draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, voc_labels, 20); draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, 0, 20); show_image(im, "predictions"); save_image(im, "predictions"); show_image(sized, "resized"); free_image(im); free_image(sized); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
static void yolo_image2(const char *cfg, const char *weights, const char *filename, float thresh) { int i; for (i = 0; i < 20; ++i){ char buff[256]; sprintf(buff, "/home/amenmd/myfs/source-codes/oss/darknet/data/labels/%s.png", voc_names[i]); voc_labels[i] = load_image_color(buff, 0, 0); } network net = parse_network_cfg((char *)cfg); load_weights(&net, (char *)weights); detection_layer l = net.layers[net.n-1]; set_batch_network(&net, 1); int j; float nms=.5; box *boxes = (box *)calloc(l.side*l.side*l.n, sizeof(box)); float **probs = (float **)calloc(l.side*l.side*l.n, sizeof(float *)); for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = (float *)calloc(l.classes, sizeof(float *)); image im = load_image_color((char *)filename, 0, 0); image sized = resize_image(im, net.w, net.h); float *X = sized.data; float *predictions = network_predict(net, X); convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0); if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms); //draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, voc_labels, 20); draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, voc_names, 0, 20); show_image(im, "predictions"); save_image(im, "predictions"); show_image(sized, "resized"); free_image(im); free_image(sized); }
void test_writing(char *cfgfile, char *weightfile, char *filename) { network * net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(net, weightfile); } set_batch_network(net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); resize_network(net, im.w, im.h); printf("%d %d %d\n", im.h, im.w, im.c); float *X = im.data; time=clock(); network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); image pred = get_network_image(net); image upsampled = resize_image(pred, im.w, im.h); image thresh = threshold_image(upsampled, .5); pred = thresh; show_image(pred, "prediction"); show_image(im, "orig"); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif free_image(upsampled); free_image(thresh); free_image(im); if (filename) break; } }
void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh) { image **alphabet = load_alphabet(); network *net = load_network(cfgfile, weightfile, 0); layer l = net->layers[net->n-1]; set_batch_network(net, 1); srand(2222222); float nms = .4; clock_t time; char buff[256]; char *input = buff; while(1){ if(filename){ strncpy(input, filename, 256); } else { printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input,0,0); image sized = resize_image(im, net->w, net->h); float *X = sized.data; time=clock(); network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); int nboxes = 0; detection *dets = get_network_boxes(net, 1, 1, thresh, 0, 0, 0, &nboxes); if (nms) do_nms_sort(dets, l.side*l.side*l.n, l.classes, nms); draw_detections(im, dets, l.side*l.side*l.n, thresh, coco_classes, alphabet, 80); save_image(im, "prediction"); show_image(im, "predictions"); free_detections(dets, nboxes); free_image(im); free_image(sized); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
void test_tag(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); int i = 0; char **names = get_labels("data/tags.txt"); clock_t time; int indexes[10]; char buff[256]; char *input = buff; int size = net.w; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); image r = resize_min(im, size); resize_network(&net, r.w, r.h); printf("%d %d\n", r.w, r.h); float *X = r.data; time=clock(); float *predictions = network_predict(net, X); top_predictions(net, 10, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < 10; ++i){ int index = indexes[i]; printf("%.1f%%: %s\n", predictions[index]*100, names[index]); } if(r.data != im.data) free_image(r); free_image(im); if (filename) break; } }
void valid_captcha(char *cfgfile, char *weightfile, char *filename) { char **labels = get_labels("/data/captcha/reimgs.labels.list"); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } list *plist = get_paths("/data/captcha/reimgs.fg.list"); char **paths = (char **)list_to_array(plist); int N = plist->size; int outputs = net.outputs; set_batch_network(&net, 1); srand(2222222); int i, j; for(i = 0; i < N; ++i){ if (i%100 == 0) fprintf(stderr, "%d\n", i); image im = load_image_color(paths[i], net.w, net.h); float *X = im.data; float *predictions = network_predict(net, X); //printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); int truth = -1; for(j = 0; j < 13; ++j){ if (strstr(paths[i], labels[j])) truth = j; } if (truth == -1){ fprintf(stderr, "bad: %s\n", paths[i]); return; } printf("%d, ", truth); for(j = 0; j < outputs; ++j){ if (j != 0) printf(", "); printf("%f", predictions[j]); } printf("\n"); fflush(stdout); free_image(im); if (filename) break; } }
void test_captcha(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); int i = 0; char **names = get_labels("/data/captcha/reimgs.labels.list"); char buff[256]; char *input = buff; int indexes[26]; while(1){ if(filename){ strncpy(input, filename, 256); }else{ //printf("Enter Image Path: "); //fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, net.w, net.h); float *X = im.data; float *predictions = network_predict(net, X); top_predictions(net, 26, indexes); //printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < 26; ++i){ int index = indexes[i]; if(i != 0) printf(", "); printf("%s %f", names[index], predictions[index]); } printf("\n"); fflush(stdout); free_image(im); if (filename) break; } }
void test_coco(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile) { load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; while(1) { if(filename) { strncpy(input, filename, 256); } else { printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input,0,0); image sized = resize_image(im, net.w, net.h); float *X = sized.data; time=clock(); float *predictions = network_predict(net, X); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); draw_coco(im, predictions, 7, "predictions"); free_image(im); free_image(sized); #ifdef OPENCV cvWaitKey(0); cvDestroyAllWindows(); #endif if (filename) break; } }
void test_imagenet(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile, 1); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); int i = 0; char **names = get_labels("data/shortnames.txt"); clock_t time; int indexes[10]; char buff[256]; char *input = buff; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 256, 256); float *X = im.data; time=clock(); float *predictions = network_predict(net, X); top_predictions(net, 10, indexes); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); for(i = 0; i < 10; ++i){ int index = indexes[i]; printf("%s: %f\n", names[index], predictions[index]); } free_image(im); if (filename) break; } }
void test_super(char *cfgfile, char *weightfile, char *filename) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); clock_t time; char buff[256]; char *input = buff; while(1){ if(filename){ strncpy(input, filename, 256); }else{ printf("Enter Image Path: "); fflush(stdout); input = fgets(input, 256, stdin); if(!input) return; strtok(input, "\n"); } image im = load_image_color(input, 0, 0); resize_network(&net, im.w, im.h); printf("%d %d\n", im.w, im.h); float *X = im.data; time=clock(); network_predict(net, X); image out = get_network_image(net); printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time)); save_image(out, "out"); free_image(im); if (filename) break; } }
void test_writing(char *cfgfile, char *weightfile, char *outfile) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); srand(2222222); clock_t time; char filename[256]; fgets(filename, 256, stdin); strtok(filename, "\n"); image im = load_image_color(filename, 0, 0); //image im = load_image_color("/home/pjreddie/darknet/data/figs/C02-1001-Figure-1.png", 0, 0); image sized = resize_image(im, 256, 256); printf("%d %d %d\n", im.h, im.w, im.c); float *X = sized.data; time=clock(); float *predictions = network_predict(net, X); printf("%s: Predicted in %f seconds.\n", filename, sec(clock()-time)); image pred = get_network_image(net); if (outfile) { printf("Save image as %s.png (shape: %d %d)\n", outfile, pred.w, pred.h); save_image(pred, outfile); } else { show_image(pred, "prediction"); #ifdef OPENCV cvWaitKey(0); #endif } free_image(im); free_image(sized); }
void validate_detector_recall(char *cfgfile, char *weightfile) { network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay); srand(time(0)); list *plist = get_paths("data/voc.2007.test"); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n-1]; int classes = l.classes; int j, k; 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 *)); int m = plist->size; int i=0; float thresh = .001; float iou_thresh = .5; float nms = .4; int total = 0; int correct = 0; int proposals = 0; float avg_iou = 0; for(i = 0; i < m; ++i){ char *path = paths[i]; image orig = load_image_color(path, 0, 0); image sized = resize_image(orig, net.w, net.h); char *id = basecfg(path); network_predict(net, sized.data); get_region_boxes(l, 1, 1, thresh, probs, boxes, 1, 0, .5); if (nms) do_nms(boxes, probs, l.w*l.h*l.n, 1, nms); char labelpath[4096]; find_replace(path, "images", "labels", labelpath); find_replace(labelpath, "JPEGImages", "labels", labelpath); find_replace(labelpath, ".jpg", ".txt", labelpath); find_replace(labelpath, ".JPEG", ".txt", labelpath); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); for(k = 0; k < l.w*l.h*l.n; ++k){ if(probs[k][0] > thresh){ ++proposals; } } for (j = 0; j < num_labels; ++j) { ++total; box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float best_iou = 0; for(k = 0; k < l.w*l.h*l.n; ++k){ float iou = box_iou(boxes[k], t); if(probs[k][0] > thresh && iou > best_iou){ best_iou = iou; } } avg_iou += best_iou; if(best_iou > iou_thresh){ ++correct; } } fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total); free(id); free_image(orig); free_image(sized); } }
void validate_yolo_classify(char *datacfg, char *cfgfile, char *weightfile) { list *options = read_data_cfg(datacfg); //char *train_list = option_find_str(options, "train", "data/train_list.txt"); //char *test_list = option_find_str(options, "test", "data/test_list.txt"); char *valid_list = option_find_str(options, "valid", "data/valid_list.txt"); //char *backup_directory = option_find_str(options, "backup", "/backup/"); //char *label_list = option_find_str(options, "labels", "data/labels_list.txt"); //int classes = option_find_int(options, "classes", 2); //char **labels = get_labels(label_list); network net = parse_network_cfg(cfgfile); if(weightfile){ load_weights(&net, weightfile); } set_batch_network(&net, 1); 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"); list *plist = get_paths(valid_list); char **paths = (char **)list_to_array(plist); layer l = net.layers[net.n-1]; int classes = l.classes; int square = l.sqrt; int side = l.side; 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(side*side*l.n, sizeof(box)); float **probs = calloc(side*side*l.n, sizeof(float *)); for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *)); int m = plist->size; int i=0; //float thresh = .001; float thresh = .2; float iou_thresh = .5; //float nms = 0; float nms = 0.5; int total = 0; int correct = 0; int class_correct = 0; int proposals = 0; float avg_iou = 0; for(i = 0; i < m; ++i){ char *path = paths[i]; image orig = load_image_color(path, 0, 0); image sized = resize_image(orig, net.w, net.h); char *id = basecfg(path); float *predictions = network_predict(net, sized.data); convert_yolo_detections(predictions, classes, l.n, square, side, 1, 1, thresh, probs, boxes, 0); //if (nms) do_nms(boxes, probs, side*side*l.n, 1, nms); if (nms) do_nms(boxes, probs, side*side*l.n, classes, nms); char *labelpath = find_replace(path, "images", "labels"); labelpath = find_replace(labelpath, "JPEGImages", "labels"); labelpath = find_replace(labelpath, ".jpg", ".txt"); labelpath = find_replace(labelpath, ".JPEG", ".txt"); labelpath = find_replace(labelpath, ".bmp", ".txt"); labelpath = find_replace(labelpath, ".dib", ".txt"); labelpath = find_replace(labelpath, ".jpe", ".txt"); labelpath = find_replace(labelpath, ".jp2", ".txt"); labelpath = find_replace(labelpath, ".png", ".txt"); labelpath = find_replace(labelpath, ".pbm", ".txt"); labelpath = find_replace(labelpath, ".pgm", ".txt"); labelpath = find_replace(labelpath, ".ppm", ".txt"); labelpath = find_replace(labelpath, ".sr", ".txt"); labelpath = find_replace(labelpath, ".ras", ".txt"); labelpath = find_replace(labelpath, ".tiff", ".txt"); labelpath = find_replace(labelpath, ".tif", ".txt"); int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); for(k = 0; k < side*side*l.n; ++k){ int class = max_index(probs[k], classes); float prob = probs[k][class]; //fprintf(stderr, "path=%s\t\tk=%d\tprob=%f\tclass=%d\n", path, k, prob, class); if(prob > thresh){ ++proposals; } } for (j = 0; j < num_labels; ++j) { ++total; box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h}; float best_iou = 0; int pre_class = -1; for(k = 0; k < side*side*l.n; ++k){ float iou = box_iou(boxes[k], t); int class = max_index(probs[k], classes); float prob = probs[k][class]; //fprintf(stderr, "path=%s\t\tk=%d\tprob=%f\tclass=%d\n", path, k, prob, class); if(prob > thresh && iou > best_iou){ best_iou = iou; pre_class = class; } } avg_iou += best_iou; if(best_iou > iou_thresh){ ++correct; } if(pre_class == truth[j].id){ ++class_correct; } //fprintf(stderr, "true_class=%d\tpre_class=%d\n", truth[j].id, pre_class); } fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\t\tClassify:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total, 100.*class_correct/total); free(id); free_image(orig); free_image(sized); }
void run_nightmare(int argc, char **argv) { srand(0); if(argc < 4){ fprintf(stderr, "usage: %s %s [cfg] [weights] [image] [layer] [options! (optional)]\n", argv[0], argv[1]); return; } char *cfg = argv[2]; char *weights = argv[3]; char *input = argv[4]; int max_layer = atoi(argv[5]); int range = find_int_arg(argc, argv, "-range", 1); int norm = find_int_arg(argc, argv, "-norm", 1); int rounds = find_int_arg(argc, argv, "-rounds", 1); int iters = find_int_arg(argc, argv, "-iters", 10); int octaves = find_int_arg(argc, argv, "-octaves", 4); float zoom = find_float_arg(argc, argv, "-zoom", 1.); float rate = find_float_arg(argc, argv, "-rate", .04); float thresh = find_float_arg(argc, argv, "-thresh", 1.); float rotate = find_float_arg(argc, argv, "-rotate", 0); float momentum = find_float_arg(argc, argv, "-momentum", .9); float lambda = find_float_arg(argc, argv, "-lambda", .01); char *prefix = find_char_arg(argc, argv, "-prefix", 0); int reconstruct = find_arg(argc, argv, "-reconstruct"); int smooth_size = find_int_arg(argc, argv, "-smooth", 1); network net = parse_network_cfg(cfg); load_weights(&net, weights); char *cfgbase = basecfg(cfg); char *imbase = basecfg(input); set_batch_network(&net, 1); image im = load_image_color(input, 0, 0); if(0){ float scale = 1; if(im.w > 512 || im.h > 512){ if(im.w > im.h) scale = 512.0/im.w; else scale = 512.0/im.h; } image resized = resize_image(im, scale*im.w, scale*im.h); free_image(im); im = resized; } float *features = 0; image update; if (reconstruct){ resize_network(&net, im.w, im.h); int zz = 0; network_predict(net, im.data); image out_im = get_network_image(net); image crop = crop_image(out_im, zz, zz, out_im.w-2*zz, out_im.h-2*zz); //flip_image(crop); image f_im = resize_image(crop, out_im.w, out_im.h); free_image(crop); printf("%d features\n", out_im.w*out_im.h*out_im.c); im = resize_image(im, im.w, im.h); f_im = resize_image(f_im, f_im.w, f_im.h); features = f_im.data; int i; for(i = 0; i < 14*14*512; ++i){ features[i] += rand_uniform(-.19, .19); } free_image(im); im = make_random_image(im.w, im.h, im.c); update = make_image(im.w, im.h, im.c); } int e; int n; for(e = 0; e < rounds; ++e){ fprintf(stderr, "Iteration: "); fflush(stderr); for(n = 0; n < iters; ++n){ fprintf(stderr, "%d, ", n); fflush(stderr); if(reconstruct){ reconstruct_picture(net, features, im, update, rate, momentum, lambda, smooth_size, 1); //if ((n+1)%30 == 0) rate *= .5; show_image(im, "reconstruction"); #ifdef OPENCV cvWaitKey(10); #endif }else{ int layer = max_layer + rand()%range - range/2; int octave = rand()%octaves; optimize_picture(&net, im, layer, 1/pow(1.33333333, octave), rate, thresh, norm); } } fprintf(stderr, "done\n"); if(0){ image g = grayscale_image(im); free_image(im); im = g; } char buff[256]; if (prefix){ sprintf(buff, "%s/%s_%s_%d_%06d",prefix, imbase, cfgbase, max_layer, e); }else{ sprintf(buff, "%s_%s_%d_%06d",imbase, cfgbase, max_layer, e); } printf("%d %s\n", e, buff); save_image(im, buff); //show_image(im, buff); //cvWaitKey(0); if(rotate){ image rot = rotate_image(im, rotate); free_image(im); im = rot; } image crop = crop_image(im, im.w * (1. - zoom)/2., im.h * (1.-zoom)/2., im.w*zoom, im.h*zoom); image resized = resize_image(crop, im.w, im.h); free_image(im); free_image(crop); im = resized; } }