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.

212 lines
6.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "darknet.h"
  2. #include "../src/image.h"
  3. #include <opencv2/opencv.hpp>
  4. using namespace cv;
  5. namespace DN{
  6. #ifdef OPENCV
  7. //image get_image_from_stream(CvCapture *cap);
  8. //image ipl_to_image(IplImage* src);
  9. void reconstruct_picture(network net, float *features, image recon, image update, float rate, float momentum, float lambda, int smooth_size, int iters);
  10. typedef struct {
  11. float *x;
  12. float *y;
  13. } float_pair;
  14. float_pair get_rnn_vid_data(network net, char **files, int n, int batch, int steps)
  15. {
  16. int b;
  17. assert(net.batch == steps + 1);
  18. image out_im = get_network_image(net);
  19. int output_size = out_im.w*out_im.h*out_im.c;
  20. printf("%d %d %d\n", out_im.w, out_im.h, out_im.c);
  21. float *feats = (float*)calloc(net.batch*batch*output_size, sizeof(float));
  22. for (b = 0; b < batch; ++b){
  23. int input_size = net.w*net.h*net.c;
  24. float *input = (float*)calloc(input_size*net.batch, sizeof(float));
  25. char *filename = files[rand() % n];
  26. CvCapture *cap = cvCaptureFromFile(filename);
  27. int frames = cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_COUNT);
  28. int index = rand() % (frames - steps - 2);
  29. if (frames < (steps + 4)){
  30. --b;
  31. free(input);
  32. continue;
  33. }
  34. printf("frames: %d, index: %d\n", frames, index);
  35. cvSetCaptureProperty(cap, CV_CAP_PROP_POS_FRAMES, index);
  36. int i;
  37. for (i = 0; i < net.batch; ++i){
  38. IplImage* src = cvQueryFrame(cap);
  39. image im = ipl_to_image(src);
  40. rgbgr_image(im);
  41. image re = resize_image(im, net.w, net.h);
  42. //show_image(re, "loaded");
  43. //cvWaitKey(10);
  44. memcpy(input + i*input_size, re.data, input_size*sizeof(float));
  45. free_image(im);
  46. free_image(re);
  47. }
  48. float *output = network_predict(net, input);
  49. free(input);
  50. for (i = 0; i < net.batch; ++i){
  51. memcpy(feats + (b + i*batch)*output_size, output + i*output_size, output_size*sizeof(float));
  52. }
  53. cvReleaseCapture(&cap);
  54. }
  55. //printf("%d %d %d\n", out_im.w, out_im.h, out_im.c);
  56. float_pair p = { 0 };
  57. p.x = feats;
  58. p.y = feats + output_size*batch; //+ out_im.w*out_im.h*out_im.c;
  59. return p;
  60. }
  61. void train_vid_rnn(char *cfgfile, char *weightfile)
  62. {
  63. char *train_videos = "data/vid/train.txt";
  64. char *backup_directory = "/home/pjreddie/backup/";
  65. srand(time(0));
  66. char *base = basecfg(cfgfile);
  67. printf("%s\n", base);
  68. float avg_loss = -1;
  69. network net = parse_network_cfg(cfgfile);
  70. if (weightfile){
  71. load_weights(&net, weightfile);
  72. }
  73. printf("Learning Rate: %g, Momentum: %g, Decay: %g\n", net.learning_rate, net.momentum, net.decay);
  74. int imgs = net.batch*net.subdivisions;
  75. int i = *net.seen / imgs;
  76. list *plist = get_paths(train_videos);
  77. int N = plist->size;
  78. char **paths = (char **)list_to_array(plist);
  79. clock_t time;
  80. int steps = net.time_steps;
  81. int batch = net.batch / net.time_steps;
  82. network extractor = parse_network_cfg("cfg/extractor.cfg");
  83. load_weights(&extractor, "/home/pjreddie/trained/yolo-coco.conv");
  84. while (get_current_batch(net) < net.max_batches){
  85. i += 1;
  86. time = clock();
  87. float_pair p = get_rnn_vid_data(extractor, paths, N, batch, steps);
  88. copy_cpu(net.inputs*net.batch, p.x, 1, net.input, 1);
  89. copy_cpu(net.truths*net.batch, p.y, 1, net.truth, 1);
  90. float loss = train_network_datum(net) / (net.batch);
  91. free(p.x);
  92. if (avg_loss < 0) avg_loss = loss;
  93. avg_loss = avg_loss*.9 + loss*.1;
  94. fprintf(stderr, "%d: %f, %f avg, %f rate, %lf seconds\n", i, loss, avg_loss, get_current_rate(net), sec(clock() - time));
  95. if (i % 100 == 0){
  96. char buff[256];
  97. sprintf(buff, "%s/%s_%d.weights", backup_directory, base, i);
  98. save_weights(net, buff);
  99. }
  100. if (i % 10 == 0){
  101. char buff[256];
  102. sprintf(buff, "%s/%s.backup", backup_directory, base);
  103. save_weights(net, buff);
  104. }
  105. }
  106. char buff[256];
  107. sprintf(buff, "%s/%s_final.weights", backup_directory, base);
  108. save_weights(net, buff);
  109. }
  110. image save_reconstruction(network net, image *init, float *feat, char *name, int i)
  111. {
  112. image recon;
  113. if (init) {
  114. recon = copy_image(*init);
  115. }
  116. else {
  117. recon = make_random_image(net.w, net.h, 3);
  118. }
  119. image update = make_image(net.w, net.h, 3);
  120. reconstruct_picture(net, feat, recon, update, .01, .9, .1, 2, 50);
  121. char buff[256];
  122. sprintf(buff, "%s%d", name, i);
  123. save_image(recon, buff);
  124. free_image(update);
  125. return recon;
  126. }
  127. void generate_vid_rnn(char *cfgfile, char *weightfile)
  128. {
  129. network extractor = parse_network_cfg("cfg/extractor.recon.cfg");
  130. load_weights(&extractor, "/home/pjreddie/trained/yolo-coco.conv");
  131. network net = parse_network_cfg(cfgfile);
  132. if (weightfile){
  133. load_weights(&net, weightfile);
  134. }
  135. set_batch_network(&extractor, 1);
  136. set_batch_network(&net, 1);
  137. int i;
  138. CvCapture *cap = cvCaptureFromFile("/extra/vid/ILSVRC2015/Data/VID/snippets/val/ILSVRC2015_val_00007030.mp4");
  139. float *feat;
  140. float *next;
  141. image last;
  142. for (i = 0; i < 25; ++i){
  143. image im = get_image_from_stream(cap);
  144. image re = resize_image(im, extractor.w, extractor.h);
  145. feat = network_predict(extractor, re.data);
  146. if (i > 0){
  147. printf("%f %f\n", mean_array(feat, 14 * 14 * 512), variance_array(feat, 14 * 14 * 512));
  148. printf("%f %f\n", mean_array(next, 14 * 14 * 512), variance_array(next, 14 * 14 * 512));
  149. printf("%f\n", mse_array(feat, 14 * 14 * 512));
  150. axpy_cpu(14 * 14 * 512, -1, feat, 1, next, 1);
  151. printf("%f\n", mse_array(next, 14 * 14 * 512));
  152. }
  153. next = network_predict(net, feat);
  154. free_image(im);
  155. free_image(save_reconstruction(extractor, 0, feat, "feat", i));
  156. free_image(save_reconstruction(extractor, 0, next, "next", i));
  157. if (i == 24) last = copy_image(re);
  158. free_image(re);
  159. }
  160. for (i = 0; i < 30; ++i){
  161. next = network_predict(net, next);
  162. image nw = save_reconstruction(extractor, &last, next, "new", i);
  163. free_image(last);
  164. last = nw;
  165. }
  166. }
  167. void run_vid_rnn(int argc, char **argv)
  168. {
  169. if (argc < 4){
  170. fprintf(stderr, "usage: %s %s [train/test/valid] [cfg] [weights (optional)]\n", argv[0], argv[1]);
  171. return;
  172. }
  173. char *cfg = argv[3];
  174. char *weights = (argc > 4) ? argv[4] : 0;
  175. //char *filename = (argc > 5) ? argv[5]: 0;
  176. if (0 == strcmp(argv[2], "train")) train_vid_rnn(cfg, weights);
  177. else if (0 == strcmp(argv[2], "generate")) generate_vid_rnn(cfg, weights);
  178. }
  179. #else
  180. void run_vid_rnn(int argc, char **argv){}
  181. #endif
  182. };