From dbcdac88a571d638e55adc512148dc7b609cbfee Mon Sep 17 00:00:00 2001
From: Vinjn Zhang <vinjn@users.noreply.github.com>
Date: Sun, 20 May 2018 06:20:55 +0000
Subject: [PATCH] darknet.py - loading dll files by absolute name

---
 src/detector.c |  133 +++++++++++++++++++++++++++++++++-----------
 1 files changed, 99 insertions(+), 34 deletions(-)

diff --git a/src/detector.c b/src/detector.c
index e891cd7..bbe60cf 100644
--- a/src/detector.c
+++ b/src/detector.c
@@ -21,10 +21,10 @@
 
 #ifndef CV_VERSION_EPOCH
 #include "opencv2/videoio/videoio_c.h"
-#define OPENCV_VERSION CVAUX_STR(CV_VERSION_MAJOR)""CVAUX_STR(CV_VERSION_MINOR)""CVAUX_STR(CV_VERSION_REVISION)
+#define OPENCV_VERSION CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)"" CVAUX_STR(CV_VERSION_REVISION)
 #pragma comment(lib, "opencv_world" OPENCV_VERSION ".lib")
 #else
-#define OPENCV_VERSION CVAUX_STR(CV_VERSION_EPOCH)""CVAUX_STR(CV_VERSION_MAJOR)""CVAUX_STR(CV_VERSION_MINOR)
+#define OPENCV_VERSION CVAUX_STR(CV_VERSION_EPOCH)"" CVAUX_STR(CV_VERSION_MAJOR)"" CVAUX_STR(CV_VERSION_MINOR)
 #pragma comment(lib, "opencv_core" OPENCV_VERSION ".lib")
 #pragma comment(lib, "opencv_imgproc" OPENCV_VERSION ".lib")
 #pragma comment(lib, "opencv_highgui" OPENCV_VERSION ".lib")
@@ -36,6 +36,17 @@
 
 static int coco_ids[] = {1,2,3,4,5,6,7,8,9,10,11,13,14,15,16,17,18,19,20,21,22,23,24,25,27,28,31,32,33,34,35,36,37,38,39,40,41,42,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,67,70,72,73,74,75,76,77,78,79,80,81,82,84,85,86,87,88,89,90};
 
+void replace_image_to_label(char *input_path, char *output_path) {
+	find_replace(input_path, "images", "labels", output_path);
+	find_replace(output_path, "JPEGImages", "labels", output_path);
+	find_replace(output_path, ".jpg", ".txt", output_path);
+	find_replace(output_path, ".png", ".txt", output_path);
+	find_replace(output_path, ".bmp", ".txt", output_path);
+	find_replace(output_path, ".JPG", ".txt", output_path);
+	find_replace(output_path, ".JPEG", ".txt", output_path);
+	find_replace(output_path, ".ppm", ".txt", output_path);
+}
+
 void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int dont_show)
 {
     list *options = read_data_cfg(datacfg);
@@ -66,6 +77,11 @@
     srand(time(0));
     network net = nets[0];
 
+	if ((net.batch * net.subdivisions) == 1) {
+		printf("\n Error: You set incorrect value batch=1 for Training! You should set batch=64 subdivision=64 \n");
+		getchar();
+	}
+
     int imgs = net.batch * net.subdivisions * ngpus;
     printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
     data train, buffer;
@@ -121,12 +137,18 @@
     while(get_current_batch(net) < net.max_batches){
 		if(l.random && count++%10 == 0){
             printf("Resizing\n");
-			int dim = (rand() % 12 + (init_w/32 - 5)) * 32;	// +-160
-            //if (get_current_batch(net)+100 > net.max_batches) dim = 544;
+			//int dim = (rand() % 12 + (init_w/32 - 5)) * 32;	// +-160
             //int dim = (rand() % 4 + 16) * 32;
-            printf("%d\n", dim);
-            args.w = dim;
-            args.h = dim;
+			//if (get_current_batch(net)+100 > net.max_batches) dim = 544;
+			int random_val = rand() % 12;
+			int dim_w = (random_val + (init_w / 32 - 5)) * 32;	// +-160
+			int dim_h = (random_val + (init_h / 32 - 5)) * 32;	// +-160
+			if (dim_w < 32) dim_w = 32;
+			if (dim_h < 32) dim_h = 32;
+
+			printf("%d x %d \n", dim_w, dim_h);
+			args.w = dim_w;
+			args.h = dim_h;
 
             pthread_join(load_thread, 0);
             train = buffer;
@@ -134,7 +156,7 @@
             load_thread = load_data(args);
 
             for(i = 0; i < ngpus; ++i){
-                resize_network(nets + i, dim, dim);
+                resize_network(nets + i, dim_w, dim_h);
             }
             net = nets[0];
         }
@@ -416,7 +438,7 @@
 		fprintf(fp, "\n]\n");
 		fclose(fp);
 	}
-	fprintf(stderr, "Total Detection Time: %f Seconds\n", time(0) - start);
+	fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)time(0) - start);
 }
 
 void validate_detector_recall(char *datacfg, char *cfgfile, char *weightfile)
