Skip to content

Commit

Permalink
MERRY CHRISTMAS I BROKE ALL YOUR DETECTION THINGS
Browse files Browse the repository at this point in the history
  • Loading branch information
pjreddie committed Dec 26, 2017
1 parent 80d9bec commit 6e79145
Show file tree
Hide file tree
Showing 36 changed files with 1,178 additions and 701 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GPU=0
CUDNN=0
OPENCV=0
OPENMP=0
GPU=1
CUDNN=1
OPENCV=1
OPENMP=1
DEBUG=0

ARCH= -gencode arch=compute_30,code=sm_30 \
Expand Down Expand Up @@ -57,8 +57,8 @@ CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif

OBJ=gemm.o utils.o cuda.o deconvolutional_layer.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 detection_layer.o route_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o attention.o darknet.o
OBJ=gemm.o utils.o cuda.o deconvolutional_layer.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 detection_layer.o route_layer.o upsample_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o logistic_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o lstm_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o darknet.o
ifeq ($(GPU), 1)
LDFLAGS+= -lstdc++
OBJ+=convolutional_kernels.o deconvolutional_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 avgpool_layer_kernels.o
Expand Down
10 changes: 5 additions & 5 deletions cfg/darknet.cfg
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[net]
# Train
# batch=128
# subdivisions=1
# Test
batch=1
subdivisions=1
# Test
# batch=1
# subdivisions=1
height=224
width=224
height=256
width=256
channels=3
momentum=0.9
decay=0.0005
Expand Down
2 changes: 1 addition & 1 deletion cfg/tiny-yolo.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ filters=425
activation=linear

[region]
anchors = 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828
anchors = 18.3274,21.6763, 59.9827,66.001, 106.83,175.179, 252.25,112.889, 312.657,293.385
bias_match=1
classes=80
coords=4
Expand Down
10 changes: 5 additions & 5 deletions cfg/yolo.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[net]
# Testing
batch=1
subdivisions=1
# batch=1
# subdivisions=1
# Training
# batch=64
# subdivisions=8
batch=64
subdivisions=8
width=608
height=608
channels=3
Expand Down Expand Up @@ -239,7 +239,7 @@ activation=linear


[region]
anchors = 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828
anchors = 18.3274,21.6763, 59.9827,66.001, 106.83,175.179, 252.25,112.889, 312.657,293.385
bias_match=1
classes=80
coords=4
Expand Down
53 changes: 52 additions & 1 deletion examples/classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
if(indexes[j] == class) avg_topk += 1;
}

printf("%s, %d, %f, %f, \n", paths[i], class, pred[0], pred[1]);
printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
}
}
Expand Down Expand Up @@ -704,6 +705,44 @@ void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_
}
}

void file_output_classifier(char *datacfg, char *filename, char *weightfile, char *listfile)
{
int i,j;
network *net = load_network(filename, weightfile, 0);
set_batch_network(net, 1);
srand(time(0));

list *options = read_data_cfg(datacfg);

//char *label_list = option_find_str(options, "names", "data/labels.list");
int classes = option_find_int(options, "classes", 2);

list *plist = get_paths(listfile);

char **paths = (char **)list_to_array(plist);
int m = plist->size;
free_list(plist);

for(i = 0; i < m; ++i){
image im = load_image_color(paths[i], 0, 0);
image resized = resize_min(im, net->w);
image crop = crop_image(resized, (resized.w - net->w)/2, (resized.h - net->h)/2, net->w, net->h);

float *pred = network_predict(net, crop.data);
if(net->hierarchy) hierarchy_predictions(pred, net->outputs, net->hierarchy, 0, 1);

if(resized.data != im.data) free_image(resized);
free_image(im);
free_image(crop);

printf("%s", paths[i]);
for(j = 0; j < classes; ++j){
printf("\t%g", pred[j]);
}
printf("\n");
}
}


