Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created headless detector test phase with JSON predictions and bounding boxes for every input image; all outputted to single JSON file #1939

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
99a4252
added test_headless to detector.c and single_json method to image.c f…
Nov 15, 2018
8bf9fc8
added test_headless to detector.c and single_json method to image.c f…
Nov 15, 2018
bc42f77
Update README.md
gpsmit Nov 15, 2018
a6dcd7c
Update README.md
gpsmit Nov 15, 2018
2de4a7a
merged alexeyab upstream master into my repository
gpsmit Dec 10, 2018
a63d5a6
Merge remote-tracking branch 'upstream/master'
gpsmit Jan 8, 2019
3699db6
Merged upstream
gpsmit Feb 18, 2019
d03393e
Updated stb libraries with newer versions to resolve a stb assertion …
gpsmit Feb 18, 2019
d66a9ae
using stbi_assert instead of default assert to avoid core dump on bad…
gpsmit Feb 20, 2019
e3c07f1
merged upstream alexeyab master
gpsmit Mar 1, 2019
46752a6
Merge remote-tracking branch 'upstream/master'
gpsmit Mar 1, 2019
892881c
Merge remote-tracking branch 'upstream/master'
gpsmit Jul 30, 2019
1098117
Merge remote-tracking branch 'upstream/master'
gpsmit Jul 30, 2019
eccbde5
Merge branch 'master' into master
cenit Aug 3, 2019
843f928
using stbi_assert in more places to prevent darknet from core dumping…
gpsmit Sep 19, 2019
072aad1
Merge remote-tracking branch 'upstream/master'
gpsmit Sep 19, 2019
5dc2d75
Update README.md
gpsmit Sep 19, 2019
b9bc365
Merge branch 'master' of https://github.com/AlexeyAB/darknet
Jul 21, 2021
d752890
Merge branch 'master' into pr/1939
cenit Aug 27, 2023
547b303
restore cpp compatibility
cenit Aug 28, 2023
ffad451
Merge branch 'master' into pr/1939
cenit Oct 1, 2023
7ab0d9f
Merge branch 'master' into pr/1939
cenit Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ LDFLAGS+= -L/usr/local/zed/lib -lsl_zed
endif
endif

OBJ=image_opencv.o http_stream.o gemm.o utils.o dark_cuda.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o darknet.o detection_layer.o captcha.o route_layer.o writing.o box.o nightmare.o normalization_layer.o avgpool_layer.o coco.o dice.o yolo.o detector.o layer.o compare.o classifier.o local_layer.o swag.o shortcut_layer.o representation_layer.o activation_layer.o rnn_layer.o gru_layer.o rnn.o rnn_vid.o crnn_layer.o demo.o tag.o cifar.o go.o batchnorm_layer.o art.o region_layer.o reorg_layer.o reorg_old_layer.o super.o voxel.o tree.o yolo_layer.o gaussian_yolo_layer.o upsample_layer.o lstm_layer.o conv_lstm_layer.o scale_channels_layer.o sam_layer.o
OBJ=image_opencv.o http_stream.o gemm.o utils.o dark_cuda.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o darknet.o detection_layer.o captcha.o route_layer.o writing.o box.o nightmare.o normalization_layer.o avgpool_layer.o coco.o dice.o yolo.o detector.o layer.o compare.o classifier.o local_layer.o swag.o shortcut_layer.o representation_layer.o activation_layer.o rnn_layer.o gru_layer.o rnn.o rnn_vid.o crnn_layer.o demo.o tag.o cifar.o go.o batchnorm_layer.o art.o region_layer.o reorg_layer.o reorg_old_layer.o super.o voxel.o tree.o yolo_layer.o gaussian_yolo_layer.o upsample_layer.o lstm_layer.o conv_lstm_layer.o scale_channels_layer.o sam_layer.o jWrite.o
ifeq ($(GPU), 1)
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o activation_kernels.o im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o network_kernels.o avgpool_layer_kernels.o
Expand Down
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,50 @@ public:
};
```

## Json output

New command within detector: **test_headless** which takes a **names** file, **cfg** file and **weights** file, with an input file (with image paths) and output json file:

`./darknet detector test_headless data/coco.names cfg/yolov3.cfg ../yolov3.weights -in_filename input.txt -out_filename output.json`

example output:

```json
{
"darknet headless output": [
{
"fileName": "/path/to/source/image.jpg",
"predictions": [
{
"height": 332,
"labels": [
{
"label": "Clothing",
"score": 30.192739
}
],
"left_x": 4,
"top_y": 137,
"width": 482
},
{
"height": 375,
"labels": [
{
"label": "Person",
"score": 40.249172
}
],
"left_x": 13,
"top_y": 84,
"width": 471
}
]
}
]
}
```

## Citation

```
Expand Down
74 changes: 73 additions & 1 deletion src/detector.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, i
}

