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

compute_image_mean.cpp 3.5 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  1. #include <stdint.h>
  2. #include <algorithm>
  3. #include <string>
  4. #include <utility>
  5. #include <vector>
  6. #include "boost/scoped_ptr.hpp"
  7. #include "gflags/gflags.h"
  8. #include "glog/logging.h"
  9. #include "caffe/proto/caffe.pb.h"
  10. #include "caffe/util/db.hpp"
  11. #include "caffe/util/io.hpp"
  12. using namespace caffe; // NOLINT(build/namespaces)
  13. using std::max;
  14. using std::pair;
  15. using boost::scoped_ptr;
  16. DEFINE_string(backend, "lmdb",
  17. "The backend {leveldb, lmdb} containing the images");
  18. int main(int argc, char** argv) {
  19. #ifdef USE_OPENCV
  20. ::google::InitGoogleLogging(argv[0]);
  21. // Print output to stderr (while still logging)
  22. FLAGS_alsologtostderr = 1;
  23. #ifndef GFLAGS_GFLAGS_H_
  24. namespace gflags = google;
  25. #endif
  26. gflags::SetUsageMessage("Compute the mean_image of a set of images given by"
  27. " a leveldb/lmdb\n"
  28. "Usage:\n"
  29. " compute_image_mean [FLAGS] INPUT_DB [OUTPUT_FILE]\n");
  30. gflags::ParseCommandLineFlags(&argc, &argv, true);
  31. if (argc < 2 || argc > 3) {
  32. gflags::ShowUsageWithFlagsRestrict(argv[0], "tools/compute_image_mean");
  33. return 1;
  34. }
  35. scoped_ptr<db::DB> db(db::GetDB(FLAGS_backend));
  36. db->Open(argv[1], db::READ);
  37. scoped_ptr<db::Cursor> cursor(db->NewCursor());
  38. BlobProto sum_blob;
  39. int count = 0;
  40. // load first datum
  41. Datum datum;
  42. datum.ParseFromString(cursor->value());
  43. if (DecodeDatumNative(&datum)) {
  44. LOG(INFO) << "Decoding Datum";
  45. }
  46. sum_blob.set_num(1);
  47. sum_blob.set_channels(datum.channels());
  48. sum_blob.set_height(datum.height());
  49. sum_blob.set_width(datum.width());
  50. const int data_size = datum.channels() * datum.height() * datum.width();
  51. int size_in_datum = std::max<int>(datum.data().size(),
  52. datum.float_data_size());
  53. for (int i = 0; i < size_in_datum; ++i) {
  54. sum_blob.add_data(0.);
  55. }
  56. LOG(INFO) << "Starting iteration";
  57. while (cursor->valid()) {
  58. Datum datum;
  59. datum.ParseFromString(cursor->value());
  60. DecodeDatumNative(&datum);
  61. const std::string& data = datum.data();
  62. size_in_datum = std::max<int>(datum.data().size(),
  63. datum.float_data_size());
  64. CHECK_EQ(size_in_datum, data_size) << "Incorrect data field size " <<
  65. size_in_datum;
  66. if (data.size() != 0) {
  67. CHECK_EQ(data.size(), size_in_datum);
  68. for (int i = 0; i < size_in_datum; ++i) {
  69. sum_blob.set_data(i, sum_blob.data(i) + (uint8_t)data[i]);
  70. }
  71. } else {
  72. CHECK_EQ(datum.float_data_size(), size_in_datum);
  73. for (int i = 0; i < size_in_datum; ++i) {
  74. sum_blob.set_data(i, sum_blob.data(i) +
  75. static_cast<float>(datum.float_data(i)));
  76. }
  77. }
  78. ++count;
  79. if (count % 10000 == 0) {
  80. LOG(INFO) << "Processed " << count << " files.";
  81. }
  82. cursor->Next();
  83. }
  84. if (count % 10000 != 0) {
  85. LOG(INFO) << "Processed " << count << " files.";
  86. }
  87. for (int i = 0; i < sum_blob.data_size(); ++i) {
  88. sum_blob.set_data(i, sum_blob.data(i) / count);
  89. }
  90. // Write to disk
  91. if (argc == 3) {
  92. LOG(INFO) << "Write to " << argv[2];
  93. WriteProtoToBinaryFile(sum_blob, argv[2]);
  94. }
  95. const int channels = sum_blob.channels();
  96. const int dim = sum_blob.height() * sum_blob.width();
  97. std::vector<float> mean_values(channels, 0.0);
  98. LOG(INFO) << "Number of channels: " << channels;
  99. for (int c = 0; c < channels; ++c) {
  100. for (int i = 0; i < dim; ++i) {
  101. mean_values[c] += sum_blob.data(dim * c + i);
  102. }
  103. LOG(INFO) << "mean_value channel [" << c << "]: " << mean_values[c] / dim;
  104. }
  105. #else
  106. LOG(FATAL) << "This tool requires OpenCV; compile with USE_OPENCV.";
  107. #endif // USE_OPENCV
  108. return 0;
  109. }
Tip!

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

Comments

Loading...