void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
{
Expand Down Expand Up @@ -922,15 +961,26 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
srand(2222222);
CvCapture * cap;

int w = 1280;
int h = 720;

if(filename){
cap = cvCaptureFromFile(filename);
}else{
cap = cvCaptureFromCAM(cam_index);
}

if(w){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
}
if(h){
cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
}

int top = option_find_int(options, "top", 1);

char *name_list = option_find_str(options, "names", 0);
char *label_list = option_find_str(options, "labels", 0);
char *name_list = option_find_str(options, "names", label_list);
char **names = get_labels(name_list);

int *indexes = calloc(top, sizeof(int));
Expand Down Expand Up @@ -998,6 +1048,7 @@ void run_classifier(int argc, char **argv)
char *layer_s = (argc > 7) ? argv[7]: 0;
int layer = layer_s ? atoi(layer_s) : -1;
if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename, top);
else if(0==strcmp(argv[2], "fout")) file_output_classifier(data, cfg, weights, filename);
else if(0==strcmp(argv[2], "try")) try_classifier(data, cfg, weights, filename, atoi(layer_s));
else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights, gpus, ngpus, clear);
else if(0==strcmp(argv[2], "demo")) demo_classifier(data, cfg, weights, cam_index, filename);
Expand Down
53 changes: 23 additions & 30 deletions examples/coco.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ void train_coco(char *cfgfile, char *weightfile)
save_weights(net, buff);
}

void print_cocos(FILE *fp, int image_id, box *boxes, float **probs, int num_boxes, int classes, int w, int h)
static void print_cocos(FILE *fp, int image_id, detection *dets, int num_boxes, int classes, int w, int h)
{
int i, j;
for(i = 0; i < num_boxes; ++i){
float xmin = boxes[i].x - boxes[i].w/2.;
float xmax = boxes[i].x + boxes[i].w/2.;
float ymin = boxes[i].y - boxes[i].h/2.;
float ymax = boxes[i].y + boxes[i].h/2.;
float xmin = dets[i].bbox.x - dets[i].bbox.w/2.;
float xmax = dets[i].bbox.x + dets[i].bbox.w/2.;
float ymin = dets[i].bbox.y - dets[i].bbox.h/2.;
float ymax = dets[i].bbox.y + dets[i].bbox.h/2.;

if (xmin < 0) xmin = 0;
if (ymin < 0) ymin = 0;
Expand All @@ -114,7 +114,7 @@ void print_cocos(FILE *fp, int image_id, box *boxes, float **probs, int num_boxe
float bh = ymax - ymin;

for(j = 0; j < classes; ++j){
if (probs[i][j]) fprintf(fp, "{\"image_id\":%d, \"category_id\":%d, \"bbox\":[%f, %f, %f, %f], \"score\":%f},\n", image_id, coco_ids[j], bx, by, bw, bh, probs[i][j]);
if (dets[i].prob[j]) fprintf(fp, "{\"image_id\":%d, \"category_id\":%d, \"bbox\":[%f, %f, %f, %f], \"score\":%f},\n", image_id, coco_ids[j], bx, by, bw, bh, dets[i].prob[j]);
}
}
}
Expand All @@ -140,17 +140,13 @@ void validate_coco(char *cfg, char *weights)

layer l = net->layers[net->n-1];
int classes = l.classes;
int side = l.side;

int j;
char buff[1024];
snprintf(buff, 1024, "%s/coco_results.json", base);
FILE *fp = fopen(buff, "w");
fprintf(fp, "[\n");

box *boxes = calloc(side*side*l.n, sizeof(box));
float **probs = calloc(side*side*l.n, sizeof(float *));
for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));
detection *dets = make_network_boxes(net);

int m = plist->size;
int i=0;
Expand Down Expand Up @@ -199,9 +195,9 @@ void validate_coco(char *cfg, char *weights)
network_predict(net, X);
int w = val[t].w;
int h = val[t].h;
get_detection_boxes(l, w, h, thresh, probs, boxes, 0);
if (nms) do_nms_sort(boxes, probs, side*side*l.n, classes, iou_thresh);
print_cocos(fp, image_id, boxes, probs, side*side*l.n, classes, w, h);
fill_network_boxes(net, w, h, thresh, 0, 0, 0, dets);
if (nms) do_nms_sort(dets, l.side*l.side*l.n, classes, iou_thresh);
print_cocos(fp, image_id, dets, l.side*l.side*l.n, classes, w, h);
free_image(val[t]);
free_image(val_resized[t]);
}
Expand Down Expand Up @@ -235,17 +231,14 @@ void validate_coco_recall(char *cfgfile, char *weightfile)
snprintf(buff, 1024, "%s%s.txt", base, coco_classes[j]);
fps[j] = fopen(buff, "w");
}
box *boxes = calloc(side*side*l.n, sizeof(box));
float **probs = calloc(side*side*l.n, sizeof(float *));
for(j = 0; j < side*side*l.n; ++j) probs[j] = calloc(classes, sizeof(float *));
detection *dets = make_network_boxes(net);

