This file was deleted.

@@ -1,6 +1,10 @@
[net]
batch=64
subdivisions=2
# Testing
batch=1
subdivisions=1
# Training
# batch=64
# subdivisions=8
height=448
width=448
channels=3
@@ -1,6 +1,10 @@
[net]
# Testing
batch=1
subdivisions=1
# Training
# batch=64
# subdivisions=8
height=448
width=448
channels=3

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,6 +1,10 @@
[net]
batch=64
subdivisions=8
# Testing
batch=1
subdivisions=1
# Training
# batch=64
# subdivisions=2
width=416
height=416
channels=3
@@ -1,10 +1,10 @@
[net]
# Training
# batch=64
# subdivisions=2
# Testing
batch=1
subdivisions=1
# Training
# batch=64
# subdivisions=2
width=416
height=416
channels=3
File renamed without changes.
@@ -5,8 +5,8 @@ subdivisions=1
# Training
# batch=64
# subdivisions=8
width=608
height=608
width=416
height=416
channels=3
momentum=0.9
decay=0.0005

Large diffs are not rendered by default.

@@ -1,6 +1,10 @@
[net]
# Testing
batch=1
subdivisions=1
# Training
# batch=64
# subdivisions=2
width=416
height=416
channels=3
@@ -12,14 +16,15 @@ exposure = 1.5
hue=.1

learning_rate=0.001
max_batches = 120000
burn_in=1000
max_batches = 500200
policy=steps
steps=-1,100,80000,100000
scales=.1,10,.1,.1
steps=400000,450000
scales=.1,.1

[convolutional]
batch_normalize=1
filters=32
filters=16
size=3
stride=1
pad=1
@@ -31,7 +36,7 @@ stride=2

[convolutional]
batch_normalize=1
filters=64
filters=32
size=3
stride=1
pad=1
@@ -41,25 +46,9 @@ activation=leaky
size=2
stride=2

