Skip to content

Commit ec6f3b8

Browse files
committed
minor cleanup in rcnn-finetuning -- rcnn feature computation tested at
this commit (in addition to all caffe unit tests passing)
1 parent dfec947 commit ec6f3b8

File tree

2 files changed

+59
-49
lines changed

2 files changed

+59
-49
lines changed

matlab/caffe/matcaffe.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static int init_key = -2;
2525
// matlab uses RGB color channel order
2626
// images need to have the data mean subtracted
2727
//
28-
// Data coming in from matlab needs to be in the order
28+
// Data coming in from matlab needs to be in the order
2929
// [width, height, channels, images]
3030
// where width is the fastest dimension.
3131
// Here is the rough matlab for putting image data into the correct
@@ -131,7 +131,7 @@ static mxArray* do_get_weights() {
131131
prev_layer_name = layer_names[i];
132132
const mwSize dims[2] = {layer_blobs.size(), 1};
133133
mx_layer_cells = mxCreateCellArray(2, dims);
134-
mxSetField(mx_layers, mx_layer_index, "weights", mx_layer_cells);
134+
mxSetField(mx_layers, mx_layer_index, "weights", mx_layer_cells);
135135
mxSetField(mx_layers, mx_layer_index, "layer_names",
136136
mxCreateString(layer_names[i].c_str()));
137137
mx_layer_index++;
@@ -142,18 +142,19 @@ static mxArray* do_get_weights() {
142142
// where width is the fastest dimension
143143
mwSize dims[4] = {layer_blobs[j]->width(), layer_blobs[j]->height(),
144144
layer_blobs[j]->channels(), layer_blobs[j]->num()};
145-
mxArray* mx_weights = mxCreateNumericArray(4, dims, mxSINGLE_CLASS, mxREAL);
145+
mxArray* mx_weights = mxCreateNumericArray(4, dims, mxSINGLE_CLASS,
146+
mxREAL);
146147
mxSetCell(mx_layer_cells, j, mx_weights);
147148
float* weights_ptr = reinterpret_cast<float*>(mxGetPr(mx_weights));
148149

149-
// mexPrintf("layer: %s (%d) blob: %d %d: (%d, %d, %d) %d\n",
150-
// layer_names[i].c_str(), i, j, layer_blobs[j]->num(),
151-
// layer_blobs[j]->height(), layer_blobs[j]->width(),
152-
// layer_blobs[j]->channels(), layer_blobs[j]->count());
150+
// mexPrintf("layer: %s (%d) blob: %d %d: (%d, %d, %d) %d\n",
151+
// layer_names[i].c_str(), i, j, layer_blobs[j]->num(),
152+
// layer_blobs[j]->height(), layer_blobs[j]->width(),
153+
// layer_blobs[j]->channels(), layer_blobs[j]->count());
153154

154155
switch (Caffe::mode()) {
155156
case Caffe::CPU:
156-
memcpy(weights_ptr, layer_blobs[j]->cpu_data(),
157+
memcpy(weights_ptr, layer_blobs[j]->cpu_data(),
157158
sizeof(float) * layer_blobs[j]->count());
158159
break;
159160
case Caffe::GPU:
@@ -219,6 +220,7 @@ static void init(MEX_ARGS) {
219220
mxFree(param_file);
220221
mxFree(model_file);
221222

223+
// NOLINT_NEXT_LINE(runtime/threadsafe_fn)
222224
init_key = rand();
223225
if (nlhs == 1) {
224226
plhs[0] = mxCreateDoubleScalar(init_key);

src/caffe/layers/window_data_layer.cpp

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
#include <stdint.h>
66
#include <pthread.h>
77

8+
#include <algorithm>
89
#include <string>
910
#include <vector>
1011
#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"
1218

1319
#include "caffe/layer.hpp"
1420
#include "caffe/util/io.hpp"
1521
#include "caffe/vision_layers.hpp"
1622

17-
#include <opencv2/core/core.hpp>
18-
#include <opencv2/highgui/highgui.hpp>
19-
#include <opencv2/imgproc/imgproc.hpp>
20-
2123
using std::string;
2224
using std::map;
2325
using std::pair;
@@ -30,7 +32,7 @@ namespace caffe {
3032

3133
template <typename Dtype>
3234
void* WindowDataLayerPrefetch(void* layer_pointer) {
33-
WindowDataLayer<Dtype>* layer =
35+
WindowDataLayer<Dtype>* layer =
3436
reinterpret_cast<WindowDataLayer<Dtype>*>(layer_pointer);
3537

3638
// At each iteration, sample N windows where N*p are foreground (object)
@@ -56,7 +58,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
5658
// zero out batch
5759
memset(top_data, 0, sizeof(Dtype)*layer->prefetch_data_->count());
5860

59-
const int num_fg = static_cast<int>(static_cast<float>(batchsize)
61+
const int num_fg = static_cast<int>(static_cast<float>(batchsize)
6062
* fg_fraction);
6163
const int num_samples[2] = { batchsize - num_fg, num_fg };
6264

@@ -65,23 +67,26 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
6567
for (int is_fg = 0; is_fg < 2; ++is_fg) {
6668
for (int dummy = 0; dummy < num_samples[is_fg]; ++dummy) {
6769
// sample a window
68-
vector<float> window = (is_fg)
70+
vector<float> window = (is_fg)
71+
// NOLINT_NEXT_LINE(runtime/threadsafe_fn)
6972
? layer->fg_windows_[rand() % layer->fg_windows_.size()]
73+
// NOLINT_NEXT_LINE(runtime/threadsafe_fn)
7074
: layer->bg_windows_[rand() % layer->bg_windows_.size()];
7175

7276
bool do_mirror = false;
77+
// NOLINT_NEXT_LINE(runtime/threadsafe_fn)
7378
if (mirror && rand() % 2) {
7479
do_mirror = true;
7580
}
7681

7782
// load the image containing the window
78-
pair<std::string, vector<int> > image =
83+
pair<std::string, vector<int> > image =
7984
layer->image_database_[window[WindowDataLayer<Dtype>::IMAGE_INDEX]];
8085

8186
cv::Mat cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR);
8287
if (!cv_img.data) {
8388
LOG(ERROR) << "Could not open or find file " << image.first;
84-
return (void*)NULL;
89+
return reinterpret_cast<void*>(NULL);
8590
}
8691
const int channels = cv_img.channels();
8792

@@ -94,7 +99,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
9499
int pad_w = 0;
95100
int pad_h = 0;
96101
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
98103
// such that after warping the expanded region to cropsize x cropsize
99104
// there's exactly context_pad amount of padding on each side
100105
Dtype context_scale = static_cast<Dtype>(cropsize) /
@@ -116,7 +121,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
116121
x2 = static_cast<int>(round(center_x + half_width*context_scale));
117122
y1 = static_cast<int>(round(center_y - half_height*context_scale));
118123
y2 = static_cast<int>(round(center_y + half_height*context_scale));
119-
124+
120125
// the expanded region may go outside of the image
121126
// so we compute the clipped (expanded) region and keep track of
122127
// the extent beyond the image
@@ -139,17 +144,17 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
139144
int clipped_height = y2-y1+1;
140145
int clipped_width = x2-x1+1;
141146

142-
// scale factors that would be used to warp the unclipped
147+
// scale factors that would be used to warp the unclipped
143148
// expanded region
144-
Dtype scale_x =
149+
Dtype scale_x =
145150
static_cast<Dtype>(cropsize)/static_cast<Dtype>(unclipped_width);
146-
Dtype scale_y =
151+
Dtype scale_y =
147152
static_cast<Dtype>(cropsize)/static_cast<Dtype>(unclipped_height);
148153

149154
// size to warp the clipped expanded region to
150-
cv_crop_size.width =
155+
cv_crop_size.width =
151156
static_cast<int>(round(static_cast<Dtype>(clipped_width)*scale_x));
152-
cv_crop_size.height =
157+
cv_crop_size.height =
153158
static_cast<int>(round(static_cast<Dtype>(clipped_height)*scale_y));
154159
pad_x1 = static_cast<int>(round(static_cast<Dtype>(pad_x1)*scale_x));
155160
pad_x2 = static_cast<int>(round(static_cast<Dtype>(pad_x2)*scale_x));
@@ -176,9 +181,9 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
176181

177182
cv::Rect roi(x1, y1, x2-x1+1, y2-y1+1);
178183
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,
180185
cv_crop_size, 0, 0, cv::INTER_LINEAR);
181-
186+
182187
// horizontal flip at random
183188
if (do_mirror) {
184189
cv::flip(cv_cropped_img, cv_cropped_img, 1);
@@ -188,12 +193,13 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
188193
for (int c = 0; c < channels; ++c) {
189194
for (int h = 0; h < cv_cropped_img.rows; ++h) {
190195
for (int w = 0; w < cv_cropped_img.cols; ++w) {
191-
Dtype pixel =
196+
Dtype pixel =
192197
static_cast<Dtype>(cv_cropped_img.at<cv::Vec3b>(h, w)[c]);
193198

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]
195201
= (pixel
196-
- mean[(c * mean_height + h + mean_off + pad_h)
202+
- mean[(c * mean_height + h + mean_off + pad_h)
197203
* mean_width + w + mean_off + pad_w])
198204
* scale;
199205
}
@@ -207,11 +213,12 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
207213
// useful debugging code for dumping transformed windows to disk
208214
string file_id;
209215
std::stringstream ss;
216+
// NOLINT_NEXT_LINE(runtime/threadsafe_fn)
210217
ss << rand();
211218
ss >> file_id;
212-
std::ofstream inf((string("dump/") + file_id +
219+
std::ofstream inf((string("dump/") + file_id +
213220
string("_info.txt")).c_str(), std::ofstream::out);
214-
inf << image.first << std::endl
221+
inf << image.first << std::endl
215222
<< window[WindowDataLayer<Dtype>::X1]+1 << std::endl
216223
<< window[WindowDataLayer<Dtype>::Y1]+1 << std::endl
217224
<< window[WindowDataLayer<Dtype>::X2]+1 << std::endl
@@ -220,15 +227,15 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
220227
<< top_label[itemid] << std::endl
221228
<< is_fg << std::endl;
222229
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(),
225232
std::ofstream::out | std::ofstream::binary);
226233
for (int c = 0; c < channels; ++c) {
227234
for (int h = 0; h < cropsize; ++h) {
228235
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]),
232239
sizeof(Dtype));
233240
}
234241
}
@@ -240,7 +247,7 @@ void* WindowDataLayerPrefetch(void* layer_pointer) {
240247
}
241248
}
242249

243-
return (void*)NULL;
250+
return reinterpret_cast<void*>(NULL);
244251
}
245252

246253
template <typename Dtype>
@@ -251,9 +258,9 @@ WindowDataLayer<Dtype>::~WindowDataLayer<Dtype>() {
251258
template <typename Dtype>
252259
void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
253260
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
257264
// to decide which is which.
258265

259266
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,
270277
// class_index overlap x1 y1 x2 y2
271278

272279
LOG(INFO) << "Window data layer:" << std::endl
273-
<< " foreground (object) overlap threshold: "
280+
<< " foreground (object) overlap threshold: "
274281
<< this->layer_param_.det_fg_threshold() << std::endl
275-
<< " background (non-object) overlap threshold: "
282+
<< " background (non-object) overlap threshold: "
276283
<< this->layer_param_.det_bg_threshold() << std::endl
277284
<< " foreground sampling fraction: "
278285
<< this->layer_param_.det_fg_fraction();
279286

280287
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 "
282289
<< this->layer_param_.source() << std::endl;
283290

284291
map<int, int> label_hist;
@@ -313,7 +320,7 @@ void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
313320
window[WindowDataLayer::Y1] = y1;
314321
window[WindowDataLayer::X2] = x2;
315322
window[WindowDataLayer::Y2] = y2;
316-
323+
317324
// add window to foreground list or background list
318325
if (overlap >= this->layer_param_.det_fg_threshold()) {
319326
int label = window[WindowDataLayer::LABEL];
@@ -332,7 +339,7 @@ void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
332339

333340
if (image_index % 100 == 0) {
334341
LOG(INFO) << "num: " << image_index << " "
335-
<< image_path << " "
342+
<< image_path << " "
336343
<< image_size[0] << " "
337344
<< image_size[1] << " "
338345
<< image_size[2] << " "
@@ -342,12 +349,13 @@ void WindowDataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
342349

343350
LOG(INFO) << "Number of images: " << image_index+1;
344351

345-
for (map<int,int>::iterator it = label_hist.begin();
352+
for (map<int, int>::iterator it = label_hist.begin();
346353
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";
348356
}
349357

350-
LOG(INFO) << "Amount of context padding: "
358+
LOG(INFO) << "Amount of context padding: "
351359
<< this->layer_param_.det_context_pad();
352360

353361
LOG(INFO) << "Crop mode: " << this->layer_param_.det_crop_mode();

0 commit comments

Comments
 (0)