From c6978cab02e6accf531db9cd35749975b72bcf9d Mon Sep 17 00:00:00 2001 From: Nicola Grassi Date: Mon, 14 Dec 2020 16:04:35 +0100 Subject: [PATCH 1/3] Allowed training with data tagged on more classes than configuration. I was need this feature to be able to decrease the number of classes and train with same tagged images database. --- build/darknet/darknet.vcxproj | 22 +++++++------- src/coco.c | 2 +- src/data.c | 57 ++++++++++++++++++++++++++++++++--- src/data.h | 1 + src/detector.c | 16 +++++----- src/yolo.c | 2 +- 6 files changed, 76 insertions(+), 24 deletions(-) diff --git a/build/darknet/darknet.vcxproj b/build/darknet/darknet.vcxproj index 5282de26d91..6ead76b4cce 100644 --- a/build/darknet/darknet.vcxproj +++ b/build/darknet/darknet.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,38 +21,38 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 8.1 + 10.0.18362.0 Application true - v140 + v141 MultiByte Application true - v140 + v141 MultiByte Application false - v140 + v141 true MultiByte Application false - v140 + v141 true MultiByte - + @@ -132,7 +132,7 @@ true true true - $(OPENCV_DIR)\include;C:\opencv_3.0\opencv\build\include;..\..\include;..\..\3rdparty\stb\include;..\..\3rdparty\pthreads\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(CUDNN)\include;$(cudnn)\include + $(OPENCV_DIR)\include;C:\opencv_3\opencv_3.2.0\build\include;..\..\include;..\..\3rdparty\stb\include;..\..\3rdparty\pthreads\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(CUDNN)\include;$(cudnn)\include OPENCV;CUDNN_HALF;CUDNN;_TIMESPEC_DEFINED;_SCL_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;_CONSOLE;_LIB;%(PreprocessorDefinitions) c11 c++1y @@ -147,13 +147,13 @@ true true true - $(OPENCV_DIR)\x64\vc15\lib;$(OPENCV_DIR)\x64\vc14\lib;C:\opencv_3.0\opencv\build\x64\vc14\lib;$(CUDA_PATH)\lib\$(PlatformName);$(CUDNN)\lib\x64;$(cudnn)\lib\x64;..\..\3rdparty\pthreads\lib;%(AdditionalLibraryDirectories) + C:\Opencv_3\Opencv_3.2.0\build\x64\vc14\lib;$(CUDNN)\lib\x64;$(CUDA_PATH)\lib\$(PlatformName);$(cudnn)\lib\x64;..\..\3rdparty\pthreads\lib;%(AdditionalLibraryDirectories) pthreadVC2.lib;cublas.lib;curand.lib;cudart.lib;%(AdditionalDependencies) $(OutDir)\$(TargetName)$(TargetExt) 64 - compute_35,sm_35;compute_86,sm_86 + compute_30,sm_30;compute_75,sm_75 @@ -302,6 +302,6 @@ - + \ No newline at end of file diff --git a/src/coco.c b/src/coco.c index 8c386a224bd..e2f27c1859a 100644 --- a/src/coco.c +++ b/src/coco.c @@ -294,7 +294,7 @@ void validate_coco_recall(char *cfgfile, char *weightfile) replace_image_to_label(path, labelpath); int num_labels = 0; - box_label *truth = read_boxes(labelpath, &num_labels); + box_label *truth = read_boxes_class(labelpath, &num_labels, classes); for(k = 0; k < side*side*l.n; ++k){ if(probs[k][0] > thresh){ ++proposals; diff --git a/src/data.c b/src/data.c index d33f2199082..3a8f6cdc513 100644 --- a/src/data.c +++ b/src/data.c @@ -193,7 +193,6 @@ matrix load_image_augment_paths(char **paths, int n, int use_flip, int min, int return X; } - box_label *read_boxes(char *filename, int *n) { box_label* boxes = (box_label*)xcalloc(1, sizeof(box_label)); @@ -220,7 +219,57 @@ box_label *read_boxes(char *filename, int *n) float x, y, h, w; int id; int count = 0; + while (fscanf(file, "%d %f %f %f %f", &id, &x, &y, &w, &h) == 5) { + boxes = (box_label*)xrealloc(boxes, (count + 1) * sizeof(box_label)); + boxes[count].track_id = count + img_hash; + //printf(" boxes[count].track_id = %d, count = %d \n", boxes[count].track_id, count); + boxes[count].id = id; + boxes[count].x = x; + boxes[count].y = y; + boxes[count].h = h; + boxes[count].w = w; + boxes[count].left = x - w / 2; + boxes[count].right = x + w / 2; + boxes[count].top = y - h / 2; + boxes[count].bottom = y + h / 2; + ++count; + } + fclose(file); + *n = count; + return boxes; +} + +box_label *read_boxes_class(char *filename, int *n, int max_num_classes) +{ + box_label* boxes = (box_label*)xcalloc(1, sizeof(box_label)); + FILE *file = fopen(filename, "r"); + if (!file) { + printf("Can't open label file. (This can be normal only if you use MSCOCO): %s \n", filename); + //file_error(filename); + FILE* fw = fopen("bad.list", "a"); + fwrite(filename, sizeof(char), strlen(filename), fw); + char *new_line = "\n"; + fwrite(new_line, sizeof(char), strlen(new_line), fw); + fclose(fw); + if (check_mistakes) { + printf("\n Error in read_boxes_class() \n"); + getchar(); + } + + *n = 0; + return boxes; + } + const int max_obj_img = 4000;// 30000; + const int img_hash = (custom_hash(filename) % max_obj_img)*max_obj_img; + //printf(" img_hash = %d, filename = %s; ", img_hash, filename); + float x, y, h, w; + int id; + int count = 0; while(fscanf(file, "%d %f %f %f %f", &id, &x, &y, &w, &h) == 5){ + if (id >= max_num_classes) { + printf("Warn class %d >= %d on %s \n", id, max_num_classes, filename); + continue; + } boxes = (box_label*)xrealloc(boxes, (count + 1) * sizeof(box_label)); boxes[count].track_id = count + img_hash; //printf(" boxes[count].track_id = %d, count = %d \n", boxes[count].track_id, count); @@ -303,7 +352,7 @@ void fill_truth_swag(char *path, float *truth, int classes, int flip, float dx, replace_image_to_label(path, labelpath); int count = 0; - box_label *boxes = read_boxes(labelpath, &count); + box_label *boxes = read_boxes_class(labelpath, &count, classes); randomize_boxes(boxes, count); correct_boxes(boxes, count, dx, dy, sx, sy, flip); float x,y,w,h; @@ -337,7 +386,7 @@ void fill_truth_region(char *path, float *truth, int classes, int num_boxes, int replace_image_to_label(path, labelpath); int count = 0; - box_label *boxes = read_boxes(labelpath, &count); + box_label *boxes = read_boxes_class(labelpath, &count, classes); randomize_boxes(boxes, count); correct_boxes(boxes, count, dx, dy, sx, sy, flip); float x,y,w,h; @@ -382,7 +431,7 @@ int fill_truth_detection(const char *path, int num_boxes, int truth_size, float int count = 0; int i; - box_label *boxes = read_boxes(labelpath, &count); + box_label *boxes = read_boxes_class(labelpath, &count, classes); int min_w_h = 0; float lowest_w = 1.F / net_w; float lowest_h = 1.F / net_h; diff --git a/src/data.h b/src/data.h index ecbd0188e7f..fabe3f5c01e 100644 --- a/src/data.h +++ b/src/data.h @@ -95,6 +95,7 @@ data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *h float aspect, float hue, float saturation, float exposure, int use_mixup, int use_blur, int show_imgs, float label_smooth_eps, int dontuse_opencv, int contrastive); data load_go(char *filename); +box_label *read_boxes_class(char *filename, int *n, int max_num_classes); box_label *read_boxes(char *filename, int *n); data load_cifar10_data(char *filename); data load_all_cifar10(); diff --git a/src/detector.c b/src/detector.c index a2fdf0bf3cd..676214f5908 100644 --- a/src/detector.c +++ b/src/detector.c @@ -55,7 +55,7 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i if (net_classes != names_size) { printf("\n Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n", name_list, names_size, net_classes, cfgfile); - if (net_classes > names_size) getchar(); + //if (net_classes > names_size) getchar(); } free_ptrs((void**)names, net_map.layers[net_map.n - 1].classes); } @@ -880,6 +880,7 @@ void validate_detector_recall(char *datacfg, char *cfgfile, char *weightfile) int num_labels = 0; box_label *truth = read_boxes(labelpath, &num_labels); + printf("CI PASSA--\n"); for (k = 0; k < nboxes; ++k) { if (dets[k].objectness > thresh) { ++proposals; @@ -962,7 +963,7 @@ float validate_detector_map(char *datacfg, char *cfgfile, char *weightfile, floa if (net.layers[net.n - 1].classes != names_size) { printf("\n Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n", name_list, names_size, net.layers[net.n - 1].classes, cfgfile); - getchar(); + //getchar(); } srand(time(0)); printf("\n calculation mAP (mean average precision)...\n"); @@ -1075,7 +1076,7 @@ float validate_detector_map(char *datacfg, char *cfgfile, char *weightfile, floa char labelpath[4096]; replace_image_to_label(path, labelpath); int num_labels = 0; - box_label *truth = read_boxes(labelpath, &num_labels); + box_label *truth = read_boxes_class(labelpath, &num_labels, classes); int j; for (j = 0; j < num_labels; ++j) { truth_classes_count[truth[j].id]++; @@ -1091,7 +1092,7 @@ float validate_detector_map(char *datacfg, char *cfgfile, char *weightfile, floa char labelpath_dif[4096]; replace_image_to_label(path_dif, labelpath_dif); - truth_dif = read_boxes(labelpath_dif, &num_labels_dif); + truth_dif = read_boxes_class(labelpath_dif, &num_labels_dif, classes); } const int checkpoint_detections_count = detections_count; @@ -1450,8 +1451,9 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int char labelpath[4096]; replace_image_to_label(path, labelpath); + int num_labels = 0; - box_label *truth = read_boxes(labelpath, &num_labels); + box_label *truth = read_boxes_class(labelpath, &num_labels, classes); //printf(" new path: %s \n", labelpath); char *buff = (char*)xcalloc(6144, sizeof(char)); for (j = 0; j < num_labels; ++j) @@ -1620,7 +1622,7 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam if (net.layers[net.n - 1].classes != names_size) { printf("\n Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n", name_list, names_size, net.layers[net.n - 1].classes, cfgfile); - if (net.layers[net.n - 1].classes > names_size) getchar(); + //if (net.layers[net.n - 1].classes > names_size) getchar(); } srand(2222222); char buff[256]; @@ -1791,7 +1793,7 @@ void draw_object(char *datacfg, char *cfgfile, char *weightfile, char *filename, if (net.layers[net.n - 1].classes != names_size) { printf("\n Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n", name_list, names_size, net.layers[net.n - 1].classes, cfgfile); - if (net.layers[net.n - 1].classes > names_size) getchar(); + //if (net.layers[net.n - 1].classes > names_size) getchar(); } srand(2222222); diff --git a/src/yolo.c b/src/yolo.c index 384c487291e..b6fe7ce337f 100644 --- a/src/yolo.c +++ b/src/yolo.c @@ -253,7 +253,7 @@ void validate_yolo_recall(char *cfgfile, char *weightfile) replace_image_to_label(path, labelpath); int num_labels = 0; - box_label *truth = read_boxes(labelpath, &num_labels); + box_label *truth = read_boxes_class(labelpath, &num_labels, classes); for(k = 0; k < side*side*l.n; ++k){ if(probs[k][0] > thresh){ ++proposals; From 1cf58ecbeb94b64738c2e3dbadb609db4048c35c Mon Sep 17 00:00:00 2001 From: Nicola Grassi Date: Mon, 14 Dec 2020 16:51:22 +0100 Subject: [PATCH 2/3] edited vcxproj for compatibility with pull request --- build/darknet/darknet.vcxproj | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/build/darknet/darknet.vcxproj b/build/darknet/darknet.vcxproj index 6ead76b4cce..5282de26d91 100644 --- a/build/darknet/darknet.vcxproj +++ b/build/darknet/darknet.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -21,38 +21,38 @@ {4CF5694F-12A5-4012-8D94-9A0915E9FEB5} darknet - 10.0.18362.0 + 8.1 Application true - v141 + v140 MultiByte Application true - v141 + v140 MultiByte Application false - v141 + v140 true MultiByte Application false - v141 + v140 true MultiByte - + @@ -132,7 +132,7 @@ true true true - $(OPENCV_DIR)\include;C:\opencv_3\opencv_3.2.0\build\include;..\..\include;..\..\3rdparty\stb\include;..\..\3rdparty\pthreads\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(CUDNN)\include;$(cudnn)\include + $(OPENCV_DIR)\include;C:\opencv_3.0\opencv\build\include;..\..\include;..\..\3rdparty\stb\include;..\..\3rdparty\pthreads\include;%(AdditionalIncludeDirectories);$(CudaToolkitIncludeDir);$(CUDNN)\include;$(cudnn)\include OPENCV;CUDNN_HALF;CUDNN;_TIMESPEC_DEFINED;_SCL_SECURE_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;_CRT_RAND_S;GPU;WIN32;_CONSOLE;_LIB;%(PreprocessorDefinitions) c11 c++1y @@ -147,13 +147,13 @@ true true true - C:\Opencv_3\Opencv_3.2.0\build\x64\vc14\lib;$(CUDNN)\lib\x64;$(CUDA_PATH)\lib\$(PlatformName);$(cudnn)\lib\x64;..\..\3rdparty\pthreads\lib;%(AdditionalLibraryDirectories) + $(OPENCV_DIR)\x64\vc15\lib;$(OPENCV_DIR)\x64\vc14\lib;C:\opencv_3.0\opencv\build\x64\vc14\lib;$(CUDA_PATH)\lib\$(PlatformName);$(CUDNN)\lib\x64;$(cudnn)\lib\x64;..\..\3rdparty\pthreads\lib;%(AdditionalLibraryDirectories) pthreadVC2.lib;cublas.lib;curand.lib;cudart.lib;%(AdditionalDependencies) $(OutDir)\$(TargetName)$(TargetExt) 64 - compute_30,sm_30;compute_75,sm_75 + compute_35,sm_35;compute_86,sm_86 @@ -302,6 +302,6 @@ - + \ No newline at end of file From 21b9be2b0ed16c5cae87bb54af48430e90bcaf5e Mon Sep 17 00:00:00 2001 From: Stefano Sinigardi Date: Sun, 1 Oct 2023 22:13:27 +0200 Subject: [PATCH 3/3] Restore build --- src/data.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/data.c b/src/data.c index f981ab9bb4e..903eb29c060 100644 --- a/src/data.c +++ b/src/data.c @@ -245,10 +245,6 @@ box_label *read_boxes_class(char *filename, int *n, int max_num_classes) char *new_line = "\n"; fwrite(new_line, sizeof(char), strlen(new_line), fw); fclose(fw); - if (check_mistakes) { - printf("\n Error in read_boxes_class() \n"); - getchar(); - } *n = 0; return boxes;