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.

262 lines
6.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. #include <assert.h>
  3. namespace DN{
  4. void train_regressor(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
  5. {
  6. int i;
  7. float avg_loss = -1;
  8. char *base = basecfg(cfgfile);
  9. printf("%s\n", base);
  10. printf("%d\n", ngpus);
  11. network *nets = (network*)calloc(ngpus, sizeof(network));
  12. srand((unsigned int)time(0));
  13. int seed = rand();
  14. for (i = 0; i < ngpus; ++i){
  15. srand(seed);
  16. #ifdef GPU
  17. cuda_set_device(gpus[i]);
  18. #endif
  19. nets[i] = parse_network_cfg(cfgfile);
  20. if (weightfile){
  21. load_weights(&nets[i], weightfile);
  22. }
  23. if (clear) *nets[i].seen = 0;
  24. nets[i].learning_rate *= ngpus;
  25. }
  26. srand((unsigned int)time(0));
  27. network net = nets[0];
  28. int imgs = net.batch * net.subdivisions * ngpus;
  29. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  30. list *options = read_data_cfg(datacfg);
  31. char *backup_directory = option_find_str(options, "backup", "/backup/");
  32. char *train_list = option_find_str(options, "train", "data/train.list");
  33. list *plist = get_paths(train_list);
  34. char **paths = (char **)list_to_array(plist);
  35. printf("%d\n", plist->size);
  36. int N = plist->size;
  37. clock_t time;
  38. load_args args = { 0 };
  39. args.w = net.w;
  40. args.h = net.h;
  41. args.threads = 32;
  42. args.min = net.min_crop;
  43. args.max = net.max_crop;
  44. args.angle = net.angle;
  45. args.aspect = net.aspect;
  46. args.exposure = net.exposure;
  47. args.saturation = net.saturation;
  48. args.hue = net.hue;
  49. args.size = net.w;
  50. args.paths = paths;
  51. args.n = imgs;
  52. args.m = N;
  53. args.type = REGRESSION_DATA;
  54. data train;
  55. data buffer;
  56. #ifdef __linux__
  57. pthread_t load_thread;
  58. #else
  59. HANDLE load_thread;
  60. #endif
  61. args.d = &buffer;
  62. load_thread = load_data(args);
  63. int epoch = (*net.seen) / N;
  64. while (((int)get_current_batch(net)) < net.max_batches || net.max_batches == 0){
  65. time = clock();
  66. #ifdef __linux__
  67. pthread_join(load_thread, 0);
  68. #else
  69. WaitForSingleObject(load_thread, INFINITE);
  70. CloseHandle(load_thread);
  71. #endif
  72. train = buffer;
  73. load_thread = load_data(args);
  74. printf("Loaded: %lf seconds\n", sec(clock() - time));
  75. time = clock();
  76. float loss = 0;
  77. #ifdef GPU
  78. if(ngpus == 1){
  79. loss = train_network(net, train);
  80. } else {
  81. loss = train_networks(nets, ngpus, train, 4);
  82. }
  83. #else
  84. loss = train_network(net, train);
  85. #endif
  86. if (avg_loss == -1) avg_loss = loss;
  87. avg_loss = avg_loss*.9f + loss*.1f;
  88. 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);
  89. free_data(train);
  90. if (((int)*net.seen) / N > epoch){
  91. epoch = *net.seen / N;
  92. char buff[256];
  93. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, epoch);
  94. save_weights(net, buff);
  95. }
  96. if (get_current_batch(net) % 100 == 0){
  97. char buff[256];
  98. sprintf(buff, "%s/%s.backup", backup_directory, base);
  99. save_weights(net, buff);
  100. }
  101. }
  102. char buff[256];
  103. sprintf(buff, "%s/%s.weights", backup_directory, base);
  104. save_weights(net, buff);
  105. free_network(net);
  106. free_ptrs((void**)paths, plist->size);
  107. free_list(plist);
  108. free(base);
  109. }
  110. void predict_regressor(char *cfgfile, char *weightfile, char *filename)
  111. {
  112. network net = parse_network_cfg(cfgfile);
  113. if (weightfile){
  114. load_weights(&net, weightfile);
  115. }
  116. set_batch_network(&net, 1);
  117. srand(2222222);
  118. clock_t time;
  119. char buff[256];
  120. char *input = buff;
  121. while (1){
  122. if (filename){
  123. strncpy(input, filename, 256);
  124. }
  125. else{
  126. printf("Enter Image Path: ");
  127. fflush(stdout);
  128. input = fgets(input, 256, stdin);
  129. if (!input) return;
  130. strtok(input, "\n");
  131. }
  132. image im = load_image_color(input, 0, 0);
  133. image sized = letterbox_image(im, net.w, net.h);
  134. float *X = sized.data;
  135. time = clock();
  136. float *predictions = network_predict(net, X);
  137. printf("Predicted: %f\n", predictions[0]);
  138. printf("%s: Predicted in %f seconds.\n", input, sec(clock() - time));
  139. free_image(im);
  140. free_image(sized);
  141. if (filename) break;
  142. }
  143. }
  144. void demo_regressor(char *datacfg, char *cfgfile, char *weightfile, int cam_index, const char *filename)
  145. {
  146. #ifdef OPENCV
  147. printf("Regressor Demo\n");
  148. network net = parse_network_cfg(cfgfile);
  149. if (weightfile){
  150. load_weights(&net, weightfile);
  151. }
  152. set_batch_network(&net, 1);
  153. srand(2222222);
  154. CvCapture * cap;
  155. if (filename){
  156. cap = cvCaptureFromFile(filename);
  157. }
  158. else{
  159. cap = cvCaptureFromCAM(cam_index);
  160. }
  161. if (!cap) error("Couldn't connect to webcam.\n");
  162. cvNamedWindow("Regressor", CV_WINDOW_NORMAL);
  163. cvResizeWindow("Regressor", 512, 512);
  164. float fps = 0;
  165. while (1){
  166. struct timeval tval_before, tval_after, tval_result;
  167. gettimeofday(&tval_before, NULL);
  168. image in = get_image_from_stream(cap);
  169. image in_s = letterbox_image(in, net.w, net.h);
  170. show_image(in, "Regressor");
  171. float *predictions = network_predict(net, in_s.data);
  172. printf("\033[2J");
  173. printf("\033[1;1H");
  174. printf("\nFPS:%.0f\n", fps);
  175. printf("People: %f\n", predictions[0]);
  176. free_image(in_s);
  177. free_image(in);
  178. cvWaitKey(10);
  179. gettimeofday(&tval_after, NULL);
  180. timersub(&tval_after, &tval_before, &tval_result);
  181. float curr = 1000000.f / ((long int)tval_result.tv_usec);
  182. fps = .9*fps + .1*curr;
  183. }
  184. #endif
  185. }
  186. void run_regressor(int argc, char **argv)
  187. {
  188. if (argc < 4){
  189. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  190. return;
  191. }
  192. char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
  193. int *gpus = 0;
  194. int gpu = 0;
  195. int ngpus = 0;
  196. if (gpu_list){
  197. printf("%s\n", gpu_list);
  198. int len = strlen(gpu_list);
  199. ngpus = 1;
  200. int i;
  201. for (i = 0; i < len; ++i){
  202. if (gpu_list[i] == ',') ++ngpus;
  203. }
  204. gpus = (int*)calloc(ngpus, sizeof(int));
  205. for (i = 0; i < ngpus; ++i){
  206. gpus[i] = atoi(gpu_list);
  207. gpu_list = strchr(gpu_list, ',') + 1;
  208. }
  209. }
  210. else {
  211. gpu = gpu_index;
  212. gpus = &gpu;
  213. ngpus = 1;
  214. }
  215. int cam_index = find_int_arg(argc, argv, "-c", 0);
  216. int clear = find_arg(argc, argv, "-clear");
  217. char *data = argv[3];
  218. char *cfg = argv[4];
  219. char *weights = (argc > 5) ? argv[5] : 0;
  220. char *filename = (argc > 6) ? argv[6] : 0;
  221. if (0 == strcmp(argv[2], "test")) predict_regressor(data, cfg, weights);
  222. else if (0 == strcmp(argv[2], "train")) train_regressor(data, cfg, weights, gpus, ngpus, clear);
  223. else if (0 == strcmp(argv[2], "demo")) demo_regressor(data, cfg, weights, cam_index, filename);
  224. }
  225. };