You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.3 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. namespace DN{
  3. char *dice_labels[] = { "face1", "face2", "face3", "face4", "face5", "face6" };
  4. void train_dice(char *cfgfile, char *weightfile)
  5. {
  6. srand((unsigned int)time(0));
  7. float avg_loss = -1;
  8. char *base = basecfg(cfgfile);
  9. char *backup_directory = "/home/pjreddie/backup/";
  10. printf("%s\n", base);
  11. network net = parse_network_cfg(cfgfile);
  12. if (weightfile){
  13. load_weights(&net, weightfile);
  14. }
  15. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  16. int imgs = 1024;
  17. int i = *net.seen / imgs;
  18. char **labels = dice_labels;
  19. list *plist = get_paths("data/dice/dice.train.list");
  20. char **paths = (char **)list_to_array(plist);
  21. printf("%d\n", plist->size);
  22. clock_t time;
  23. while (1){
  24. ++i;
  25. time = clock();
  26. data train = load_data_old(paths, imgs, plist->size, labels, 6, net.w, net.h);
  27. printf("Loaded: %lf seconds\n", sec(clock() - time));
  28. time = clock();
  29. float loss = train_network(net, train);
  30. if (avg_loss == -1) avg_loss = loss;
  31. avg_loss = avg_loss*.9f + loss*.1f;
  32. printf("%d: %f, %f avg, %lf seconds, %ld images\n", i, loss, avg_loss, sec(clock() - time), *net.seen);
  33. free_data(train);
  34. if ((i % 100) == 0) net.learning_rate *= .1f;
  35. if (i % 100 == 0){
  36. char buff[256];
  37. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
  38. save_weights(net, buff);
  39. }
  40. }
  41. }
  42. void validate_dice(char *filename, char *weightfile)
  43. {
  44. network net = parse_network_cfg(filename);
  45. if (weightfile){
  46. load_weights(&net, weightfile);
  47. }
  48. srand((unsigned int)time(0));
  49. char **labels = dice_labels;
  50. list *plist = get_paths("data/dice/dice.val.list");
  51. char **paths = (char **)list_to_array(plist);
  52. int m = plist->size;
  53. free_list(plist);
  54. data val = load_data_old(paths, m, 0, labels, 6, net.w, net.h);
  55. float *acc = network_accuracies(net, val, 2);
  56. printf("Validation Accuracy: %f, %d images\n", acc[0], m);
  57. free_data(val);
  58. }
  59. void test_dice(char *cfgfile, char *weightfile, char *filename)
  60. {
  61. network net = parse_network_cfg(cfgfile);
  62. if (weightfile){
  63. load_weights(&net, weightfile);
  64. }
  65. set_batch_network(&net, 1);
  66. srand(2222222);
  67. int i = 0;
  68. char **names = dice_labels;
  69. char buff[256];
  70. char *input = buff;
  71. int indexes[6];
  72. while (1){
  73. if (filename){
  74. strncpy(input, filename, 256);
  75. }
  76. else{
  77. printf("Enter Image Path: ");
  78. fflush(stdout);
  79. input = fgets(input, 256, stdin);
  80. if (!input) return;
  81. strtok(input, "\n");
  82. }
  83. image im = load_image_color(input, net.w, net.h);
  84. float *X = im.data;
  85. float *predictions = network_predict(net, X);
  86. top_predictions(net, 6, indexes);
  87. for (i = 0; i < 6; ++i){
  88. int index = indexes[i];
  89. printf("%s: %f\n", names[index], predictions[index]);
  90. }
  91. free_image(im);
  92. if (filename) break;
  93. }
  94. }
  95. void run_dice(int argc, char **argv)
  96. {
  97. if (argc < 4){
  98. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  99. return;
  100. }
  101. char *cfg = argv[3];
  102. char *weights = (argc > 4) ? argv[4] : 0;
  103. char *filename = (argc > 5) ? argv[5] : 0;
  104. if (0 == strcmp(argv[2], "test")) test_dice(cfg, weights, filename);
  105. else if (0 == strcmp(argv[2], "train")) train_dice(cfg, weights);
  106. else if (0 == strcmp(argv[2], "valid")) validate_dice(cfg, weights);
  107. }
  108. };