1 #include "roi_align_op.h" 3 #include "caffe2/utils/eigen_utils.h" 4 #include "caffe2/utils/math.h" 22 void pre_calc_for_bilinear_interpolate(
25 const int pooled_height,
26 const int pooled_width,
35 std::vector<PreCalc<T>>& pre_calc) {
36 int pre_calc_index = 0;
37 for (
int ph = 0; ph < pooled_height; ph++) {
38 for (
int pw = 0; pw < pooled_width; pw++) {
39 for (
int iy = 0; iy < iy_upper; iy++) {
40 const T yy = roi_start_h + ph * bin_size_h +
41 static_cast<T>(iy + .5f) * bin_size_h /
42 static_cast<T>(roi_bin_grid_h);
43 for (
int ix = 0; ix < ix_upper; ix++) {
44 const T xx = roi_start_w + pw * bin_size_w +
45 static_cast<T>(ix + .5f) * bin_size_w /
46 static_cast<T>(roi_bin_grid_w);
51 if (y < -1.0 || y > height || x < -1.0 || x > width) {
62 pre_calc[pre_calc_index] = pc;
79 if (y_low >= height - 1) {
80 y_high = y_low = height - 1;
86 if (x_low >= width - 1) {
87 x_high = x_low = width - 1;
95 T hy = 1. - ly, hx = 1. - lx;
96 T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
100 pc.pos1 = y_low * width + x_low;
101 pc.pos2 = y_low * width + x_high;
102 pc.pos3 = y_high * width + x_low;
103 pc.pos4 = y_high * width + x_high;
108 pre_calc[pre_calc_index] = pc;
117 template <
typename T>
118 void ROIAlignForward(
120 const T* bottom_data,
121 const T& spatial_scale,
125 const int pooled_height,
126 const int pooled_width,
127 const int sampling_ratio,
128 const T* bottom_rois,
131 StorageOrder order) {
132 DCHECK(roi_cols == 4 || roi_cols == 5);
134 int n_rois = nthreads / channels / pooled_width / pooled_height;
137 #pragma omp parallel for 139 for (
int n = 0; n < n_rois; n++) {
140 int index_n = n * channels * pooled_width * pooled_height;
143 const T* offset_bottom_rois = bottom_rois + n * roi_cols;
144 int roi_batch_ind = 0;
146 roi_batch_ind = offset_bottom_rois[0];
147 offset_bottom_rois++;
151 T roi_start_w = offset_bottom_rois[0] * spatial_scale;
152 T roi_start_h = offset_bottom_rois[1] * spatial_scale;
153 T roi_end_w = offset_bottom_rois[2] * spatial_scale;
154 T roi_end_h = offset_bottom_rois[3] * spatial_scale;
161 T roi_width = std::max(roi_end_w - roi_start_w, (
T)1.);
162 T roi_height = std::max(roi_end_h - roi_start_h, (
T)1.);
163 T bin_size_h =
static_cast<T>(roi_height) / static_cast<T>(pooled_height);
164 T bin_size_w =
static_cast<T>(roi_width) / static_cast<T>(pooled_width);
167 int roi_bin_grid_h = (sampling_ratio > 0)
169 : ceil(roi_height / pooled_height);
171 (sampling_ratio > 0) ? sampling_ratio : ceil(roi_width / pooled_width);
174 const T count = roi_bin_grid_h * roi_bin_grid_w;
178 std::vector<PreCalc<T>> pre_calc(
179 roi_bin_grid_h * roi_bin_grid_w * pooled_width * pooled_height);
180 pre_calc_for_bilinear_interpolate(
195 if (order == StorageOrder::NCHW) {
196 for (
int c = 0; c < channels; c++) {
197 int index_n_c = index_n + c * pooled_width * pooled_height;
198 const T* offset_bottom_data =
199 bottom_data + (roi_batch_ind * channels + c) * height * width;
200 int pre_calc_index = 0;
202 for (
int ph = 0; ph < pooled_height; ph++) {
203 for (
int pw = 0; pw < pooled_width; pw++) {
204 int index = index_n_c + ph * pooled_width + pw;
207 for (
int iy = 0; iy < roi_bin_grid_h; iy++) {
208 for (
int ix = 0; ix < roi_bin_grid_w; ix++) {
209 PreCalc<T> pc = pre_calc[pre_calc_index];
210 output_val += pc.w1 * offset_bottom_data[pc.pos1] +
211 pc.w2 * offset_bottom_data[pc.pos2] +
212 pc.w3 * offset_bottom_data[pc.pos3] +
213 pc.w4 * offset_bottom_data[pc.pos4];
220 top_data[index] = output_val;
226 if (order == StorageOrder::NHWC) {
227 const T* offset_bottom_data =
228 bottom_data + roi_batch_ind * channels * height * width;
229 int pre_calc_index = 0;
231 for (
int ph = 0; ph < pooled_height; ph++) {
232 for (
int pw = 0; pw < pooled_width; pw++) {
233 EVecXf output_vals = EVecXf::Zero(channels);
235 for (
int iy = 0; iy < roi_bin_grid_h; iy++) {
236 for (
int ix = 0; ix < roi_bin_grid_w; ix++) {
237 PreCalc<T> pc = pre_calc[pre_calc_index];
239 ConstEigenVectorMap<T> data_1(
240 offset_bottom_data + channels * pc.pos1, channels);
241 ConstEigenVectorMap<T> data_2(
242 offset_bottom_data + channels * pc.pos2, channels);
243 ConstEigenVectorMap<T> data_3(
244 offset_bottom_data + channels * pc.pos3, channels);
245 ConstEigenVectorMap<T> data_4(
246 offset_bottom_data + channels * pc.pos4, channels);
248 output_vals += pc.w1 * data_1 + pc.w2 * data_2 + pc.w3 * data_3 +
254 output_vals /= count;
256 int index_nhw = index_n + (ph * pooled_width + pw) * channels;
258 top_data + index_nhw, output_vals.data(), channels *
sizeof(
T));
269 bool RoIAlignOp<float, CPUContext>::RunOnDevice() {
273 if (R.numel() == 0) {
274 std::vector<int64_t> sizes;
276 if (order_ == StorageOrder::NCHW) {
277 sizes = {0, X.dim32(1), pooled_height_, pooled_width_};
278 }
else if (order_ == StorageOrder::NHWC) {
279 sizes = {0, pooled_height_, pooled_width_, X.dim32(3)};
282 Output(0, sizes, at::dtype<float>());
286 CAFFE_ENFORCE_EQ(R.dim(), 2);
288 CAFFE_ENFORCE(R.dim32(1) == 4 || R.dim32(1) == 5);
290 assert(sampling_ratio_ >= 0);
292 if (order_ == StorageOrder::NCHW) {
295 {R.dim32(0), X.dim32(1), pooled_height_, pooled_width_},
297 int output_size = Y->numel();
298 ROIAlignForward<float>(
310 Y->template mutable_data<float>(),
312 }
else if (order_ == StorageOrder::NHWC) {
315 {R.dim32(0), pooled_height_, pooled_width_, X.dim32(3)},
317 int output_size = Y->numel();
318 ROIAlignForward<float>(
330 Y->template mutable_data<float>(),
337 REGISTER_CPU_OPERATOR(RoIAlign, RoIAlignOp<float, CPUContext>);
340 OPERATOR_SCHEMA(RoIAlign)
344 Region of Interest (RoI) align operation as used in Mask R-CNN. 348 "(float) default 1.0; Spatial scale of the input feature map X " 349 "relative to the input image. E.g., 0.0625 if X has a stride of 16 " 350 "w.r.t. the input image.")
351 .Arg(
"pooled_h",
"(int) default 1; Pooled output Y's height.")
352 .Arg(
"pooled_w",
"(int) default 1; Pooled output Y's width.")
355 "(int) default -1; number of sampling points in the interpolation grid " 356 "used to compute the output value of each pooled output bin. If > 0, " 357 "then exactly sampling_ratio x sampling_ratio grid points are used. If " 358 "<= 0, then an adaptive number of grid points are used (computed as " 359 "ceil(roi_width / pooled_w), and likewise for height).")
360 .Input(0,
"X",
"4D feature map input of shape (N, C, H, W).")
364 "2D input of shape (R, 4 or 5) specifying R RoIs " 365 "representing: batch index in [0, N - 1], x1, y1, x2, y2. The RoI " 366 "coordinates are in the coordinate system of the input image. For " 367 "inputs corresponding to a single image, batch index can be excluded " 368 "to have just 4 columns.")
372 "4D output of shape (R, C, pooled_h, pooled_w). The r-th batch element " 373 "is a pooled feature map cooresponding to the r-th RoI.");
379 C10_REGISTER_CAFFE2_OPERATOR_CPU(
381 (std::vector<c10::Argument>{
390 (std::vector<c10::Argument>{
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...