@@ -463,13 +485,15 @@
 		if (nms) do_nms_obj(dets, nboxes, 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, ".png", ".txt", labelpath);
-		find_replace(labelpath, ".bmp", ".txt", labelpath);
-		find_replace(labelpath, ".JPG", ".txt", labelpath);
-		find_replace(labelpath, ".JPEG", ".txt", labelpath);
+		replace_image_to_label(path, labelpath);
+		//find_replace(path, "images", "labels", labelpath);
+		//find_replace(labelpath, "JPEGImages", "labels", labelpath);
+		//find_replace(labelpath, ".jpg", ".txt", labelpath);
+		//find_replace(labelpath, ".png", ".txt", labelpath);
+		//find_replace(labelpath, ".bmp", ".txt", labelpath);
+		//find_replace(labelpath, ".JPG", ".txt", labelpath);
+		//find_replace(labelpath, ".JPEG", ".txt", labelpath);
+		//find_replace(labelpath, ".ppm", ".txt", labelpath);
 
 		int num_labels = 0;
 		box_label *truth = read_boxes(labelpath, &num_labels);
@@ -620,13 +644,15 @@
 			if (nms) do_nms_sort(dets, nboxes, l.classes, 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, ".png", ".txt", labelpath);
-			find_replace(labelpath, ".bmp", ".txt", labelpath);
-			find_replace(labelpath, ".JPG", ".txt", labelpath);
-			find_replace(labelpath, ".JPEG", ".txt", labelpath);
+			replace_image_to_label(path, labelpath);
+			//find_replace(path, "images", "labels", labelpath);
+			//find_replace(labelpath, "JPEGImages", "labels", labelpath);
+			//find_replace(labelpath, ".jpg", ".txt", labelpath);
+			//find_replace(labelpath, ".png", ".txt", labelpath);
+			//find_replace(labelpath, ".bmp", ".txt", labelpath);
+			//find_replace(labelpath, ".JPG", ".txt", labelpath);
+			//find_replace(labelpath, ".JPEG", ".txt", labelpath);
+			//find_replace(labelpath, ".ppm", ".txt", labelpath);
 			int num_labels = 0;
 			box_label *truth = read_boxes(labelpath, &num_labels);
 			int i, j;
@@ -883,13 +909,15 @@
 	for (i = 0; i < number_of_images; ++i) {
 		char *path = paths[i];
 		char labelpath[4096];
-		find_replace(path, "images", "labels", labelpath);
-		find_replace(labelpath, "JPEGImages", "labels", labelpath);
-		find_replace(labelpath, ".jpg", ".txt", labelpath);
-		find_replace(labelpath, ".png", ".txt", labelpath);
-		find_replace(labelpath, ".bmp", ".txt", labelpath);
-		find_replace(labelpath, ".JPG", ".txt", labelpath);
-		find_replace(labelpath, ".JPEG", ".txt", labelpath);
+		replace_image_to_label(path, labelpath);
+		//find_replace(path, "images", "labels", labelpath);
+		//find_replace(labelpath, "JPEGImages", "labels", labelpath);
+		//find_replace(labelpath, ".jpg", ".txt", labelpath);
+		//find_replace(labelpath, ".png", ".txt", labelpath);
+		//find_replace(labelpath, ".bmp", ".txt", labelpath);
+		//find_replace(labelpath, ".JPG", ".txt", labelpath);
+		//find_replace(labelpath, ".JPEG", ".txt", labelpath);
+		//find_replace(labelpath, ".ppm", ".txt", labelpath);
 		int num_labels = 0;
 		box_label *truth = read_boxes(labelpath, &num_labels);
 		//printf(" new path: %s \n", labelpath);
@@ -1041,7 +1069,8 @@
 }
 #endif // OPENCV
 
