8 float *v = calloc(n,
sizeof(
float));
10 for(i = 0; i < n; ++i){
11 char *p = strchr(labels[i],
' ');
18 void train_classifier(
char *datacfg,
char *cfgfile,
char *weightfile,
int *gpus,
int ngpus,
int clear)
25 printf(
"%d\n", ngpus);
30 for(i = 0; i < ngpus; ++i){
46 char *backup_directory =
option_find_str(options,
"backup",
"/backup/");
48 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
49 char *train_list =
option_find_str(options,
"train",
"data/train.list");
60 printf(
"%d\n", plist->
size);
72 printf(
"%d %d\n", args.
min, args.
max);
98 int epoch = (*net->
seen)/N;
100 if(net->
random && count++%40 == 0){
101 printf(
"Resizing\n");
102 int dim = (rand() % 11 + 4) * 32;
111 printf(
"%d %d\n", args.
min, args.
max);
113 pthread_join(load_thread, 0);
118 for(i = 0; i < ngpus; ++i){
125 pthread_join(load_thread, 0);
137 loss = train_networks(nets, ngpus, train, 4);
142 if(avg_loss == -1) avg_loss = loss;
143 avg_loss = avg_loss*.9 + loss*.1;
144 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);
146 if(*net->
seen/N > epoch){
147 epoch = *net->
seen/N;
149 sprintf(buff,
"%s/%s_%d.weights",backup_directory,base, epoch);
154 sprintf(buff,
"%s/%s.backup",backup_directory,base);
159 sprintf(buff,
"%s/%s.weights", backup_directory, base);
161 pthread_join(load_thread, 0);
164 if(labels)
free_ptrs((
void**)labels, classes);
178 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
179 char *valid_list =
option_find_str(options,
"valid",
"data/train.list");
194 int num = (i+1)*m/splits - i*m/splits;
211 for(i = 1; i <= splits; ++i){
214 pthread_join(load_thread, 0);
217 num = (i+1)*m/splits - i*m/splits;
218 char **part = paths+(i*m/splits);
223 printf(
"Loaded: %d images in %lf seconds\n", val.
X.
rows,
sec(clock()-time));
229 printf(
"%d: top 1: %f, top %d: %f, %lf seconds, %d images\n", i, avg_acc/i, topk, avg_topk/i,
sec(clock()-time), val.
X.
rows);
243 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
244 char *valid_list =
option_find_str(options,
"valid",
"data/train.list");
257 int *indexes = calloc(topk,
sizeof(
int));
259 for(i = 0; i < m; ++i){
261 char *path = paths[i];
263 if(strstr(path, labels[j])){
273 images[0] =
crop_image(im, -shift, -shift, w, h);
274 images[1] =
crop_image(im, shift, -shift, w, h);
276 images[3] =
crop_image(im, -shift, shift, w, h);
277 images[4] =
crop_image(im, shift, shift, w, h);
279 images[5] =
crop_image(im, -shift, -shift, w, h);
280 images[6] =
crop_image(im, shift, -shift, w, h);
282 images[8] =
crop_image(im, -shift, shift, w, h);
283 images[9] =
crop_image(im, shift, shift, w, h);
284 float *pred = calloc(classes,
sizeof(
float));
285 for(j = 0; j < 10; ++j){
288 axpy_cpu(classes, 1, p, 1, pred, 1);
292 top_k(pred, classes, topk, indexes);
294 if(indexes[0] ==
class) avg_acc += 1;
295 for(j = 0; j < topk; ++j){
296 if(indexes[j] ==
class) avg_topk += 1;
299 printf(
"%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
312 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
313 char *valid_list =
option_find_str(options,
"valid",
"data/train.list");
326 int *indexes = calloc(topk,
sizeof(
int));
329 for(i = 0; i < m; ++i){
331 char *path = paths[i];
333 if(strstr(path, labels[j])){
349 top_k(pred, classes, topk, indexes);
351 if(indexes[0] ==
class) avg_acc += 1;
352 for(j = 0; j < topk; ++j){
353 if(indexes[j] ==
class) avg_topk += 1;
356 printf(
"%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
370 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
373 char *valid_list =
option_find_str(options,
"valid",
"data/train.list");
386 int *indexes = calloc(topk,
sizeof(
int));
388 for(i = 0; i < m; ++i){
390 char *path = paths[i];
392 if(strstr(path, labels[j])){
408 top_k(pred, classes, topk, indexes);
410 if(indexes[0] ==
class) avg_acc += 1;
411 for(j = 0; j < topk; ++j){
412 if(indexes[j] ==
class) avg_topk += 1;
415 printf(
"%s, %d, %f, %f, \n", paths[i],
class, pred[0], pred[1]);
416 printf(
"%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
429 char *label_list =
option_find_str(options,
"labels",
"data/labels.list");
430 char *valid_list =
option_find_str(options,
"valid",
"data/train.list");
437 int scales[] = {224, 256, 288, 320};
438 int nscales =
sizeof(scales)/
sizeof(scales[0]);
446 int *indexes = calloc(topk,
sizeof(
int));
448 for(i = 0; i < m; ++i){
450 char *path = paths[i];
452 if(strstr(path, labels[j])){
457 float *pred = calloc(classes,
sizeof(
float));
459 for(j = 0; j < nscales; ++j){
464 axpy_cpu(classes, 1, p, 1, pred, 1);
467 axpy_cpu(classes, 1, p, 1, pred, 1);
471 top_k(pred, classes, topk, indexes);
473 if(indexes[0] ==
class) avg_acc += 1;
474 for(j = 0; j < topk; ++j){
475 if(indexes[j] ==
class) avg_topk += 1;
478 printf(
"%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
482 void try_classifier(
char *datacfg,
char *cfgfile,
char *weightfile,
char *filename,
int layer_num)
491 if(!name_list) name_list =
option_find_str(options,
"labels",
"data/labels.list");
497 int *indexes = calloc(top,
sizeof(
int));
502 strncpy(input, filename, 256);
504 printf(
"Enter Image Path: ");
506 input = fgets(input, 256, stdin);
513 float mean[] = {0.48263312050943, 0.45230225481413, 0.40099074308742};
514 float std[] = {0.22590347483426, 0.22120921437787, 0.22103996251583};
516 var[0] = std[0]*std[0];
517 var[1] = std[1]*std[1];
518 var[2] = std[2]*std[2];
527 for(i = 0; i < l.
c; ++i){
533 for(i = 0; i < l.
outputs; ++i){
534 printf(
"%f\n", l.
output[i]);
550 printf(
"%s: Predicted in %f seconds.\n", input,
sec(clock()-time));
551 for(i = 0; i < top; ++i){
552 int index = indexes[i];
553 printf(
"%s: %f\n", names[index], predictions[index]);
569 if(!name_list) name_list =
option_find_str(options,
"labels",
"data/labels.list");
575 int *indexes = calloc(top,
sizeof(
int));
580 strncpy(input, filename, 256);
582 printf(
"Enter Image Path: ");
584 input = fgets(input, 256, stdin);
600 fprintf(stderr,
"%s: Predicted in %f seconds.\n", input,
sec(clock()-time));
601 for(i = 0; i < top; ++i){
602 int index = indexes[i];
605 printf(
"%5.2f%%: %s\n", predictions[index]*100, names[index]);
623 char *label_list =
option_find_str(options,
"names",
"data/labels.list");
634 for(i = 0; i < m; ++i){
645 printf(
"%s\n", labels[ind]);
665 int *indexes = calloc(top,
sizeof(
int));
667 for(i = 0; i < m; ++i){
669 char *path = paths[i];
677 for(j = 0; j < top; ++j){
678 printf(
"\t%d", indexes[j]);
685 fprintf(stderr,
"%lf seconds, %d images, %d total\n",
what_time_is_it_now() - time, i+1, m);
689 void test_classifier(
char *datacfg,
char *cfgfile,
char *weightfile,
int target_layer)
722 for(curr = net->
batch; curr < m; curr += net->batch){
725 pthread_join(load_thread, 0);
729 args.
paths = paths + curr;
730 if (curr + net->
batch > m) args.
n = m - curr;
733 fprintf(stderr,
"Loaded: %d images in %lf seconds\n", val.
X.
rows,
sec(clock()-time));
739 if (target_layer >= 0){
743 for(i = 0; i < pred.
rows; ++i){
744 printf(
"%s", paths[curr-net->
batch+i]);
745 for(j = 0; j < pred.
cols; ++j){
746 printf(
"\t%g", pred.
vals[i][j]);
753 fprintf(stderr,
"%lf seconds, %d images, %d total\n",
sec(clock()-time), val.
X.
rows, curr);
776 for(i = 0; i < m; ++i){
788 printf(
"%s", paths[i]);
790 printf(
"\t%g", pred[j]);
797 void threat_classifier(
char *datacfg,
char *cfgfile,
char *weightfile,
int cam_index,
const char *filename)
803 printf(
"Classifier Demo\n");
812 cap = cvCaptureFromFile(filename);
814 cap = cvCaptureFromCAM(cam_index);
822 int *indexes = calloc(top,
sizeof(
int));
824 if(!cap)
error(
"Couldn't connect to webcam.\n");
834 struct timeval tval_before, tval_after, tval_result;
835 gettimeofday(&tval_before, NULL);
837 image in = get_image_from_stream(cap);
845 int y2 = out.
h - out.
h/20;
847 int border = .01*out.
h;
848 int h = y2 - y1 - 2*border;
849 int w = x2 - x1 - 2*border;
852 float curr_threat = 0;
854 curr_threat = predictions[0] * 0 +
855 predictions[1] * .6 +
858 curr_threat = predictions[218] +
865 threat = roll * curr_threat + (1-roll) * threat;
867 draw_box_width(out, x2 + border, y1 + .02*h, x2 + .5 * w, y1 + .02*h + border, border, 0,0,0);
870 y1 + .02*h - 2*border,
871 x2 + .5 * w + 6*border,
872 y1 + .02*h + 3*border, 3*border, 1,0,0);
875 y1 + .02*h - 2*border,
876 x2 + .5 * w + 6*border,
877 y1 + .02*h + 3*border, .5*border, 0,0,0);
878 draw_box_width(out, x2 + border, y1 + .42*h, x2 + .5 * w, y1 + .42*h + border, border, 0,0,0);
881 y1 + .42*h - 2*border,
882 x2 + .5 * w + 6*border,
883 y1 + .42*h + 3*border, 3*border, 1,1,0);
886 y1 + .42*h - 2*border,
887 x2 + .5 * w + 6*border,
888 y1 + .42*h + 3*border, .5*border, 0,0,0);
891 for(i = 0; i < threat * h ; ++i){
892 float ratio = (float) i / h;
893 float r = (ratio < .5) ? (2*(ratio)) : 1;
894 float g = (ratio < .5) ? 1 : 1 - 2*(ratio - .5);
895 draw_box_width(out, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0);
899 sprintf(buff,
"/home/pjreddie/tmp/threat_%06d", count);
904 printf(
"\nFPS:%.0f\n",fps);
906 for(i = 0; i < top; ++i){
907 int index = indexes[i];
908 printf(
"%.1f%%: %s\n", predictions[index]*100, names[index]);
917 gettimeofday(&tval_after, NULL);
918 timersub(&tval_after, &tval_before, &tval_result);
919 float curr = 1000000.f/((
long int)tval_result.tv_usec);
920 fps = .9*fps + .1*curr;
926 void gun_classifier(
char *datacfg,
char *cfgfile,
char *weightfile,
int cam_index,
const char *filename)
929 int bad_cats[] = {218, 539, 540, 1213, 1501, 1742, 1911, 2415, 4348, 19223, 368, 369, 370, 1133, 1200, 1306, 2122, 2301, 2537, 2823, 3179, 3596, 3639, 4489, 5107, 5140, 5289, 6240, 6631, 6762, 7048, 7171, 7969, 7984, 7989, 8824, 8927, 9915, 10270, 10448, 13401, 15205, 18358, 18894, 18895, 19249, 19697};
931 printf(
"Classifier Demo\n");
940 cap = cvCaptureFromFile(filename);
942 cap = cvCaptureFromCAM(cam_index);
950 int *indexes = calloc(top,
sizeof(
int));
952 if(!cap)
error(
"Couldn't connect to webcam.\n");
953 cvNamedWindow(
"Threat Detection", CV_WINDOW_NORMAL);
954 cvResizeWindow(
"Threat Detection", 512, 512);
959 struct timeval tval_before, tval_after, tval_result;
960 gettimeofday(&tval_before, NULL);
962 image in = get_image_from_stream(cap);
972 for(i = 0; i <
sizeof(bad_cats)/
sizeof(bad_cats[0]); ++i){
973 int index = bad_cats[i];
974 if(predictions[index] > .01){
975 printf(
"Threat Detected!\n");
980 if(!threat) printf(
"Scanning...\n");
981 for(i = 0; i <
sizeof(bad_cats)/
sizeof(bad_cats[0]); ++i){
982 int index = bad_cats[i];
983 if(predictions[index] > .01){
984 printf(
"%s\n", names[index]);
992 gettimeofday(&tval_after, NULL);
993 timersub(&tval_after, &tval_before, &tval_result);
994 float curr = 1000000.f/((
long int)tval_result.tv_usec);
995 fps = .9*fps + .1*curr;
1000 void demo_classifier(
char *datacfg,
char *cfgfile,
char *weightfile,
int cam_index,
const char *filename)
1003 char *base =
basecfg(cfgfile);
1005 printf(
"Classifier Demo\n");
1017 cap = cvCaptureFromFile(filename);
1019 cap = cvCaptureFromCAM(cam_index);
1023 cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH, w);
1026 cvSetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT, h);
1035 int *indexes = calloc(top,
sizeof(
int));
1037 if(!cap)
error(
"Couldn't connect to webcam.\n");
1038 cvNamedWindow(base, CV_WINDOW_NORMAL);
1039 cvResizeWindow(base, 512, 512);
1044 struct timeval tval_before, tval_after, tval_result;
1045 gettimeofday(&tval_before, NULL);
1047 image in = get_image_from_stream(cap);
1056 printf(
"\033[1;1H");
1057 printf(
"\nFPS:%.0f\n",fps);
1062 float rgb[3] = {1,1,1};
1063 for(i = 0; i < top; ++i){
1064 printf(
"%d\n", toph);
1065 int index = indexes[i];
1066 printf(
"%.1f%%: %s\n", predictions[index]*100, names[index]);
1069 sprintf(buff,
"%3.1f%%: %s\n", predictions[index]*100, names[index]);
1080 gettimeofday(&tval_after, NULL);
1081 timersub(&tval_after, &tval_before, &tval_result);
1082 float curr = 1000000.f/((
long int)tval_result.tv_usec);
1083 fps = .9*fps + .1*curr;
1092 fprintf(stderr,
"usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
1103 int clear =
find_arg(argc, argv,
"-clear");
1104 char *
data = argv[3];
1105 char *cfg = argv[4];
1106 char *weights = (argc > 5) ? argv[5] : 0;
1107 char *filename = (argc > 6) ? argv[6]: 0;
1108 char *layer_s = (argc > 7) ? argv[7]: 0;
1109 int layer = layer_s ? atoi(layer_s) : -1;
1110 if(0==strcmp(argv[2],
"predict"))
predict_classifier(data, cfg, weights, filename, top);
1112 else if(0==strcmp(argv[2],
"try"))
try_classifier(data, cfg, weights, filename, atoi(layer_s));
1113 else if(0==strcmp(argv[2],
"train"))
train_classifier(data, cfg, weights, gpus, ngpus, clear);
1114 else if(0==strcmp(argv[2],
"demo"))
demo_classifier(data, cfg, weights, cam_index, filename);
1115 else if(0==strcmp(argv[2],
"gun"))
gun_classifier(data, cfg, weights, cam_index, filename);
1116 else if(0==strcmp(argv[2],
"threat"))
threat_classifier(data, cfg, weights, cam_index, filename);
1117 else if(0==strcmp(argv[2],
"test"))
test_classifier(data, cfg, weights, layer);
1118 else if(0==strcmp(argv[2],
"csv"))
csv_classifier(data, cfg, weights);
image resize_image(image im, int w, int h)
pthread_t load_data_in_thread(load_args args)
int find_arg(int argc, char *argv[], char *arg)
void set_batch_network(network *net, int b)
void run_classifier(int argc, char **argv)
int max_index(float *a, int n)
float * get_regression_values(char **labels, int n)
void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
int show_image(image p, const char *name, int ms)
char * find_char_arg(int argc, char **argv, char *arg, char *def)
char * basecfg(char *cfgfile)
void change_leaves(tree *t, char *leaf_list)
image resize_max(image im, int max)
void ** list_to_array(list *l)
char * option_find_str(list *l, char *key, char *def)
float train_network(network *net, data d)
void top_k(float *a, int n, int k, int *index)
float * network_accuracies(network *net, data d, int n)
void csv_classifier(char *datacfg, char *cfgfile, char *weightfile)
void validate_classifier_multi(char *datacfg, char *cfg, char *weights)
void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top)
void free_network(network *net)
void save_weights(network *net, char *filename)
void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b)
image get_label(image **characters, char *string, int size)
void validate_classifier_10(char *datacfg, char *filename, char *weightfile)
int resize_network(network *net, int w, int h)
image resize_min(image im, int min)
tree * read_tree(char *filename)
int option_find_int_quiet(list *l, char *key, int def)
void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
int * read_intlist(char *s, int *n, int d)
void draw_label(image a, int r, int c, image label, const float *rgb)
image center_crop_image(image im, int w, int h)
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
image load_image_color(char *filename, int w, int h)
void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
float get_current_rate(network *net)
void normalize_cpu(float *x, float *mean, float *variance, int batch, int filters, int spatial)
float sec(clock_t clocks)
void file_output_classifier(char *datacfg, char *filename, char *weightfile, char *listfile)
void label_classifier(char *datacfg, char *filename, char *weightfile)
int find_int_arg(int argc, char **argv, char *arg, int def)
network * load_network(char *cfg, char *weights, int clear)
char ** get_labels(char *filename)
void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
image crop_image(image im, int dx, int dy, int w, int h)
matrix network_predict_data(network *net, data test)
void free_matrix(matrix m)
void * load_thread(void *ptr)
void cuda_set_device(int n)
void validate_classifier_crop(char *datacfg, char *filename, char *weightfile)
list * read_data_cfg(char *filename)
void try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int layer_num)
void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
size_t get_current_batch(network *net)
void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_layer)
void validate_classifier_full(char *datacfg, char *filename, char *weightfile)
list * get_paths(char *filename)
void top_predictions(network *net, int n, int *index)
pthread_t load_data(load_args args)
void error(const char *s)
int option_find_int(list *l, char *key, int def)
void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves, int stride)
double what_time_is_it_now()