From 3d9aa2af4718a3bd5bbe23de2022987cb767e9c5 Mon Sep 17 00:00:00 2001 From: AlexeyAB Date: Tue, 7 Apr 2020 22:40:20 +0300 Subject: [PATCH] `./darknet detector calc_anchors` calculates both anchors= and counters_per_class= --- README.md | 2 ++ src/detector.c | 35 ++++++++++++++++++++++++++++++++++- src/network_kernels.cu | 2 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a00d8924..ce394485 100644 --- a/README.md +++ b/README.md @@ -104,8 +104,10 @@ You can get cfg-files by path: `darknet/cfg/` * **PyTorch > ONNX > CoreML > iOS** how to convert cfg/weights-files to pt-file: [ultralytics/yolov3](https://github.com/ultralytics/yolov3#darknet-conversion) and [iOS App](https://itunes.apple.com/app/id1452689527) * **TensorRT** for YOLOv3 (-70% faster inference): [Yolo is natively supported in DeepStream 4.0](https://news.developer.nvidia.com/deepstream-sdk-4-now-available/) * **TVM** - compilation of deep learning models (Keras, MXNet, PyTorch, Tensorflow, CoreML, DarkNet) into minimum deployable modules on diverse hardware backends (CPUs, GPUs, FPGA, and specialized accelerators): https://tvm.ai/about +* **OpenDataCam** - It detects, tracks and counts moving objects by using Yolo: https://github.com/opendatacam/opendatacam#-hardware-pre-requisite * **Netron** - Visualizer for neural networks: https://github.com/lutzroeder/netron + #### Datasets * MS COCO: use `./scripts/get_coco_dataset.sh` to get labeled MS COCO detection dataset diff --git a/src/detector.c b/src/detector.c index cd781633..b1a56e51 100644 --- a/src/detector.c +++ b/src/detector.c @@ -1369,6 +1369,9 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int int number_of_images = plist->size; char **paths = (char **)list_to_array(plist); + int classes = option_find_int(options, "classes", 1); + int* counter_per_class = (int*)xcalloc(classes, sizeof(int)); + srand(time(0)); int number_of_boxes = 0; printf(" read labels from %d images \n", number_of_images); @@ -1395,6 +1398,12 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int system(buff); if (check_mistakes) getchar(); } + if (truth[j].id >= classes) { + classes = truth[j].id + 1; + counter_per_class = (int*)xrealloc(counter_per_class, classes * sizeof(int)); + } + counter_per_class[truth[j].id]++; + number_of_boxes++; rel_width_height_array = (float*)xrealloc(rel_width_height_array, 2 * number_of_boxes * sizeof(float)); @@ -1461,10 +1470,33 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int } else avg_iou += best_iou; } + + char buff[1024]; + FILE* fwc = fopen("counters_per_class.txt", "wb"); + if (fwc) { + sprintf(buff, "counters_per_class = "); + printf("\n%s", buff); + fwrite(buff, sizeof(char), strlen(buff), fwc); + for (i = 0; i < classes; ++i) { + sprintf(buff, "%d", counter_per_class[i]); + printf("%s", buff); + fwrite(buff, sizeof(char), strlen(buff), fwc); + if (i < classes - 1) { + fwrite(", ", sizeof(char), 2, fwc); + printf(", "); + } + } + printf("\n"); + fclose(fwc); + } + else { + printf(" Error: file counters_per_class.txt can't be open \n"); + } + avg_iou = 100 * avg_iou / number_of_boxes; printf("\n avg IoU = %2.2f %% \n", avg_iou); - char buff[1024]; + FILE* fw = fopen("anchors.txt", "wb"); if (fw) { printf("\nSaving anchors to the file: anchors.txt \n"); @@ -1494,6 +1526,7 @@ void calc_anchors(char *datacfg, int num_of_clusters, int width, int height, int #endif // OPENCV } free(rel_width_height_array); + free(counter_per_class); getchar(); } diff --git a/src/network_kernels.cu b/src/network_kernels.cu index dbf57531..c611e229 100644 --- a/src/network_kernels.cu +++ b/src/network_kernels.cu @@ -245,7 +245,7 @@ void backward_network_gpu(network net, network_state state) } if (net.adversarial) { int x_size = get_network_input_size(net)*net.batch; - printf(" x_size = %d, original_delta = %p, original_input = %p, net.learning_rate = %d \n", + printf(" x_size = %d, original_delta = %p, original_input = %p, net.learning_rate = %f \n", x_size, original_delta, original_input, net.learning_rate); axpy_ongpu(x_size, net.learning_rate, original_delta, 1, original_input, 1); constrain_min_max_ongpu(x_size, 0, 1, original_input, 1);