1 #include "network.h"
2 #include "utils.h"
3 #include "parser.h"
4 #include "option_list.h"
5 #include "blas.h"
6 #include "assert.h"
7 #include "classifier.h"
8 #include "dark_cuda.h"
9 #ifdef WIN32
10 #include <time.h>
11 #include "gettimeofday.h"
12 #else
13 #include <sys/time.h>
14 #endif
15
16 float validate_classifier_single(char *datacfg, char *filename, char *weightfile, network *existing_net, int topk_custom);
17
get_regression_values(char ** labels,int n)18 float *get_regression_values(char **labels, int n)
19 {
20 float* v = (float*)xcalloc(n, sizeof(float));
21 int i;
22 for(i = 0; i < n; ++i){
23 char *p = strchr(labels[i], ' ');
24 *p = 0;
25 v[i] = atof(p+1);
26 }
27 return v;
28 }
29
train_classifier(char * datacfg,char * cfgfile,char * weightfile,int * gpus,int ngpus,int clear,int dontuse_opencv,int dont_show,int mjpeg_port,int calc_topk,int show_imgs,char * chart_path)30 void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear, int dontuse_opencv, int dont_show, int mjpeg_port, int calc_topk, int show_imgs, char* chart_path)
31 {
32 int i;
33
34 float avg_loss = -1;
35 char *base = basecfg(cfgfile);
36 printf("%s\n", base);
37 printf("%d\n", ngpus);
38 network* nets = (network*)xcalloc(ngpus, sizeof(network));
39
40 srand(time(0));
41 int seed = rand();
42 for(i = 0; i < ngpus; ++i){
43 srand(seed);
44 #ifdef GPU
45 cuda_set_device(gpus[i]);
46 #endif
47 nets[i] = parse_network_cfg(cfgfile);
48 if(weightfile){
49 load_weights(&nets[i], weightfile);
50 }
51 if (clear) {
52 *nets[i].seen = 0;
53 *nets[i].cur_iteration = 0;
54 }
55 nets[i].learning_rate *= ngpus;
56 }
57 srand(time(0));
58 network net = nets[0];
59
60 int imgs = net.batch * net.subdivisions * ngpus;
61
62 printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
63 list *options = read_data_cfg(datacfg);
64
65 char *backup_directory = option_find_str(options, "backup", "/backup/");
66 char *label_list = option_find_str(options, "labels", "data/labels.list");
67 char *train_list = option_find_str(options, "train", "data/train.list");
68 int classes = option_find_int(options, "classes", 2);
69 int topk_data = option_find_int(options, "top", 5);
70 char topk_buff[10];
71 sprintf(topk_buff, "top%d", topk_data);
72 if (classes != net.layers[net.n - 1].inputs) {
73 printf("\n Error: num of filters = %d in the last conv-layer in cfg-file doesn't match to classes = %d in data-file \n",
74 net.layers[net.n - 1].inputs, classes);
75 getchar();
76 }
77
78 char **labels = get_labels(label_list);
79 list *plist = get_paths(train_list);
80 char **paths = (char **)list_to_array(plist);
81 printf("%d\n", plist->size);
82 int train_images_num = plist->size;
83 clock_t time;
84
85 load_args args = {0};
86 args.w = net.w;
87 args.h = net.h;
88 args.c = net.c;
89 args.threads = 32;
90 args.hierarchy = net.hierarchy;
91
92 args.dontuse_opencv = dontuse_opencv;
93 args.min = net.min_crop;
94 args.max = net.max_crop;
95 args.flip = net.flip;
96 args.blur = net.blur;
97 args.angle = net.angle;
98 args.aspect = net.aspect;
99 args.exposure = net.exposure;
100 args.saturation = net.saturation;
101 args.hue = net.hue;
102 args.size = net.w > net.h ? net.w : net.h;
103
104 args.label_smooth_eps = net.label_smooth_eps;
105 args.mixup = net.mixup;
106 if (dont_show && show_imgs) show_imgs = 2;
107 args.show_imgs = show_imgs;
108
109 args.paths = paths;
110 args.classes = classes;
111 args.n = imgs;
112 args.m = train_images_num;
113 args.labels = labels;
114 args.type = CLASSIFICATION_DATA;
115
116 #ifdef OPENCV
117 //args.threads = 3;
118 mat_cv* img = NULL;
119 float max_img_loss = 10;
120 int number_of_lines = 100;
121 int img_size = 1000;
122 char windows_name[100];
123 sprintf(windows_name, "chart_%s.png", base);
124 if (!dontuse_opencv) img = draw_train_chart(windows_name, max_img_loss, net.max_batches, number_of_lines, img_size, dont_show, chart_path);
125 #endif //OPENCV
126
127 data train;
128 data buffer;
129 pthread_t load_thread;
130 args.d = &buffer;
131 load_thread = load_data(args);
132
133 int iter_save = get_current_batch(net);
134 int iter_save_last = get_current_batch(net);
135 int iter_topk = get_current_batch(net);
136 float topk = 0;
137
138 int count = 0;
139 double start, time_remaining, avg_time = -1, alpha_time = 0.01;
140 start = what_time_is_it_now();
141
142 while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
143 time=clock();
144
145 pthread_join(load_thread, 0);
146 train = buffer;
147 load_thread = load_data(args);
148
149 printf("Loaded: %lf seconds\n", sec(clock()-time));
150 time=clock();
151
152 float loss = 0;
153 #ifdef GPU
154 if(ngpus == 1){
155 loss = train_network(net, train);
156 } else {
157 loss = train_networks(nets, ngpus, train, 4);
158 }
159 #else
160 loss = train_network(net, train);
161 #endif
162 if(avg_loss == -1 || isnan(avg_loss) || isinf(avg_loss)) avg_loss = loss;
163 avg_loss = avg_loss*.9 + loss*.1;
164
165 i = get_current_batch(net);
166
167 int calc_topk_for_each = iter_topk + 2 * train_images_num / (net.batch * net.subdivisions); // calculate TOPk for each 2 Epochs
168 calc_topk_for_each = fmax(calc_topk_for_each, net.burn_in);
169 calc_topk_for_each = fmax(calc_topk_for_each, 100);
170 if (i % 10 == 0) {
171 if (calc_topk) {
172 fprintf(stderr, "\n (next TOP%d calculation at %d iterations) ", topk_data, calc_topk_for_each);
173 if (topk > 0) fprintf(stderr, " Last accuracy TOP%d = %2.2f %% \n", topk_data, topk * 100);
174 }
175
176 if (net.cudnn_half) {
177 if (i < net.burn_in * 3) fprintf(stderr, " Tensor Cores are disabled until the first %d iterations are reached.\n", 3 * net.burn_in);
178 else fprintf(stderr, " Tensor Cores are used.\n");
179 }
180 }
181
182 int draw_precision = 0;
183 if (calc_topk && (i >= calc_topk_for_each || i == net.max_batches)) {
184 iter_topk = i;
185 topk = validate_classifier_single(datacfg, cfgfile, weightfile, &net, topk_data); // calc TOP-n
186 printf("\n accuracy %s = %f \n", topk_buff, topk);
187 draw_precision = 1;
188 }
189
190 time_remaining = ((net.max_batches - i) / ngpus) * (what_time_is_it_now() - start) / 60 / 60;
191 // set initial value, even if resume training from 10000 iteration
192 if (avg_time < 0) avg_time = time_remaining;
193 else avg_time = alpha_time * time_remaining + (1 - alpha_time) * avg_time;
194 start = what_time_is_it_now();
195 printf("%d, %.3f: %f, %f avg, %f rate, %lf seconds, %ld images, %f hours left\n", get_current_batch(net), (float)(*net.seen)/ train_images_num, loss, avg_loss, get_current_rate(net), sec(clock()-time), *net.seen, avg_time);
196 #ifdef OPENCV
197 if (!dontuse_opencv) draw_train_loss(windows_name, img, img_size, avg_loss, max_img_loss, i, net.max_batches, topk, draw_precision, topk_buff, dont_show, mjpeg_port, avg_time);
198 #endif // OPENCV
199
200 if (i >= (iter_save + 1000)) {
201 iter_save = i;
202 #ifdef GPU
203 if (ngpus != 1) sync_nets(nets, ngpus, 0);
204 #endif
205 char buff[256];
206 sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
207 save_weights(net, buff);
208 }
209
210 if (i >= (iter_save_last + 100)) {
211 iter_save_last = i;
212 #ifdef GPU
213 if (ngpus != 1) sync_nets(nets, ngpus, 0);
214 #endif
215 char buff[256];
216 sprintf(buff, "%s/%s_last.weights", backup_directory, base);
217 save_weights(net, buff);
218 }
219 free_data(train);
220 }
221 #ifdef GPU
222 if (ngpus != 1) sync_nets(nets, ngpus, 0);
223 #endif
224 char buff[256];
225 sprintf(buff, "%s/%s_final.weights", backup_directory, base);
226 save_weights(net, buff);
227
228 #ifdef OPENCV
229 release_mat(&img);
230 destroy_all_windows_cv();
231 #endif
232
233 pthread_join(load_thread, 0);
234 free_data(buffer);
235
236 //free_network(net);
237 for (i = 0; i < ngpus; ++i) free_network(nets[i]);
238 free(nets);
239
240 //free_ptrs((void**)labels, classes);
241 free(labels);
242 free_ptrs((void**)paths, plist->size);
243 free_list(plist);
244 free(nets);
245 free(base);
246
247 free_list_contents_kvp(options);
248 free_list(options);
249
250 }
251
252
253 /*
254 void train_classifier(char *datacfg, char *cfgfile, char *weightfile, int clear)
255 {
256 srand(time(0));
257 float avg_loss = -1;
258 char *base = basecfg(cfgfile);
259 printf("%s\n", base);
260 network net = parse_network_cfg(cfgfile);
261 if(weightfile){
262 load_weights(&net, weightfile);
263 }
264 if(clear) *net.seen = 0;
265
266 int imgs = net.batch * net.subdivisions;
267
268 printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
269 list *options = read_data_cfg(datacfg);
270
271 char *backup_directory = option_find_str(options, "backup", "/backup/");
272 char *label_list = option_find_str(options, "labels", "data/labels.list");
273 char *train_list = option_find_str(options, "train", "data/train.list");
274 int classes = option_find_int(options, "classes", 2);
275
276 char **labels = get_labels(label_list);
277 list *plist = get_paths(train_list);
278 char **paths = (char **)list_to_array(plist);
279 printf("%d\n", plist->size);
280 int N = plist->size;
281 clock_t time;
282
283 load_args args = {0};
284 args.w = net.w;
285 args.h = net.h;
286 args.threads = 8;
287
288 args.min = net.min_crop;
289 args.max = net.max_crop;
290 args.flip = net.flip;
291 args.angle = net.angle;
292 args.aspect = net.aspect;
293 args.exposure = net.exposure;
294 args.saturation = net.saturation;
295 args.hue = net.hue;
296 args.size = net.w;
297 args.hierarchy = net.hierarchy;
298
299 args.paths = paths;
300 args.classes = classes;
301 args.n = imgs;
302 args.m = N;
303 args.labels = labels;
304 args.type = CLASSIFICATION_DATA;
305
306 data train;
307 data buffer;
308 pthread_t load_thread;
309 args.d = &buffer;
310 load_thread = load_data(args);
311
312 int epoch = (*net.seen)/N;
313 while(get_current_batch(net) < net.max_batches || net.max_batches == 0){
314 time=clock();
315
316 pthread_join(load_thread, 0);
317 train = buffer;
318 load_thread = load_data(args);
319
320 printf("Loaded: %lf seconds\n", sec(clock()-time));
321 time=clock();
322
323 #ifdef OPENCV
324 if(0){
325 int u;
326 for(u = 0; u < imgs; ++u){
327 image im = float_to_image(net.w, net.h, 3, train.X.vals[u]);
328 show_image(im, "loaded");
329 cvWaitKey(0);
330 }
331 }
332 #endif
333
334 float loss = train_network(net, train);
335 free_data(train);
336
337 if(avg_loss == -1) avg_loss = loss;
338 avg_loss = avg_loss*.9 + loss*.1;
339 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);
340 if(*net.seen/N > epoch){
341 epoch = *net.seen/N;
342 char buff[256];
343 sprintf(buff, "%s/%s_%d.weights",backup_directory,base, epoch);
344 save_weights(net, buff);
345 }
346 if(get_current_batch(net)%100 == 0){
347 char buff[256];
348 sprintf(buff, "%s/%s.backup",backup_directory,base);
349 save_weights(net, buff);
350 }
351 }
352 char buff[256];
353 sprintf(buff, "%s/%s.weights", backup_directory, base);
354 save_weights(net, buff);
355
356 free_network(net);
357 free_ptrs((void**)labels, classes);
358 free_ptrs((void**)paths, plist->size);
359 free_list(plist);
360 free(base);
361 }
362 */
363
validate_classifier_crop(char * datacfg,char * filename,char * weightfile)364 void validate_classifier_crop(char *datacfg, char *filename, char *weightfile)
365 {
366 int i = 0;
367 network net = parse_network_cfg(filename);
368 if(weightfile){
369 load_weights(&net, weightfile);
370 }
371 srand(time(0));
372
373 list *options = read_data_cfg(datacfg);
374
375 char *label_list = option_find_str(options, "labels", "data/labels.list");
376 char *valid_list = option_find_str(options, "valid", "data/train.list");
377 int classes = option_find_int(options, "classes", 2);
378 int topk = option_find_int(options, "top", 1);
379 if (topk > classes) topk = classes;
380
381 char **labels = get_labels(label_list);
382 list *plist = get_paths(valid_list);
383
384 char **paths = (char **)list_to_array(plist);
385 int m = plist->size;
386 free_list(plist);
387
388 clock_t time;
389 float avg_acc = 0;
390 float avg_topk = 0;
391 int splits = m/1000;
392 int num = (i+1)*m/splits - i*m/splits;
393
394 data val, buffer;
395
396 load_args args = {0};
397 args.w = net.w;
398 args.h = net.h;
399
400 args.paths = paths;
401 args.classes = classes;
402 args.n = num;
403 args.m = 0;
404 args.labels = labels;
405 args.d = &buffer;
406 args.type = OLD_CLASSIFICATION_DATA;
407
408 pthread_t load_thread = load_data_in_thread(args);
409 for(i = 1; i <= splits; ++i){
410 time=clock();
411
412 pthread_join(load_thread, 0);
413 val = buffer;
414
415 num = (i+1)*m/splits - i*m/splits;
416 char **part = paths+(i*m/splits);
417 if(i != splits){
418 args.paths = part;
419 load_thread = load_data_in_thread(args);
420 }
421 printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
422
423 time=clock();
424 float *acc = network_accuracies(net, val, topk);
425 avg_acc += acc[0];
426 avg_topk += acc[1];
427 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);
428 free_data(val);
429 }
430 }
431
validate_classifier_10(char * datacfg,char * filename,char * weightfile)432 void validate_classifier_10(char *datacfg, char *filename, char *weightfile)
433 {
434 int i, j;
435 network net = parse_network_cfg(filename);
436 set_batch_network(&net, 1);
437 if(weightfile){
438 load_weights(&net, weightfile);
439 }
440 srand(time(0));
441
442 list *options = read_data_cfg(datacfg);
443
444 char *label_list = option_find_str(options, "labels", "data/labels.list");
445 char *valid_list = option_find_str(options, "valid", "data/train.list");
446 int classes = option_find_int(options, "classes", 2);
447 int topk = option_find_int(options, "top", 1);
448 if (topk > classes) topk = classes;
449
450 char **labels = get_labels(label_list);
451 list *plist = get_paths(valid_list);
452
453 char **paths = (char **)list_to_array(plist);
454 int m = plist->size;
455 free_list(plist);
456
457 float avg_acc = 0;
458 float avg_topk = 0;
459 int* indexes = (int*)xcalloc(topk, sizeof(int));
460
461 for(i = 0; i < m; ++i){
462 int class_id = -1;
463 char *path = paths[i];
464 for(j = 0; j < classes; ++j){
465 if(strstr(path, labels[j])){
466 class_id = j;
467 break;
468 }
469 }
470 int w = net.w;
471 int h = net.h;
472 int shift = 32;
473 image im = load_image_color(paths[i], w+shift, h+shift);
474 image images[10];
475 images[0] = crop_image(im, -shift, -shift, w, h);
476 images[1] = crop_image(im, shift, -shift, w, h);
477 images[2] = crop_image(im, 0, 0, w, h);
478 images[3] = crop_image(im, -shift, shift, w, h);
479 images[4] = crop_image(im, shift, shift, w, h);
480 flip_image(im);
481 images[5] = crop_image(im, -shift, -shift, w, h);
482 images[6] = crop_image(im, shift, -shift, w, h);
483 images[7] = crop_image(im, 0, 0, w, h);
484 images[8] = crop_image(im, -shift, shift, w, h);
485 images[9] = crop_image(im, shift, shift, w, h);
486 float* pred = (float*)xcalloc(classes, sizeof(float));
487 for(j = 0; j < 10; ++j){
488 float *p = network_predict(net, images[j].data);
489 if(net.hierarchy) hierarchy_predictions(p, net.outputs, net.hierarchy, 1);
490 axpy_cpu(classes, 1, p, 1, pred, 1);
491 free_image(images[j]);
492 }
493 free_image(im);
494 top_k(pred, classes, topk, indexes);
495 free(pred);
496 if(indexes[0] == class_id) avg_acc += 1;
497 for(j = 0; j < topk; ++j){
498 if(indexes[j] == class_id) avg_topk += 1;
499 }
500
501 printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
502 }
503 free(indexes);
504 }
505
validate_classifier_full(char * datacfg,char * filename,char * weightfile)506 void validate_classifier_full(char *datacfg, char *filename, char *weightfile)
507 {
508 int i, j;
509 network net = parse_network_cfg(filename);
510 set_batch_network(&net, 1);
511 if(weightfile){
512 load_weights(&net, weightfile);
513 }
514 srand(time(0));
515
516 list *options = read_data_cfg(datacfg);
517
518 char *label_list = option_find_str(options, "labels", "data/labels.list");
519 char *valid_list = option_find_str(options, "valid", "data/train.list");
520 int classes = option_find_int(options, "classes", 2);
521 int topk = option_find_int(options, "top", 1);
522 if (topk > classes) topk = classes;
523
524 char **labels = get_labels(label_list);
525 list *plist = get_paths(valid_list);
526
527 char **paths = (char **)list_to_array(plist);
528 int m = plist->size;
529 free_list(plist);
530
531 float avg_acc = 0;
532 float avg_topk = 0;
533 int* indexes = (int*)xcalloc(topk, sizeof(int));
534
535 int size = net.w;
536 for(i = 0; i < m; ++i){
537 int class_id = -1;
538 char *path = paths[i];
539 for(j = 0; j < classes; ++j){
540 if(strstr(path, labels[j])){
541 class_id = j;
542 break;
543 }
544 }
545 image im = load_image_color(paths[i], 0, 0);
546 image resized = resize_min(im, size);
547 resize_network(&net, resized.w, resized.h);
548 //show_image(im, "orig");
549 //show_image(crop, "cropped");
550 //cvWaitKey(0);
551 float *pred = network_predict(net, resized.data);
552 if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1);
553
554 free_image(im);
555 free_image(resized);
556 top_k(pred, classes, topk, indexes);
557
558 if(indexes[0] == class_id) avg_acc += 1;
559 for(j = 0; j < topk; ++j){
560 if(indexes[j] == class_id) avg_topk += 1;
561 }
562
563 printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
564 }
565 free(indexes);
566 }
567
568
validate_classifier_single(char * datacfg,char * filename,char * weightfile,network * existing_net,int topk_custom)569 float validate_classifier_single(char *datacfg, char *filename, char *weightfile, network *existing_net, int topk_custom)
570 {
571 int i, j;
572 network net;
573 int old_batch = -1;
574 if (existing_net) {
575 net = *existing_net; // for validation during training
576 old_batch = net.batch;
577 set_batch_network(&net, 1);
578 }
579 else {
580 net = parse_network_cfg_custom(filename, 1, 0);
581 if (weightfile) {
582 load_weights(&net, weightfile);
583 }
584 //set_batch_network(&net, 1);
585 fuse_conv_batchnorm(net);
586 calculate_binary_weights(net);
587 }
588 srand(time(0));
589
590 list *options = read_data_cfg(datacfg);
591
592 char *label_list = option_find_str(options, "labels", "data/labels.list");
593 char *leaf_list = option_find_str(options, "leaves", 0);
594 if(leaf_list) change_leaves(net.hierarchy, leaf_list);
595 char *valid_list = option_find_str(options, "valid", "data/train.list");
596 int classes = option_find_int(options, "classes", 2);
597 int topk = option_find_int(options, "top", 1);
598 if (topk_custom > 0) topk = topk_custom; // for validation during training
599 if (topk > classes) topk = classes;
600 printf(" TOP calculation...\n");
601
602 char **labels = get_labels(label_list);
603 list *plist = get_paths(valid_list);
604
605 char **paths = (char **)list_to_array(plist);
606 int m = plist->size;
607 free_list(plist);
608
609 float avg_acc = 0;
610 float avg_topk = 0;
611 int* indexes = (int*)xcalloc(topk, sizeof(int));
612
613 for(i = 0; i < m; ++i){
614 int class_id = -1;
615 char *path = paths[i];
616 for(j = 0; j < classes; ++j){
617 if(strstr(path, labels[j])){
618 class_id = j;
619 break;
620 }
621 }
622 image im = load_image_color(paths[i], 0, 0);
623 image resized = resize_min(im, net.w);
624 image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
625 //show_image(im, "orig");
626 //show_image(crop, "cropped");
627 //cvWaitKey(0);
628 float *pred = network_predict(net, crop.data);
629 if(net.hierarchy) hierarchy_predictions(pred, net.outputs, net.hierarchy, 1);
630
631 if(resized.data != im.data) free_image(resized);
632 free_image(im);
633 free_image(crop);
634 top_k(pred, classes, topk, indexes);
635
636 if(indexes[0] == class_id) avg_acc += 1;
637 for(j = 0; j < topk; ++j){
638 if(indexes[j] == class_id) avg_topk += 1;
639 }
640
641 if (existing_net) printf("\r");
642 else printf("\n");
643 printf("%d: top 1: %f, top %d: %f", i, avg_acc/(i+1), topk, avg_topk/(i+1));
644 }
645 free(indexes);
646 if (existing_net) {
647 set_batch_network(&net, old_batch);
648 }
649 float topk_result = avg_topk / i;
650 return topk_result;
651 }
652
validate_classifier_multi(char * datacfg,char * filename,char * weightfile)653 void validate_classifier_multi(char *datacfg, char *filename, char *weightfile)
654 {
655 int i, j;
656 network net = parse_network_cfg(filename);
657 set_batch_network(&net, 1);
658 if(weightfile){
659 load_weights(&net, weightfile);
660 }
661 srand(time(0));
662
663 list *options = read_data_cfg(datacfg);
664
665 char *label_list = option_find_str(options, "labels", "data/labels.list");
666 char *valid_list = option_find_str(options, "valid", "data/train.list");
667 int classes = option_find_int(options, "classes", 2);
668 int topk = option_find_int(options, "top", 1);
669 if (topk > classes) topk = classes;
670
671 char **labels = get_labels(label_list);
672 list *plist = get_paths(valid_list);
673 int scales[] = {224, 288, 320, 352, 384};
674 int nscales = sizeof(scales)/sizeof(scales[0]);
675
676 char **paths = (char **)list_to_array(plist);
677 int m = plist->size;
678 free_list(plist);
679
680 float avg_acc = 0;
681 float avg_topk = 0;
682 int* indexes = (int*)xcalloc(topk, sizeof(int));
683
684 for(i = 0; i < m; ++i){
685 int class_id = -1;
686 char *path = paths[i];
687 for(j = 0; j < classes; ++j){
688 if(strstr(path, labels[j])){
689 class_id = j;
690 break;
691 }
692 }
693 float* pred = (float*)xcalloc(classes, sizeof(float));
694 image im = load_image_color(paths[i], 0, 0);
695 for(j = 0; j < nscales; ++j){
696 image r = resize_min(im, scales[j]);
697 resize_network(&net, r.w, r.h);
698 float *p = network_predict(net, r.data);
699 if(net.hierarchy) hierarchy_predictions(p, net.outputs, net.hierarchy, 1);
700 axpy_cpu(classes, 1, p, 1, pred, 1);
701 flip_image(r);
702 p = network_predict(net, r.data);
703 axpy_cpu(classes, 1, p, 1, pred, 1);
704 if(r.data != im.data) free_image(r);
705 }
706 free_image(im);
707 top_k(pred, classes, topk, indexes);
708 free(pred);
709 if(indexes[0] == class_id) avg_acc += 1;
710 for(j = 0; j < topk; ++j){
711 if(indexes[j] == class_id) avg_topk += 1;
712 }
713
714 printf("%d: top 1: %f, top %d: %f\n", i, avg_acc/(i+1), topk, avg_topk/(i+1));
715 }
716 free(indexes);
717 }
718
try_classifier(char * datacfg,char * cfgfile,char * weightfile,char * filename,int layer_num)719 void try_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int layer_num)
720 {
721 network net = parse_network_cfg_custom(cfgfile, 1, 0);
722 if(weightfile){
723 load_weights(&net, weightfile);
724 }
725 set_batch_network(&net, 1);
726 srand(2222222);
727
728 list *options = read_data_cfg(datacfg);
729
730 char *name_list = option_find_str(options, "names", 0);
731 if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
732 int classes = option_find_int(options, "classes", 2);
733 int top = option_find_int(options, "top", 1);
734 if (top > classes) top = classes;
735
736 char **names = get_labels(name_list);
737 clock_t time;
738 int* indexes = (int*)xcalloc(top, sizeof(int));
739 char buff[256];
740 char *input = buff;
741 while(1){
742 if(filename){
743 strncpy(input, filename, 256);
744 }else{
745 printf("Enter Image Path: ");
746 fflush(stdout);
747 input = fgets(input, 256, stdin);
748 if(!input) break;
749 strtok(input, "\n");
750 }
751 image orig = load_image_color(input, 0, 0);
752 image r = resize_min(orig, 256);
753 image im = crop_image(r, (r.w - 224 - 1)/2 + 1, (r.h - 224 - 1)/2 + 1, 224, 224);
754 float mean[] = {0.48263312050943, 0.45230225481413, 0.40099074308742};
755 float std[] = {0.22590347483426, 0.22120921437787, 0.22103996251583};
756 float var[3];
757 var[0] = std[0]*std[0];
758 var[1] = std[1]*std[1];
759 var[2] = std[2]*std[2];
760
761 normalize_cpu(im.data, mean, var, 1, 3, im.w*im.h);
762
763 float *X = im.data;
764 time=clock();
765 float *predictions = network_predict(net, X);
766
767 layer l = net.layers[layer_num];
768 int i;
769 for(i = 0; i < l.c; ++i){
770 if(l.rolling_mean) printf("%f %f %f\n", l.rolling_mean[i], l.rolling_variance[i], l.scales[i]);
771 }
772 #ifdef GPU
773 cuda_pull_array(l.output_gpu, l.output, l.outputs);
774 #endif
775 for(i = 0; i < l.outputs; ++i){
776 printf("%f\n", l.output[i]);
777 }
778 /*
779
780 printf("\n\nWeights\n");
781 for(i = 0; i < l.n*l.size*l.size*l.c; ++i){
782 printf("%f\n", l.filters[i]);
783 }
784
785 printf("\n\nBiases\n");
786 for(i = 0; i < l.n; ++i){
787 printf("%f\n", l.biases[i]);
788 }
789 */
790
791 top_predictions(net, top, indexes);
792 printf("%s: Predicted in %f seconds.\n", input, sec(clock()-time));
793 for(i = 0; i < top; ++i){
794 int index = indexes[i];
795 printf("%s: %f\n", names[index], predictions[index]);
796 }
797 free_image(im);
798 if (filename) break;
799 }
800 free(indexes);
801 }
802
predict_classifier(char * datacfg,char * cfgfile,char * weightfile,char * filename,int top)803 void predict_classifier(char *datacfg, char *cfgfile, char *weightfile, char *filename, int top)
804 {
805 network net = parse_network_cfg_custom(cfgfile, 1, 0);
806 if(weightfile){
807 load_weights(&net, weightfile);
808 }
809 set_batch_network(&net, 1);
810 srand(2222222);
811
812 fuse_conv_batchnorm(net);
813 calculate_binary_weights(net);
814
815 list *options = read_data_cfg(datacfg);
816
817 char *name_list = option_find_str(options, "names", 0);
818 if(!name_list) name_list = option_find_str(options, "labels", "data/labels.list");
819 int classes = option_find_int(options, "classes", 2);
820 printf(" classes = %d, output in cfg = %d \n", classes, net.layers[net.n - 1].c);
821 if (classes != net.layers[net.n - 1].inputs) {
822 printf("\n Error: num of filters = %d in the last conv-layer in cfg-file doesn't match to classes = %d in data-file \n",
823 net.layers[net.n - 1].inputs, classes);
824 getchar();
825 }
826 if (top == 0) top = option_find_int(options, "top", 1);
827 if (top > classes) top = classes;
828
829 int i = 0;
830 char **names = get_labels(name_list);
831 clock_t time;
832 int* indexes = (int*)xcalloc(top, sizeof(int));
833 char buff[256];
834 char *input = buff;
835 //int size = net.w;
836 while(1){
837 if(filename){
838 strncpy(input, filename, 256);
839 }else{
840 printf("Enter Image Path: ");
841 fflush(stdout);
842 input = fgets(input, 256, stdin);
843 if(!input) break;
844 strtok(input, "\n");
845 }
846 image im = load_image_color(input, 0, 0);
847 image resized = resize_min(im, net.w);
848 image r = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
849 //image r = resize_min(im, size);
850 //resize_network(&net, r.w, r.h);
851 printf("%d %d\n", r.w, r.h);
852
853 float *X = r.data;
854
855 double time = get_time_point();
856 float *predictions = network_predict(net, X);
857 printf("%s: Predicted in %lf milli-seconds.\n", input, ((double)get_time_point() - time) / 1000);
858
859 if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 0);
860 top_k(predictions, net.outputs, top, indexes);
861
862 for(i = 0; i < top; ++i){
863 int index = indexes[i];
864 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");
865 else printf("%s: %f\n",names[index], predictions[index]);
866 }
867 if(r.data != im.data) free_image(r);
868 free_image(im);
869 free_image(resized);
870 if (filename) break;
871 }
872 free(indexes);
873 free_network(net);
874 free_list_contents_kvp(options);
875 free_list(options);
876 }
877
878
label_classifier(char * datacfg,char * filename,char * weightfile)879 void label_classifier(char *datacfg, char *filename, char *weightfile)
880 {
881 int i;
882 network net = parse_network_cfg(filename);
883 set_batch_network(&net, 1);
884 if(weightfile){
885 load_weights(&net, weightfile);
886 }
887 srand(time(0));
888
889 list *options = read_data_cfg(datacfg);
890
891 char *label_list = option_find_str(options, "names", "data/labels.list");
892 char *test_list = option_find_str(options, "test", "data/train.list");
893 int classes = option_find_int(options, "classes", 2);
894
895 char **labels = get_labels(label_list);
896 list *plist = get_paths(test_list);
897
898 char **paths = (char **)list_to_array(plist);
899 int m = plist->size;
900 free_list(plist);
901
902 for(i = 0; i < m; ++i){
903 image im = load_image_color(paths[i], 0, 0);
904 image resized = resize_min(im, net.w);
905 image crop = crop_image(resized, (resized.w - net.w)/2, (resized.h - net.h)/2, net.w, net.h);
906 float *pred = network_predict(net, crop.data);
907
908 if(resized.data != im.data) free_image(resized);
909 free_image(im);
910 free_image(crop);
911 int ind = max_index(pred, classes);
912
913 printf("%s\n", labels[ind]);
914 }
915 }
916
917
test_classifier(char * datacfg,char * cfgfile,char * weightfile,int target_layer)918 void test_classifier(char *datacfg, char *cfgfile, char *weightfile, int target_layer)
919 {
920 int curr = 0;
921 network net = parse_network_cfg(cfgfile);
922 if(weightfile){
923 load_weights(&net, weightfile);
924 }
925 srand(time(0));
926 fuse_conv_batchnorm(net);
927 calculate_binary_weights(net);
928
929 list *options = read_data_cfg(datacfg);
930
931 char *test_list = option_find_str(options, "test", "data/test.list");
932 int classes = option_find_int(options, "classes", 2);
933
934 list *plist = get_paths(test_list);
935
936 char **paths = (char **)list_to_array(plist);
937 int m = plist->size;
938 free_list(plist);
939
940 clock_t time;
941
942 data val, buffer;
943
944 load_args args = {0};
945 args.w = net.w;
946 args.h = net.h;
947 args.paths = paths;
948 args.classes = classes;
949 args.n = net.batch;
950 args.m = 0;
951 args.labels = 0;
952 args.d = &buffer;
953 args.type = OLD_CLASSIFICATION_DATA;
954
955 pthread_t load_thread = load_data_in_thread(args);
956 for(curr = net.batch; curr < m; curr += net.batch){
957 time=clock();
958
959 pthread_join(load_thread, 0);
960 val = buffer;
961
962 if(curr < m){
963 args.paths = paths + curr;
964 if (curr + net.batch > m) args.n = m - curr;
965 load_thread = load_data_in_thread(args);
966 }
967 fprintf(stderr, "Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock()-time));
968
969 time=clock();
970 matrix pred = network_predict_data(net, val);
971
972 int i, j;
973 if (target_layer >= 0){
974 //layer l = net.layers[target_layer];
975 }
976
977 for(i = 0; i < pred.rows; ++i){
978 printf("%s", paths[curr-net.batch+i]);
979 for(j = 0; j < pred.cols; ++j){
980 printf("\t%g", pred.vals[i][j]);
981 }
982 printf("\n");
983 }
984
985 free_matrix(pred);
986
987 fprintf(stderr, "%lf seconds, %d images, %d total\n", sec(clock()-time), val.X.rows, curr);
988 free_data(val);
989 }
990 }
991
992
threat_classifier(char * datacfg,char * cfgfile,char * weightfile,int cam_index,const char * filename)993 void threat_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
994 {
995 #ifdef OPENCV
996 float threat = 0;
997 float roll = .2;
998
999 printf("Classifier Demo\n");
1000 network net = parse_network_cfg(cfgfile);
1001 if(weightfile){
1002 load_weights(&net, weightfile);
1003 }
1004 set_batch_network(&net, 1);
1005 list *options = read_data_cfg(datacfg);
1006
1007 srand(2222222);
1008 cap_cv * cap;
1009
1010 if (filename) {
1011 //cap = cvCaptureFromFile(filename);
1012 cap = get_capture_video_stream(filename);
1013 }
1014 else {
1015 //cap = cvCaptureFromCAM(cam_index);
1016 cap = get_capture_webcam(cam_index);
1017 }
1018
1019 int classes = option_find_int(options, "classes", 2);
1020 int top = option_find_int(options, "top", 1);
1021 if (top > classes) top = classes;
1022
1023 char *name_list = option_find_str(options, "names", 0);
1024 char **names = get_labels(name_list);
1025
1026 int* indexes = (int*)xcalloc(top, sizeof(int));
1027
1028 if(!cap) error("Couldn't connect to webcam.\n");
1029 create_window_cv("Threat", 0, 512, 512);
1030 float fps = 0;
1031 int i;
1032
1033 int count = 0;
1034
1035 while(1){
1036 ++count;
1037 struct timeval tval_before, tval_after, tval_result;
1038 gettimeofday(&tval_before, NULL);
1039
1040 //image in = get_image_from_stream(cap);
1041 image in = get_image_from_stream_cpp(cap);
1042 if(!in.data) break;
1043 image in_s = resize_image(in, net.w, net.h);
1044
1045 image out = in;
1046 int x1 = out.w / 20;
1047 int y1 = out.h / 20;
1048 int x2 = 2*x1;
1049 int y2 = out.h - out.h/20;
1050
1051 int border = .01*out.h;
1052 int h = y2 - y1 - 2*border;
1053 int w = x2 - x1 - 2*border;
1054
1055 float *predictions = network_predict(net, in_s.data);
1056 float curr_threat = 0;
1057 if(1){
1058 curr_threat = predictions[0] * 0 +
1059 predictions[1] * .6 +
1060 predictions[2];
1061 } else {
1062 curr_threat = predictions[218] +
1063 predictions[539] +
1064 predictions[540] +
1065 predictions[368] +
1066 predictions[369] +
1067 predictions[370];
1068 }
1069 threat = roll * curr_threat + (1-roll) * threat;
1070
1071 draw_box_width(out, x2 + border, y1 + .02*h, x2 + .5 * w, y1 + .02*h + border, border, 0,0,0);
1072 if(threat > .97) {
1073 draw_box_width(out, x2 + .5 * w + border,
1074 y1 + .02*h - 2*border,
1075 x2 + .5 * w + 6*border,
1076 y1 + .02*h + 3*border, 3*border, 1,0,0);
1077 }
1078 draw_box_width(out, x2 + .5 * w + border,
1079 y1 + .02*h - 2*border,
1080 x2 + .5 * w + 6*border,
1081 y1 + .02*h + 3*border, .5*border, 0,0,0);
1082 draw_box_width(out, x2 + border, y1 + .42*h, x2 + .5 * w, y1 + .42*h + border, border, 0,0,0);
1083 if(threat > .57) {
1084 draw_box_width(out, x2 + .5 * w + border,
1085 y1 + .42*h - 2*border,
1086 x2 + .5 * w + 6*border,
1087 y1 + .42*h + 3*border, 3*border, 1,1,0);
1088 }
1089 draw_box_width(out, x2 + .5 * w + border,
1090 y1 + .42*h - 2*border,
1091 x2 + .5 * w + 6*border,
1092 y1 + .42*h + 3*border, .5*border, 0,0,0);
1093
1094 draw_box_width(out, x1, y1, x2, y2, border, 0,0,0);
1095 for(i = 0; i < threat * h ; ++i){
1096 float ratio = (float) i / h;
1097 float r = (ratio < .5) ? (2*(ratio)) : 1;
1098 float g = (ratio < .5) ? 1 : 1 - 2*(ratio - .5);
1099 draw_box_width(out, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0);
1100 }
1101 top_predictions(net, top, indexes);
1102 char buff[256];
1103 sprintf(buff, "tmp/threat_%06d", count);
1104 //save_image(out, buff);
1105
1106 #ifndef _WIN32
1107 printf("\033[2J");
1108 printf("\033[1;1H");
1109 #endif
1110 printf("\nFPS:%.0f\n",fps);
1111
1112 for(i = 0; i < top; ++i){
1113 int index = indexes[i];
1114 printf("%.1f%%: %s\n", predictions[index]*100, names[index]);
1115 }
1116
1117 if(1){
1118 show_image(out, "Threat");
1119 wait_key_cv(10);
1120 }
1121 free_image(in_s);
1122 free_image(in);
1123
1124 gettimeofday(&tval_after, NULL);
1125 timersub(&tval_after, &tval_before, &tval_result);
1126 float curr = 1000000.f/((long int)tval_result.tv_usec);
1127 fps = .9*fps + .1*curr;
1128 }
1129 #endif
1130 }
1131
1132
gun_classifier(char * datacfg,char * cfgfile,char * weightfile,int cam_index,const char * filename)1133 void gun_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
1134 {
1135 #ifdef OPENCV_DISABLE
1136 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};
1137
1138 printf("Classifier Demo\n");
1139 network net = parse_network_cfg(cfgfile);
1140 if(weightfile){
1141 load_weights(&net, weightfile);
1142 }
1143 set_batch_network(&net, 1);
1144 list *options = read_data_cfg(datacfg);
1145
1146 srand(2222222);
1147 CvCapture * cap;
1148
1149 if (filename) {
1150 //cap = cvCaptureFromFile(filename);
1151 cap = get_capture_video_stream(filename);
1152 }
1153 else {
1154 //cap = cvCaptureFromCAM(cam_index);
1155 cap = get_capture_webcam(cam_index);
1156 }
1157
1158 int classes = option_find_int(options, "classes", 2);
1159 int top = option_find_int(options, "top", 1);
1160 if (top > classes) top = classes;
1161
1162 char *name_list = option_find_str(options, "names", 0);
1163 char **names = get_labels(name_list);
1164
1165 int* indexes = (int*)xcalloc(top, sizeof(int));
1166
1167 if(!cap) error("Couldn't connect to webcam.\n");
1168 cvNamedWindow("Threat Detection", CV_WINDOW_NORMAL);
1169 cvResizeWindow("Threat Detection", 512, 512);
1170 float fps = 0;
1171 int i;
1172
1173 while(1){
1174 struct timeval tval_before, tval_after, tval_result;
1175 gettimeofday(&tval_before, NULL);
1176
1177 //image in = get_image_from_stream(cap);
1178 image in = get_image_from_stream_cpp(cap);
1179 image in_s = resize_image(in, net.w, net.h);
1180 show_image(in, "Threat Detection");
1181
1182 float *predictions = network_predict(net, in_s.data);
1183 top_predictions(net, top, indexes);
1184
1185 printf("\033[2J");
1186 printf("\033[1;1H");
1187
1188 int threat = 0;
1189 for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){
1190 int index = bad_cats[i];
1191 if(predictions[index] > .01){
1192 printf("Threat Detected!\n");
1193 threat = 1;
1194 break;
1195 }
1196 }
1197 if(!threat) printf("Scanning...\n");
1198 for(i = 0; i < sizeof(bad_cats)/sizeof(bad_cats[0]); ++i){
1199 int index = bad_cats[i];
1200 if(predictions[index] > .01){
1201 printf("%s\n", names[index]);
1202 }
1203 }
1204
1205 free_image(in_s);
1206 free_image(in);
1207
1208 cvWaitKey(10);
1209
1210 gettimeofday(&tval_after, NULL);
1211 timersub(&tval_after, &tval_before, &tval_result);
1212 float curr = 1000000.f/((long int)tval_result.tv_usec);
1213 fps = .9*fps + .1*curr;
1214 }
1215 #endif
1216 }
1217
demo_classifier(char * datacfg,char * cfgfile,char * weightfile,int cam_index,const char * filename,int benchmark,int benchmark_layers)1218 void demo_classifier(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename, int benchmark, int benchmark_layers)
1219 {
1220 #ifdef OPENCV
1221 printf("Classifier Demo\n");
1222 network net = parse_network_cfg_custom(cfgfile, 1, 0);
1223 if(weightfile){
1224 load_weights(&net, weightfile);
1225 }
1226 net.benchmark_layers = benchmark_layers;
1227 set_batch_network(&net, 1);
1228 list *options = read_data_cfg(datacfg);
1229
1230 fuse_conv_batchnorm(net);
1231 calculate_binary_weights(net);
1232
1233 srand(2222222);
1234 cap_cv * cap;
1235
1236 if(filename){
1237 cap = get_capture_video_stream(filename);
1238 }else{
1239 cap = get_capture_webcam(cam_index);
1240 }
1241
1242 int classes = option_find_int(options, "classes", 2);
1243 int top = option_find_int(options, "top", 1);
1244 if (top > classes) top = classes;
1245
1246 char *name_list = option_find_str(options, "names", 0);
1247 char **names = get_labels(name_list);
1248
1249 int* indexes = (int*)xcalloc(top, sizeof(int));
1250
1251 if(!cap) error("Couldn't connect to webcam.\n");
1252 if (!benchmark) create_window_cv("Classifier", 0, 512, 512);
1253 float fps = 0;
1254 int i;
1255
1256 double start_time = get_time_point();
1257 float avg_fps = 0;
1258 int frame_counter = 0;
1259
1260 while(1){
1261 struct timeval tval_before, tval_after, tval_result;
1262 gettimeofday(&tval_before, NULL);
1263
1264 //image in = get_image_from_stream(cap);
1265 image in_s, in;
1266 if (!benchmark) {
1267 in = get_image_from_stream_cpp(cap);
1268 in_s = resize_image(in, net.w, net.h);
1269 show_image(in, "Classifier");
1270 }
1271 else {
1272 static image tmp;
1273 if (!tmp.data) tmp = make_image(net.w, net.h, 3);
1274 in_s = tmp;
1275 }
1276
1277 double time = get_time_point();
1278 float *predictions = network_predict(net, in_s.data);
1279 double frame_time_ms = (get_time_point() - time)/1000;
1280 frame_counter++;
1281
1282 if(net.hierarchy) hierarchy_predictions(predictions, net.outputs, net.hierarchy, 1);
1283 top_predictions(net, top, indexes);
1284
1285 #ifndef _WIN32
1286 printf("\033[2J");
1287 printf("\033[1;1H");
1288 #endif
1289
1290
1291 if (!benchmark) {
1292 printf("\rFPS: %.2f (use -benchmark command line flag for correct measurement)\n", fps);
1293 for (i = 0; i < top; ++i) {
1294 int index = indexes[i];
1295 printf("%.1f%%: %s\n", predictions[index] * 100, names[index]);
1296 }
1297 printf("\n");
1298
1299 free_image(in_s);
1300 free_image(in);
1301
1302 int c = wait_key_cv(10);// cvWaitKey(10);
1303 if (c == 27 || c == 1048603) break;
1304 }
1305 else {
1306 printf("\rFPS: %.2f \t AVG_FPS = %.2f ", fps, avg_fps);
1307 }
1308
1309 //gettimeofday(&tval_after, NULL);
1310 //timersub(&tval_after, &tval_before, &tval_result);
1311 //float curr = 1000000.f/((long int)tval_result.tv_usec);
1312 float curr = 1000.f / frame_time_ms;
1313 if (fps == 0) fps = curr;
1314 else fps = .9*fps + .1*curr;
1315
1316 float spent_time = (get_time_point() - start_time) / 1000000;
1317 if (spent_time >= 3.0f) {
1318 //printf(" spent_time = %f \n", spent_time);
1319 avg_fps = frame_counter / spent_time;
1320 frame_counter = 0;
1321 start_time = get_time_point();
1322 }
1323 }
1324 #endif
1325 }
1326
1327
run_classifier(int argc,char ** argv)1328 void run_classifier(int argc, char **argv)
1329 {
1330 if(argc < 4){
1331 fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
1332 return;
1333 }
1334
1335 int mjpeg_port = find_int_arg(argc, argv, "-mjpeg_port", -1);
1336 char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
1337 int *gpus = 0;
1338 int gpu = 0;
1339 int ngpus = 0;
1340 if(gpu_list){
1341 printf("%s\n", gpu_list);
1342 int len = strlen(gpu_list);
1343 ngpus = 1;
1344 int i;
1345 for(i = 0; i < len; ++i){
1346 if (gpu_list[i] == ',') ++ngpus;
1347 }
1348 gpus = (int*)xcalloc(ngpus, sizeof(int));
1349 for(i = 0; i < ngpus; ++i){
1350 gpus[i] = atoi(gpu_list);
1351 gpu_list = strchr(gpu_list, ',')+1;
1352 }
1353 } else {
1354 gpu = gpu_index;
1355 gpus = &gpu;
1356 ngpus = 1;
1357 }
1358
1359 int dont_show = find_arg(argc, argv, "-dont_show");
1360 int benchmark = find_arg(argc, argv, "-benchmark");
1361 int benchmark_layers = find_arg(argc, argv, "-benchmark_layers");
1362 if (benchmark_layers) benchmark = 1;
1363 int dontuse_opencv = find_arg(argc, argv, "-dontuse_opencv");
1364 int show_imgs = find_arg(argc, argv, "-show_imgs");
1365 int calc_topk = find_arg(argc, argv, "-topk");
1366 int cam_index = find_int_arg(argc, argv, "-c", 0);
1367 int top = find_int_arg(argc, argv, "-t", 0);
1368 int clear = find_arg(argc, argv, "-clear");
1369 char *data = argv[3];
1370 char *cfg = argv[4];
1371 char *weights = (argc > 5) ? argv[5] : 0;
1372 char *filename = (argc > 6) ? argv[6]: 0;
1373 char *layer_s = (argc > 7) ? argv[7]: 0;
1374 int layer = layer_s ? atoi(layer_s) : -1;
1375 char* chart_path = find_char_arg(argc, argv, "-chart", 0);
1376 if(0==strcmp(argv[2], "predict")) predict_classifier(data, cfg, weights, filename, top);
1377 else if(0==strcmp(argv[2], "try")) try_classifier(data, cfg, weights, filename, atoi(layer_s));
1378 else if(0==strcmp(argv[2], "train")) train_classifier(data, cfg, weights, gpus, ngpus, clear, dontuse_opencv, dont_show, mjpeg_port, calc_topk, show_imgs, chart_path);
1379 else if(0==strcmp(argv[2], "demo")) demo_classifier(data, cfg, weights, cam_index, filename, benchmark, benchmark_layers);
1380 else if(0==strcmp(argv[2], "gun")) gun_classifier(data, cfg, weights, cam_index, filename);
1381 else if(0==strcmp(argv[2], "threat")) threat_classifier(data, cfg, weights, cam_index, filename);
1382 else if(0==strcmp(argv[2], "test")) test_classifier(data, cfg, weights, layer);
1383 else if(0==strcmp(argv[2], "label")) label_classifier(data, cfg, weights);
1384 else if(0==strcmp(argv[2], "valid")) validate_classifier_single(data, cfg, weights, NULL, -1);
1385 else if(0==strcmp(argv[2], "validmulti")) validate_classifier_multi(data, cfg, weights);
1386 else if(0==strcmp(argv[2], "valid10")) validate_classifier_10(data, cfg, weights);
1387 else if(0==strcmp(argv[2], "validcrop")) validate_classifier_crop(data, cfg, weights);
1388 else if(0==strcmp(argv[2], "validfull")) validate_classifier_full(data, cfg, weights);
1389
1390 if (gpus && gpu_list && ngpus > 1) free(gpus);
1391 }
1392