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.

365 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. namespace DN{
  3. char *voc_names[] = { "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" };
  4. void train_yolo(char *cfgfile, char *weightfile)
  5. {
  6. char *train_images = "/data/voc/train.txt";
  7. char *backup_directory = "/home/pjreddie/backup/";
  8. srand((unsigned int)time(0));
  9. char *base = basecfg(cfgfile);
  10. printf("%s\n", base);
  11. float avg_loss = -1;
  12. network net = parse_network_cfg(cfgfile);
  13. if (weightfile){
  14. load_weights(&net, weightfile);
  15. }
  16. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  17. int imgs = net.batch*net.subdivisions;
  18. int i = *net.seen / imgs;
  19. data train, buffer;
  20. layer l = net.layers[net.n - 1];
  21. int side = l.side;
  22. int classes = l.classes;
  23. float jitter = l.jitter;
  24. list *plist = get_paths(train_images);
  25. //int N = plist->size;
  26. char **paths = (char **)list_to_array(plist);
  27. load_args args = { 0 };
  28. args.w = net.w;
  29. args.h = net.h;
  30. args.paths = paths;
  31. args.n = imgs;
  32. args.m = plist->size;
  33. args.classes = classes;
  34. args.jitter = jitter;
  35. args.num_boxes = side;
  36. args.d = &buffer;
  37. args.type = REGION_DATA;
  38. args.angle = net.angle;
  39. args.exposure = net.exposure;
  40. args.saturation = net.saturation;
  41. args.hue = net.hue;
  42. #ifdef __linux__
  43. pthread_t load_thread = load_data_in_thread(args);
  44. #else
  45. HANDLE load_thread = load_data_in_thread(args);
  46. #endif
  47. clock_t time;
  48. //while(i*imgs < N*120){
  49. while (((int)get_current_batch(net)) < net.max_batches){
  50. i += 1;
  51. time = clock();
  52. #ifdef __linux__
  53. pthread_join(load_thread, 0);
  54. #else
  55. WaitForSingleObject(load_thread, INFINITE);
  56. CloseHandle(load_thread);
  57. #endif
  58. train = buffer;
  59. load_thread = load_data_in_thread(args);
  60. printf("Loaded: %lf seconds\n", sec(clock() - time));
  61. time = clock();
  62. float loss = train_network(net, train);
  63. if (avg_loss < 0) avg_loss = loss;
  64. avg_loss = avg_loss*.9f + loss*.1f;
  65. 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);
  66. if (i % 1000 == 0 || (i < 1000 && i % 100 == 0)){
  67. char buff[256];
  68. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
  69. save_weights(net, buff);
  70. }
  71. free_data(train);
  72. }
  73. char buff[256];
  74. sprintf(buff, "%s/%s_final.weights", backup_directory, base);
  75. save_weights(net, buff);
  76. }
  77. void print_yolo_detections(FILE **fps, char *id, box *boxes, float **probs, int total, int classes, int w, int h)
  78. {
  79. int i, j;
  80. for (i = 0; i < total; ++i){
  81. float xmin = boxes[i].x - boxes[i].w / 2.f;
  82. float xmax = boxes[i].x + boxes[i].w / 2.f;
  83. float ymin = boxes[i].y - boxes[i].h / 2.f;
  84. float ymax = boxes[i].y + boxes[i].h / 2.f;
  85. if (xmin < 0.f) xmin = 0.f;
  86. if (ymin < 0.f) ymin = 0.f;
  87. if (xmax >(float)w) xmax = (float)w;
  88. if (ymax >(float)h) ymax = (float)h;
  89. for (j = 0; j < classes; ++j){
  90. if (probs[i][j]) fprintf(fps[j], "%s %f %f %f %f %f\n", id, probs[i][j],
  91. xmin, ymin, xmax, ymax);
  92. }
  93. }
  94. }
  95. void validate_yolo(char *cfgfile, char *weightfile)
  96. {
  97. network net = parse_network_cfg(cfgfile);
  98. if (weightfile){
  99. load_weights(&net, weightfile);
  100. }
  101. set_batch_network(&net, 1);
  102. fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  103. srand((unsigned int)time(0));
  104. char *base = "results/comp4_det_test_";
  105. //list *plist = get_paths("data/voc.2007.test");
  106. list *plist = get_paths("/home/pjreddie/data/voc/2007_test.txt");
  107. //list *plist = get_paths("data/voc.2012.test");
  108. char **paths = (char **)list_to_array(plist);
  109. layer l = net.layers[net.n - 1];
  110. int classes = l.classes;
  111. int j;
  112. FILE **fps = (FILE **)calloc(classes, sizeof(FILE *));
  113. for (j = 0; j < classes; ++j){
  114. char buff[1024];
  115. _snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
  116. fps[j] = fopen(buff, "w");
  117. }
  118. box *boxes = (box*)calloc(l.side*l.side*l.n, sizeof(box));
  119. float **probs = (float**)calloc(l.side*l.side*l.n, sizeof(float *));
  120. for (j = 0; j < l.side*l.side*l.n; ++j) probs[j] = (float*)calloc(classes, sizeof(float *));
  121. int m = plist->size;
  122. int i = 0;
  123. int t;
  124. float thresh = .001f;
  125. int nms = 1;
  126. float iou_thresh = .5f;
  127. int nthreads = 8;
  128. image *val = (image*)calloc(nthreads, sizeof(image));
  129. image *val_resized = (image*)calloc(nthreads, sizeof(image));
  130. image *buf = (image*)calloc(nthreads, sizeof(image));
  131. image *buf_resized = (image*)calloc(nthreads, sizeof(image));
  132. #ifdef __linux__
  133. pthread_t *thr = calloc(nthreads, sizeof(pthread_t));
  134. #else
  135. HANDLE *thr = (HANDLE *)calloc(nthreads, sizeof(HANDLE));
  136. #endif
  137. load_args args = { 0 };
  138. args.w = net.w;
  139. args.h = net.h;
  140. args.type = IMAGE_DATA;
  141. for (t = 0; t < nthreads; ++t){
  142. args.path = paths[i + t];
  143. args.im = &buf[t];
  144. args.resized = &buf_resized[t];
  145. thr[t] = load_data_in_thread(args);
  146. }
  147. time_t start = time(0);
  148. for (i = nthreads; i < m + nthreads; i += nthreads){
  149. fprintf(stderr, "%d\n", i);
  150. for (t = 0; t < nthreads && i + t - nthreads < m; ++t){
  151. #ifdef __linux__
  152. pthread_join(thr[t], 0);
  153. #else
  154. WaitForSingleObject(thr[t], INFINITE);
  155. CloseHandle(thr[t]);
  156. #endif
  157. val[t] = buf[t];
  158. val_resized[t] = buf_resized[t];
  159. }
  160. for (t = 0; t < nthreads && i + t < m; ++t){
  161. args.path = paths[i + t];
  162. args.im = &buf[t];
  163. args.resized = &buf_resized[t];
  164. thr[t] = load_data_in_thread(args);
  165. }
  166. for (t = 0; t < nthreads && i + t - nthreads < m; ++t){
  167. char *path = paths[i + t - nthreads];
  168. char *id = basecfg(path);
  169. float *X = val_resized[t].data;
  170. network_predict(net, X);
  171. int w = val[t].w;
  172. int h = val[t].h;
  173. get_detection_boxes(l, w, h, thresh, probs, boxes, 0);
  174. if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, classes, iou_thresh);
  175. print_yolo_detections(fps, id, boxes, probs, l.side*l.side*l.n, classes, w, h);
  176. free(id);
  177. free_image(val[t]);
  178. free_image(val_resized[t]);
  179. }
  180. }
  181. fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
  182. }
  183. void validate_yolo_recall(char *cfgfile, char *weightfile)
  184. {
  185. network net = parse_network_cfg(cfgfile);
  186. if (weightfile){
  187. load_weights(&net, weightfile);
  188. }
  189. set_batch_network(&net, 1);
  190. fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  191. srand((unsigned int)time(0));
  192. char *base = "results/comp4_det_test_";
  193. list *plist = get_paths("data/voc.2007.test");
  194. char **paths = (char **)list_to_array(plist);
  195. layer l = net.layers[net.n - 1];
  196. int classes = l.classes;
  197. int side = l.side;
  198. int j, k;
  199. FILE **fps = (FILE**)calloc(classes, sizeof(FILE *));
  200. for (j = 0; j < classes; ++j){
  201. char buff[1024];
  202. _snprintf(buff, 1024, "%s%s.txt", base, voc_names[j]);
  203. fps[j] = fopen(buff, "w");
  204. }
  205. box *boxes = (box*)calloc(side*side*l.n, sizeof(box));
  206. float **probs = (float**)calloc(side*side*l.n, sizeof(float *));
  207. for (j = 0; j < side*side*l.n; ++j) probs[j] = (float*)calloc(classes, sizeof(float *));
  208. int m = plist->size;
  209. int i = 0;
  210. float thresh = .001f;
  211. float iou_thresh = .5f;
  212. float nms = 0.f;
  213. int total = 0;
  214. int correct = 0;
  215. int proposals = 0;
  216. float avg_iou = 0.f;
  217. for (i = 0; i < m; ++i){
  218. char *path = paths[i];
  219. image orig = load_image_color(path, 0, 0);
  220. image sized = resize_image(orig, net.w, net.h);
  221. char *id = basecfg(path);
  222. network_predict(net, sized.data);
  223. get_detection_boxes(l, orig.w, orig.h, thresh, probs, boxes, 1);
  224. if (nms) do_nms(boxes, probs, side*side*l.n, 1, nms);
  225. char labelpath[4096];
  226. find_replace(path, "images", "labels", labelpath);
  227. find_replace(labelpath, "JPEGImages", "labels", labelpath);
  228. find_replace(labelpath, ".jpg", ".txt", labelpath);
  229. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  230. int num_labels = 0;
  231. box_label *truth = read_boxes(labelpath, &num_labels);
  232. for (k = 0; k < side*side*l.n; ++k){
  233. if (probs[k][0] > thresh){
  234. ++proposals;
  235. }
  236. }
  237. for (j = 0; j < num_labels; ++j) {
  238. ++total;
  239. box t = { truth[j].x, truth[j].y, truth[j].w, truth[j].h };
  240. float best_iou = 0;
  241. for (k = 0; k < side*side*l.n; ++k){
  242. float iou = box_iou(boxes[k], t);
  243. if (probs[k][0] > thresh && iou > best_iou){
  244. best_iou = iou;
  245. }
  246. }
  247. avg_iou += best_iou;
  248. if (best_iou > iou_thresh){
  249. ++correct;
  250. }
  251. }
  252. fprintf(stderr, "%5d %5d %5d\tRPs/Img: %.2f\tIOU: %.2f%%\tRecall:%.2f%%\n", i, correct, total, (float)proposals / (i + 1), avg_iou * 100 / total, 100.*correct / total);
  253. free(id);
  254. free_image(orig);
  255. free_image(sized);
  256. }
  257. }
  258. void test_yolo(char *cfgfile, char *weightfile, char *filename, float thresh)
  259. {
  260. image **alphabet = load_alphabet();
  261. network net = parse_network_cfg(cfgfile);
  262. if (weightfile){
  263. load_weights(&net, weightfile);
  264. }
  265. layer l = net.layers[net.n - 1];
  266. set_batch_network(&net, 1);
  267. srand(2222222);
  268. clock_t time;
  269. char buff[256];
  270. char *input = buff;
  271. int j;
  272. float nms = .4f;
  273. box *boxes = (box*)calloc(l.side*l.side*l.n, sizeof(box));
  274. float **probs = (float**)calloc(l.side*l.side*l.n, sizeof(float *));
  275. for (j = 0; j < l.side*l.side*l.n; ++j) probs[j] = (float*)calloc(l.classes, sizeof(float *));
  276. while (1){
  277. if (filename){
  278. strncpy(input, filename, 256);
  279. }
  280. else {
  281. printf("Enter Image Path: ");
  282. fflush(stdout);
  283. input = fgets(input, 256, stdin);
  284. if (!input) return;
  285. strtok(input, "\n");
  286. }
  287. image im = load_image_color(input, 0, 0);
  288. image sized = resize_image(im, net.w, net.h);
  289. float *X = sized.data;
  290. time = clock();
  291. network_predict(net, X);
  292. printf("%s: Predicted in %f seconds.\n", input, sec(clock() - time));
  293. get_detection_boxes(l, 1, 1, thresh, probs, boxes, 0);
  294. if (nms) do_nms_sort(boxes, probs, l.side*l.side*l.n, l.classes, nms);
  295. draw_detections(im, l.side*l.side*l.n, thresh, boxes, probs, 0, voc_names, alphabet, 20);
  296. save_image(im, "predictions");
  297. show_image(im, "predictions");
  298. free_image(im);
  299. free_image(sized);
  300. #ifdef OPENCV
  301. cvWaitKey(0);
  302. cvDestroyAllWindows();
  303. #endif
  304. if (filename) break;
  305. }
  306. }
  307. void run_yolo(int argc, char **argv)
  308. {
  309. char *prefix = find_char_arg(argc, argv, "-prefix", 0);
  310. float thresh = find_float_arg(argc, argv, "-thresh", .2f);
  311. int cam_index = find_int_arg(argc, argv, "-c", 0);
  312. int frame_skip = find_int_arg(argc, argv, "-s", 0);
  313. if (argc < 4){
  314. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  315. return;
  316. }
  317. int avg = find_int_arg(argc, argv, "-avg", 1);
  318. char *cfg = argv[3];
  319. char *weights = (argc > 4) ? argv[4] : 0;
  320. char *filename = (argc > 5) ? argv[5] : 0;
  321. if (0 == strcmp(argv[2], "test")) test_yolo(cfg, weights, filename, thresh);
  322. else if (0 == strcmp(argv[2], "train")) train_yolo(cfg, weights);
  323. else if (0 == strcmp(argv[2], "valid")) validate_yolo(cfg, weights);
  324. else if (0 == strcmp(argv[2], "recall")) validate_yolo_recall(cfg, weights);
  325. else if (0 == strcmp(argv[2], "demo")) demo(cfg, weights, thresh, cam_index, filename, voc_names, 20, frame_skip, prefix, avg, .5, 0, 0, 0, 0);
  326. }
  327. };