|
5 | 5 | #include <stdint.h> |
6 | 6 | #include <pthread.h> |
7 | 7 |
|
| 8 | +#include <algorithm> |
8 | 9 | #include <string> |
9 | 10 | #include <vector> |
10 | 11 | #include <map> |
11 | | -#include <fstream> |
| 12 | +#include <fstream> // NOLINT(readability/streams) |
| 13 | +#include <utility> |
| 14 | + |
| 15 | +#include "opencv2/core/core.hpp" |
| 16 | +#include "opencv2/highgui/highgui.hpp" |
| 17 | +#include "opencv2/imgproc/imgproc.hpp" |
12 | 18 |
|
13 | 19 | #include "caffe/layer.hpp" |
14 | 20 | #include "caffe/util/io.hpp" |
15 | 21 | #include "caffe/vision_layers.hpp" |
16 | 22 |
|
17 | | -#include <opencv2/core/core.hpp> |
18 | | -#include <opencv2/highgui/highgui.hpp> |
19 | | -#include <opencv2/imgproc/imgproc.hpp> |
20 | | - |
21 | 23 | using std::string; |
22 | 24 | using std::map; |
23 | 25 | using std::pair; |
@@ -30,7 +32,7 @@ namespace caffe { |
30 | 32 |
|
31 | 33 | template <typename Dtype> |
32 | 34 | void* WindowDataLayerPrefetch(void* layer_pointer) { |
33 | | - WindowDataLayer<Dtype>* layer = |
| 35 | + WindowDataLayer<Dtype>* layer = |
34 | 36 | reinterpret_cast<WindowDataLayer<Dtype>*>(layer_pointer); |
35 | 37 |
|
36 | 38 | // At each iteration, sample N windows where N*p are foreground (object) |
@@ -56,7 +58,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
56 | 58 | // zero out batch |
57 | 59 | memset(top_data, 0, sizeof(Dtype)*layer->prefetch_data_->count()); |
58 | 60 |
|
59 | | - const int num_fg = static_cast<int>(static_cast<float>(batchsize) |
| 61 | + const int num_fg = static_cast<int>(static_cast<float>(batchsize) |
60 | 62 | * fg_fraction); |
61 | 63 | const int num_samples[2] = { batchsize - num_fg, num_fg }; |
62 | 64 |
|
@@ -65,23 +67,26 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
65 | 67 | for (int is_fg = 0; is_fg < 2; ++is_fg) { |
66 | 68 | for (int dummy = 0; dummy < num_samples[is_fg]; ++dummy) { |
67 | 69 | // sample a window |
68 | | - vector<float> window = (is_fg) |
| 70 | + vector<float> window = (is_fg) |
| 71 | + // NOLINT_NEXT_LINE(runtime/threadsafe_fn) |
69 | 72 | ? layer->fg_windows_[rand() % layer->fg_windows_.size()] |
| 73 | + // NOLINT_NEXT_LINE(runtime/threadsafe_fn) |
70 | 74 | : layer->bg_windows_[rand() % layer->bg_windows_.size()]; |
71 | 75 |
|
72 | 76 | bool do_mirror = false; |
| 77 | + // NOLINT_NEXT_LINE(runtime/threadsafe_fn) |
73 | 78 | if (mirror && rand() % 2) { |
74 | 79 | do_mirror = true; |
75 | 80 | } |
76 | 81 |
|
77 | 82 | // load the image containing the window |
78 | | - pair<std::string, vector<int> > image = |
| 83 | + pair<std::string, vector<int> > image = |
79 | 84 | layer->image_database_[window[WindowDataLayer<Dtype>::IMAGE_INDEX]]; |
80 | 85 |
|
81 | 86 | cv::Mat cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR); |
82 | 87 | if (!cv_img.data) { |
83 | 88 | LOG(ERROR) << "Could not open or find file " << image.first; |
84 | | - return (void*)NULL; |
| 89 | + return reinterpret_cast<void*>(NULL); |
85 | 90 | } |
86 | 91 | const int channels = cv_img.channels(); |
87 | 92 |
|
@@ -94,7 +99,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
94 | 99 | int pad_w = 0; |
95 | 100 | int pad_h = 0; |
96 | 101 | if (context_pad > 0 || use_square) { |
97 | | - // scale factor by which to expand the original region |
| 102 | + // scale factor by which to expand the original region |
98 | 103 | // such that after warping the expanded region to cropsize x cropsize |
99 | 104 | // there's exactly context_pad amount of padding on each side |
100 | 105 | Dtype context_scale = static_cast<Dtype>(cropsize) / |
@@ -116,7 +121,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
116 | 121 | x2 = static_cast<int>(round(center_x + half_width*context_scale)); |
117 | 122 | y1 = static_cast<int>(round(center_y - half_height*context_scale)); |
118 | 123 | y2 = static_cast<int>(round(center_y + half_height*context_scale)); |
119 | | - |
| 124 | + |
120 | 125 | // the expanded region may go outside of the image |
121 | 126 | // so we compute the clipped (expanded) region and keep track of |
122 | 127 | // the extent beyond the image |
@@ -139,17 +144,17 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
139 | 144 | int clipped_height = y2-y1+1; |
140 | 145 | int clipped_width = x2-x1+1; |
141 | 146 |
|
142 | | - // scale factors that would be used to warp the unclipped |
| 147 | + // scale factors that would be used to warp the unclipped |
143 | 148 | // expanded region |
144 | | - Dtype scale_x = |
| 149 | + Dtype scale_x = |
145 | 150 | static_cast<Dtype>(cropsize)/static_cast<Dtype>(unclipped_width); |
146 | | - Dtype scale_y = |
| 151 | + Dtype scale_y = |
147 | 152 | static_cast<Dtype>(cropsize)/static_cast<Dtype>(unclipped_height); |
148 | 153 |
|
149 | 154 | // size to warp the clipped expanded region to |
150 | | - cv_crop_size.width = |
| 155 | + cv_crop_size.width = |
151 | 156 | static_cast<int>(round(static_cast<Dtype>(clipped_width)*scale_x)); |
152 | | - cv_crop_size.height = |
| 157 | + cv_crop_size.height = |
153 | 158 | static_cast<int>(round(static_cast<Dtype>(clipped_height)*scale_y)); |
154 | 159 | pad_x1 = static_cast<int>(round(static_cast<Dtype>(pad_x1)*scale_x)); |
155 | 160 | pad_x2 = static_cast<int>(round(static_cast<Dtype>(pad_x2)*scale_x)); |
@@ -176,9 +181,9 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
176 | 181 |
|
177 | 182 | cv::Rect roi(x1, y1, x2-x1+1, y2-y1+1); |
178 | 183 | cv::Mat cv_cropped_img = cv_img(roi); |
179 | | - cv::resize(cv_cropped_img, cv_cropped_img, |
| 184 | + cv::resize(cv_cropped_img, cv_cropped_img, |
180 | 185 | cv_crop_size, 0, 0, cv::INTER_LINEAR); |
181 | | - |
| 186 | + |
182 | 187 | // horizontal flip at random |
183 | 188 | if (do_mirror) { |
184 | 189 | cv::flip(cv_cropped_img, cv_cropped_img, 1); |
@@ -188,12 +193,13 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
188 | 193 | for (int c = 0; c < channels; ++c) { |
189 | 194 | for (int h = 0; h < cv_cropped_img.rows; ++h) { |
190 | 195 | for (int w = 0; w < cv_cropped_img.cols; ++w) { |
191 | | - Dtype pixel = |
| 196 | + Dtype pixel = |
192 | 197 | static_cast<Dtype>(cv_cropped_img.at<cv::Vec3b>(h, w)[c]); |
193 | 198 |
|
194 | | - top_data[((itemid * channels + c) * cropsize + h + pad_h) * cropsize + w + pad_w] |
| 199 | + top_data[((itemid * channels + c) * cropsize + h + pad_h) |
| 200 | + * cropsize + w + pad_w] |
195 | 201 | = (pixel |
196 | | - - mean[(c * mean_height + h + mean_off + pad_h) |
| 202 | + - mean[(c * mean_height + h + mean_off + pad_h) |
197 | 203 | * mean_width + w + mean_off + pad_w]) |
198 | 204 | * scale; |
199 | 205 | } |
@@ -207,11 +213,12 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
207 | 213 | // useful debugging code for dumping transformed windows to disk |
208 | 214 | string file_id; |
209 | 215 | std::stringstream ss; |
| 216 | + // NOLINT_NEXT_LINE(runtime/threadsafe_fn) |
210 | 217 | ss << rand(); |
211 | 218 | ss >> file_id; |
212 | | - std::ofstream inf((string("dump/") + file_id + |
| 219 | + std::ofstream inf((string("dump/") + file_id + |
213 | 220 | string("_info.txt")).c_str(), std::ofstream::out); |
214 | | - inf << image.first << std::endl |
| 221 | + inf << image.first << std::endl |
215 | 222 | << window[WindowDataLayer<Dtype>::X1]+1 << std::endl |
216 | 223 | << window[WindowDataLayer<Dtype>::Y1]+1 << std::endl |
217 | 224 | << window[WindowDataLayer<Dtype>::X2]+1 << std::endl |
@@ -220,15 +227,15 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
220 | 227 | << top_label[itemid] << std::endl |
221 | 228 | << is_fg << std::endl; |
222 | 229 | inf.close(); |
223 | | - std::ofstream top_data_file((string("dump/") + file_id + |
224 | | - string("_data.txt")).c_str(), |
| 230 | + std::ofstream top_data_file((string("dump/") + file_id + |
| 231 | + string("_data.txt")).c_str(), |
225 | 232 | std::ofstream::out | std::ofstream::binary); |
226 | 233 | for (int c = 0; c < channels; ++c) { |
227 | 234 | for (int h = 0; h < cropsize; ++h) { |
228 | 235 | for (int w = 0; w < cropsize; ++w) { |
229 | | - top_data_file.write( |
230 | | - reinterpret_cast<char*>(&top_data[((itemid * channels + c) |
231 | | - * cropsize + h) * cropsize + w]), |
| 236 | + top_data_file.write(reinterpret_cast<char*>( |
| 237 | + &top_data[((itemid * channels + c) * cropsize + h) |
| 238 | + * cropsize + w]), |
232 | 239 | sizeof(Dtype)); |
233 | 240 | } |
234 | 241 | } |
@@ -240,7 +247,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) { |
240 | 247 | } |
241 | 248 | } |
242 | 249 |
|
243 | | - return (void*)NULL; |
| 250 | + return reinterpret_cast<void*>(NULL); |
244 | 251 | } |
245 | 252 |
|
246 | 253 | template <typename Dtype> |
@@ -251,9 +258,9 @@ WindowDataLayer<Dtype>::~WindowDataLayer<Dtype>() { |
251 | 258 | template <typename Dtype> |
252 | 259 | void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom, |
253 | 260 | vector<Blob<Dtype>*>* top) { |
254 | | - // SetUp runs through the window_file and creates two structures |
255 | | - // that hold windows: one for foreground (object) windows and one |
256 | | - // for background (non-object) windows. We use an overlap threshold |
| 261 | + // SetUp runs through the window_file and creates two structures |
| 262 | + // that hold windows: one for foreground (object) windows and one |
| 263 | + // for background (non-object) windows. We use an overlap threshold |
257 | 264 | // to decide which is which. |
258 | 265 |
|
259 | 266 | CHECK_EQ(bottom.size(), 0) << "Window data Layer takes no input blobs."; |
@@ -270,15 +277,15 @@ void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom, |
270 | 277 | // class_index overlap x1 y1 x2 y2 |
271 | 278 |
|
272 | 279 | LOG(INFO) << "Window data layer:" << std::endl |
273 | | - << " foreground (object) overlap threshold: " |
| 280 | + << " foreground (object) overlap threshold: " |
274 | 281 | << this->layer_param_.det_fg_threshold() << std::endl |
275 | | - << " background (non-object) overlap threshold: " |
| 282 | + << " background (non-object) overlap threshold: " |
276 | 283 | << this->layer_param_.det_bg_threshold() << std::endl |
277 | 284 | << " foreground sampling fraction: " |
278 | 285 | << this->layer_param_.det_fg_fraction(); |
279 | 286 |
|
280 | 287 | std::ifstream infile(this->layer_param_.source().c_str()); |
281 | | - CHECK(infile.good()) << "Failed to open window file " |
| 288 | + CHECK(infile.good()) << "Failed to open window file " |
282 | 289 | << this->layer_param_.source() << std::endl; |
283 | 290 |
|
284 | 291 | map<int, int> label_hist; |
@@ -313,7 +320,7 @@ void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom, |
313 | 320 | window[WindowDataLayer::Y1] = y1; |
314 | 321 | window[WindowDataLayer::X2] = x2; |
315 | 322 | window[WindowDataLayer::Y2] = y2; |
316 | | - |
| 323 | + |
317 | 324 | // add window to foreground list or background list |
318 | 325 | if (overlap >= this->layer_param_.det_fg_threshold()) { |
319 | 326 | int label = window[WindowDataLayer::LABEL]; |
@@ -332,7 +339,7 @@ void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom, |
332 | 339 |
|
333 | 340 | if (image_index % 100 == 0) { |
334 | 341 | LOG(INFO) << "num: " << image_index << " " |
335 | | - << image_path << " " |
| 342 | + << image_path << " " |
336 | 343 | << image_size[0] << " " |
337 | 344 | << image_size[1] << " " |
338 | 345 | << image_size[2] << " " |
@@ -342,12 +349,13 @@ void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom, |
342 | 349 |
|
343 | 350 | LOG(INFO) << "Number of images: " << image_index+1; |
344 | 351 |
|
345 | | - for (map<int,int>::iterator it = label_hist.begin(); |
| 352 | + for (map<int, int>::iterator it = label_hist.begin(); |
346 | 353 | it != label_hist.end(); ++it) { |
347 | | - LOG(INFO) << "class " << it->first << " has " << label_hist[it->first] << " samples"; |
| 354 | + LOG(INFO) << "class " << it->first << " has " << label_hist[it->first] |
| 355 | + << " samples"; |
348 | 356 | } |
349 | 357 |
|
350 | | - LOG(INFO) << "Amount of context padding: " |
| 358 | + LOG(INFO) << "Amount of context padding: " |
351 | 359 | << this->layer_param_.det_context_pad(); |
352 | 360 |
|
353 | 361 | LOG(INFO) << "Crop mode: " << this->layer_param_.det_crop_mode(); |
|