From 028696bf15efeca3acb3db8c42a96f7b9e0f55ff Mon Sep 17 00:00:00 2001
From: iovodov <b@ovdv.ru>
Date: Thu, 03 May 2018 13:33:46 +0000
Subject: [PATCH] Output improvements for detector results: When printing detector results, output was done in random order, obfuscating results for interpreting. Now: 1. Text output includes coordinates of rects in (left,right,top,bottom in pixels) along with label and score 2. Text output is sorted by rect lefts to simplify finding appropriate rects on image 3. If several class probs are > thresh for some detection, the most probable is written first and coordinates for others are not repeated 4. Rects are imprinted in image in order by their best class prob, so most probable rects are always on top and not overlayed by less probable ones 5. Most probable label for rect is always written first Also: 6. Message about low GPU memory include required amount
---
src/detection_layer.c | 81 +++++++++++++++++++++++++++++++++++-----
1 files changed, 70 insertions(+), 11 deletions(-)
diff --git a/src/detection_layer.c b/src/detection_layer.c
index 1b0f126..0a1c107 100644
--- a/src/detection_layer.c
+++ b/src/detection_layer.c
@@ -22,13 +22,20 @@
l.coords = coords;
l.rescore = rescore;
l.side = side;
+ l.w = side;
+ l.h = side;
assert(side*side*((1 + l.coords)*l.n + l.classes) == inputs);
l.cost = calloc(1, sizeof(float));
l.outputs = l.inputs;
l.truths = l.side*l.side*(1+l.coords+l.classes);
l.output = calloc(batch*l.outputs, sizeof(float));
l.delta = calloc(batch*l.outputs, sizeof(float));
+
+ l.forward = forward_detection_layer;
+ l.backward = backward_detection_layer;
#ifdef GPU
+ l.forward_gpu = forward_detection_layer_gpu;
+ l.backward_gpu = backward_detection_layer_gpu;
l.output_gpu = cuda_make_array(l.output, batch*l.outputs);
l.delta_gpu = cuda_make_array(l.delta, batch*l.outputs);
#endif
@@ -44,17 +51,16 @@
int locations = l.side*l.side;
int i,j;
memcpy(l.output, state.input, l.outputs*l.batch*sizeof(float));
+ //if(l.reorg) reorg(l.output, l.w*l.h, size*l.n, l.batch, 1);
int b;
if (l.softmax){
for(b = 0; b < l.batch; ++b){
int index = b*l.inputs;
for (i = 0; i < locations; ++i) {
int offset = i*l.classes;
- softmax_array(l.output + index + offset, l.classes, 1,
- l.output + index + offset);
+ softmax(l.output + index + offset, l.classes, 1,
+ l.output + index + offset, 1);
}
- int offset = locations*l.classes;
- activate_array(l.output + index + offset, locations*l.n*(1+l.coords), LOGISTIC);
}
}
if(state.train){
@@ -133,11 +139,9 @@
best_index = 0;
}
}
- /*
- if(1 && *(state.net.seen) < 100000){
+ if(l.random && *(state.net.seen) < 64000){
best_index = rand()%l.n;
}
- */
int box_index = index + locations*(l.classes + l.n) + (i*l.n + best_index) * l.coords;
int tbox_index = truth_index + 1 + l.classes;
@@ -175,10 +179,6 @@
avg_iou += iou;
++count;
}
- if(l.softmax){
- gradient_array(l.output + index + locations*l.classes, locations*l.n*(1+l.coords),
- LOGISTIC, l.delta + index + locations*l.classes);
- }
}
if(0){
@@ -208,9 +208,11 @@
}
+ *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
printf("Detection Avg IOU: %f, Pos Cat: %f, All Cat: %f, Pos Obj: %f, Any Obj: %f, count: %d\n", avg_iou/count, avg_cat/count, avg_allcat/(count*l.classes), avg_obj/count, avg_anyobj/(l.batch*locations*l.n), count);
+ //if(l.reorg) reorg(l.delta, l.w*l.h, size*l.n, l.batch, 0);
}
}
@@ -219,6 +221,35 @@
axpy_cpu(l.batch*l.inputs, 1, l.delta, 1, state.delta, 1);
}
+void get_detection_boxes(layer l, int w, int h, float thresh, float **probs, box *boxes, int only_objectness)
+{
+ int i,j,n;
+ float *predictions = l.output;
+ //int per_cell = 5*num+classes;
+ for (i = 0; i < l.side*l.side; ++i){
+ int row = i / l.side;
+ int col = i % l.side;
+ for(n = 0; n < l.n; ++n){
+ int index = i*l.n + n;
+ int p_index = l.side*l.side*l.classes + i*l.n + n;
+ float scale = predictions[p_index];
+ int box_index = l.side*l.side*(l.classes + l.n) + (i*l.n + n)*4;
+ boxes[index].x = (predictions[box_index + 0] + col) / l.side * w;
+ boxes[index].y = (predictions[box_index + 1] + row) / l.side * h;
+ boxes[index].w = pow(predictions[box_index + 2], (l.sqrt?2:1)) * w;
+ boxes[index].h = pow(predictions[box_index + 3], (l.sqrt?2:1)) * h;
+ for(j = 0; j < l.classes; ++j){
+ int class_index = i*l.classes;
+ float prob = scale*predictions[class_index+j];
+ probs[index][j] = (prob > thresh) ? prob : 0;
+ }
+ if(only_objectness){
+ probs[index][0] = scale;
+ }
+ }
+ }
+}
+
#ifdef GPU
void forward_detection_layer_gpu(const detection_layer l, network_state state)
@@ -254,3 +285,31 @@
}
#endif
+void get_detection_detections(layer l, int w, int h, float thresh, detection *dets)
+{
+ int i, j, n;
+ float *predictions = l.output;
+ //int per_cell = 5*num+classes;
+ for (i = 0; i < l.side*l.side; ++i) {
+ int row = i / l.side;
+ int col = i % l.side;
+ for (n = 0; n < l.n; ++n) {
+ int index = i*l.n + n;
+ int p_index = l.side*l.side*l.classes + i*l.n + n;
+ float scale = predictions[p_index];
+ int box_index = l.side*l.side*(l.classes + l.n) + (i*l.n + n) * 4;
+ box b;
+ b.x = (predictions[box_index + 0] + col) / l.side * w;
+ b.y = (predictions[box_index + 1] + row) / l.side * h;
+ b.w = pow(predictions[box_index + 2], (l.sqrt ? 2 : 1)) * w;
+ b.h = pow(predictions[box_index + 3], (l.sqrt ? 2 : 1)) * h;
+ dets[index].bbox = b;
+ dets[index].objectness = scale;
+ for (j = 0; j < l.classes; ++j) {
+ int class_index = i*l.classes;
+ float prob = scale*predictions[class_index + j];
+ dets[index].prob[j] = (prob > thresh) ? prob : 0;
+ }
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.10.0