Some bug fixes, random stuff

This commit is contained in:
Joseph Redmon
2015-09-01 11:21:01 -07:00
parent 9d42f49a24
commit 8bcdee8658
30 changed files with 629 additions and 205 deletions

View File

@ -17,7 +17,7 @@ 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
void draw_coco(image im, float *pred, int side, char *label)
{
int classes = 81;
int classes = 1;
int elems = 4+classes;
int j;
int r, c;
@ -26,10 +26,9 @@ void draw_coco(image im, float *pred, int side, char *label)
for(c = 0; c < side; ++c){
j = (r*side + c) * elems;
int class = max_index(pred+j, classes);
if (class == 0) continue;
if (pred[j+class] > 0.2){
int width = pred[j+class]*5 + 1;
printf("%f %s\n", pred[j+class], coco_classes[class-1]);
printf("%f %s\n", pred[j+class], "object"); //coco_classes[class-1]);
float red = get_color(0,class,classes);
float green = get_color(1,class,classes);
float blue = get_color(2,class,classes);
@ -37,10 +36,10 @@ void draw_coco(image im, float *pred, int side, char *label)
j += classes;
box predict = {pred[j+0], pred[j+1], pred[j+2], pred[j+3]};
box anchor = {(c+.5)/side, (r+.5)/side, .5, .5};
box decode = decode_box(predict, anchor);
predict.x = (predict.x+c)/side;
predict.y = (predict.y+r)/side;
draw_bbox(im, decode, width, red, green, blue);
draw_bbox(im, predict, width, red, green, blue);
}
}
}
@ -49,7 +48,8 @@ void draw_coco(image im, float *pred, int side, char *label)
void train_coco(char *cfgfile, char *weightfile)
{
char *train_images = "/home/pjreddie/data/coco/train.txt";
//char *train_images = "/home/pjreddie/data/coco/train.txt";
char *train_images = "/home/pjreddie/data/voc/test/train.txt";
char *backup_directory = "/home/pjreddie/backup/";
srand(time(0));
data_seed = time(0);
@ -65,8 +65,11 @@ void train_coco(char *cfgfile, char *weightfile)
int i = net.seen/imgs;
data train, buffer;
int classes = 81;
int side = 7;
layer l = net.layers[net.n - 1];
int side = l.side;
int classes = l.classes;
list *plist = get_paths(train_images);
int N = plist->size;
@ -95,9 +98,9 @@ void train_coco(char *cfgfile, char *weightfile)
printf("Loaded: %lf seconds\n", sec(clock()-time));
/*
image im = float_to_image(net.w, net.h, 3, train.X.vals[114]);
image im = float_to_image(net.w, net.h, 3, train.X.vals[113]);
image copy = copy_image(im);
draw_coco(copy, train.y.vals[114], 7, "truth");
draw_coco(copy, train.y.vals[113], 7, "truth");
cvWaitKey(0);
free_image(copy);
*/
@ -109,12 +112,19 @@ void train_coco(char *cfgfile, char *weightfile)
avg_loss = avg_loss*.9 + loss*.1;
printf("%d: %f, %f avg, %lf seconds, %d images\n", i, loss, avg_loss, sec(clock()-time), i*imgs);
if((i-1)*imgs <= 80*N && i*imgs > N*80){
fprintf(stderr, "First stage done.\n");
if((i-1)*imgs <= N && i*imgs > N){
fprintf(stderr, "First stage done\n");
net.learning_rate *= 10;
char buff[256];
sprintf(buff, "%s/%s_first_stage.weights", backup_directory, base);
save_weights(net, buff);
return;
}
if((i-1)*imgs <= 80*N && i*imgs > N*80){
fprintf(stderr, "Second stage done.\n");
char buff[256];
sprintf(buff, "%s/%s_second_stage.weights", backup_directory, base);
save_weights(net, buff);
}
if(i%1000==0){
char buff[256];
@ -128,25 +138,52 @@ void train_coco(char *cfgfile, char *weightfile)
save_weights(net, buff);
}
void convert_cocos(float *predictions, int classes, int objectness, int background, int num_boxes, int w, int h, float thresh, float **probs, box *boxes)
void get_probs(float *predictions, int total, int classes, int inc, float **probs)
{
int i,j;
int per_box = 4+classes+(background || objectness);
for (i = 0; i < num_boxes*num_boxes; ++i){
float scale = 1;
if(objectness) scale = 1-predictions[i*per_box];
int offset = i*per_box+(background||objectness);
for (i = 0; i < total; ++i){
int index = i*inc;
float scale = predictions[index];
probs[i][0] = scale;
for(j = 0; j < classes; ++j){
float prob = scale*predictions[offset+j];
probs[i][j] = scale*predictions[index+j+1];
}
}
}
void get_boxes(float *predictions, int n, int num_boxes, int per_box, box *boxes)
{
int i,j;
for (i = 0; i < num_boxes*num_boxes; ++i){
for(j = 0; j < n; ++j){
int index = i*n+j;
int offset = index*per_box;
int row = i / num_boxes;
int col = i % num_boxes;
boxes[index].x = (predictions[offset + 0] + col) / num_boxes;
boxes[index].y = (predictions[offset + 1] + row) / num_boxes;
boxes[index].w = predictions[offset + 2];
boxes[index].h = predictions[offset + 3];
}
}
}
void convert_cocos(float *predictions, int classes, int num_boxes, int num, int w, int h, float thresh, float **probs, box *boxes)
{
int i,j;
int per_box = 4+classes;
for (i = 0; i < num_boxes*num_boxes*num; ++i){
int offset = i*per_box;
for(j = 0; j < classes; ++j){
float prob = predictions[offset+j];
probs[i][j] = (prob > thresh) ? prob : 0;
}
int row = i / num_boxes;
int col = i % num_boxes;
offset += classes;
boxes[i].x = (predictions[offset + 0] + col) / num_boxes * w;
boxes[i].y = (predictions[offset + 1] + row) / num_boxes * h;
boxes[i].w = pow(predictions[offset + 2], 2) * w;
boxes[i].h = pow(predictions[offset + 3], 2) * h;
boxes[i].x = (predictions[offset + 0] + col) / num_boxes;
boxes[i].y = (predictions[offset + 1] + row) / num_boxes;
boxes[i].w = predictions[offset + 2];
boxes[i].h = predictions[offset + 3];
}
}
@ -181,6 +218,179 @@ int get_coco_image_id(char *filename)
return atoi(p+1);
}
void validate_recall(char *cfgfile, char *weightfile)
{
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
}
set_batch_network(&net, 1);
fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
srand(time(0));
char *val_images = "/home/pjreddie/data/voc/test/2007_test.txt";
list *plist = get_paths(val_images);
char **paths = (char **)list_to_array(plist);
layer l = net.layers[net.n - 1];
int num_boxes = l.side;
int num = l.n;
int classes = l.classes;
int j;
box *boxes = calloc(num_boxes*num_boxes*num, sizeof(box));
float **probs = calloc(num_boxes*num_boxes*num, sizeof(float *));
for(j = 0; j < num_boxes*num_boxes*num; ++j) probs[j] = calloc(classes+1, sizeof(float *));
int N = plist->size;
int i=0;
int k;
float iou_thresh = .5;
float thresh = .1;
int total = 0;
int correct = 0;
float avg_iou = 0;
int nms = 0;
int proposals = 0;
for (i = 0; i < N; ++i) {
char *path = paths[i];
image orig = load_image_color(path, 0, 0);
image resized = resize_image(orig, net.w, net.h);
float *X = resized.data;
float *predictions = network_predict(net, X);
get_boxes(predictions+1+classes, num, num_boxes, 5+classes, boxes);
get_probs(predictions, num*num_boxes*num_boxes, classes, 5+classes, probs);
if (nms) do_nms(boxes, probs, num*num_boxes*num_boxes, (classes>0) ? classes : 1, iou_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");
int num_labels = 0;
box_label *truth = read_boxes(labelpath, &num_labels);
for(k = 0; k < num_boxes*num_boxes*num; ++k){
if(probs[k][0] > thresh){
++proposals;
}
}
for (j = 0; j < num_labels; ++j) {
++total;
box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h};
float best_iou = 0;
for(k = 0; k < num_boxes*num_boxes*num; ++k){
float iou = box_iou(boxes[k], t);
if(probs[k][0] > thresh && iou > best_iou){
best_iou = iou;
}
}
avg_iou += best_iou;
if(best_iou > iou_thresh){
++correct;
}
}
free(truth);
free_image(orig);
free_image(resized);
fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals/(i+1), avg_iou*100/total, 100.*correct/total);
}
}
void extract_boxes(char *cfgfile, char *weightfile)
{
network net = parse_network_cfg(cfgfile);
if(weightfile){
load_weights(&net, weightfile);
}
set_batch_network(&net, 1);
fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
srand(time(0));
char *val_images = "/home/pjreddie/data/voc/test/train.txt";
list *plist = get_paths(val_images);
char **paths = (char **)list_to_array(plist);
layer l = net.layers[net.n - 1];
int num_boxes = l.side;
int num = l.n;
int classes = l.classes;
int j;
box *boxes = calloc(num_boxes*num_boxes*num, sizeof(box));
float **probs = calloc(num_boxes*num_boxes*num, sizeof(float *));
for(j = 0; j < num_boxes*num_boxes*num; ++j) probs[j] = calloc(classes+1, sizeof(float *));
int N = plist->size;
int i=0;
int k;
int count = 0;
float iou_thresh = .1;
for (i = 0; i < N; ++i) {
fprintf(stderr, "%5d %5d\n", i, count);
char *path = paths[i];
image orig = load_image_color(path, 0, 0);
image resized = resize_image(orig, net.w, net.h);
float *X = resized.data;
float *predictions = network_predict(net, X);
get_boxes(predictions+1+classes, num, num_boxes, 5+classes, boxes);
get_probs(predictions, num*num_boxes*num_boxes, classes, 5+classes, probs);
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");
int num_labels = 0;
box_label *truth = read_boxes(labelpath, &num_labels);
FILE *label = stdin;
for(k = 0; k < num_boxes*num_boxes*num; ++k){
int overlaps = 0;
for (j = 0; j < num_labels; ++j) {
box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h};
float iou = box_iou(boxes[k], t);
if (iou > iou_thresh){
if (!overlaps) {
char buff[256];
sprintf(buff, "/home/pjreddie/extracted/labels/%d.txt", count);
label = fopen(buff, "w");
overlaps = 1;
}
fprintf(label, "%d %f\n", truth[j].id, iou);
}
}
if (overlaps) {
char buff[256];
sprintf(buff, "/home/pjreddie/extracted/imgs/%d", count++);
int dx = (boxes[k].x - boxes[k].w/2) * orig.w;
int dy = (boxes[k].y - boxes[k].h/2) * orig.h;
int w = boxes[k].w * orig.w;
int h = boxes[k].h * orig.h;
image cropped = crop_image(orig, dx, dy, w, h);
image sized = resize_image(cropped, 224, 224);
#ifdef OPENCV
save_image_jpg(sized, buff);
#endif
free_image(sized);
free_image(cropped);
fclose(label);
}
}
free(truth);
free_image(orig);
free_image(resized);
}
}
void validate_coco(char *cfgfile, char *weightfile)
{
network net = parse_network_cfg(cfgfile);
@ -188,7 +398,6 @@ void validate_coco(char *cfgfile, char *weightfile)
load_weights(&net, weightfile);
}
set_batch_network(&net, 1);
detection_layer layer = get_network_detection_layer(net);
fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
srand(time(0));
@ -196,10 +405,9 @@ void validate_coco(char *cfgfile, char *weightfile)
list *plist = get_paths("data/coco_val_5k.list");
char **paths = (char **)list_to_array(plist);
int classes = layer.classes;
int objectness = layer.objectness;
int background = layer.background;
int num_boxes = sqrt(get_detection_layer_locations(layer));
int num_boxes = 9;
int num = 4;
int classes = 1;
int j;
char buff[1024];
@ -207,9 +415,9 @@ void validate_coco(char *cfgfile, char *weightfile)
FILE *fp = fopen(buff, "w");
fprintf(fp, "[\n");
box *boxes = calloc(num_boxes*num_boxes, sizeof(box));
float **probs = calloc(num_boxes*num_boxes, sizeof(float *));
for(j = 0; j < num_boxes*num_boxes; ++j) probs[j] = calloc(classes, sizeof(float *));
box *boxes = calloc(num_boxes*num_boxes*num, sizeof(box));
float **probs = calloc(num_boxes*num_boxes*num, sizeof(float *));
for(j = 0; j < num_boxes*num_boxes*num; ++j) probs[j] = calloc(classes, sizeof(float *));
int m = plist->size;
int i=0;
@ -257,7 +465,7 @@ void validate_coco(char *cfgfile, char *weightfile)
float *predictions = network_predict(net, X);
int w = val[t].w;
int h = val[t].h;
convert_cocos(predictions, classes, objectness, background, num_boxes, w, h, thresh, probs, boxes);
convert_cocos(predictions, classes, num_boxes, num, w, h, thresh, probs, boxes);
if (nms) do_nms(boxes, probs, num_boxes, classes, iou_thresh);
print_cocos(fp, image_id, boxes, probs, num_boxes, classes, w, h);
free_image(val[t]);
@ -319,5 +527,6 @@ void run_coco(int argc, char **argv)
char *filename = (argc > 5) ? argv[5]: 0;
if(0==strcmp(argv[2], "test")) test_coco(cfg, weights, filename);
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], "extract")) extract_boxes(cfg, weights);
else if(0==strcmp(argv[2], "valid")) validate_recall(cfg, weights);
}