int save_after_iterations = option_find_int(options, "saveweights", (net.max_batches < 10000) ? 1000 : 10000 ); // configure when to write weights. Very useful for smaller datasets!
int save_last_weights_after = option_find_int(options, "savelast", 100);
int save_last_weights_after = option_find_int(options, "savelast", 100);
printf("Weights are saved after: %d iterations. Last weights (*_last.weight) are stored every %d iterations. \n", save_after_iterations, save_last_weights_after );


Expand Down Expand Up @@ -1773,6 +1773,76 @@ void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filenam
free_network(net);
}

void test_headless(char *datacfg, char *cfgfile, char *weightfile, float thresh, float hier_thresh, char *in_filename, char *out_filename)
{

char *name_list = datacfg;
int names_size = 0;
char **names = get_labels_custom(name_list, &names_size); //get_labels(name_list);

network net = parse_network_cfg_custom(cfgfile, 1, 0); // set batch=1
if(weightfile){
load_weights(&net, weightfile);
}
//set_batch_network(&net, 1);
fuse_conv_batchnorm(net);
calculate_binary_weights(net);
if (net.layers[net.n - 1].classes != names_size) {
printf(" 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();
}
srand(2222222);

char input[1024];
float nms=.45; // 0.4F

FILE *outputfile;
FILE *inputfile;
inputfile = fopen(in_filename, "r");
outputfile = fopen(out_filename, "w");
printf("Headless operation - writing all output to: %s \n", out_filename);
fprintf(outputfile, "{\n\"darknet headless output\":\n");
fprintf(outputfile, "[\n");
int first = 1;
while(fgets(input, 1024, inputfile) != NULL){
if(first == 0){
fprintf(outputfile, ",\n");
}
int len = strlen(input);
if( input[len-1] == '\n' )
input[len-1] = 0;
image im = load_image(input,0,0,net.c);
int letterbox = 0;
image sized = resize_image(im, net.w, net.h);
layer l = net.layers[net.n-1];
float *X = sized.data;

network_predict(net, X);

printf("%s: Predicted \n", input);

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);
single_json(im, dets, nboxes, thresh, names, l.classes, input, outputfile);
first = 0;
// free memory
free_detections(dets, nboxes);
free_image(im);
free_image(sized);

}

fprintf(outputfile, "\n]\n}");
fclose(outputfile);
fclose(inputfile);

free_ptrs((void**)names, net.layers[net.n - 1].classes);
free_network(net);

}

#if defined(OPENCV) && defined(GPU)

// adversarial attack dnn
Expand Down Expand Up @@ -1962,6 +2032,7 @@ void run_detector(int argc, char **argv)
int json_port = find_int_arg(argc, argv, "-json_port", -1);
char *http_post_host = find_char_arg(argc, argv, "-http_post_host", 0);
int time_limit_sec = find_int_arg(argc, argv, "-time_limit_sec", 0);
char *in_filename = find_char_arg(argc, argv, "-in_filename", 0);
char *out_filename = find_char_arg(argc, argv, "-out_filename", 0);
char *json_file_output = find_char_arg(argc, argv, "-json_file_output", 0);
char *outfile = find_char_arg(argc, argv, "-out", 0);
Expand Down Expand Up @@ -2042,6 +2113,7 @@ void run_detector(int argc, char **argv)
free_list_contents_kvp(options);
free_list(options);
}
else if(0==strcmp(argv[2], "test_headless")) test_headless(datacfg, cfg, weights, thresh, hier_thresh, in_filename, out_filename);
else printf(" There isn't such command: %s", argv[2]);

if (gpus && gpu_list && ngpus > 1) free(gpus);
Expand Down
77 changes: 68 additions & 9 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif
#include <math.h>

#define STBI_ASSERT(x)
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
Expand All @@ -20,6 +21,11 @@
#include <stb_image_write.h>
#endif

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "jWrite.h"

float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };

float get_color(int c, int x, int max)
Expand All @@ -35,7 +41,7 @@ float get_color(int c, int x, int max)

static float get_pixel(image m, int x, int y, int c)
{
assert(x < m.w && y < m.h && c < m.c);
STBI_ASSERT(x < m.w && y < m.h && c < m.c);
return m.data[c*m.h*m.w + y*m.w + x];
}
static float get_pixel_extend(image m, int x, int y, int c)
Expand All @@ -53,12 +59,12 @@ static float get_pixel_extend(image m, int x, int y, int c)
static void set_pixel(image m, int x, int y, int c, float val)
{
if (x < 0 || y < 0 || c < 0 || x >= m.w || y >= m.h || c >= m.c) return;
assert(x < m.w && y < m.h && c < m.c);
STBI_ASSERT(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] = val;
}
static void add_pixel(image m, int x, int y, int c, float val)
{
assert(x < m.w && y < m.h && c < m.c);
STBI_ASSERT(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] += val;
}

Expand Down Expand Up @@ -472,6 +478,59 @@ void draw_detections_v3(image im, detection *dets, int num, float thresh, char *
free(selected_detections);
}