-void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, int dont_show)
+void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh,
+				   float hier_thresh, int dont_show, int ext_output, int save_labels)
 {
     list *options = read_data_cfg(datacfg);
     char *name_list = option_find_str(options, "names", "data/names.list");
@@ -1093,13 +1122,45 @@
 		int nboxes = 0;
 		detection *dets = get_network_boxes(&net, im.w, im.h, thresh, hier_thresh, 0, 1, &nboxes, letterbox);
 		if (nms) do_nms_sort(dets, nboxes, l.classes, nms);
-		draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes);
-		free_detections(dets, nboxes);
+		draw_detections_v3(im, dets, nboxes, thresh, names, alphabet, l.classes, ext_output);
         save_image(im, "predictions");
 		if (!dont_show) {
 			show_image(im, "predictions");
 		}
 
+		// pseudo labeling concept - fast.ai
+		if(save_labels)
+		{
+			char labelpath[4096];
+			replace_image_to_label(input, labelpath);
+			//find_replace(input, ".jpg", ".txt", labelpath);
+			//find_replace(labelpath, ".png", ".txt", labelpath);
+			//find_replace(labelpath, ".bmp", ".txt", labelpath);
+			//find_replace(labelpath, ".JPG", ".txt", labelpath);
+			//find_replace(labelpath, ".JPEG", ".txt", labelpath);
+			//find_replace(labelpath, ".ppm", ".txt", labelpath);
+
+			FILE* fw = fopen(labelpath, "wb");
+			int i;
+			for (i = 0; i < nboxes; ++i) {
+				char buff[1024];
+				int class_id = -1;
+				float prob = 0;
+				for (j = 0; j < l.classes; ++j) {
+					if (dets[i].prob[j] > thresh && dets[i].prob[j] > prob) {
+						prob = dets[i].prob[j];
+						class_id = j;
+					}
+				}
+				if (class_id >= 0) {
+					sprintf(buff, "%d %2.4f %2.4f %2.4f %2.4f\n", class_id, dets[i].bbox.x, dets[i].bbox.y, dets[i].bbox.w, dets[i].bbox.h);
+					fwrite(buff, sizeof(char), strlen(buff), fw);
+				}
+			}
+			fclose(fw);
+		}
+
+		free_detections(dets, nboxes);
         free_image(im);
         free_image(sized);
         //free(boxes);
@@ -1145,6 +1206,10 @@
 	int num_of_clusters = find_int_arg(argc, argv, "-num_of_clusters", 5);
 	int width = find_int_arg(argc, argv, "-width", -1);
 	int height = find_int_arg(argc, argv, "-height", -1);
+	// extended output in test mode (output of rect bound coords)
+	// and for recall mode (extended output table-like format with results for best_class fit)
+	int ext_output = find_arg(argc, argv, "-ext_output");
+	int save_labels = find_arg(argc, argv, "-save_labels");
     if(argc < 4){
         fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
         return;
@@ -1181,7 +1246,7 @@
 		if(strlen(weights) > 0)
 			if (weights[strlen(weights) - 1] == 0x0d) weights[strlen(weights) - 1] = 0;
     char *filename = (argc > 6) ? argv[6]: 0;
-    if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, dont_show);
+    if(0==strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, dont_show, ext_output, save_labels);
     else if(0==strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear, dont_show);
     else if(0==strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile);
     else if(0==strcmp(argv[2], "recall")) validate_detector_recall(datacfg, cfg, weights);

--
Gitblit v1.10.0