Beispiel #1
0
void *detect_in_thread_coco(void *ptr)
{
    float nms = .4;

    layer_t l = net.layers[net.n-1];
    float *X = det_s.data;
    float *prediction = network_predict(net, X);

    memcpy(predictions[demo_index], prediction, l.outputs*sizeof(float));
    mean_arrays(predictions, FRAMES, l.outputs, avg);

    free_image(det_s);
    convert_coco_detections(avg, 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");

    images[demo_index] = det;
    det = images[(demo_index + FRAMES/2 + 1)%FRAMES];
    demo_index = (demo_index + 1)%FRAMES;

    draw_detections(det, l.side*l.side*l.n, demo_thresh, boxes, probs, coco_classes, coco_labels, 80);
    return 0;
}
Beispiel #2
0
void *detect_in_thread(void *ptr)
{
    float nms = .4;

    layer l = net.layers[net.n-1];
    float *X = det_s.data;
    float *prediction = network_predict(net, X);

    memcpy(predictions[demo_index], prediction, l.outputs*sizeof(float));
    mean_arrays(predictions, FRAMES, l.outputs, avg);
    l.output = avg;

    free_image(det_s);
    if(l.type == DETECTION){
        get_detection_boxes(l, 1, 1, demo_thresh, probs, boxes, 0);
    } else if (l.type == REGION){
        get_region_boxes(l, 1, 1, demo_thresh, probs, boxes, 0, 0, demo_hier_thresh);
    } else {
        error("Last layer must produce detections\n");
    }
    if (nms > 0) do_nms(boxes, probs, l.w*l.h*l.n, l.classes, nms);
    printf("\033[2J");
    printf("\033[1;1H");
    printf("\nFPS:%.1f\n",fps);
    printf("Objects:\n\n");

    images[demo_index] = det;
    det = images[(demo_index + FRAMES/2 + 1)%FRAMES];
    demo_index = (demo_index + 1)%FRAMES;

    draw_detections(det, l.w*l.h*l.n, demo_thresh, boxes, probs, demo_names, demo_alphabet, demo_classes);

    return 0;
}
Beispiel #3
0
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;
    }
}
Beispiel #4
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;
    }
}
Beispiel #5
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;
}
Beispiel #6
0
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;
    }
}
Beispiel #7
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;
}
Beispiel #8
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;
}
Beispiel #9
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);
}
Beispiel #10
0
void validate_yolo_recall(char *cfgfile, char *weightfile, char *val_images, char *out_dir, float th, int log, int draw)
{

	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));

	//create output directory if it does not exist
	struct stat st= {0};
	if(stat(out_dir,&st)==-1){
		fprintf(stderr,"Creating output directory\n");
		mkdir(out_dir,0700);
	}

	char *base = out_dir;
	list *plist = get_paths(val_images);
	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 rows = l.rows;
	int cols = l.cols;

	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(rows*cols*l.n, sizeof(box));
	float **probs = calloc(rows*cols*l.n, sizeof(float *));
	for(j = 0; j < rows*cols*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));

	int m = plist->size;
	int i=0;

	float thresh = th;
	float iou_thresh[11] = {0.0,0.05,0.1,0.15,0.20,0.25,0.30,0.35,0.40,0.45,0.5};
	float nms = 0.1;

	int total = 0;
	int correct[11] = {0,0,0,0,0,0,0,0,0,0,0};
	int proposals = 0;
	float avg_iou = 0;
	Vector id_found;
	Vector id_invalid;
	initArray(&id_found,5);
	initArray(&id_invalid,5);

	for(i = 0; i < m; ++i){
		char * image_path = strtok(paths[i]," ");
        char * label_path = strtok(NULL," ");

		image orig = load_image(image_path, 0, 0,net.c);
		image color;
		if(draw)
			color = load_image(image_path, 0, 0, 3);
		image sized = resize_image(orig, net.w, net.h);
		//char *id = basecfg(path);
		float *predictions = network_predict(net, sized.data);
		convert_detections(predictions, classes, l.n, square, rows, cols, 1, 1, thresh, probs, boxes, 0);
		if (nms) do_nms(boxes, probs, rows*cols*l.n, 1, nms);



		int num_labels = 0;
		box_label *truth = read_boxes(label_path, &num_labels);
		int old_p = proposals;
		for(k = 0; k < rows*cols*l.n; ++k){
			if(probs[k][0] > thresh){
				++proposals;
			}
		}

		if(old_p!=proposals){
			if(log){
				char filename[256];
				sprintf(filename, "%s/%d.txt", base,i);
				printf("log in file %s\n",filename);
				FILE * out = fopen(filename, "w");
				fprintf(out,"W\tH\tX\tY\n");
				for(k=0; k<rows*cols*l.n; ++k){
					if(probs[k][0] > thresh){
						fprintf(out, "%f\t%f\t%f\t%f\n",boxes[k].w,boxes[k].h,boxes[k].x,boxes[k].y);
					}
				}
				fclose(out);
			}
			if(draw){
				draw_detections(color, l.rows*l.cols*l.n, thresh, boxes, probs, voc_names, voc_labels, CLASSNUM);

				show_image(color, "predictions");

				#ifdef OPENCV
				cvWaitKey(0);
				//cvDestroyAllWindows();
				#endif
			}
		}
		for (j = 0; j < num_labels; ++j) {
			++total;

			while(id_found.used <= truth[j].id){
				insertArray(&id_invalid,0);
				insertArray(&id_found,0);
			}
							
			if(truth[j].classe > CLASSNUM-1)
				id_invalid.array[truth[j].id]=1;

			box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h};
			float best_iou = 0;
			for(k = 0; k < rows*cols*l.n; ++k){
				float iou = box_iou(boxes[k], t);
				//find overlapping prediction
				if(iou > best_iou){
					//find the predicted class
					float best_score = thresh;
					int best_class_index = -1;
					for(int c=0; c<CLASSNUM; c++){
						if(probs[k][c]>best_score){
							best_score = probs[k][c];
							best_class_index = c;
						}
					}
					//check if it's good or not
					if(best_class_index == truth[j].classe)
						best_iou = iou;
				}
			}
			avg_iou += best_iou;
			for(int k=0; k<11; k++){
				if(best_iou > iou_thresh[k]){
					id_found.array[truth[j].id]=1;
					++correct[k];
				}
			}
		}
		if(i%10==0){
			printf("\033[2J");
			printf("\033[1;1H");
			printf("#img\tPred\tTP\ttot\tRPs/Img\tAvg-IOU\tRecall\tPrecision\n");
			printf("%5d\t%5d\t%5d\t%5d\t%.2f\t%.2f%%\t%.2f%%\t%.2f%%\n", i, proposals, correct[10], total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct[10]/total, 100.*correct[10]/proposals);
			printf("IOU_th\tTP\tFP\tRecall\tPrecision\n");
			for(int k=0; k<11; k++){
				printf("%.2f%%\t%5d\t%5d\t%.2f%%\t%.2f%%\t\n", iou_thresh[k], correct[k], proposals-correct[k], 100.*correct[k]/total, 100.*correct[k]/proposals);
			}
			int found=0;
			int invalid = 0;
			for(int i=0; i<id_found.used; i++){
				if(id_invalid.array[i]!=1)
					found+=id_found.array[i];
				invalid+=id_invalid.array[i];
			}
			printf("Founded: %d/%d\t%d\n", found, id_found.used-invalid,invalid);
		}
		//free(id);
		free_image(orig);
		free_image(sized);
	}
	for(j = 0; j < classes; ++j){
		fprintf(fps[j],"IOU_th;TP;FP;Recall;Precision\n");
		for(int k=0; k<11; k++){
			fprintf(fps[j],"%.2f%%;%5d;%5d;%.2f%%;%.2f%%;\n", iou_thresh[k], correct[k], proposals-correct[k], 100.*correct[k]/total, 100.*correct[k]/proposals);
		}
		fprintf(fps[j], "\n\nFounded;Total;\n");
		int found=0;
		int invalid = 0;
		for(int i=0; i<id_found.used; i++){
			if(id_invalid.array[i]!=1)
					found+=id_found.array[i];
			invalid+=id_invalid.array[i];
		}
		fprintf(fps[j], "%d;%d;\n", found, id_found.used-invalid);
		fclose(fps[j]);
	}
	freeArray(&id_found);
}