00001 #include "network.h"
00002 #include "utils.h"
00003 #include "parser.h"
00004 #include "option_list.h"
00005 #include "blas.h"
00006 #include "assert.h"
00007 #include "classifier.h"
00008 #include "cuda.h"
00009 #include <sys/time.h>
00010
00011 #ifdef OPENCV
00012 #include "opencv2/highgui/highgui_c.h"
00013 image get_image_from_stream(CvCapture *cap);
00014 #endif
00015
00016 float *get_regression_values(char **labels, int n)
00017 {
00018 float *v = calloc(n, sizeof(float));
00019 int i;
00020 for(i = 0; i < n; ++i){
00021 char *p = strchr(labels[i], ' ');
00022 *p = 0;
00023 v[i] = atof(p+1);
00024 }
00025 return v;
00026 }
00027
00028 void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
00029 {
00030 int i;
00031
00032 float avg_loss = -1;
00033 char *base = basecfg(cfgfile);
00034 printf("%s\n", base);
00035 printf("%d\n", ngpus);
00036 network *nets = calloc(ngpus, sizeof(network));
00037
00038 srand(time(0));
00039 int seed = rand();
00040 for(i = 0; i < ngpus; ++i){
00041 srand(seed);
00042 #ifdef GPU
00043 cuda_set_device(gpus[i]);
00044 #endif
00045 nets[i] = parse_network_cfg(cfgfile);
00046 if(weightfile){
00047 load_weights(&nets[i], weightfile);
00048 }
00049 if(clear) *nets[i].seen = 0;
00050 nets[i].learning_rate *= ngpus;
00051 }
00052 srand(time(0));
00053 network net = nets[0];
00054
00055 int imgs = net.batch * net.subdivisions * ngpus;
00056
00057 printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
00058 list *options = read_data_cfg(datacfg);
00059
00060 char *backup_directory = option_find_str(options, "backup", "/backup/");
00061 char *label_list = option_find_str(options, "labels", "data/labels.list");
00062 char *train_list = option_find_str(options, "train", "data/train.list");
00063 int classes = option_find_int(options, "classes", 2);
00064
00065 char **labels = get_labels(label_list);
00066 list *plist = get_paths(train_list);
00067 char **paths = (char **)list_to_array(plist);
00068 printf("%d\n", plist->size);
00069 int N = plist->size;
00070 clock_t time;
00071
00072 load_args args = {0};
00073 args.w = net.w;
00074 args.h = net.h;
00075 args.threads = 32;
00076 args.hierarchy = net.hierarchy;
00077
00078 args.min = net.min_crop;
00079 args.max = net.max_crop;
00080 args.angle = net.angle;
00081 args.aspect = net.aspect;
00082 args.exposure = net.exposure;
00083 args.saturation = net.saturation;
00084 args.hue = net.hue;
00085 args.size = net.w;
00086
00087 args.paths = paths;
00088 args.classes = classes;
00089 args.n = imgs;
00090 args.m = N;
00091 args.labels = labels;
00092 args.type = CLASSIFICATION_DATA;
00093
00094 data train;
00095 data buffer;
00096 pthread_t load_thread;
00097 args.d = &buffer;
00098 load_thread = load_data(args);
00099
00100 int epoch = (*net.seen)/N;
00101 while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
00102 time=clock();
00103
00104 pthread_join(load_thread, 0);
00105 train = buffer;
00106 load_thread = load_data(args);
00107
00108 printf("Loaded: %lf seconds\n", sec(clock()-time));
00109 time=clock();
00110
00111 float loss = 0;
00112 #ifdef GPU
00113 if(ngpus == 1){
00114 loss = train_network(net, train);
00115 } else {
00116 loss = train_networks(nets, ngpus, train, 4);
00117 }
00118 #else
00119 loss = train_network(net, train);
00120 #endif
00121 if(avg_loss == -1) avg_loss = loss;
00122 avg_loss = avg_loss*.9 + loss*.1;
00123 printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), (float)(*net.seen)/N, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen);
00124 free_data(train);
00125 if(*net.seen/N > epoch){
00126 epoch = *net.seen/N;
00127 char buff[256];
00128 sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch);
00129 save_weights(net, buff);
00130 }
00131 if(get_current_batch(net)%100 == 0){
00132 char buff[256];
00133 sprintf(buff, "%s/%s.backup",backup_directory,base);
00134 save_weights(net, buff);
00135 }
00136 }
00137 char buff[256];
00138 sprintf(buff, "%s/%s.weights", backup_directory, base);
00139 save_weights(net, buff);
00140
00141 free_network(net);
00142 free_ptrs((void**)labels, classes);
00143 free_ptrs((void**)paths, plist->size);
00144 free_list(plist);
00145 free(base);
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 void validate_classifier_crop(char *datacfg, char *filename, char *weightfile)
00260 {
00261 int i = 0;
00262 network net = parse_network_cfg(filename);
00263 if(weightfile){
00264 load_weights(&net, weightfile);
00265 }
00266 srand(time(0));
00267
00268 list *options = read_data_cfg(datacfg);
00269
00270 char *label_list = option_find_str(options, "labels", "data/labels.list");
00271 char *valid_list = option_find_str(options, "valid", "data/train.list");
00272 int classes = option_find_int(options, "classes", 2);
00273 int topk = option_find_int(options, "top", 1);
00274
00275 char **labels = get_labels(label_list);
00276 list *plist = get_paths(valid_list);
00277
00278 char **paths = (char **)list_to_array(plist);
00279 int m = plist->size;
00280 free_list(plist);
00281
00282 clock_t time;
00283 float avg_acc = 0;
00284 float avg_topk = 0;
00285 int splits = m/1000;
00286 int num = (i+1)*m/splits - i*m/splits;
00287
00288 data val, buffer;
00289
00290 load_args args = {0};
00291 args.w = net.w;
00292 args.h = net.h;
00293
00294 args.paths = paths;
00295 args.classes = classes;
00296 args.n = num;
00297 args.m = 0;
00298 args.labels = labels;
00299 args.d = &buffer;
00300 args.type = OLD_CLASSIFICATION_DATA;
00301
00302 pthread_t load_thread = load_data_in_thread(args);
00303 for(i = 1; i <= splits; ++i){
00304 time=clock();
00305
00306 pthread_join(load_thread, 0);
00307 val = buffer;
00308
00309 num = (i+1)*m/splits - i*m/splits;
00310 char **part = paths+(i*m/splits);
00311 if(i != splits){
00312 args.paths = part;
00313 load_thread = load_data_in_thread(args);
00314 }
00315 printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
00316
00317 time=clock();
00318 float *acc = network_accuracies(net, val, topk);
00319 avg_acc += acc[0];
00320 avg_topk += acc[1];
00321 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);
00322 free_data(val);
00323 }
00324 }
00325
00326 void validate_classifier_10(char *datacfg, char *filename, char *weightfile)
00327 {
00328 int i, j;
00329 network net = parse_network_cfg(filename);
00330 set_batch_network(&net, 1);
00331 if(weightfile){
00332 load_weights(&net, weightfile);
00333 }
00334 srand(time(0));
00335
00336 list *options = read_data_cfg(datacfg);
00337
00338 char *label_list = option_find_str(options, "labels", "data/labels.list");
00339 char *valid_list = option_find_str(options, "valid", "data/train.list");
00340 int classes = option_find_int(options, "classes", 2);
00341 int topk = option_find_int(options, "top", 1);
00342
00343 char **labels = get_labels(label_list);
00344 list *plist = get_paths(valid_list);
00345
00346 char **paths = (char **)list_to_array(plist);
00347 int m = plist->size;
00348 free_list(plist);
00349
00350 float avg_acc = 0;
00351 float avg_topk = 0;
00352 int *indexes = calloc(topk, sizeof(int));
00353
00354 for(i = 0; i < m; ++i){
00355 int class = -1;
00356 char *path = paths[i];
00357 for(j = 0; j < classes; ++j){
00358 if(strstr(path, labels[j])){
00359 class = j;
00360 break;
00361 }
00362 }
00363 int w = net.w;
00364 int h = net.h;
00365 int shift = 32;
00366 image im = load_image_color(paths[i], w+shift, h+shift);
00367 image images[10];
00368 images[0] = crop_image(im, -shift, -shift, w, h);
00369 images[1] = crop_image(im, shift, -shift, w, h);
00370 images[2] = crop_image(im, 0, 0, w, h);
00371 images[3] = crop_image(im, -shift, shift, w, h);
00372 images[4] = crop_image(im, shift, shift, w, h);
00373 flip_image(im);
00374 images[5] = crop_image(im, -shift, -shift, w, h);
00375 images[6] = crop_image(im, shift, -shift, w, h);
00376 images[7] = crop_image(im, 0, 0, w, h);
00377 images[8] = crop_image(im, -shift, shift, w, h);
00378 images[9] = crop_image(im, shift, shift, w, h);
00379 float *pred = calloc(classes, sizeof(float));
00380 for(j = 0; j < 10; ++j){
00381 float *p = network_predict(net, images[j].data);
00382 if(net.hierarchy) hierarchy_predictions(p, net.outputs, net.hierarchy, 1);
00383 axpy_cpu(classes, 1, p, 1, pred, 1);
00384 free_image(images[j]);
00385 }
00386 free_image(im);
00387 top_k(pred, classes, topk, indexes);
00388 free(pred);
00389 if(indexes[0] == class) avg_acc += 1;
00390 for(j = 0; j < topk; ++j){
00391 if(indexes[j] == class) avg_topk += 1;
00392 }
00393
00394 printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
00395 }
00396 }
00397
00398 void validate_classifier_full(char *datacfg, char *filename, char *weightfile)
00399 {
00400 int i, j;
00401 network net = parse_network_cfg(filename);
00402 set_batch_network(&net, 1);
00403 if(weightfile){
00404 load_weights(&net, weightfile);
00405 }
00406 srand(time(0));
00407
00408 list *options = read_data_cfg(datacfg);
00409
00410 char *label_list = option_find_str(options, "labels", "data/labels.list");
00411 char *valid_list = option_find_str(options, "valid", "data/train.list");
00412 int classes = option_find_int(options, "classes", 2);
00413 int topk = option_find_int(options, "top", 1);
00414
00415 char **labels = get_labels(label_list);
00416 list *plist = get_paths(valid_list);
00417
00418 char **paths = (char **)list_to_array(plist);
00419 int m = plist->size;
00420 free_list(plist);
00421
00422 float avg_acc = 0;
00423 float avg_topk = 0;
00424 int *indexes = calloc(topk, sizeof(int));
00425
00426 int size = net.w;
00427 for(i = 0; i < m; ++i){
00428 int class = -1;
00429 char *path = paths[i];
00430 for(j = 0; j < classes; ++j){
00431 if(strstr(path, labels[j])){
00432 class = j;
00433 break;
00434 }
00435 }
00436 image im = load_image_color(paths[i], 0, 0);
00437 image resized = resize_min(im, size);
00438 resize_network(&net, resized.w, resized.h);
00439
00440
00441
00442 float *pred = network_predict(net, resized.data);
00443 if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1);
00444
00445 free_image(im);
00446 free_image(resized);
00447 top_k(pred, classes, topk, indexes);
00448
00449 if(indexes[0] == class) avg_acc += 1;
00450 for(j = 0; j < topk; ++j){
00451 if(indexes[j] == class) avg_topk += 1;
00452 }
00453
00454 printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
00455 }
00456 }
00457
00458
00459 void validate_classifier_single(char *datacfg, char *filename, char *weightfile)
00460 {
00461 int i, j;
00462 network net = parse_network_cfg(filename);
00463 if(weightfile){
00464 load_weights(&net, weightfile);
00465 }
00466 set_batch_network(&net, 1);
00467 srand(time(0));
00468
00469 list *options = read_data_cfg(datacfg);
00470
00471 char *label_list = option_find_str(options, "labels", "data/labels.list");
00472 char *leaf_list = option_find_str(options, "leaves", 0);
00473 if(leaf_list) change_leaves(net.hierarchy, leaf_list);
00474 char *valid_list = option_find_str(options, "valid", "data/train.list");
00475 int classes = option_find_int(options, "classes", 2);
00476 int topk = option_find_int(options, "top", 1);
00477
00478 char **labels = get_labels(label_list);
00479 list *plist = get_paths(valid_list);
00480
00481 char **paths = (char **)list_to_array(plist);
00482 int m = plist->size;
00483 free_list(plist);
00484
00485 float avg_acc = 0;
00486 float avg_topk = 0;
00487 int *indexes = calloc(topk, sizeof(int));
00488
00489 for(i = 0; i < m; ++i){
00490 int class = -1;
00491 char *path = paths[i];
00492 for(j = 0; j < classes; ++j){
00493 if(strstr(path, labels[j])){
00494 class = j;
00495 break;
00496 }
00497 }
00498 image im = load_image_color(paths[i], 0, 0);
00499 image resized = resize_min(im, net.w);
00500 image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
00501
00502
00503
00504 float *pred = network_predict(net, crop.data);
00505 if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1);
00506
00507 if(resized.data != im.data) free_image(resized);
00508 free_image(im);
00509 free_image(crop);
00510 top_k(pred, classes, topk, indexes);
00511
00512 if(indexes[0] == class) avg_acc += 1;
00513 for(j = 0; j < topk; ++j){
00514 if(indexes[j] == class) avg_topk += 1;
00515 }
00516
00517 printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
00518 }
00519 }
00520
00521 void validate_classifier_multi(char *datacfg, char *filename, char *weightfile)
00522 {
00523 int i, j;
00524 network net = parse_network_cfg(filename);
00525 set_batch_network(&net, 1);
00526 if(weightfile){
00527 load_weights(&net, weightfile);
00528 }
00529 srand(time(0));
00530
00531 list *options = read_data_cfg(datacfg);
00532
00533 char *label_list = option_find_str(options, "labels", "data/labels.list");
00534 char *valid_list = option_find_str(options, "valid", "data/train.list");
00535 int classes = option_find_int(options, "classes", 2);
00536 int topk = option_find_int(options, "top", 1);
00537
00538 char **labels = get_labels(label_list);
00539 list *plist = get_paths(valid_list);
00540 int scales[] = {224, 288, 320, 352, 384};
00541 int nscales = sizeof(scales)/sizeof(scales[0]);
00542
00543 char **paths = (char **)list_to_array(plist);
00544 int m = plist->size;
00545 free_list(plist);
00546
00547 float avg_acc = 0;
00548 float avg_topk = 0;
00549 int *indexes = calloc(topk, sizeof(int));
00550
00551 for(i = 0; i < m; ++i){
00552 int class = -1;
00553 char *path = paths[i];
00554 for(j = 0; j < classes; ++j){
00555 if(strstr(path, labels[j])){
00556 class = j;
00557 break;
00558 }
00559 }
00560 float *pred = calloc(classes, sizeof(float));
00561 image im = load_image_color(paths[i], 0, 0);
00562 for(j = 0; j < nscales; ++j){
00563 image r = resize_min(im, scales[j]);
00564 resize_network(&net, r.w, r.h);
00565 float *p = network_predict(net, r.data);
00566 if(net.hierarchy) hierarchy_predictions(p, net.outputs, net.hierarchy, 1);
00567 axpy_cpu(classes, 1, p, 1, pred, 1);
00568 flip_image(r);
00569 p = network_predict(net, r.data);
00570 axpy_cpu(classes, 1, p, 1, pred, 1);
00571 if(r.data != im.data) free_image(r);
00572 }
00573 free_image(im);
00574 top_k(pred, classes, topk, indexes);
00575 free(pred);
00576 if(indexes[0] == class) avg_acc += 1;
00577 for(j = 0; j < topk; ++j){
00578 if(indexes[j] == class) avg_topk += 1;
00579 }
00580
00581 printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
00582 }
00583 }
00584
00585 void try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int layer_num)
00586 {
00587 network net = parse_network_cfg(cfgfile);
00588 if(weightfile){
00589 load_weights(&net, weightfile);
00590 }
00591 set_batch_network(&net, 1);
00592 srand(2222222);
00593
00594 list *options = read_data_cfg(datacfg);
00595
00596 char *name_list = option_find_str(options, "names", 0);
00597 if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
00598 int top = option_find_int(options, "top", 1);
00599
00600 int i = 0;
00601 char **names = get_labels(name_list);
00602 clock_t time;
00603 int *indexes = calloc(top, sizeof(int));
00604 char buff[256];
00605 char *input = buff;
00606 while(1){
00607 if(filename){
00608 strncpy(input, filename, 256);
00609 }else{
00610 printf("Enter Image Path: ");
00611 fflush(stdout);
00612 input = fgets(input, 256, stdin);
00613 if(!input) return;
00614 strtok(input, "\n");
00615 }
00616 image orig = load_image_color(input, 0, 0);
00617 image r = resize_min(orig, 256);
00618 image im = crop_image(r, (r.w - 224 - 1)/2 + 1, (r.h - 224 - 1)/2 + 1, 224, 224);
00619 float mean[] = {0.48263312050943, 0.45230225481413, 0.40099074308742};
00620 float std[] = {0.22590347483426, 0.22120921437787, 0.22103996251583};
00621 float var[3];
00622 var[0] = std[0]*std[0];
00623 var[1] = std[1]*std[1];
00624 var[2] = std[2]*std[2];
00625
00626 normalize_cpu(im.data, mean, var, 1, 3, im.w*im.h);
00627
00628 float *X = im.data;
00629 time=clock();
00630 float *predictions = network_predict(net, X);
00631
00632 layer l = net.layers[layer_num];
00633 for(i = 0; i < l.c; ++i){
00634 if(l.rolling_mean) printf("%f %f %f\n", l.rolling_mean[i], l.rolling_variance[i], l.scales[i]);
00635 }
00636 #ifdef GPU
00637 cuda_pull_array(l.output_gpu, l.output, l.outputs);
00638 #endif
00639 for(i = 0; i < l.outputs; ++i){
00640 printf("%f\n", l.output[i]);
00641 }
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 top_predictions(net, top, indexes);
00656 printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
00657 for(i = 0; i < top; ++i){
00658 int index = indexes[i];
00659 printf("%s: %f\n", names[index], predictions[index]);
00660 }
00661 free_image(im);
00662 if (filename) break;
00663 }
00664 }
00665
00666 void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top)
00667 {
00668 network net = parse_network_cfg(cfgfile);
00669 if(weightfile){
00670 load_weights(&net, weightfile);
00671 }
00672 set_batch_network(&net, 1);
00673 srand(2222222);
00674
00675 list *options = read_data_cfg(datacfg);
00676
00677 char *name_list = option_find_str(options, "names", 0);
00678 if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
00679 if(top == 0) top = option_find_int(options, "top", 1);
00680
00681 int i = 0;
00682 char **names = get_labels(name_list);
00683 clock_t time;
00684 int *indexes = calloc(top, sizeof(int));
00685 char buff[256];
00686 char *input = buff;
00687 int size = net.w;
00688 while(1){
00689 if(filename){
00690 strncpy(input, filename, 256);
00691 }else{
00692 printf("Enter Image Path: ");
00693 fflush(stdout);
00694 input = fgets(input, 256, stdin);
00695 if(!input) return;
00696 strtok(input, "\n");
00697 }
00698 image im = load_image_color(input, 0, 0);
00699 image r = resize_min(im, size);
00700 resize_network(&net, r.w, r.h);
00701 printf("%d %d\n", r.w, r.h);
00702
00703 float *X = r.data;
00704 time=clock();
00705 float *predictions = network_predict(net, X);
00706 if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 0);
00707 top_k(predictions, net.outputs, top, indexes);
00708 printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
00709 for(i = 0; i < top; ++i){
00710 int index = indexes[i];
00711 if(net.hierarchy) printf("%d, %s: %f, parent: %s \n",index, names[index], predictions[index], (net.hierarchy->parent[index] >= 0) ? names[net.hierarchy->parent[index]] : "Root");
00712 else printf("%s: %f\n",names[index], predictions[index]);
00713 }
00714 if(r.data != im.data) free_image(r);
00715 free_image(im);
00716 if (filename) break;
00717 }
00718 }
00719
00720
00721 void label_classifier(char *datacfg, char *filename, char *weightfile)
00722 {
00723 int i;
00724 network net = parse_network_cfg(filename);
00725 set_batch_network(&net, 1);
00726 if(weightfile){
00727 load_weights(&net, weightfile);
00728 }
00729 srand(time(0));
00730
00731 list *options = read_data_cfg(datacfg);
00732
00733 char *label_list = option_find_str(options, "names", "data/labels.list");
00734 char *test_list = option_find_str(options, "test", "data/train.list");
00735 int classes = option_find_int(options, "classes", 2);
00736
00737 char **labels = get_labels(label_list);
00738 list *plist = get_paths(test_list);
00739
00740 char **paths = (char **)list_to_array(plist);
00741 int m = plist->size;
00742 free_list(plist);
00743
00744 for(i = 0; i < m; ++i){
00745 image im = load_image_color(paths[i], 0, 0);
00746 image resized = resize_min(im, net.w);
00747 image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
00748 float *pred = network_predict(net, crop.data);
00749
00750 if(resized.data != im.data) free_image(resized);
00751 free_image(im);
00752 free_image(crop);
00753 int ind = max_index(pred, classes);
00754
00755 printf("%s\n", labels[ind]);
00756 }
00757 }
00758
00759
00760 void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_layer)
00761 {
00762 int curr = 0;
00763 network net = parse_network_cfg(cfgfile);
00764 if(weightfile){
00765 load_weights(&net, weightfile);
00766 }
00767 srand(time(0));
00768
00769 list *options = read_data_cfg(datacfg);
00770
00771 char *test_list = option_find_str(options, "test", "data/test.list");
00772 int classes = option_find_int(options, "classes", 2);
00773
00774 list *plist = get_paths(test_list);
00775
00776 char **paths = (char **)list_to_array(plist);
00777 int m = plist->size;
00778 free_list(plist);
00779
00780 clock_t time;
00781
00782 data val, buffer;
00783
00784 load_args args = {0};
00785 args.w = net.w;
00786 args.h = net.h;
00787 args.paths = paths;
00788 args.classes = classes;
00789 args.n = net.batch;
00790 args.m = 0;
00791 args.labels = 0;
00792 args.d = &buffer;
00793 args.type = OLD_CLASSIFICATION_DATA;
00794
00795 pthread_t load_thread = load_data_in_thread(args);
00796 for(curr = net.batch; curr < m; curr += net.batch){
00797 time=clock();
00798
00799 pthread_join(load_thread, 0);
00800 val = buffer;
00801
00802 if(curr < m){
00803 args.paths = paths + curr;
00804 if (curr + net.batch > m) args.n = m - curr;
00805 load_thread = load_data_in_thread(args);
00806 }
00807 fprintf(stderr, "Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
00808
00809 time=clock();
00810 matrix pred = network_predict_data(net, val);
00811
00812 int i, j;
00813 if (target_layer >= 0){
00814
00815 }
00816
00817 for(i = 0; i < pred.rows; ++i){
00818 printf("%s", paths[curr-net.batch+i]);
00819 for(j = 0; j < pred.cols; ++j){
00820 printf("\t%g", pred.vals[i][j]);
00821 }
00822 printf("\n");
00823 }
00824
00825 free_matrix(pred);
00826
00827 fprintf(stderr, "%lf seconds, %d images, %d total\n", sec(clock()-time), val.X.rows, curr);
00828 free_data(val);
00829 }
00830 }
00831
00832
00833 void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
00834 {
00835 #ifdef OPENCV
00836 float threat = 0;
00837 float roll = .2;
00838
00839 printf("Classifier Demo\n");
00840 network net = parse_network_cfg(cfgfile);
00841 if(weightfile){
00842 load_weights(&net, weightfile);
00843 }
00844 set_batch_network(&net, 1);
00845 list *options = read_data_cfg(datacfg);
00846
00847 srand(2222222);
00848 CvCapture * cap;
00849
00850 if(filename){
00851 cap = cvCaptureFromFile(filename);
00852 }else{
00853 cap = cvCaptureFromCAM(cam_index);
00854 }
00855
00856 int top = option_find_int(options, "top", 1);
00857
00858 char *name_list = option_find_str(options, "names", 0);
00859 char **names = get_labels(name_list);
00860
00861 int *indexes = calloc(top, sizeof(int));
00862
00863 if(!cap) error("Couldn't connect to webcam.\n");
00864
00865
00866 float fps = 0;
00867 int i;
00868
00869 int count = 0;
00870
00871 while(1){
00872 ++count;
00873 struct timeval tval_before, tval_after, tval_result;
00874 gettimeofday(&tval_before, NULL);
00875
00876 image in = get_image_from_stream(cap);
00877 if(!in.data) break;
00878 image in_s = resize_image(in, net.w, net.h);
00879
00880 image out = in;
00881 int x1 = out.w / 20;
00882 int y1 = out.h / 20;
00883 int x2 = 2*x1;
00884 int y2 = out.h - out.h/20;
00885
00886 int border = .01*out.h;
00887 int h = y2 - y1 - 2*border;
00888 int w = x2 - x1 - 2*border;
00889
00890 float *predictions = network_predict(net, in_s.data);
00891 float curr_threat = 0;
00892 if(1){
00893 curr_threat = predictions[0] * 0 +
00894 predictions[1] * .6 +
00895 predictions[2];
00896 } else {
00897 curr_threat = predictions[218] +
00898 predictions[539] +
00899 predictions[540] +
00900 predictions[368] +
00901 predictions[369] +
00902 predictions[370];
00903 }
00904 threat = roll * curr_threat + (1-roll) * threat;
00905
00906 draw_box_width(out, x2 + border, y1 + .02*h, x2 + .5 * w, y1 + .02*h + border, border, 0,0,0);
00907 if(threat > .97) {
00908 draw_box_width(out, x2 + .5 * w + border,
00909 y1 + .02*h - 2*border,
00910 x2 + .5 * w + 6*border,
00911 y1 + .02*h + 3*border, 3*border, 1,0,0);
00912 }
00913 draw_box_width(out, x2 + .5 * w + border,
00914 y1 + .02*h - 2*border,
00915 x2 + .5 * w + 6*border,
00916 y1 + .02*h + 3*border, .5*border, 0,0,0);
00917 draw_box_width(out, x2 + border, y1 + .42*h, x2 + .5 * w, y1 + .42*h + border, border, 0,0,0);
00918 if(threat > .57) {
00919 draw_box_width(out, x2 + .5 * w + border,
00920 y1 + .42*h - 2*border,
00921 x2 + .5 * w + 6*border,
00922 y1 + .42*h + 3*border, 3*border, 1,1,0);
00923 }
00924 draw_box_width(out, x2 + .5 * w + border,
00925 y1 + .42*h - 2*border,
00926 x2 + .5 * w + 6*border,
00927 y1 + .42*h + 3*border, .5*border, 0,0,0);
00928
00929 draw_box_width(out, x1, y1, x2, y2, border, 0,0,0);
00930 for(i = 0; i < threat * h ; ++i){
00931 float ratio = (float) i / h;
00932 float r = (ratio < .5) ? (2*(ratio)) : 1;
00933 float g = (ratio < .5) ? 1 : 1 - 2*(ratio - .5);
00934 draw_box_width(out, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0);
00935 }
00936 top_predictions(net, top, indexes);
00937 char buff[256];
00938 sprintf(buff, "/home/pjreddie/tmp/threat_%06d", count);
00939
00940
00941 printf("\033[2J");
00942 printf("\033[1;1H");
00943 printf("\nFPS:%.0f\n",fps);
00944
00945 for(i = 0; i < top; ++i){
00946 int index = indexes[i];
00947 printf("%.1f%%: %s\n", predictions[index]*100, names[index]);
00948 }
00949
00950 if(1){
00951 show_image(out, "Threat");
00952 cvWaitKey(10);
00953 }
00954 free_image(in_s);
00955 free_image(in);
00956
00957 gettimeofday(&tval_after, NULL);
00958 timersub(&tval_after, &tval_before, &tval_result);
00959 float curr = 1000000.f/((long int)tval_result.tv_usec);
00960 fps = .9*fps + .1*curr;
00961 }
00962 #endif
00963 }
00964
00965
00966 void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
00967 {
00968 #ifdef OPENCV
00969 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};
00970
00971 printf("Classifier Demo\n");
00972 network net = parse_network_cfg(cfgfile);
00973 if(weightfile){
00974 load_weights(&net, weightfile);
00975 }
00976 set_batch_network(&net, 1);
00977 list *options = read_data_cfg(datacfg);
00978
00979 srand(2222222);
00980 CvCapture * cap;
00981
00982 if(filename){
00983 cap = cvCaptureFromFile(filename);
00984 }else{
00985 cap = cvCaptureFromCAM(cam_index);
00986 }
00987
00988 int top = option_find_int(options, "top", 1);
00989
00990 char *name_list = option_find_str(options, "names", 0);
00991 char **names = get_labels(name_list);
00992
00993 int *indexes = calloc(top, sizeof(int));
00994
00995 if(!cap) error("Couldn't connect to webcam.\n");
00996 cvNamedWindow("Threat Detection", CV_WINDOW_NORMAL);
00997 cvResizeWindow("Threat Detection", 512, 512);
00998 float fps = 0;
00999 int i;
01000
01001 while(1){
01002 struct timeval tval_before, tval_after, tval_result;
01003 gettimeofday(&tval_before, NULL);
01004
01005 image in = get_image_from_stream(cap);
01006 image in_s = resize_image(in, net.w, net.h);
01007 show_image(in, "Threat Detection");
01008
01009 float *predictions = network_predict(net, in_s.data);
01010 top_predictions(net, top, indexes);
01011
01012 printf("\033[2J");
01013 printf("\033[1;1H");
01014
01015 int threat = 0;
01016 for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){
01017 int index = bad_cats[i];
01018 if(predictions[index] > .01){
01019 printf("Threat Detected!\n");
01020 threat = 1;
01021 break;
01022 }
01023 }
01024 if(!threat) printf("Scanning...\n");
01025 for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){
01026 int index = bad_cats[i];
01027 if(predictions[index] > .01){
01028 printf("%s\n", names[index]);
01029 }
01030 }
01031
01032 free_image(in_s);
01033 free_image(in);
01034
01035 cvWaitKey(10);
01036
01037 gettimeofday(&tval_after, NULL);
01038 timersub(&tval_after, &tval_before, &tval_result);
01039 float curr = 1000000.f/((long int)tval_result.tv_usec);
01040 fps = .9*fps + .1*curr;
01041 }
01042 #endif
01043 }
01044
01045 void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
01046 {
01047 #ifdef OPENCV
01048 printf("Classifier Demo\n");
01049 network net = parse_network_cfg(cfgfile);
01050 if(weightfile){
01051 load_weights(&net, weightfile);
01052 }
01053 set_batch_network(&net, 1);
01054 list *options = read_data_cfg(datacfg);
01055
01056 srand(2222222);
01057 CvCapture * cap;
01058
01059 if(filename){
01060 cap = cvCaptureFromFile(filename);
01061 }else{
01062 cap = cvCaptureFromCAM(cam_index);
01063 }
01064
01065 int top = option_find_int(options, "top", 1);
01066
01067 char *name_list = option_find_str(options, "names", 0);
01068 char **names = get_labels(name_list);
01069
01070 int *indexes = calloc(top, sizeof(int));
01071
01072 if(!cap) error("Couldn't connect to webcam.\n");
01073 cvNamedWindow("Classifier", CV_WINDOW_NORMAL);
01074 cvResizeWindow("Classifier", 512, 512);
01075 float fps = 0;
01076 int i;
01077
01078 while(1){
01079 struct timeval tval_before, tval_after, tval_result;
01080 gettimeofday(&tval_before, NULL);
01081
01082 image in = get_image_from_stream(cap);
01083 image in_s = resize_image(in, net.w, net.h);
01084 show_image(in, "Classifier");
01085
01086 float *predictions = network_predict(net, in_s.data);
01087 if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 1);
01088 top_predictions(net, top, indexes);
01089
01090 printf("\033[2J");
01091 printf("\033[1;1H");
01092 printf("\nFPS:%.0f\n",fps);
01093
01094 for(i = 0; i < top; ++i){
01095 int index = indexes[i];
01096 printf("%.1f%%: %s\n", predictions[index]*100, names[index]);
01097 }
01098
01099 free_image(in_s);
01100 free_image(in);
01101
01102 cvWaitKey(10);
01103
01104 gettimeofday(&tval_after, NULL);
01105 timersub(&tval_after, &tval_before, &tval_result);
01106 float curr = 1000000.f/((long int)tval_result.tv_usec);
01107 fps = .9*fps + .1*curr;
01108 }
01109 #endif
01110 }
01111
01112
01113 void run_classifier(int argc, char **argv)
01114 {
01115 if(argc < 4){
01116 fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
01117 return;
01118 }
01119
01120 char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
01121 int *gpus = 0;
01122 int gpu = 0;
01123 int ngpus = 0;
01124 if(gpu_list){
01125 printf("%s\n", gpu_list);
01126 int len = strlen(gpu_list);
01127 ngpus = 1;
01128 int i;
01129 for(i = 0; i < len; ++i){
01130 if (gpu_list[i] == ',') ++ngpus;
01131 }
01132 gpus = calloc(ngpus, sizeof(int));
01133 for(i = 0; i < ngpus; ++i){
01134 gpus[i] = atoi(gpu_list);
01135 gpu_list = strchr(gpu_list, ',')+1;
01136 }
01137 } else {
01138 gpu = gpu_index;
01139 gpus = &gpu;
01140 ngpus = 1;
01141 }
01142
01143 int cam_index = find_int_arg(argc, argv, "-c", 0);
01144 int top = find_int_arg(argc, argv, "-t", 0);
01145 int clear = find_arg(argc, argv, "-clear");
01146 char *data = argv[3];
01147 char *cfg = argv[4];
01148 char *weights = (argc > 5) ? argv[5] : 0;
01149 char *filename = (argc > 6) ? argv[6]: 0;
01150 char *layer_s = (argc > 7) ? argv[7]: 0;
01151 int layer = layer_s ? atoi(layer_s) : -1;
01152 if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename, top);
01153 else if(0==strcmp(argv[2], "try")) try_classifier(data, cfg, weights, filename, atoi(layer_s));
01154 else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights, gpus, ngpus, clear);
01155 else if(0==strcmp(argv[2], "demo")) demo_classifier(data, cfg, weights, cam_index, filename);
01156 else if(0==strcmp(argv[2], "gun")) gun_classifier(data, cfg, weights, cam_index, filename);
01157 else if(0==strcmp(argv[2], "threat")) threat_classifier(data, cfg, weights, cam_index, filename);
01158 else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights, layer);
01159 else if(0==strcmp(argv[2], "label")) label_classifier(data, cfg, weights);
01160 else if(0==strcmp(argv[2], "valid")) validate_classifier_single(data, cfg, weights);
01161 else if(0==strcmp(argv[2], "validmulti")) validate_classifier_multi(data, cfg, weights);
01162 else if(0==strcmp(argv[2], "valid10")) validate_classifier_10(data, cfg, weights);
01163 else if(0==strcmp(argv[2], "validcrop")) validate_classifier_crop(data, cfg, weights);
01164 else if(0==strcmp(argv[2], "validfull")) validate_classifier_full(data, cfg, weights);
01165 }
01166
01167