Пример #1
0
void Darknet::yoloImage(const QString &filename, float thresh)
{
	const Mat ori = OpenCV::loadImage(getAbs(filename), -1);
	Mat img;
	cv::resize(ori, img, cv::Size(priv->net.w, priv->net.h));
	image im = toDarkImage(img);
	//image im = load_image_color((char *)qPrintable(getAbs(filename)), 0, 0);

	//image sized = resize_image(im, priv->net.w, priv->net.h);
	//float *X = sized.data;
	float *X = im.data;
	float *predictions = network_predict(priv->net, X);

	float nms=.5;
	const detection_layer l = priv->l;
	convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, priv->probs, priv->boxes, 0);
	if (nms) do_nms_sort(priv->boxes, priv->probs, l.side*l.side*l.n, l.classes, nms);
	draw_detections(im, l.side*l.side*l.n, thresh, priv->boxes, priv->probs, voc_names, 0, 20);
	show_image(im, "predictions");
	save_image(im, "predictions");

	//show_image(sized, "resized");
	free_image(im);
	//free_image(sized);
}
Пример #2
0
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;
    }
}
Пример #3
0
void *detect_in_thread(void *ptr)
{
    float nms = .4;

    layer_t l = net.layers[net.n-1];
    float *X = det_s.data;
    float *predictions = network_predict(net, X);
    free_image(det_s);
    convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, demo_thresh, probs, boxes, 0);
    if (nms > 0) do_nms(boxes, probs, l.side*l.side*l.n, l.classes, nms);
    printf("\033[2J");
    printf("\033[1;1H");
    printf("\nFPS:%.0f\n",fps);
    printf("Objects:\n\n");
    draw_detections(det, l.side*l.side*l.n, demo_thresh, boxes, probs, voc_names, voc_labels, 20);
    return 0;
}
Пример #4
0
Mat Darknet::predict(const Mat &ori, float thresh)
{
	Mat img;
	cv::resize(ori, img, cv::Size(priv->net.w, priv->net.h));
	image im = toDarkImage(img);

	float nms = 0.5;
	/* NOTE: network_predict doesn't allocate any data */
	float *predictions = network_predict(priv->net, im.data);
	const detection_layer l = priv->l;
	convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, priv->probs, priv->boxes, 0);
	if (nms)
		do_nms_sort(priv->boxes, priv->probs, l.side*l.side*l.n, l.classes, nms);
	draw_detections(im, l.side*l.side*l.n, thresh, priv->boxes, priv->probs, voc_names, 0, 20);
	Mat ret = toCVImage(im);
	free_image(im);
	return ret;
}
Пример #5
0
void Darknet::predict(const QString &filename, float thresh)
{
	const Mat ori = OpenCV::loadImage(getAbs(filename), -1);
	Mat img;
	cv::resize(ori, img, cv::Size(priv->net.w, priv->net.h));
	image im = toDarkImage(img);

	float nms = 0.5;
	/* NOTE: network_predict doesn't allocate any data */
	float *predictions = network_predict(priv->net, im.data);
	const detection_layer l = priv->l;
	convert_yolo_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, priv->probs, priv->boxes, 0);
	if (nms)
		do_nms_sort(priv->boxes, priv->probs, l.side*l.side*l.n, l.classes, nms);
	draw_detections(im, l.side*l.side*l.n, thresh, priv->boxes, priv->probs, voc_names, 0, 20);
	save_image(im, (char *)qPrintable(getAbs(QString(filename).replace(".jpg", "_pred.jpg"))));
	//ffDebug() << priv->net.n << priv->net.outputs << priv->l.classes;
}
Пример #6
0
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);
}
Пример #7
0
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);
    }
Пример #8
0
void validate_yolo(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(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;
    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;
    int t;

    float thresh = .001;
    int nms = 1;
    float iou_thresh = .5;

    int nthreads = 2;
    image *val = calloc(nthreads, sizeof(image));
    image *val_resized = calloc(nthreads, sizeof(image));
    image *buf = calloc(nthreads, sizeof(image));
    image *buf_resized = calloc(nthreads, sizeof(image));
    pthread_t *thr = calloc(nthreads, sizeof(pthread_t));

    load_args args = {0};
    args.w = net.w;
    args.h = net.h;
    args.type = IMAGE_DATA;

    for(t = 0; t < nthreads; ++t){
        args.path = paths[i+t];
        args.im = &buf[t];
        args.resized = &buf_resized[t];
        thr[t] = load_data_in_thread(args);
    }
    time_t start = time(0);
    for(i = nthreads; i < m+nthreads; i += nthreads){
        fprintf(stderr, "%d\n", i);
        for(t = 0; t < nthreads && i+t-nthreads < m; ++t){
            pthread_join(thr[t], 0);
            val[t] = buf[t];
            val_resized[t] = buf_resized[t];
        }
        for(t = 0; t < nthreads && i+t < m; ++t){
            args.path = paths[i+t];
            args.im = &buf[t];
            args.resized = &buf_resized[t];
            thr[t] = load_data_in_thread(args);
        }
        for(t = 0; t < nthreads && i+t-nthreads < m; ++t){
            char *path = paths[i+t-nthreads];
            char *id = basecfg(path);
            float *X = val_resized[t].data;
            float *predictions = network_predict(net, X);
            int w = val[t].w;
            int h = val[t].h;
            convert_yolo_detections(predictions, classes, l.n, square, side, w, h, thresh, probs, boxes, 0);
            if (nms) do_nms_sort(boxes, probs, side*side*l.n, classes, iou_thresh);
            print_yolo_detections(fps, id, boxes, probs, side*side*l.n, classes, w, h);
            free(id);
            free_image(val[t]);
            free_image(val_resized[t]);
        }
    }
    fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
}
Пример #9
0
void validate_yolo_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));

    char *base = "results/comp4_det_test_";
    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 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];
#ifndef _MSC_VER
        snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
#else
        sprintf_s(buff, 1024, "%s%s.txt", base, voc_names[j]);
#endif
        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 iou_thresh = .5;
    float nms = 0;

    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);
        float *predictions = network_predict(net, sized.data);
        convert_yolo_detections(predictions, classes, l.n, square, side, 1, 1, thresh, probs, boxes, 1);
        if (nms) do_nms(boxes, probs, side*side*l.n, 1, 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");

        int num_labels = 0;
        box_label *truth = read_boxes(labelpath, &num_labels);
        for(k = 0; k < side*side*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 < side*side*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);
    }
}