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.

173 lines
4.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. #include <opencv2/opencv.hpp>
  3. using namespace cv;
  4. namespace DN{
  5. void extract_voxel(char *lfile, char *rfile, char *prefix)
  6. {
  7. #ifdef OPENCV
  8. int w = 1920;
  9. int h = 1080;
  10. int shift = 0;
  11. int count = 0;
  12. CvCapture *lcap = cvCaptureFromFile(lfile);
  13. CvCapture *rcap = cvCaptureFromFile(rfile);
  14. while (1){
  15. image l = get_image_from_stream(lcap);
  16. image r = get_image_from_stream(rcap);
  17. if (!l.w || !r.w) break;
  18. if (count % 100 == 0) {
  19. shift = best_3d_shift_r(l, r, -l.h / 100, l.h / 100);
  20. printf("%d\n", shift);
  21. }
  22. image ls = crop_image(l, (l.w - w) / 2, (l.h - h) / 2, w, h);
  23. image rs = crop_image(r, 105 + (r.w - w) / 2, (r.h - h) / 2 + shift, w, h);
  24. char buff[256];
  25. sprintf(buff, "%s_%05d_l", prefix, count);
  26. save_image(ls, buff);
  27. sprintf(buff, "%s_%05d_r", prefix, count);
  28. save_image(rs, buff);
  29. free_image(l);
  30. free_image(r);
  31. free_image(ls);
  32. free_image(rs);
  33. ++count;
  34. }
  35. #else
  36. printf("need OpenCV for extraction\n");
  37. #endif
  38. }
  39. void train_voxel(char *cfgfile, char *weightfile)
  40. {
  41. char *train_images = "/data/imagenet/imagenet1k.train.list";
  42. char *backup_directory = "/home/pjreddie/backup/";
  43. srand((unsigned int)time(0));
  44. char *base = basecfg(cfgfile);
  45. printf("%s\n", base);
  46. float avg_loss = -1;
  47. network net = parse_network_cfg(cfgfile);
  48. if (weightfile){
  49. load_weights(&net, weightfile);
  50. }
  51. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  52. int imgs = net.batch*net.subdivisions;
  53. int i = *net.seen / imgs;
  54. data train, buffer;
  55. list *plist = get_paths(train_images);
  56. //int N = plist->size;
  57. char **paths = (char **)list_to_array(plist);
  58. load_args args = { 0 };
  59. args.w = net.w;
  60. args.h = net.h;
  61. args.scale = 4;
  62. args.paths = paths;
  63. args.n = imgs;
  64. args.m = plist->size;
  65. args.d = &buffer;
  66. args.type = SUPER_DATA;
  67. #ifdef __linux__
  68. pthread_t load_thread = load_data_in_thread(args);
  69. #else
  70. HANDLE load_thread = load_data_in_thread(args);
  71. #endif
  72. clock_t time;
  73. //while(i*imgs < N*120){
  74. while (((int)get_current_batch(net)) < net.max_batches){
  75. i += 1;
  76. time = clock();
  77. #ifdef __linux__
  78. pthread_join(load_thread, 0);
  79. #else
  80. WaitForSingleObject(load_thread, INFINITE);
  81. CloseHandle(load_thread);
  82. #endif
  83. train = buffer;
  84. load_thread = load_data_in_thread(args);
  85. printf("Loaded: %lf seconds\n", sec(clock() - time));
  86. time = clock();
  87. float loss = train_network(net, train);
  88. if (avg_loss < 0) avg_loss = loss;
  89. avg_loss = avg_loss*.9f + loss*.1f;
  90. printf("%d: %f, %f avg, %f rate, %lf seconds, %d images\n", i, loss, avg_loss, get_current_rate(net), sec(clock() - time), i*imgs);
  91. if (i % 1000 == 0){
  92. char buff[256];
  93. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
  94. save_weights(net, buff);
  95. }
  96. if (i % 100 == 0){
  97. char buff[256];
  98. sprintf(buff, "%s/%s.backup", backup_directory, base);
  99. save_weights(net, buff);
  100. }
  101. free_data(train);
  102. }
  103. char buff[256];
  104. sprintf(buff, "%s/%s_final.weights", backup_directory, base);
  105. save_weights(net, buff);
  106. }
  107. void test_voxel(char *cfgfile, char *weightfile, char *filename)
  108. {
  109. network net = parse_network_cfg(cfgfile);
  110. if (weightfile){
  111. load_weights(&net, weightfile);
  112. }
  113. set_batch_network(&net, 1);
  114. srand(2222222);
  115. clock_t time;
  116. char buff[256];
  117. char *input = buff;
  118. while (1){
  119. if (filename){
  120. strncpy(input, filename, 256);
  121. }
  122. else{
  123. printf("Enter Image Path: ");
  124. fflush(stdout);
  125. input = fgets(input, 256, stdin);
  126. if (!input) return;
  127. strtok(input, "\n");
  128. }
  129. image im = load_image_color(input, 0, 0);
  130. resize_network(&net, im.w, im.h);
  131. printf("%d %d\n", im.w, im.h);
  132. float *X = im.data;
  133. time = clock();
  134. network_predict(net, X);
  135. image out = get_network_image(net);
  136. printf("%s: Predicted in %f seconds.\n", input, sec(clock() - time));
  137. save_image(out, "out");
  138. free_image(im);
  139. if (filename) break;
  140. }
  141. }
  142. void run_voxel(int argc, char **argv)
  143. {
  144. if (argc < 4){
  145. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  146. return;
  147. }
  148. char *cfg = argv[3];
  149. char *weights = (argc > 4) ? argv[4] : 0;
  150. char *filename = (argc > 5) ? argv[5] : 0;
  151. if (0 == strcmp(argv[2], "train")) train_voxel(cfg, weights);
  152. else if (0 == strcmp(argv[2], "test")) test_voxel(cfg, weights, filename);
  153. else if (0 == strcmp(argv[2], "extract")) extract_voxel(argv[3], argv[4], argv[5]);
  154. /*
  155. else if(0==strcmp(argv[2], "valid")) validate_voxel(cfg, weights);
  156. */
  157. }
  158. };