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.

293 lines
7.4 KiB

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