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.

366 lines
10 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <stdio.h>
  2. #include "network.h"
  3. #include "detection_layer.h"
  4. #include "cost_layer.h"
  5. #include "utils.h"
  6. #include "parser.h"
  7. #include "box.h"
  8. namespace DN{
  9. void train_compare(char *cfgfile, char *weightfile)
  10. {
  11. srand((unsigned int)time(0));
  12. float avg_loss = -1;
  13. char *base = basecfg(cfgfile);
  14. char *backup_directory = "/home/pjreddie/backup/";
  15. printf("%s\n", base);
  16. network net = parse_network_cfg(cfgfile);
  17. if (weightfile){
  18. load_weights(&net, weightfile);
  19. }
  20. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  21. int imgs = 1024;
  22. list *plist = get_paths("data/compare.train.list");
  23. char **paths = (char **)list_to_array(plist);
  24. int N = plist->size;
  25. printf("%d\n", N);
  26. clock_t time;
  27. HANDLE load_thread;
  28. data train;
  29. data buffer;
  30. load_args args;
  31. memset(&args, 0, sizeof(load_args));
  32. args.w = net.w;
  33. args.h = net.h;
  34. args.paths = paths;
  35. args.classes = 20;
  36. args.n = imgs;
  37. args.m = N;
  38. args.d = &buffer;
  39. args.type = COMPARE_DATA;
  40. load_thread = load_data_in_thread(args);
  41. int epoch = *net.seen / N;
  42. int i = 0;
  43. while (1){
  44. ++i;
  45. time = clock();
  46. //pthread_join(load_thread, 0);
  47. WaitForSingleObject(load_thread, INFINITE);
  48. CloseHandle(load_thread);
  49. train = buffer;
  50. load_thread = load_data_in_thread(args);
  51. printf("Loaded: %lf seconds\n", sec(clock() - time));
  52. time = clock();
  53. float loss = train_network(net, train);
  54. if (avg_loss == -1) avg_loss = loss;
  55. avg_loss = avg_loss*.9f + loss*.1f;
  56. printf("%.3f: %f, %f avg, %lf seconds, %ld images\n", (float)*net.seen / N, loss, avg_loss, sec(clock() - time), *net.seen);
  57. free_data(train);
  58. if (i % 100 == 0){
  59. char buff[256];
  60. sprintf(buff, "%s/%s_%d_minor_%d.weights", backup_directory, base, epoch, i);
  61. save_weights(net, buff);
  62. }
  63. if (((int)*net.seen) / N > epoch){
  64. epoch = *net.seen / N;
  65. i = 0;
  66. char buff[256];
  67. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, epoch);
  68. save_weights(net, buff);
  69. if (epoch % 22 == 0) net.learning_rate *= .1f;
  70. }
  71. }
  72. //pthread_join(load_thread, 0);
  73. WaitForSingleObject(load_thread, INFINITE);
  74. CloseHandle(load_thread);
  75. free_data(buffer);
  76. free_network(net);
  77. free_ptrs((void**)paths, plist->size);
  78. free_list(plist);
  79. free(base);
  80. }
  81. void validate_compare(char *filename, char *weightfile)
  82. {
  83. int i = 0;
  84. network net = parse_network_cfg(filename);
  85. if (weightfile){
  86. load_weights(&net, weightfile);
  87. }
  88. srand((unsigned int)time(0));
  89. list *plist = get_paths("data/compare.val.list");
  90. //list *plist = get_paths("data/compare.val.old");
  91. char **paths = (char **)list_to_array(plist);
  92. int N = plist->size / 2;
  93. free_list(plist);
  94. clock_t time;
  95. int correct = 0;
  96. int total = 0;
  97. int splits = 10;
  98. int num = (i + 1)*N / splits - i*N / splits;
  99. data val, buffer;
  100. load_args args = { 0 };
  101. args.w = net.w;
  102. args.h = net.h;
  103. args.paths = paths;
  104. args.classes = 20;
  105. args.n = num;
  106. args.m = 0;
  107. args.d = &buffer;
  108. args.type = COMPARE_DATA;
  109. HANDLE load_thread = load_data_in_thread(args);
  110. for (i = 1; i <= splits; ++i){
  111. time = clock();
  112. //pthread_join(load_thread, 0);
  113. WaitForSingleObject(load_thread, INFINITE);
  114. CloseHandle(load_thread);
  115. val = buffer;
  116. num = (i + 1)*N / splits - i*N / splits;
  117. char **part = paths + (i*N / splits);
  118. if (i != splits){
  119. args.paths = part;
  120. load_thread = load_data_in_thread(args);
  121. }
  122. printf("Loaded: %d images in %lf seconds\n", val.X.rows, sec(clock() - time));
  123. time = clock();
  124. matrix pred = network_predict_data(net, val);
  125. int j, k;
  126. for (j = 0; j < val.y.rows; ++j){
  127. for (k = 0; k < 20; ++k){
  128. if (val.y.vals[j][k * 2] != val.y.vals[j][k * 2 + 1]){
  129. ++total;
  130. if ((val.y.vals[j][k * 2] < val.y.vals[j][k * 2 + 1]) == (pred.vals[j][k * 2] < pred.vals[j][k * 2 + 1])){
  131. ++correct;
  132. }
  133. }
  134. }
  135. }
  136. free_matrix(pred);
  137. printf("%d: Acc: %f, %lf seconds, %d images\n", i, (float)correct / total, sec(clock() - time), val.X.rows);
  138. free_data(val);
  139. }
  140. }
  141. typedef struct {
  142. network net;
  143. char *filename;
  144. int cls;
  145. int classes;
  146. float elo;
  147. float *elos;
  148. } sortable_bbox;
  149. int total_compares = 0;
  150. int current_class = 0;
  151. int elo_comparator(const void*a, const void *b)
  152. {
  153. sortable_bbox box1 = *(sortable_bbox*)a;
  154. sortable_bbox box2 = *(sortable_bbox*)b;
  155. if (box1.elos[current_class] == box2.elos[current_class]) return 0;
  156. if (box1.elos[current_class] > box2.elos[current_class]) return -1;
  157. return 1;
  158. }
  159. int bbox_comparator(const void *a, const void *b)
  160. {
  161. ++total_compares;
  162. sortable_bbox box1 = *(sortable_bbox*)a;
  163. sortable_bbox box2 = *(sortable_bbox*)b;
  164. network net = box1.net;
  165. int cls = box1.cls;
  166. image im1 = load_image_color(box1.filename, net.w, net.h);
  167. image im2 = load_image_color(box2.filename, net.w, net.h);
  168. float *X = (float*)calloc(net.w*net.h*net.c, sizeof(float));
  169. memcpy(X, im1.data, im1.w*im1.h*im1.c*sizeof(float));
  170. memcpy(X + im1.w*im1.h*im1.c, im2.data, im2.w*im2.h*im2.c*sizeof(float));
  171. float *predictions = network_predict(net, X);
  172. free_image(im1);
  173. free_image(im2);
  174. free(X);
  175. if (predictions[cls * 2] > predictions[cls * 2 + 1]){
  176. return 1;
  177. }
  178. return -1;
  179. }
  180. void bbox_update(sortable_bbox *a, sortable_bbox *b, int cls, int result)
  181. {
  182. int k = 32;
  183. float EA = 1.f / (1.f + pow(10.f, (b->elos[cls] - a->elos[cls]) / 400.f));
  184. float EB = 1.f / (1.f + pow(10.f, (a->elos[cls] - b->elos[cls]) / 400.f));
  185. float SA = result ? 1.f : 0.f;
  186. float SB = result ? 0.f : 1.f;
  187. a->elos[cls] += (float)(k*(SA - EA));
  188. b->elos[cls] += (float)(k*(SB - EB));
  189. }
  190. void bbox_fight(network net, sortable_bbox *a, sortable_bbox *b, int classes, int cls)
  191. {
  192. image im1 = load_image_color(a->filename, net.w, net.h);
  193. image im2 = load_image_color(b->filename, net.w, net.h);
  194. float *X = (float*)calloc(net.w*net.h*net.c, sizeof(float));
  195. memcpy(X, im1.data, im1.w*im1.h*im1.c*sizeof(float));
  196. memcpy(X + im1.w*im1.h*im1.c, im2.data, im2.w*im2.h*im2.c*sizeof(float));
  197. float *predictions = network_predict(net, X);
  198. ++total_compares;
  199. int i;
  200. for (i = 0; i < classes; ++i){
  201. if (cls < 0 || cls == i){
  202. int result = predictions[i * 2] > predictions[i * 2 + 1];
  203. bbox_update(a, b, i, result);
  204. }
  205. }
  206. free_image(im1);
  207. free_image(im2);
  208. free(X);
  209. }
  210. void SortMaster3000(char *filename, char *weightfile)
  211. {
  212. int i = 0;
  213. network net = parse_network_cfg(filename);
  214. if (weightfile){
  215. load_weights(&net, weightfile);
  216. }
  217. srand((unsigned int)time(0));
  218. set_batch_network(&net, 1);
  219. list *plist = get_paths("data/compare.sort.list");
  220. //list *plist = get_paths("data/compare.val.old");
  221. char **paths = (char **)list_to_array(plist);
  222. int N = plist->size;
  223. free_list(plist);
  224. sortable_bbox *boxes = (sortable_bbox *)calloc(N, sizeof(sortable_bbox));
  225. printf("Sorting %d boxes...\n", N);
  226. for (i = 0; i < N; ++i){
  227. boxes[i].filename = paths[i];
  228. boxes[i].net = net;
  229. boxes[i].cls = 7;
  230. boxes[i].elo = 1500;
  231. }
  232. clock_t time = clock();
  233. qsort(boxes, N, sizeof(sortable_bbox), bbox_comparator);
  234. for (i = 0; i < N; ++i){
  235. printf("%s\n", boxes[i].filename);
  236. }
  237. printf("Sorted in %d compares, %f secs\n", total_compares, sec(clock() - time));
  238. }
  239. void BattleRoyaleWithCheese(char *filename, char *weightfile)
  240. {
  241. int classes = 20;
  242. int i, j;
  243. network net = parse_network_cfg(filename);
  244. if (weightfile){
  245. load_weights(&net, weightfile);
  246. }
  247. srand((unsigned int)time(0));
  248. set_batch_network(&net, 1);
  249. list *plist = get_paths("data/compare.sort.list");
  250. //list *plist = get_paths("data/compare.small.list");
  251. //list *plist = get_paths("data/compare.cat.list");
  252. //list *plist = get_paths("data/compare.val.old");
  253. char **paths = (char **)list_to_array(plist);
  254. int N = plist->size;
  255. int total = N;
  256. free_list(plist);
  257. sortable_bbox *boxes = (sortable_bbox *)calloc(N, sizeof(sortable_bbox));
  258. printf("Battling %d boxes...\n", N);
  259. for (i = 0; i < N; ++i){
  260. boxes[i].filename = paths[i];
  261. boxes[i].net = net;
  262. boxes[i].classes = classes;
  263. boxes[i].elos = (float*)calloc(classes, sizeof(float));;
  264. for (j = 0; j < classes; ++j){
  265. boxes[i].elos[j] = 1500;
  266. }
  267. }
  268. int round;
  269. clock_t time = clock();
  270. for (round = 1; round <= 4; ++round){
  271. clock_t round_time = clock();
  272. printf("Round: %d\n", round);
  273. shuffle(boxes, N, sizeof(sortable_bbox));
  274. for (i = 0; i < N / 2; ++i){
  275. bbox_fight(net, boxes + i * 2, boxes + i * 2 + 1, classes, -1);
  276. }
  277. printf("Round: %f secs, %d remaining\n", sec(clock() - round_time), N);
  278. }
  279. int cls;
  280. for (cls = 0; cls < classes; ++cls){
  281. N = total;
  282. current_class = cls;
  283. qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
  284. N /= 2;
  285. for (round = 1; round <= 100; ++round){
  286. clock_t round_time = clock();
  287. printf("Round: %d\n", round);
  288. sorta_shuffle(boxes, N, sizeof(sortable_bbox), 10);
  289. for (i = 0; i < N / 2; ++i){
  290. bbox_fight(net, boxes + i * 2, boxes + i * 2 + 1, classes, cls);
  291. }
  292. qsort(boxes, N, sizeof(sortable_bbox), elo_comparator);
  293. if (round <= 20) N = (N * 9 / 10) / 2 * 2;
  294. printf("Round: %f secs, %d remaining\n", sec(clock() - round_time), N);
  295. }
  296. char buff[256];
  297. sprintf(buff, "results/battle_%d.log", cls);
  298. FILE *outfp = fopen(buff, "w");
  299. for (i = 0; i < N; ++i){
  300. fprintf(outfp, "%s %f\n", boxes[i].filename, boxes[i].elos[cls]);
  301. }
  302. fclose(outfp);
  303. }
  304. printf("Tournament in %d compares, %f secs\n", total_compares, sec(clock() - time));
  305. }
  306. void run_compare(int argc, char **argv)
  307. {
  308. if (argc < 4){
  309. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  310. return;
  311. }
  312. char *cfg = argv[3];
  313. char *weights = (argc > 4) ? argv[4] : 0;
  314. //char *filename = (argc > 5) ? argv[5]: 0;
  315. if (0 == strcmp(argv[2], "train")) train_compare(cfg, weights);
  316. else if (0 == strcmp(argv[2], "valid")) validate_compare(cfg, weights);
  317. else if (0 == strcmp(argv[2], "sort")) SortMaster3000(cfg, weights);
  318. else if (0 == strcmp(argv[2], "battle")) BattleRoyaleWithCheese(cfg, weights);
  319. /*
  320. else if(0==strcmp(argv[2], "train")) train_coco(cfg, weights);
  321. else if(0==strcmp(argv[2], "extract")) extract_boxes(cfg, weights);
  322. else if(0==strcmp(argv[2], "valid")) validate_recall(cfg, weights);
  323. */
  324. }
  325. };