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.

754 lines
23 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. #include <opencv2/opencv.hpp>
  3. using namespace cv;
  4. namespace DN{
  5. static int coco_ids[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90 };
  6. void train_detector(char *datacfg, char *cfgfile, char *weightfile, int *gpus, int ngpus, int clear)
  7. {
  8. list *options = read_data_cfg(datacfg);
  9. char *train_images = option_find_str(options, "train", "data/train.list");
  10. char *backup_directory = option_find_str(options, "backup", "/backup/");
  11. srand((unsigned int)time(0));
  12. char *base = basecfg(cfgfile);
  13. printf("%s\n", base);
  14. float avg_loss = -1;
  15. network *nets = (network*)calloc(ngpus, sizeof(network));
  16. srand((unsigned int)time(0));
  17. int seed = rand();
  18. int i;
  19. for (i = 0; i < ngpus; ++i){
  20. srand(seed);
  21. #ifdef GPU
  22. cuda_set_device(gpus[i]);
  23. #endif
  24. nets[i] = load_network(cfgfile, weightfile, clear);
  25. nets[i].learning_rate *= ngpus;
  26. }
  27. srand((unsigned int)time(0));
  28. network net = nets[0];
  29. int imgs = net.batch * net.subdivisions * ngpus;
  30. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  31. data train, buffer;
  32. layer l = net.layers[net.n - 1];
  33. int classes = l.classes;
  34. float jitter = l.jitter;
  35. list *plist = get_paths(train_images);
  36. //int N = plist->size;
  37. char **paths = (char **)list_to_array(plist);
  38. load_args args = get_base_args(net);
  39. args.coords = l.coords;
  40. args.paths = paths;
  41. args.n = imgs;
  42. args.m = plist->size;
  43. args.classes = classes;
  44. args.jitter = jitter;
  45. args.num_boxes = l.max_boxes;
  46. args.d = &buffer;
  47. args.type = DETECTION_DATA;
  48. //args.type = INSTANCE_DATA;
  49. args.threads = 8;
  50. #ifdef __linux__
  51. pthread_t load_thread = load_data(args);
  52. #else
  53. HANDLE load_thread = load_data(args);
  54. #endif
  55. clock_t time;
  56. int count = 0;
  57. //while(i*imgs < N*120){
  58. while (((int)get_current_batch(net)) < net.max_batches){
  59. if (l.random && count++ % 10 == 0){
  60. printf("Resizing\n");
  61. int dim = (rand() % 10 + 10) * 32;
  62. if (((int)get_current_batch(net)) + 200 > net.max_batches) dim = 608;
  63. //int dim = (rand() % 4 + 16) * 32;
  64. printf("%d\n", dim);
  65. args.w = dim;
  66. args.h = dim;
  67. #ifdef __linux__
  68. pthread_join(load_thread, 0);
  69. #else
  70. WaitForSingleObject(load_thread, INFINITE);
  71. CloseHandle(load_thread);
  72. #endif
  73. train = buffer;
  74. free_data(train);
  75. load_thread = load_data(args);
  76. for (i = 0; i < ngpus; ++i){
  77. resize_network(nets + i, dim, dim);
  78. }
  79. net = nets[0];
  80. }
  81. time = clock();
  82. #ifdef __linux__
  83. pthread_join(load_thread, 0);
  84. #else
  85. WaitForSingleObject(load_thread, INFINITE);
  86. CloseHandle(load_thread);
  87. #endif
  88. train = buffer;
  89. load_thread = load_data(args);
  90. /*
  91. int k;
  92. for(k = 0; k < l.max_boxes; ++k){
  93. box b = float_to_box(train.y.vals[10] + 1 + k*5);
  94. if(!b.x) break;
  95. printf("loaded: %f %f %f %f\n", b.x, b.y, b.w, b.h);
  96. }
  97. */
  98. /*
  99. int zz;
  100. for(zz = 0; zz < train.X.cols; ++zz){
  101. image im = float_to_image(net.w, net.h, 3, train.X.vals[zz]);
  102. int k;
  103. for(k = 0; k < l.max_boxes; ++k){
  104. box b = float_to_box(train.y.vals[zz] + k*5, 1);
  105. printf("%f %f %f %f\n", b.x, b.y, b.w, b.h);
  106. draw_bbox(im, b, 1, 1,0,0);
  107. }
  108. show_image(im, "truth11");
  109. cvWaitKey(0);
  110. save_image(im, "truth11");
  111. }
  112. */
  113. printf("Loaded: %lf seconds\n", sec(clock() - time));
  114. time = clock();
  115. float loss = 0;
  116. #ifdef GPU
  117. if(ngpus == 1){
  118. loss = train_network(net, train);
  119. } else {
  120. loss = train_networks(nets, ngpus, train, 4);
  121. }
  122. #else
  123. loss = train_network(net, train);
  124. #endif
  125. if (avg_loss < 0) avg_loss = loss;
  126. avg_loss = avg_loss*.9f + loss*.1f;
  127. i = get_current_batch(net);
  128. printf("%ld: %f, %f avg, %f rate, %lf seconds, %d images\n", get_current_batch(net), loss, avg_loss, get_current_rate(net), sec(clock() - time), i*imgs);
  129. if (i % 100 == 0){
  130. #ifdef GPU
  131. if(ngpus != 1) sync_nets(nets, ngpus, 0);
  132. #endif
  133. char buff[256];
  134. sprintf(buff, "%s/%s.backup", backup_directory, base);
  135. save_weights(net, buff);
  136. }
  137. if (i % 10000 == 0 || (i < 1000 && i % 100 == 0)){
  138. #ifdef GPU
  139. if(ngpus != 1) sync_nets(nets, ngpus, 0);
  140. #endif
  141. char buff[256];
  142. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
  143. save_weights(net, buff);
  144. }
  145. free_data(train);
  146. }
  147. #ifdef GPU
  148. if(ngpus != 1) sync_nets(nets, ngpus, 0);
  149. #endif
  150. char buff[256];
  151. sprintf(buff, "%s/%s_final.weights", backup_directory, base);
  152. save_weights(net, buff);
  153. }
  154. static int get_coco_image_id(char *filename)
  155. {
  156. char *p = strrchr(filename, '_');
  157. return atoi(p + 1);
  158. }
  159. static void print_cocos(FILE *fp, char *image_path, box *boxes, float **probs, int num_boxes, int classes, int w, int h)
  160. {
  161. int i, j;
  162. int image_id = get_coco_image_id(image_path);
  163. for (i = 0; i < num_boxes; ++i){
  164. float xmin = boxes[i].x - boxes[i].w / 2.f;
  165. float xmax = boxes[i].x + boxes[i].w / 2.f;
  166. float ymin = boxes[i].y - boxes[i].h / 2.f;
  167. float ymax = boxes[i].y + boxes[i].h / 2.f;
  168. if (xmin < 0.f) xmin = 0.f;
  169. if (ymin < 0.f) ymin = 0.f;
  170. if (xmax >(float)w) xmax = (float)w;
  171. if (ymax >(float)h) ymax = (float)h;
  172. float bx = xmin;
  173. float by = ymin;
  174. float bw = xmax - xmin;
  175. float bh = ymax - ymin;
  176. for (j = 0; j < classes; ++j){
  177. if (probs[i][j]) fprintf(fp, "{\"image_id\":%d, \"category_id\":%d, \"bbox\":[%f, %f, %f, %f], \"score\":%f},\n", image_id, coco_ids[j], bx, by, bw, bh, probs[i][j]);
  178. }
  179. }
  180. }
  181. void print_detector_detections(FILE **fps, char *id, box *boxes, float **probs, int total, int classes, int w, int h)
  182. {
  183. int i, j;
  184. for (i = 0; i < total; ++i){
  185. float xmin = boxes[i].x - boxes[i].w / 2.f + 1.f;
  186. float xmax = boxes[i].x + boxes[i].w / 2.f + 1.f;
  187. float ymin = boxes[i].y - boxes[i].h / 2.f + 1.f;
  188. float ymax = boxes[i].y + boxes[i].h / 2.f + 1.f;
  189. if (xmin < 1.f) xmin = 1.f;
  190. if (ymin < 1.f) ymin = 1.f;
  191. if (xmax >(float)w) xmax = (float)w;
  192. if (ymax >(float)h) ymax = (float)h;
  193. for (j = 0; j < classes; ++j){
  194. if (probs[i][j]) fprintf(fps[j], "%s %f %f %f %f %f\n", id, probs[i][j],
  195. xmin, ymin, xmax, ymax);
  196. }
  197. }
  198. }
  199. void print_imagenet_detections(FILE *fp, int id, box *boxes, float **probs, int total, int classes, int w, int h)
  200. {
  201. int i, j;
  202. for (i = 0; i < total; ++i){
  203. float xmin = boxes[i].x - boxes[i].w / 2.f;
  204. float xmax = boxes[i].x + boxes[i].w / 2.f;
  205. float ymin = boxes[i].y - boxes[i].h / 2.f;
  206. float ymax = boxes[i].y + boxes[i].h / 2.f;
  207. if (xmin < 0.f) xmin = 0.f;
  208. if (ymin < 0.f) ymin = 0.f;
  209. if (xmax >(float)w) xmax = (float)w;
  210. if (ymax >(float)h) ymax = (float)h;
  211. for (j = 0; j < classes; ++j){
  212. int cls = j;
  213. if (probs[i][cls]) fprintf(fp, "%d %d %f %f %f %f %f\n", id, j + 1, probs[i][cls],
  214. xmin, ymin, xmax, ymax);
  215. }
  216. }
  217. }
  218. void validate_detector_flip(char *datacfg, char *cfgfile, char *weightfile, char *outfile)
  219. {
  220. int j;
  221. list *options = read_data_cfg(datacfg);
  222. char *valid_images = option_find_str(options, "valid", "data/train.list");
  223. char *name_list = option_find_str(options, "names", "data/names.list");
  224. char *prefix = option_find_str(options, "results", "results");
  225. char **names = get_labels(name_list);
  226. char *mapf = option_find_str(options, "map", 0);
  227. int *map = 0;
  228. if (mapf) map = read_map(mapf);
  229. network net = parse_network_cfg(cfgfile);
  230. if (weightfile){
  231. load_weights(&net, weightfile);
  232. }
  233. set_batch_network(&net, 2);
  234. fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  235. srand((unsigned int)time(0));
  236. list *plist = get_paths(valid_images);
  237. char **paths = (char **)list_to_array(plist);
  238. layer l = net.layers[net.n - 1];
  239. int classes = l.classes;
  240. char buff[1024];
  241. char *type = option_find_str(options, "eval", "voc");
  242. FILE *fp = 0;
  243. FILE **fps = 0;
  244. int coco = 0;
  245. int imagenet = 0;
  246. if (0 == strcmp(type, "coco")){
  247. if (!outfile) outfile = "coco_results";
  248. _snprintf(buff, 1024, "%s/%s.json", prefix, outfile);
  249. fp = fopen(buff, "w");
  250. fprintf(fp, "[\n");
  251. coco = 1;
  252. }
  253. else if (0 == strcmp(type, "imagenet")){
  254. if (!outfile) outfile = "imagenet-detection";
  255. _snprintf(buff, 1024, "%s/%s.txt", prefix, outfile);
  256. fp = fopen(buff, "w");
  257. imagenet = 1;
  258. classes = 200;
  259. }
  260. else {
  261. if (!outfile) outfile = "comp4_det_test_";
  262. fps = (FILE **)calloc(classes, sizeof(FILE *));
  263. for (j = 0; j < classes; ++j){
  264. _snprintf(buff, 1024, "%s/%s%s.txt", prefix, outfile, names[j]);
  265. fps[j] = fopen(buff, "w");
  266. }
  267. }
  268. box *boxes = (box*)calloc(l.w*l.h*l.n, sizeof(box));
  269. float **probs = (float**)calloc(l.w*l.h*l.n, sizeof(float *));
  270. for (j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(classes + 1, sizeof(float *));
  271. int m = plist->size;
  272. int i = 0;
  273. int t;
  274. float thresh = .005f;
  275. float nms = .45f;
  276. int nthreads = 4;
  277. image *val = (image*)calloc(nthreads, sizeof(image));
  278. image *val_resized = (image*)calloc(nthreads, sizeof(image));
  279. image *buf = (image*)calloc(nthreads, sizeof(image));
  280. image *buf_resized = (image*)calloc(nthreads, sizeof(image));
  281. #ifdef __linux__
  282. pthread_t *thr = calloc(nthreads, sizeof(pthread_t));
  283. #else
  284. HANDLE *thr = (HANDLE*)calloc(nthreads, sizeof(HANDLE));
  285. #endif
  286. image input = make_image(net.w, net.h, net.c * 2);
  287. load_args args = { 0 };
  288. args.w = net.w;
  289. args.h = net.h;
  290. //args.type = IMAGE_DATA;
  291. args.type = LETTERBOX_DATA;
  292. for (t = 0; t < nthreads; ++t){
  293. args.path = paths[i + t];
  294. args.im = &buf[t];
  295. args.resized = &buf_resized[t];
  296. thr[t] = load_data_in_thread(args);
  297. }
  298. time_t start = time(0);
  299. for (i = nthreads; i < m + nthreads; i += nthreads){
  300. fprintf(stderr, "%d\n", i);
  301. for (t = 0; t < nthreads && i + t - nthreads < m; ++t){
  302. #ifdef __linux__
  303. pthread_join(thr[t], 0);
  304. #else
  305. WaitForSingleObject(thr[t], INFINITE);
  306. CloseHandle(thr[t]);
  307. #endif
  308. val[t] = buf[t];
  309. val_resized[t] = buf_resized[t];
  310. }
  311. for (t = 0; t < nthreads && i + t < m; ++t){
  312. args.path = paths[i + t];
  313. args.im = &buf[t];
  314. args.resized = &buf_resized[t];
  315. thr[t] = load_data_in_thread(args);
  316. }
  317. for (t = 0; t < nthreads && i + t - nthreads < m; ++t){
  318. char *path = paths[i + t - nthreads];
  319. char *id = basecfg(path);
  320. copy_cpu(net.w*net.h*net.c, val_resized[t].data, 1, input.data, 1);
  321. flip_image(val_resized[t]);
  322. copy_cpu(net.w*net.h*net.c, val_resized[t].data, 1, input.data + net.w*net.h*net.c, 1);
  323. network_predict(net, input.data);
  324. int w = val[t].w;
  325. int h = val[t].h;
  326. get_region_boxes(l, w, h, net.w, net.h, thresh, probs, boxes, 0, 0, map, .5, 0);
  327. if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, classes, nms);
  328. if (coco){
  329. print_cocos(fp, path, boxes, probs, l.w*l.h*l.n, classes, w, h);
  330. }
  331. else if (imagenet){
  332. print_imagenet_detections(fp, i + t - nthreads + 1, boxes, probs, l.w*l.h*l.n, classes, w, h);
  333. }
  334. else {
  335. print_detector_detections(fps, id, boxes, probs, l.w*l.h*l.n, classes, w, h);
  336. }
  337. free(id);
  338. free_image(val[t]);
  339. free_image(val_resized[t]);
  340. }
  341. }
  342. for (j = 0; j < classes; ++j){
  343. if (fps) fclose(fps[j]);
  344. }
  345. if (coco){
  346. fseek(fp, -2, SEEK_CUR);
  347. fprintf(fp, "\n]\n");
  348. fclose(fp);
  349. }
  350. fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
  351. }
  352. void validate_detector(char *datacfg, char *cfgfile, char *weightfile, char *outfile)
  353. {
  354. int j;
  355. list *options = read_data_cfg(datacfg);
  356. char *valid_images = option_find_str(options, "valid", "data/train.list");
  357. char *name_list = option_find_str(options, "names", "data/names.list");
  358. char *prefix = option_find_str(options, "results", "results");
  359. char **names = get_labels(name_list);
  360. char *mapf = option_find_str(options, "map", 0);
  361. int *map = 0;
  362. if (mapf) map = read_map(mapf);
  363. network net = parse_network_cfg(cfgfile);
  364. if (weightfile){
  365. load_weights(&net, weightfile);
  366. }
  367. set_batch_network(&net, 1);
  368. fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  369. srand((unsigned int)time(0));
  370. list *plist = get_paths(valid_images);
  371. char **paths = (char **)list_to_array(plist);
  372. layer l = net.layers[net.n - 1];
  373. int classes = l.classes;
  374. char buff[1024];
  375. char *type = option_find_str(options, "eval", "voc");
  376. FILE *fp = 0;
  377. FILE **fps = 0;
  378. int coco = 0;
  379. int imagenet = 0;
  380. if (0 == strcmp(type, "coco")){
  381. if (!outfile) outfile = "coco_results";
  382. _snprintf(buff, 1024, "%s/%s.json", prefix, outfile);
  383. fp = fopen(buff, "w");
  384. fprintf(fp, "[\n");
  385. coco = 1;
  386. }
  387. else if (0 == strcmp(type, "imagenet")){
  388. if (!outfile) outfile = "imagenet-detection";
  389. _snprintf(buff, 1024, "%s/%s.txt", prefix, outfile);
  390. fp = fopen(buff, "w");
  391. imagenet = 1;
  392. classes = 200;
  393. }
  394. else {
  395. if (!outfile) outfile = "comp4_det_test_";
  396. fps = (FILE**)calloc(classes, sizeof(FILE *));
  397. for (j = 0; j < classes; ++j){
  398. _snprintf(buff, 1024, "%s/%s%s.txt", prefix, outfile, names[j]);
  399. fps[j] = fopen(buff, "w");
  400. }
  401. }
  402. box *boxes = (box*)calloc(l.w*l.h*l.n, sizeof(box));
  403. float **probs = (float**)calloc(l.w*l.h*l.n, sizeof(float *));
  404. for (j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(classes + 1, sizeof(float *));
  405. int m = plist->size;
  406. int i = 0;
  407. int t;
  408. float thresh = .005f;
  409. float nms = .45f;
  410. int nthreads = 4;
  411. image *val = (image*)calloc(nthreads, sizeof(image));
  412. image *val_resized = (image*)calloc(nthreads, sizeof(image));
  413. image *buf = (image*)calloc(nthreads, sizeof(image));
  414. image *buf_resized = (image*)calloc(nthreads, sizeof(image));
  415. #ifdef __linux__
  416. pthread_t *thr = calloc(nthreads, sizeof(pthread_t));
  417. #else
  418. HANDLE *thr = (HANDLE*)calloc(nthreads, sizeof(HANDLE));
  419. #endif
  420. load_args args = { 0 };
  421. args.w = net.w;
  422. args.h = net.h;
  423. //args.type = IMAGE_DATA;
  424. args.type = LETTERBOX_DATA;
  425. for (t = 0; t < nthreads; ++t){
  426. args.path = paths[i + t];
  427. args.im = &buf[t];
  428. args.resized = &buf_resized[t];
  429. thr[t] = load_data_in_thread(args);
  430. }
  431. time_t start = time(0);
  432. for (i = nthreads; i < m + nthreads; i += nthreads){
  433. fprintf(stderr, "%d\n", i);
  434. for (t = 0; t < nthreads && i + t - nthreads < m; ++t){
  435. #ifdef __linux__
  436. pthread_join(thr[t], 0);
  437. #else
  438. WaitForSingleObject(thr[t], INFINITE);
  439. CloseHandle(thr[t]);
  440. #endif
  441. val[t] = buf[t];
  442. val_resized[t] = buf_resized[t];
  443. }
  444. for (t = 0; t < nthreads && i + t < m; ++t){
  445. args.path = paths[i + t];
  446. args.im = &buf[t];
  447. args.resized = &buf_resized[t];
  448. thr[t] = load_data_in_thread(args);
  449. }
  450. for (t = 0; t < nthreads && i + t - nthreads < m; ++t){
  451. char *path = paths[i + t - nthreads];
  452. char *id = basecfg(path);
  453. float *X = val_resized[t].data;
  454. network_predict(net, X);
  455. int w = val[t].w;
  456. int h = val[t].h;
  457. get_region_boxes(l, w, h, net.w, net.h, thresh, probs, boxes, 0, 0, map, .5, 0);
  458. if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, classes, nms);
  459. if (coco){
  460. print_cocos(fp, path, boxes, probs, l.w*l.h*l.n, classes, w, h);
  461. }
  462. else if (imagenet){
  463. print_imagenet_detections(fp, i + t - nthreads + 1, boxes, probs, l.w*l.h*l.n, classes, w, h);
  464. }
  465. else {
  466. print_detector_detections(fps, id, boxes, probs, l.w*l.h*l.n, classes, w, h);
  467. }
  468. free(id);
  469. free_image(val[t]);
  470. free_image(val_resized[t]);
  471. }
  472. }
  473. for (j = 0; j < classes; ++j){
  474. if (fps) fclose(fps[j]);
  475. }
  476. if (coco){
  477. fseek(fp, -2, SEEK_CUR);
  478. fprintf(fp, "\n]\n");
  479. fclose(fp);
  480. }
  481. fprintf(stderr, "Total Detection Time: %f Seconds\n", (double)(time(0) - start));
  482. }
  483. void validate_detector_recall(char *cfgfile, char *weightfile)
  484. {
  485. network net = parse_network_cfg(cfgfile);
  486. if (weightfile){
  487. load_weights(&net, weightfile);
  488. }
  489. set_batch_network(&net, 1);
  490. fprintf(stderr, "Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  491. srand((unsigned int)time(0));
  492. list *plist = get_paths("data/coco_val_5k.list");
  493. char **paths = (char **)list_to_array(plist);
  494. layer l = net.layers[net.n - 1];
  495. int classes = l.classes;
  496. int j, k;
  497. box *boxes = (box*)calloc(l.w*l.h*l.n, sizeof(box));
  498. float **probs = (float**)calloc(l.w*l.h*l.n, sizeof(float *));
  499. for (j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(classes + 1, sizeof(float *));
  500. int m = plist->size;
  501. int i = 0;
  502. float thresh = .001f;
  503. float iou_thresh = .5f;
  504. float nms = .4f;
  505. int total = 0;
  506. int correct = 0;
  507. int proposals = 0;
  508. float avg_iou = 0;
  509. for (i = 0; i < m; ++i){
  510. char *path = paths[i];
  511. image orig = load_image_color(path, 0, 0);
  512. image sized = resize_image(orig, net.w, net.h);
  513. char *id = basecfg(path);
  514. network_predict(net, sized.data);
  515. get_region_boxes(l, sized.w, sized.h, net.w, net.h, thresh, probs, boxes, 0, 1, 0, .5, 1);
  516. if (nms) do_nms(boxes, probs, l.w*l.h*l.n, 1, nms);
  517. char labelpath[4096];
  518. find_replace(path, "images", "labels", labelpath);
  519. find_replace(labelpath, "JPEGImages", "labels", labelpath);
  520. find_replace(labelpath, ".jpg", ".txt", labelpath);
  521. find_replace(labelpath, ".JPEG", ".txt", labelpath);
  522. int num_labels = 0;
  523. box_label *truth = read_boxes(labelpath, &num_labels);
  524. for (k = 0; k < l.w*l.h*l.n; ++k){
  525. if (probs[k][0] > thresh){
  526. ++proposals;
  527. }
  528. }
  529. for (j = 0; j < num_labels; ++j) {
  530. ++total;
  531. box t = { truth[j].x, truth[j].y, truth[j].w, truth[j].h };
  532. float best_iou = 0;
  533. for (k = 0; k < l.w*l.h*l.n; ++k){
  534. float iou = box_iou(boxes[k], t);
  535. if (probs[k][0] > thresh && iou > best_iou){
  536. best_iou = iou;
  537. }
  538. }
  539. avg_iou += best_iou;
  540. if (best_iou > iou_thresh){
  541. ++correct;
  542. }
  543. }
  544. 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);
  545. free(id);
  546. free_image(orig);
  547. free_image(sized);
  548. }
  549. }
  550. void test_detector(char *datacfg, char *cfgfile, char *weightfile, char *filename, float thresh, float hier_thresh, char *outfile, int fullscreen)
  551. {
  552. list *options = read_data_cfg(datacfg);
  553. char *name_list = option_find_str(options, "names", "data/names.list");
  554. char **names = get_labels(name_list);
  555. image **alphabet = load_alphabet();
  556. network net = parse_network_cfg(cfgfile);
  557. if (weightfile){
  558. load_weights(&net, weightfile);
  559. }
  560. set_batch_network(&net, 1);
  561. srand(2222222);
  562. double time;
  563. char buff[256];
  564. char *input = buff;
  565. int j;
  566. float nms = .3f;
  567. while (1){
  568. if (filename){
  569. strncpy(input, filename, 256);
  570. }
  571. else {
  572. printf("Enter Image Path: ");
  573. fflush(stdout);
  574. input = fgets(input, 256, stdin);
  575. if (!input) return;
  576. strtok(input, "\n");
  577. }
  578. image im = load_image_color(input, 0, 0);
  579. image sized = letterbox_image(im, net.w, net.h);
  580. //image sized = resize_image(im, net.w, net.h);
  581. //image sized2 = resize_max(im, net.w);
  582. //image sized = crop_image(sized2, -((net.w - sized2.w)/2), -((net.h - sized2.h)/2), net.w, net.h);
  583. //resize_network(&net, sized.w, sized.h);
  584. layer l = net.layers[net.n - 1];
  585. box *boxes = (box*)calloc(l.w*l.h*l.n, sizeof(box));
  586. float **probs = (float**)calloc(l.w*l.h*l.n, sizeof(float *));
  587. for (j = 0; j < l.w*l.h*l.n; ++j) probs[j] = (float*)calloc(l.classes + 1, sizeof(float *));
  588. float **masks = 0;
  589. if (l.coords > 4){
  590. masks = (float**)calloc(l.w*l.h*l.n, sizeof(float*));
  591. for (j = 0; j < l.w*l.h*l.n; ++j) masks[j] = (float*)calloc(l.coords - 4, sizeof(float *));
  592. }
  593. float *X = sized.data;
  594. time = what_time_is_it_now();
  595. network_predict(net, X);
  596. printf("%s: Predicted in %f seconds.\n", input, what_time_is_it_now() - time);
  597. get_region_boxes(l, im.w, im.h, net.w, net.h, thresh, probs, boxes, masks, 0, 0, hier_thresh, 1);
  598. if (nms) do_nms_obj(boxes, probs, l.w*l.h*l.n, l.classes, nms);
  599. //else if (nms) do_nms_sort(boxes, probs, l.w*l.h*l.n, l.classes, nms);
  600. draw_detections(im, l.w*l.h*l.n, thresh, boxes, probs, masks, names, alphabet, l.classes);
  601. if (outfile){
  602. save_image(im, outfile);
  603. }
  604. else{
  605. save_image(im, "predictions");
  606. #ifdef OPENCV
  607. cvNamedWindow("predictions", CV_WINDOW_NORMAL);
  608. if (fullscreen){
  609. cvSetWindowProperty("predictions", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
  610. }
  611. show_image(im, "predictions");
  612. cvWaitKey(0);
  613. cvDestroyAllWindows();
  614. #endif
  615. }
  616. free_image(im);
  617. free_image(sized);
  618. free(boxes);
  619. free_ptrs((void **)probs, l.w*l.h*l.n);
  620. if (filename) break;
  621. }
  622. }
  623. void run_detector(int argc, char **argv)
  624. {
  625. char *prefix = find_char_arg(argc, argv, "-prefix", 0);
  626. float thresh = find_float_arg(argc, argv, "-thresh", .24f);
  627. float hier_thresh = find_float_arg(argc, argv, "-hier", .5f);
  628. int cam_index = find_int_arg(argc, argv, "-c", 0);
  629. int frame_skip = find_int_arg(argc, argv, "-s", 0);
  630. int avg = find_int_arg(argc, argv, "-avg", 3);
  631. if (argc < 4){
  632. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  633. return;
  634. }
  635. char *gpu_list = find_char_arg(argc, argv, "-gpus", 0);
  636. char *outfile = find_char_arg(argc, argv, "-out", 0);
  637. int *gpus = 0;
  638. int gpu = 0;
  639. int ngpus = 0;
  640. if (gpu_list){
  641. printf("%s\n", gpu_list);
  642. int len = strlen(gpu_list);
  643. ngpus = 1;
  644. int i;
  645. for (i = 0; i < len; ++i){
  646. if (gpu_list[i] == ',') ++ngpus;
  647. }
  648. gpus = (int*)calloc(ngpus, sizeof(int));
  649. for (i = 0; i < ngpus; ++i){
  650. gpus[i] = atoi(gpu_list);
  651. gpu_list = strchr(gpu_list, ',') + 1;
  652. }
  653. }
  654. else {
  655. gpu = gpu_index;
  656. gpus = &gpu;
  657. ngpus = 1;
  658. }
  659. int clear = find_arg(argc, argv, "-clear");
  660. int fullscreen = find_arg(argc, argv, "-fullscreen");
  661. int width = find_int_arg(argc, argv, "-w", 0);
  662. int height = find_int_arg(argc, argv, "-h", 0);
  663. int fps = find_int_arg(argc, argv, "-fps", 0);
  664. char *datacfg = argv[3];
  665. char *cfg = argv[4];
  666. char *weights = (argc > 5) ? argv[5] : 0;
  667. char *filename = (argc > 6) ? argv[6] : 0;
  668. if (0 == strcmp(argv[2], "test")) test_detector(datacfg, cfg, weights, filename, thresh, hier_thresh, outfile, fullscreen);
  669. else if (0 == strcmp(argv[2], "train")) train_detector(datacfg, cfg, weights, gpus, ngpus, clear);
  670. else if (0 == strcmp(argv[2], "valid")) validate_detector(datacfg, cfg, weights, outfile);
  671. else if (0 == strcmp(argv[2], "valid2")) validate_detector_flip(datacfg, cfg, weights, outfile);
  672. else if (0 == strcmp(argv[2], "recall")) validate_detector_recall(cfg, weights);
  673. else if (0 == strcmp(argv[2], "demo")) {
  674. list *options = read_data_cfg(datacfg);
  675. int classes = option_find_int(options, "classes", 20);
  676. char *name_list = option_find_str(options, "names", "data/names.list");
  677. char **names = get_labels(name_list);
  678. demo(cfg, weights, thresh, cam_index, filename, names, classes, frame_skip, prefix, avg, hier_thresh, width, height, fps, fullscreen);
  679. }
  680. }
  681. };