[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=64
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=128
size=3
stride=1
pad=1
@@ -69,25 +58,9 @@ activation=leaky
size=2
stride=2

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=128
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
@@ -97,37 +70,17 @@ activation=leaky
size=2
stride=2

[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=512
size=3
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=256
size=1
stride=1
pad=1
activation=leaky
[maxpool]
size=2
stride=2

[convolutional]
batch_normalize=1
@@ -139,7 +92,7 @@ activation=leaky

[maxpool]
size=2
stride=2
stride=1

[convolutional]
batch_normalize=1
@@ -149,96 +102,81 @@ stride=1
pad=1
activation=leaky

###########

[convolutional]
batch_normalize=1
filters=512
filters=256
size=1
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=1024
filters=512
size=3
stride=1
pad=1
activation=leaky

[convolutional]
batch_normalize=1
filters=512
size=1
stride=1
pad=1
activation=leaky
filters=255
activation=linear

[convolutional]
batch_normalize=1
filters=1024
size=3
stride=1
pad=1
activation=leaky


#######
[yolo]
mask = 3,4,5
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=80
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

[convolutional]
batch_normalize=1
size=3
stride=1
pad=1
filters=1024
activation=leaky
[route]
layers = -4

[convolutional]
batch_normalize=1
size=3
filters=128
size=1
stride=1
pad=1
filters=1024
activation=leaky

[route]
layers=-9

[reorg]
[upsample]
stride=2

[route]
layers=-1,-3
layers = -1, 8

[convolutional]
batch_normalize=1
filters=256
size=3
stride=1
pad=1
filters=1024
activation=leaky

[convolutional]
size=1
stride=1
pad=1
filters=425
filters=255
activation=linear

[region]
anchors = 0.738768,0.874946, 2.42204,2.65704, 4.30971,7.04493, 10.246,4.59428, 12.6868,11.8741
bias_match=1
[yolo]
mask = 1,2,3
anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
classes=80
coords=4
num=5
softmax=1
jitter=.2
rescore=1

object_scale=5
noobject_scale=1
class_scale=1
coord_scale=1

absolute=1
thresh = .6
random=0
num=6
jitter=.3
ignore_thresh = .7
truth_thresh = 1
random=1

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -1,7 +1,7 @@
GPU=0
CUDNN=0
OPENCV=1
OPENMP=1
OPENCV=0
OPENMP=0
DEBUG=0

ARCH= -gencode arch=compute_30,code=sm_30 \
@@ -26,7 +26,7 @@ ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC
CFLAGS=-Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC

ifeq ($(OPENMP), 1)
CFLAGS+= -fopenmp
@@ -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 l2norm_layer.o yolo_layer.o iseg_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 instance-segmenter.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
@@ -68,8 +68,8 @@ EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h

#all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj results $(SLIB) $(ALIB) $(EXEC)
all: obj backup results $(SLIB) $(ALIB) $(EXEC)
#all: obj results $(SLIB) $(ALIB) $(EXEC)


$(EXEC): $(EXECOBJ) $(ALIB)
@@ -97,5 +97,5 @@ results:
.PHONY: clean

clean:
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ)
rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ) $(OBJDIR)/*

This file was deleted.

@@ -1,6 +1,6 @@
![Darknet Logo](http://pjreddie.com/media/files/darknet-black-small.png)

#Darknet#
# Darknet #
Darknet is an open source neural network framework written in C and CUDA. It is fast, easy to install, and supports CPU and GPU computation.

For more information see the [Darknet project website](http://pjreddie.com/darknet).
@@ -24,7 +24,6 @@ void demo_art(char *cfgfile, char *weightfile, int cam_index)
while(1){
image in = get_image_from_stream(cap);
image in_s = resize_image(in, net->w, net->h);
show_image(in, window);

float *p = network_predict(net, in_s.data);

@@ -45,10 +44,9 @@ void demo_art(char *cfgfile, char *weightfile, int cam_index)
}
printf("]\n");

show_image(in, window, 1);
free_image(in_s);
free_image(in);

cvWaitKey(1);
}
#endif
}
@@ -44,11 +44,17 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
list *options = read_data_cfg(datacfg);

char *backup_directory = option_find_str(options, "backup", "/backup/");
int tag = option_find_int_quiet(options, "tag", 0);
char *label_list = option_find_str(options, "labels", "data/labels.list");
char *train_list = option_find_str(options, "train", "data/train.list");
char *tree = option_find_str(options, "tree", 0);
if (tree) net->hierarchy = read_tree(tree);
int classes = option_find_int(options, "classes", 2);

char **labels = get_labels(label_list);
char **labels = 0;
if(!tag){
labels = get_labels(label_list);
}
list *plist = get_paths(train_list);
char **paths = (char **)list_to_array(plist);
printf("%d\n", plist->size);
@@ -76,7 +82,11 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
args.n = imgs;
args.m = N;
args.labels = labels;
args.type = CLASSIFICATION_DATA;
if (tag){
args.type = TAG_DATA;
} else {
args.type = CLASSIFICATION_DATA;
}

data train;
data buffer;
@@ -151,7 +161,7 @@ void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus,
pthread_join(load_thread, 0);

free_network(net);
free_ptrs((void**)labels, classes);
if(labels) free_ptrs((void**)labels, classes);
free_ptrs((void**)paths, plist->size);
free_list(plist);
free(base);
@@ -385,15 +395,13 @@ void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
}
}
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);
image crop = center_crop_image(im, net->w, net->h);
//show_image(im, "orig");
//show_image(crop, "cropped");
//cvWaitKey(0);
float *pred = network_predict(net, crop.data);
if(net->hierarchy) hierarchy_predictions(pred, net->outputs, net->hierarchy, 1, 1);

if(resized.data != im.data) free_image(resized);
free_image(im);
free_image(crop);
top_k(pred, classes, topk, indexes);
@@ -403,6 +411,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));
}
}
@@ -577,6 +586,8 @@ void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *fi
}
image im = load_image_color(input, 0, 0);
image r = letterbox_image(im, net->w, net->h);
//image r = resize_min(im, 320);
//printf("%d %d\n", r.w, r.h);
//resize_network(net, r.w, r.h);
//printf("%d %d\n", r.w, r.h);

@@ -634,6 +645,45 @@ void label_classifier(char *datacfg, char *filename, char *weightfile)
}
}

void csv_classifier(char *datacfg, char *cfgfile, char *weightfile)
{
int i,j;
network *net = load_network(cfgfile, weightfile, 0);
srand(time(0));

list *options = read_data_cfg(datacfg);

char *test_list = option_find_str(options, "test", "data/test.list");
int top = option_find_int(options, "top", 1);

list *plist = get_paths(test_list);

char **paths = (char **)list_to_array(plist);
int m = plist->size;
free_list(plist);
int *indexes = calloc(top, sizeof(int));

for(i = 0; i < m; ++i){
double time = what_time_is_it_now();
char *path = paths[i];
image im = load_image_color(path, 0, 0);
image r = letterbox_image(im, net->w, net->h);
float *predictions = network_predict(net, r.data);
if(net->hierarchy) hierarchy_predictions(predictions, net->outputs, net->hierarchy, 1, 1);
top_k(predictions, net->outputs, top, indexes);

printf("%s", path);
for(j = 0; j < top; ++j){
printf("\t%d", indexes[j]);
}
printf("\n");

free_image(im);
free_image(r);

fprintf(stderr, "%lf seconds, %d images, %d total\n", what_time_is_it_now() - time, i+1, m);
}
}

void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_layer)
{
@@ -704,6 +754,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)
{
@@ -820,8 +908,7 @@ void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_i
}

if(1){
show_image(out, "Threat");
cvWaitKey(10);
show_image(out, "Threat", 10);
}
free_image(in_s);
free_image(in);
@@ -873,7 +960,6 @@ void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_inde

image in = get_image_from_stream(cap);
image in_s = resize_image(in, net->w, net->h);
show_image(in, "Threat Detection");

float *predictions = network_predict(net, in_s.data);
top_predictions(net, top, indexes);
@@ -898,11 +984,10 @@ void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_inde
}
}

show_image(in, "Threat Detection", 10);
free_image(in_s);
free_image(in);

cvWaitKey(10);

gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
float curr = 1000000.f/((long int)tval_result.tv_usec);
@@ -914,6 +999,8 @@ void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_inde
void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
{
#ifdef OPENCV
char *base = basecfg(cfgfile);
image **alphabet = load_alphabet();
printf("Classifier Demo\n");
network *net = load_network(cfgfile, weightfile, 0);
set_batch_network(net, 1);
@@ -922,22 +1009,33 @@ 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));

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow("Classifier", CV_WINDOW_NORMAL);
cvResizeWindow("Classifier", 512, 512);
cvNamedWindow(base, CV_WINDOW_NORMAL);
cvResizeWindow(base, 512, 512);
float fps = 0;
int i;

@@ -946,8 +1044,8 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
gettimeofday(&tval_before, NULL);

image in = get_image_from_stream(cap);
image in_s = resize_image(in, net->w, net->h);
show_image(in, "Classifier");
//image in_s = resize_image(in, net->w, net->h);
image in_s = letterbox_image(in, net->w, net->h);

float *predictions = network_predict(net, in_s.data);
if(net->hierarchy) hierarchy_predictions(predictions, net->outputs, net->hierarchy, 1, 1);
@@ -957,16 +1055,27 @@ void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_ind
printf("\033[1;1H");
printf("\nFPS:%.0f\n",fps);

int lh = in.h*.03;
int toph = 3*lh;

float rgb[3] = {1,1,1};
for(i = 0; i < top; ++i){
printf("%d\n", toph);
int index = indexes[i];
printf("%.1f%%: %s\n", predictions[index]*100, names[index]);

char buff[1024];
sprintf(buff, "%3.1f%%: %s\n", predictions[index]*100, names[index]);
image label = get_label(alphabet, buff, lh);
draw_label(in, toph, lh, label, rgb);
toph += 2*lh;
free_image(label);
}

show_image(in, base, 10);
free_image(in_s);
free_image(in);

cvWaitKey(10);

gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
float curr = 1000000.f/((long int)tval_result.tv_usec);
@@ -998,12 +1107,14 @@ 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);
else if(0==strcmp(argv[2], "gun")) gun_classifier(data, cfg, weights, cam_index, filename);
else if(0==strcmp(argv[2], "threat")) threat_classifier(data, cfg, weights, cam_index, filename);
else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights, layer);
else if(0==strcmp(argv[2], "csv")) csv_classifier(data, cfg, weights);
else if(0==strcmp(argv[2], "label")) label_classifier(data, cfg, weights);
else if(0==strcmp(argv[2], "valid")) validate_classifier_single(data, cfg, weights);
else if(0==strcmp(argv[2], "validmulti")) validate_classifier_multi(data, cfg, weights);
@@ -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;
@@ -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]);
}
}
}
@@ -140,18 +140,12 @@ 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 *));

int m = plist->size;
int i=0;
int t;
@@ -199,9 +193,11 @@ 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);
int nboxes = 0;
detection *dets = get_network_boxes(net, w, h, thresh, 0, 0, 0, &nboxes);
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_detections(dets, nboxes);
free_image(val[t]);
free_image(val_resized[t]);
}
@@ -235,17 +231,13 @@ 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 *));

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;
@@ -258,8 +250,10 @@ 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);

int nboxes = 0;
detection *dets = get_network_boxes(net, orig.w, orig.h, thresh, 0, 0, 1, &nboxes);
if (nms) do_nms_obj(dets, side*side*l.n, 1, nms);

char labelpath[4096];
find_replace(path, "images", "labels", labelpath);
@@ -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;
}
}
@@ -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;
}
}
@@ -289,7 +283,7 @@ void validate_coco_recall(char *cfgfile, char *weightfile)
++correct;
}
}

free_detections(dets, nboxes);
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);
free(id);
free_image(orig);
@@ -308,10 +302,6 @@ 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 *));
while(1){
if(filename){
strncpy(input, filename, 256);
@@ -328,17 +318,17 @@ 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);

int nboxes = 0;
detection *dets = get_network_boxes(net, 1, 1, thresh, 0, 0, 0, &nboxes);
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");
show_image(im, "predictions", 0);
free_detections(dets, nboxes);
free_image(im);
free_image(sized);
#ifdef OPENCV
cvWaitKey(0);
cvDestroyAllWindows();
#endif
if (filename) break;
}
}
@@ -12,9 +12,9 @@ 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_isegmenter(int argc, char **argv);
extern void run_char_rnn(int argc, char **argv);
extern void run_tag(int argc, char **argv);
extern void run_cifar(int argc, char **argv);
@@ -189,6 +189,25 @@ void partial(char *cfgfile, char *weightfile, char *outfile, int max)
save_weights_upto(net, outfile, max);
}

void print_weights(char *cfgfile, char *weightfile, int n)
{
gpu_index = -1;
network *net = load_network(cfgfile, weightfile, 1);
layer l = net->layers[n];
int i, j;
//printf("[");
for(i = 0; i < l.n; ++i){
//printf("[");
for(j = 0; j < l.size*l.size*l.c; ++j){
//if(j > 0) printf(",");
printf("%g ", l.weights[i*l.size*l.size*l.c + j]);
}
printf("\n");
//printf("]%s\n", (i == l.n-1)?"":",");
}
//printf("]");
}

void rescale_net(char *cfgfile, char *weightfile, char *outfile)
{
gpu_index = -1;
@@ -415,7 +434,7 @@ int main(int argc, char **argv)
} else if (0 == strcmp(argv[1], "detector")){
run_detector(argc, argv);
} else if (0 == strcmp(argv[1], "detect")){
float thresh = find_float_arg(argc, argv, "-thresh", .24);
float thresh = find_float_arg(argc, argv, "-thresh", .5);
char *filename = (argc > 4) ? argv[4]: 0;
char *outfile = find_char_arg(argc, argv, "-out", 0);
int fullscreen = find_arg(argc, argv, "-fullscreen");
@@ -432,10 +451,10 @@ 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], "isegmenter")){
run_isegmenter(argc, argv);
} else if (0 == strcmp(argv[1], "segmenter")){
run_segmenter(argc, argv);
} else if (0 == strcmp(argv[1], "art")){
@@ -470,6 +489,8 @@ int main(int argc, char **argv)
oneoff(argv[2], argv[3], argv[4]);
} else if (0 == strcmp(argv[1], "oneoff2")){
oneoff2(argv[2], argv[3], argv[4], atoi(argv[5]));
} else if (0 == strcmp(argv[1], "print")){
print_weights(argv[2], argv[3], atoi(argv[4]));
} else if (0 == strcmp(argv[1], "partial")){
partial(argv[2], argv[3], argv[4], atoi(argv[5]));
} else if (0 == strcmp(argv[1], "average")){

Large diffs are not rendered by default.

@@ -0,0 +1,265 @@
#include "darknet.h"
#include <sys/time.h>
#include <assert.h>

void train_isegmenter(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int display)
{
int i;

float avg_loss = -1;
char *base = basecfg(cfgfile);
printf("%s\n", base);
printf("%d\n", ngpus);
network **nets = calloc(ngpus, sizeof(network*));

srand(time(0));
int seed = rand();
for(i = 0; i < ngpus; ++i){
srand(seed);
#ifdef GPU
cuda_set_device(gpus[i]);
#endif
nets[i] = load_network(cfgfile, weightfile, clear);
nets[i]->learning_rate *= ngpus;
}
srand(time(0));
network *net = nets[0];
image pred = get_network_image(net);

int div = net->w/pred.w;
assert(pred.w * div == net->w);
assert(pred.h * div == net->h);

int imgs = net->batch * net->subdivisions * ngpus;

printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net->learning_rate, net->momentum, net->decay);
list *options = read_data_cfg(datacfg);

char *backup_directory = option_find_str(options, "backup", "/backup/");
char *train_list = option_find_str(options, "train", "data/train.list");

list *plist = get_paths(train_list);
char **paths = (char **)list_to_array(plist);
printf("%d\n", plist->size);
int N = plist->size;

load_args args = {0};
args.w = net->w;
args.h = net->h;
args.threads = 32;
args.scale = div;
args.num_boxes = 90;

args.min = net->min_crop;
args.max = net->max_crop;
args.angle = net->angle;
args.aspect = net->aspect;
args.exposure = net->exposure;
args.saturation = net->saturation;
args.hue = net->hue;
args.size = net->w;
args.classes = 80;

args.paths = paths;
args.n = imgs;
args.m = N;
args.type = ISEG_DATA;

data train;
data buffer;
pthread_t load_thread;
args.d = &buffer;
load_thread = load_data(args);

int epoch = (*net->seen)/N;
while(get_current_batch(net) < net->max_batches || net->max_batches == 0){
double time = what_time_is_it_now();

pthread_join(load_thread, 0);
train = buffer;
load_thread = load_data(args);

printf("Loaded: %lf seconds\n", what_time_is_it_now()-time);
time = what_time_is_it_now();

float loss = 0;
#ifdef GPU
if(ngpus == 1){
loss = train_network(net, train);
} else {
loss = train_networks(nets, ngpus, train, 4);
}
#else
loss = train_network(net, train);
#endif
if(display){
image tr = float_to_image(net->w/div, net->h/div, 80, train.y.vals[net->batch*(net->subdivisions-1)]);
image im = float_to_image(net->w, net->h, net->c, train.X.vals[net->batch*(net->subdivisions-1)]);
pred.c = 80;
image mask = mask_to_rgb(tr);
image prmask = mask_to_rgb(pred);
show_image(im, "input", 1);
show_image(prmask, "pred", 1);
show_image(mask, "truth", 100);
free_image(mask);
free_image(prmask);
}
if(avg_loss == -1) avg_loss = loss;
avg_loss = avg_loss*.9 + loss*.1;
printf("%ld, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images\n", get_current_batch(net), (float)(*net->seen)/N, loss, avg_loss, get_current_rate(net), what_time_is_it_now()-time, *net->seen);
free_data(train);
if(*net->seen/N > epoch){
epoch = *net->seen/N;
char buff[256];
sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch);
save_weights(net, buff);
}
if(get_current_batch(net)%100 == 0){
char buff[256];
sprintf(buff, "%s/%s.backup",backup_directory,base);
save_weights(net, buff);
}
}
char buff[256];
sprintf(buff, "%s/%s.weights", backup_directory, base);
save_weights(net, buff);

free_network(net);
free_ptrs((void**)paths, plist->size);
free_list(plist);
free(base);
}

void predict_isegmenter(char *datafile, char *cfg, char *weights, char *filename)
{
network *net = load_network(cfg, weights, 0);
set_batch_network(net, 1);
srand(2222222);

clock_t time;
char buff[256];
char *input = buff;
while(1){
if(filename){
strncpy(input, filename, 256);
}else{
printf("Enter Image Path: ");
fflush(stdout);
input = fgets(input, 256, stdin);
if(!input) return;
strtok(input, "\n");
}
image im = load_image_color(input, 0, 0);
image sized = letterbox_image(im, net->w, net->h);

float *X = sized.data;
time=clock();
float *predictions = network_predict(net, X);
image pred = get_network_image(net);
image prmask = mask_to_rgb(pred);
printf("Predicted: %f\n", predictions[0]);
printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
show_image(sized, "orig", 1);
show_image(prmask, "pred", 0);
free_image(im);
free_image(sized);
free_image(prmask);
if (filename) break;
}
}


void demo_isegmenter(char *datacfg, char *cfg, char *weights, int cam_index, const char *filename)
{
#ifdef OPENCV
printf("Classifier Demo\n");
network *net = load_network(cfg, weights, 0);
set_batch_network(net, 1);

srand(2222222);
CvCapture * cap;

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

if(!cap) error("Couldn't connect to webcam.\n");
cvNamedWindow("Segmenter", CV_WINDOW_NORMAL);
cvResizeWindow("Segmenter", 512, 512);
float fps = 0;

while(1){
struct timeval tval_before, tval_after, tval_result;
gettimeofday(&tval_before, NULL);

image in = get_image_from_stream(cap);
image in_s = letterbox_image(in, net->w, net->h);

network_predict(net, in_s.data);

printf("\033[2J");
printf("\033[1;1H");
printf("\nFPS:%.0f\n",fps);

image pred = get_network_image(net);
image prmask = mask_to_rgb(pred);
show_image(prmask, "Segmenter", 10);

free_image(in_s);
free_image(in);
free_image(prmask);

gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
float curr = 1000000.f/((long int)tval_result.tv_usec);
fps = .9*fps + .1*curr;
}
#endif
}


void run_isegmenter(int argc, char **argv)
{
if(argc < 4){
fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
return;
}

char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
int *gpus = 0;
int gpu = 0;
int ngpus = 0;
if(gpu_list){
printf("%s\n", gpu_list);
int len = strlen(gpu_list);
ngpus = 1;
int i;
for(i = 0; i < len; ++i){
if (gpu_list[i] == ',') ++ngpus;
}
gpus = calloc(ngpus, sizeof(int));
for(i = 0; i < ngpus; ++i){
gpus[i] = atoi(gpu_list);
gpu_list = strchr(gpu_list, ',')+1;
}
} else {
gpu = gpu_index;
gpus = &gpu;
ngpus = 1;
}

int cam_index = find_int_arg(argc, argv, "-c", 0);
int clear = find_arg(argc, argv, "-clear");
int display = find_arg(argc, argv, "-display");
char *data = argv[3];
char *cfg = argv[4];
char *weights = (argc > 5) ? argv[5] : 0;
char *filename = (argc > 6) ? argv[6]: 0;
if(0==strcmp(argv[2], "test")) predict_isegmenter(data, cfg, weights, filename);
else if(0==strcmp(argv[2], "train")) train_isegmenter(data, cfg, weights, gpus, ngpus, clear, display);
else if(0==strcmp(argv[2], "demo")) demo_isegmenter(data, cfg, weights, cam_index, filename);
}