From 1b5afb45838e603fa6780762eb8cc59246dc2d81 Mon Sep 17 00:00:00 2001
From: IlyaOvodov <b@ovdv.ru>
Date: Tue, 08 May 2018 11:09:35 +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/coco.c | 119 ++++++++++++++++++++++-------------------------------------
1 files changed, 44 insertions(+), 75 deletions(-)
diff --git a/src/coco.c b/src/coco.c
index aadf09d..c95e30d 100644
--- a/src/coco.c
+++ b/src/coco.c
@@ -6,6 +6,7 @@
#include "utils.h"
#include "parser.h"
#include "box.h"
+#include "demo.h"
#ifdef OPENCV
#include "opencv2/highgui/highgui_c.h"
@@ -15,39 +16,14 @@
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 draw_coco(image im, int num, float thresh, box *boxes, float **probs, char *label)
-{
- int classes = 80;
- int i;
-
- for(i = 0; i < num; ++i){
- int class = max_index(probs[i], classes);
- float prob = probs[i][class];
- if(prob > thresh){
- int width = sqrt(prob)*5 + 1;
- printf("%f %s\n", prob, coco_classes[class]);
- float red = get_color(0,class,classes);
- float green = get_color(1,class,classes);
- float blue = get_color(2,class,classes);
- box b = boxes[i];
-
- int left = (b.x-b.w/2.)*im.w;
- int right = (b.x+b.w/2.)*im.w;
- int top = (b.y-b.h/2.)*im.h;
- int bot = (b.y+b.h/2.)*im.h;
- draw_box_width(im, left, top, right, bot, width, red, green, blue);
- }
- }
- show_image(im, label);
-}
-
void train_coco(char *cfgfile, char *weightfile)
{
//char *train_images = "/home/pjreddie/data/voc/test/train.txt";
- char *train_images = "/home/pjreddie/data/coco/train.txt";
+ //char *train_images = "/home/pjreddie/data/coco/train.txt";
+ char *train_images = "data/coco.trainval.txt";
+ //char *train_images = "data/bags.train.list";
char *backup_directory = "/home/pjreddie/backup/";
srand(time(0));
- data_seed = time(0);
char *base = basecfg(cfgfile);
printf("%s\n", base);
float avg_loss = -1;
@@ -83,6 +59,11 @@
args.d = &buffer;
args.type = REGION_DATA;
+ args.angle = net.angle;
+ args.exposure = net.exposure;
+ args.saturation = net.saturation;
+ args.hue = net.hue;
+
pthread_t load_thread = load_data_in_thread(args);
clock_t time;
//while(i*imgs < N*120){
@@ -109,11 +90,16 @@
avg_loss = avg_loss*.9 + loss*.1;
printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock()-time), i*imgs);
- if(i%1000==0){
+ if(i%1000==0 || (i < 1000 && i%100 == 0)){
char buff[256];
sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
save_weights(net, buff);
}
+ if(i%100==0){
+ char buff[256];
+ sprintf(buff, "%s/%s.backup", backup_directory, base);
+ save_weights(net, buff);
+ }
free_data(train);
}
char buff[256];
@@ -121,34 +107,6 @@
save_weights(net, buff);
}
-void convert_coco_detections(float *predictions, int classes, int num, int square, int side, int w, int h, float thresh, float **probs, box *boxes, int only_objectness)
-{
- int i,j,n;
- //int per_cell = 5*num+classes;
- for (i = 0; i < side*side; ++i){
- int row = i / side;
- int col = i % side;
- for(n = 0; n < num; ++n){
- int index = i*num + n;
- int p_index = side*side*classes + i*num + n;
- float scale = predictions[p_index];
- int box_index = side*side*(classes + num) + (i*num + n)*4;
- boxes[index].x = (predictions[box_index + 0] + col) / side * w;
- boxes[index].y = (predictions[box_index + 1] + row) / side * h;
- boxes[index].w = pow(predictions[box_index + 2], (square?2:1)) * w;
- boxes[index].h = pow(predictions[box_index + 3], (square?2:1)) * h;
- for(j = 0; j < classes; ++j){
- int class_index = i*classes;
- float prob = scale*predictions[class_index+j];
- probs[index][j] = (prob > thresh) ? prob : 0;
- }
- if(only_objectness){
- probs[index][0] = scale;
- }
- }
- }
-}
-
void print_cocos(FILE *fp, int image_id, box *boxes, float **probs, int num_boxes, int classes, int w, int h)
{
int i, j;
@@ -198,7 +156,6 @@
layer l = net.layers[net.n-1];
int classes = l.classes;
- int square = l.sqrt;
int side = l.side;
int j;
@@ -215,7 +172,7 @@
int i=0;
int t;
- float thresh = .001;
+ float thresh = .01;
int nms = 1;
float iou_thresh = .5;
@@ -255,11 +212,11 @@
char *path = paths[i+t-nthreads];
int image_id = get_coco_image_id(path);
float *X = val_resized[t].data;
- float *predictions = network_predict(net, X);
+ network_predict(net, X);
int w = val[t].w;
int h = val[t].h;
- convert_coco_detections(predictions, classes, l.n, square, side, w, h, thresh, probs, boxes, 0);
- if (nms) do_nms_sort(boxes, probs, side*side*l.n, classes, iou_thresh);
+ get_detection_boxes(l, w, h, thresh, probs, boxes, 0);
+ if (nms) do_nms_sort_v2(boxes, probs, side*side*l.n, classes, iou_thresh);
print_cocos(fp, image_id, boxes, probs, side*side*l.n, classes, w, h);
free_image(val[t]);
free_image(val_resized[t]);
@@ -288,7 +245,6 @@
layer l = net.layers[net.n-1];
int classes = l.classes;
- int square = l.sqrt;
int side = l.side;
int j, k;
@@ -320,14 +276,15 @@
image orig = load_image_color(path, 0, 0);
image sized = resize_image(orig, net.w, net.h);
char *id = basecfg(path);
- float *predictions = network_predict(net, sized.data);
- convert_coco_detections(predictions, classes, l.n, square, side, 1, 1, thresh, probs, boxes, 1);
+ network_predict(net, sized.data);
+ get_detection_boxes(l, 1, 1, thresh, probs, boxes, 1);
if (nms) do_nms(boxes, probs, side*side*l.n, 1, nms_thresh);
- char *labelpath = find_replace(path, "images", "labels");
- labelpath = find_replace(labelpath, "JPEGImages", "labels");
- labelpath = find_replace(labelpath, ".jpg", ".txt");
- labelpath = find_replace(labelpath, ".JPEG", ".txt");
+ char labelpath[4096];
+ find_replace(path, "images", "labels", labelpath);
+ find_replace(labelpath, "JPEGImages", "labels", labelpath);
+ find_replace(labelpath, ".jpg", ".txt", labelpath);
+ find_replace(labelpath, ".JPEG", ".txt", labelpath);
int num_labels = 0;
box_label *truth = read_boxes(labelpath, &num_labels);
@@ -361,7 +318,7 @@
void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
{
-
+ image **alphabet = load_alphabet();
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
@@ -369,6 +326,7 @@
detection_layer l = net.layers[net.n-1];
set_batch_network(&net, 1);
srand(2222222);
+ float nms = .4;
clock_t time;
char buff[256];
char *input = buff;
@@ -390,12 +348,13 @@
image sized = resize_image(im, net.w, net.h);
float *X = sized.data;
time=clock();
- float *predictions = network_predict(net, X);
+ network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
- convert_coco_detections(predictions, l.classes, l.n, l.sqrt, l.side, 1, 1, thresh, probs, boxes, 0);
- draw_coco(im, l.side*l.side*l.n, thresh, boxes, probs, "predictions");
-
- show_image(sized, "resized");
+ get_detection_boxes(l, 1, 1, thresh, probs, boxes, 0);
+ if (nms) do_nms_sort_v2(boxes, probs, l.side*l.side*l.n, l.classes, nms);
+ draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, coco_classes, alphabet, 80);
+ save_image(im, "prediction");
+ show_image(im, "predictions");
free_image(im);
free_image(sized);
#ifdef OPENCV
@@ -408,7 +367,15 @@
void run_coco(int argc, char **argv)
{
+ int dont_show = find_arg(argc, argv, "-dont_show");
+ int http_stream_port = find_int_arg(argc, argv, "-http_port", -1);
+ char *out_filename = find_char_arg(argc, argv, "-out_filename", 0);
+ char *prefix = find_char_arg(argc, argv, "-prefix", 0);
float thresh = find_float_arg(argc, argv, "-thresh", .2);
+ float hier_thresh = find_float_arg(argc, argv, "-hier", .5);
+ int cam_index = find_int_arg(argc, argv, "-c", 0);
+ int frame_skip = find_int_arg(argc, argv, "-s", 0);
+
if(argc < 4){
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
return;
@@ -421,4 +388,6 @@
else if(0==strcmp(argv[2], "train")) train_coco(cfg, weights);
else if(0==strcmp(argv[2], "valid")) validate_coco(cfg, weights);
else if(0==strcmp(argv[2], "recall")) validate_coco_recall(cfg, weights);
+ else if(0==strcmp(argv[2], "demo")) demo(cfg, weights, thresh, hier_thresh, cam_index, filename, coco_classes, 80, frame_skip,
+ prefix, out_filename, http_stream_port, dont_show);
}
--
Gitblit v1.10.0