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.

155 lines
4.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. namespace DN{
  3. void train_writing(char *cfgfile, char *weightfile)
  4. {
  5. char *backup_directory = "/home/pjreddie/backup/";
  6. srand((unsigned int)time(0));
  7. float avg_loss = -1;
  8. char *base = basecfg(cfgfile);
  9. printf("%s\n", base);
  10. network net = parse_network_cfg(cfgfile);
  11. if (weightfile){
  12. load_weights(&net, weightfile);
  13. }
  14. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  15. int imgs = net.batch*net.subdivisions;
  16. list *plist = get_paths("figures.list");
  17. char **paths = (char **)list_to_array(plist);
  18. clock_t time;
  19. int N = plist->size;
  20. printf("N: %d\n", N);
  21. image out = get_network_image(net);
  22. data train, buffer;
  23. load_args args = { 0 };
  24. args.w = net.w;
  25. args.h = net.h;
  26. args.out_w = out.w;
  27. args.out_h = out.h;
  28. args.paths = paths;
  29. args.n = imgs;
  30. args.m = N;
  31. args.d = &buffer;
  32. args.type = WRITING_DATA;
  33. #ifdef __linux__
  34. pthread_t load_thread = load_data_in_thread(args);
  35. #else
  36. HANDLE load_thread = load_data_in_thread(args);
  37. #endif
  38. int epoch = (*net.seen) / N;
  39. while (((int)get_current_batch(net)) < net.max_batches || net.max_batches == 0){
  40. time = clock();
  41. #ifdef __linux__
  42. pthread_join(load_thread, 0);
  43. #else
  44. WaitForSingleObject(load_thread, INFINITE);
  45. CloseHandle(load_thread);
  46. #endif
  47. train = buffer;
  48. load_thread = load_data_in_thread(args);
  49. printf("Loaded %lf seconds\n", sec(clock() - time));
  50. time = clock();
  51. float loss = train_network(net, train);
  52. /*
  53. image pred = float_to_image(64, 64, 1, out);
  54. print_image(pred);
  55. */
  56. /*
  57. image im = float_to_image(256, 256, 3, train.X.vals[0]);
  58. image lab = float_to_image(64, 64, 1, train.y.vals[0]);
  59. image pred = float_to_image(64, 64, 1, out);
  60. show_image(im, "image");
  61. show_image(lab, "label");
  62. print_image(lab);
  63. show_image(pred, "pred");
  64. cvWaitKey(0);
  65. */
  66. if (avg_loss == -1) avg_loss = loss;
  67. avg_loss = avg_loss*.9f + loss*.1f;
  68. 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), sec(clock() - time), *net.seen);
  69. free_data(train);
  70. if (get_current_batch(net) % 100 == 0){
  71. char buff[256];
  72. sprintf(buff, "%s/%s_batch_%ld.weights", backup_directory, base, get_current_batch(net));
  73. save_weights(net, buff);
  74. }
  75. if (((int)*net.seen) / N > epoch){
  76. epoch = *net.seen / N;
  77. char buff[256];
  78. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, epoch);
  79. save_weights(net, buff);
  80. }
  81. }
  82. }
  83. void test_writing(char *cfgfile, char *weightfile, char *filename)
  84. {
  85. network net = parse_network_cfg(cfgfile);
  86. if (weightfile){
  87. load_weights(&net, weightfile);
  88. }
  89. set_batch_network(&net, 1);
  90. srand(2222222);
  91. clock_t time;
  92. char buff[256];
  93. char *input = buff;
  94. while (1){
  95. if (filename){
  96. strncpy(input, filename, 256);
  97. }
  98. else{
  99. printf("Enter Image Path: ");
  100. fflush(stdout);
  101. input = fgets(input, 256, stdin);
  102. if (!input) return;
  103. strtok(input, "\n");
  104. }
  105. image im = load_image_color(input, 0, 0);
  106. resize_network(&net, im.w, im.h);
  107. printf("%d %d %d\n", im.h, im.w, im.c);
  108. float *X = im.data;
  109. time = clock();
  110. network_predict(net, X);
  111. printf("%s: Predicted in %f seconds.\n", input, sec(clock() - time));
  112. image pred = get_network_image(net);
  113. image upsampled = resize_image(pred, im.w, im.h);
  114. image thresh = threshold_image(upsampled, .5);
  115. pred = thresh;
  116. show_image(pred, "prediction");
  117. show_image(im, "orig");
  118. #ifdef OPENCV
  119. cvWaitKey(0);
  120. cvDestroyAllWindows();
  121. #endif
  122. free_image(upsampled);
  123. free_image(thresh);
  124. free_image(im);
  125. if (filename) break;
  126. }
  127. }
  128. void run_writing(int argc, char **argv)
  129. {
  130. if (argc < 4){
  131. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  132. return;
  133. }
  134. char *cfg = argv[3];
  135. char *weights = (argc > 4) ? argv[4] : 0;
  136. char *filename = (argc > 5) ? argv[5] : 0;
  137. if (0 == strcmp(argv[2], "train")) train_writing(cfg, weights);
  138. else if (0 == strcmp(argv[2], "test")) test_writing(cfg, weights, filename);
  139. }
  140. };