int m = plist->size;
int i=0;

float thresh = .001;
int nms = 0;
float iou_thresh = .5;
float nms_thresh = .5;

int total = 0;
int correct = 0;
Expand All @@ -258,8 +251,9 @@ void validate_coco_recall(char *cfgfile, char *weightfile)
image sized = resize_image(orig, net->w, net->h);
char *id = basecfg(path);
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);

fill_network_boxes(net, orig.w, orig.h, thresh, 0, 0, 1, dets);
if (nms) do_nms_obj(dets, side*side*l.n, 1, nms);

char labelpath[4096];
find_replace(path, "images", "labels", labelpath);
Expand All @@ -270,7 +264,7 @@ void validate_coco_recall(char *cfgfile, char *weightfile)
int num_labels = 0;
box_label *truth = read_boxes(labelpath, &num_labels);
for(k = 0; k < side*side*l.n; ++k){
if(probs[k][0] > thresh){
if(dets[k].objectness > thresh){
++proposals;
}
}
Expand All @@ -279,8 +273,8 @@ void validate_coco_recall(char *cfgfile, char *weightfile)
box t = {truth[j].x, truth[j].y, truth[j].w, truth[j].h};
float best_iou = 0;
for(k = 0; k < side*side*l.n; ++k){
float iou = box_iou(boxes[k], t);
if(probs[k][0] > thresh && iou > best_iou){
float iou = box_iou(dets[k].bbox, t);
if(dets[k].objectness > thresh && iou > best_iou){
best_iou = iou;
}
}
Expand Down Expand Up @@ -308,10 +302,7 @@ void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
clock_t time;
char buff[256];
char *input = buff;
int j;
box *boxes = calloc(l.side*l.side*l.n, sizeof(box));
float **probs = calloc(l.side*l.side*l.n, sizeof(float *));
for(j = 0; j < l.side*l.side*l.n; ++j) probs[j] = calloc(l.classes, sizeof(float *));
detection *dets = make_network_boxes(net);
while(1){
if(filename){
strncpy(input, filename, 256);
Expand All @@ -328,9 +319,11 @@ void test_coco(char *cfgfile, char *weightfile, char *filename, float thresh)
time=clock();
network_predict(net, X);
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
get_detection_boxes(l, 1, 1, thresh, probs, boxes, 0);
if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms);
draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, 0, coco_classes, alphabet, 80);

fill_network_boxes(net, 1, 1, thresh, 0, 0, 0, dets);
if (nms) do_nms_sort(dets, l.side*l.side*l.n, l.classes, nms);

draw_detections(im, dets, l.side*l.side*l.n, thresh, coco_classes, alphabet, 80);
save_image(im, "prediction");
show_image(im, "predictions");
free_image(im);
Expand Down
3 changes: 0 additions & 3 deletions examples/darknet.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extern void run_coco(int argc, char **argv);
extern void run_captcha(int argc, char **argv);
extern void run_nightmare(int argc, char **argv);
extern void run_classifier(int argc, char **argv);
extern void run_attention(int argc, char **argv);
extern void run_regressor(int argc, char **argv);
extern void run_segmenter(int argc, char **argv);
extern void run_char_rnn(int argc, char **argv);
Expand Down Expand Up @@ -432,8 +431,6 @@ int main(int argc, char **argv)
predict_classifier("cfg/imagenet1k.data", argv[2], argv[3], argv[4], 5);
} else if (0 == strcmp(argv[1], "classifier")){
run_classifier(argc, argv);
} else if (0 == strcmp(argv[1], "attention")){
run_attention(argc, argv);
} else if (0 == strcmp(argv[1], "regressor")){
run_regressor(argc, argv);
} else if (0 == strcmp(argv[1], "segmenter")){
Expand Down
Loading

0 comments on commit 6e79145

Please sign in to comment.