void single_json(image im, detection *dets, int num, float thresh, char **names, int classes, char *input, FILE *output)
{
struct jWriteControl pic;
char buffer[4096];

jwOpen(&pic, buffer, 4096, JW_OBJECT, JW_COMPACT);
jwObj_string(&pic, "fileName", input);
jwObj_array(&pic, "predictions");
fwrite(buffer, strlen(buffer), 1, output);
int selected_detections_num;
detection_with_class* selected_detections = get_actual_detections(dets, num, thresh, &selected_detections_num, names);

int first = 1;
qsort(selected_detections, selected_detections_num, sizeof(*selected_detections), compare_by_lefts);
int i;
for (i = 0; i < selected_detections_num; ++i) {
if(first == 0){
fprintf(output, ",");
}
struct jWriteControl prd;
char predbuffer[4096];
jwOpen(&prd, predbuffer, 4096, JW_OBJECT, JW_COMPACT); //open predictions object
box bjson = selected_detections[i].det.bbox;
int leftxjson = (bjson.x - bjson.w / 2.)*im.w;
int widthjson = bjson.w*im.w;
int topyjson = (bjson.y - bjson.h / 2.)*im.h;
int heighthjson = bjson.h*im.h;
jwObj_int(&prd, "left_x", leftxjson);
jwObj_int(&prd, "top_y", topyjson);
jwObj_int(&prd, "width", widthjson);
jwObj_int(&prd, "height", heighthjson);
//labels
jwObj_array(&prd, "labels"); //labels array placeholder

int j;
for (j = 0; j < classes; ++j) {
if (selected_detections[i].det.prob[j] > thresh) {
jwArr_object(&prd);
jwObj_string(&prd, "label", names[j]); // add object class: predicted class
jwObj_double(&prd, "score", selected_detections[i].det.prob[j] * 100); // prob: probability
jwEnd(&prd); //end label object
}
}
jwEnd(&prd); //end labels array
jwClose(&prd); //end prediction object
fwrite(predbuffer, strlen(predbuffer), 1, output);
first = 0;
}
fprintf(output, "]");
fprintf(output, "}");
free(selected_detections);
}

void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
int i;
Expand Down Expand Up @@ -535,7 +594,7 @@ void draw_detections(image im, int num, float thresh, box *boxes, float **probs,

void transpose_image(image im)
{
assert(im.w == im.h);
STBI_ASSERT(im.w == im.h);
int n, m;
int c;
for(c = 0; c < im.c; ++c){
Expand All @@ -551,7 +610,7 @@ void transpose_image(image im)

void rotate_image_cw(image im, int times)
{
assert(im.w == im.h);
STBI_ASSERT(im.w == im.h);
times = (times + 400) % 4;
int i, x, y, c;
int n = im.w;
Expand Down Expand Up @@ -1099,7 +1158,7 @@ float three_way_min(float a, float b, float c)
// http://www.cs.rit.edu/~ncs/color/t_convert.html
void rgb_to_hsv(image im)
{
assert(im.c == 3);
STBI_ASSERT(im.c == 3);
int i, j;
float r, g, b;
float h, s, v;
Expand Down Expand Up @@ -1136,7 +1195,7 @@ void rgb_to_hsv(image im)

void hsv_to_rgb(image im)
{
assert(im.c == 3);
STBI_ASSERT(im.c == 3);
int i, j;
float r, g, b;
float h, s, v;
Expand Down Expand Up @@ -1177,7 +1236,7 @@ void hsv_to_rgb(image im)

image grayscale_image(image im)
{
assert(im.c == 3);
STBI_ASSERT(im.c == 3);
int i, j, k;
image gray = make_image(im.w, im.h, 1);
float scale[] = {0.587, 0.299, 0.114};
Expand All @@ -1203,7 +1262,7 @@ image threshold_image(image im, float thresh)

image blend_image(image fore, image back, float alpha)
{
assert(fore.w == back.w && fore.h == back.h && fore.c == back.c);
STBI_ASSERT(fore.w == back.w && fore.h == back.h && fore.c == back.c);
image blend = make_image(fore.w, fore.h, fore.c);
int i, j, k;
for(k = 0; k < fore.c; ++k){
Expand Down
1 change: 1 addition & 0 deletions src/image.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void draw_weighted_label(image a, int r, int c, image label, const float *rgb, c
void write_label(image a, int r, int c, image *characters, char *string, float *rgb);
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **labels, int classes);
void draw_detections_v3(image im, detection *dets, int num, float thresh, char **names, image **alphabet, int classes, int ext_output);
void single_json(image im, detection *dets, int num, float thresh, char **names, int classes, char *input, FILE *output);
image image_distance(image a, image b);
void scale_image(image m, float s);
// image crop_image(image im, int dx, int dy, int w, int h);
Expand Down
Loading
Loading