Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

data_transformer.hpp 1.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  1. #ifndef data_transformer_hpp
  2. #define data_transformer_hpp
  3. #ifndef CPU_ONLY
  4. #define CPU_ONLY
  5. #endif
  6. #include "opencv2/opencv.hpp"
  7. #include <eigen3/Eigen/Eigen>
  8. #include <caffe/caffe.hpp>
  9. /**
  10. * @Warning: The project root must be changed
  11. */
  12. const std::string project_root("/Users/liuyang/Desktop/AgeAndGenderEstimation/Source/");
  13. /**
  14. * @Brief: The data transform from OpenCV to Eigen without copy
  15. *
  16. * @param image: OpenCV Mat data
  17. *
  18. * @return Eigen::Matrix data
  19. */
  20. Eigen::MatrixXf OpenCV2Eigen(const cv::Mat& image);
  21. /**
  22. * @Brief: Translate the Eigen Mat to caffe input blob using copy function
  23. *
  24. * @param imgs: The data in the Eigen Mat
  25. * @param net: The destination of data transferring
  26. *
  27. * @Warning: Template function must be defined in the .hpp file to avoid
  28. * linking error
  29. */
  30. template <typename Dtype>
  31. void Eigen2Blob(const std::vector<std::vector<Eigen::MatrixXf>> imgs, std::shared_ptr<caffe::Net<Dtype>> net)
  32. {
  33. caffe::Blob<Dtype>* input_layer = net->input_blobs()[0];
  34. Dtype* input_data = input_layer->mutable_cpu_data();
  35. unsigned long img_number = imgs.size();
  36. unsigned long img_channel = imgs[0].size();
  37. unsigned long img_height = imgs[0][0].rows();
  38. unsigned long img_width = imgs[0][0].cols();
  39. unsigned long index = 0;
  40. for (int i = 0; i < img_number; i++)
  41. {
  42. for (int c = 0; c < img_channel; c++)
  43. {
  44. for (int h = 0; h < img_height; h++)
  45. {
  46. for (int w = 0; w < img_width; w++)
  47. {
  48. *(input_data + index) = imgs[i][c](h, w);
  49. index++;
  50. }
  51. }
  52. }
  53. }
  54. }
  55. #endif /* data_transformer_hpp */
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...