[GitHub] zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI Align

2018-05-28 Thread GitBox
zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI 
Align
URL: https://github.com/apache/incubator-mxnet/pull/10852#discussion_r191305377
 
 

 ##
 File path: src/operator/contrib/roi_align.cu
 ##
 @@ -0,0 +1,484 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file roi_align.cu
+ * \brief roi align operator
+ * \author Hang Zhang
+ * Adapted from Caffe2
+*/
+#include "./roi_align-inl.h"
+
+
+namespace mxnet {
+namespace op {
+
+#define CUDA_1D_KERNEL_LOOP(i, n) \
+  for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); \
+   i += blockDim.x * gridDim.x)
+
+using namespace mshadow::cuda;
+
+// The maximum number of blocks to use in the default kernel call.
+constexpr int ROI_MAXIMUM_NUM_BLOCKS = 4096;
+
+/**
+ * @brief Compute the number of blocks needed to run N threads.
+ */
+inline int ROI_GET_BLOCKS(const int N) {
+  return std::max(
+  std::min(
+  (N + kMaxThreadsPerBlock - 1) / kMaxThreadsPerBlock,
+  ROI_MAXIMUM_NUM_BLOCKS),
+  // Use at least 1 block, since CUDA does not allow empty block
+  1);
+}
+
+
+template 
+__device__ T bilinear_interpolate(
+const T* bottom_data,
+const int height,
+const int width,
+T y,
+T x,
+const int index /* index for debug only*/) {
+  // deal with cases that inverse elements are out of feature map boundary
+  if (y < -1.0 || y > height || x < -1.0 || x > width) {
+// empty
+return 0;
+  }
+
+  if (y <= 0) {
+y = 0;
+  }
+  if (x <= 0) {
+x = 0;
+  }
+
+  int y_low = static_cast(y);
+  int x_low = static_cast(x);
+  int y_high;
+  int x_high;
+
+  if (y_low >= height - 1) {
+y_high = y_low = height - 1;
+y = (T)y_low;
+  } else {
+y_high = y_low + 1;
+  }
+
+  if (x_low >= width - 1) {
+x_high = x_low = width - 1;
+x = (T)x_low;
+  } else {
+x_high = x_low + 1;
+  }
+
+  T ly = y - y_low;
+  T lx = x - x_low;
+  T hy = 1. - ly, hx = 1. - lx;
+  // do bilinear interpolation
+  T v1 = bottom_data[y_low * width + x_low];
+  T v2 = bottom_data[y_low * width + x_high];
+  T v3 = bottom_data[y_high * width + x_low];
+  T v4 = bottom_data[y_high * width + x_high];
+  T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
+
+  T val = (w1 * v1 + w2 * v2 + w3 * v3 + w4 * v4);
+
+  return val;
+}
+
+template 
+__global__ void RoIAlignForwardKernel(
+const int nthreads,
+const T* bottom_data,
+const T spatial_scale,
+const int channels,
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int sampling_ratio,
+const T* bottom_rois,
+T* top_data) {
+  CUDA_1D_KERNEL_LOOP(index, nthreads) {
+// (n, c, ph, pw) is an element in the pooled output
+int pw = index % pooled_width;
+int ph = (index / pooled_width) % pooled_height;
+int c = (index / pooled_width / pooled_height) % channels;
+int n = index / pooled_width / pooled_height / channels;
+
+const T* offset_bottom_rois = bottom_rois + n * 5;
+int roi_batch_ind = offset_bottom_rois[0];
+
+// Do not using rounding; this implementation detail is critical
+T roi_start_w = offset_bottom_rois[1] * spatial_scale;
+T roi_start_h = offset_bottom_rois[2] * spatial_scale;
+T roi_end_w = offset_bottom_rois[3] * spatial_scale;
+T roi_end_h = offset_bottom_rois[4] * spatial_scale;
+// T roi_start_w = round(offset_bottom_rois[1] * spatial_scale);
+// T roi_start_h = round(offset_bottom_rois[2] * spatial_scale);
+// T roi_end_w = round(offset_bottom_rois[3] * spatial_scale);
+// T roi_end_h = round(offset_bottom_rois[4] * spatial_scale);
+
+// Force malformed ROIs to be 1x1
+T roi_width = max(roi_end_w - roi_start_w, (T)1.);
+T roi_height = max(roi_end_h - roi_start_h, (T)1.);
+T bin_size_h = static_cast(roi_height) / static_cast(pooled_height);
+T bin_size_w = static_cast(roi_width) / static_cast(pooled_width);
+
+const T* offset_bottom_data =
+bottom_data + (roi_batch_ind * channels + c) * height * width;
+
+// We use roi_bin_grid to sample the grid and mimic 

[GitHub] zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI Align

2018-05-18 Thread GitBox
zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI 
Align
URL: https://github.com/apache/incubator-mxnet/pull/10852#discussion_r189396430
 
 

 ##
 File path: src/operator/contrib/roi_align.cc
 ##
 @@ -0,0 +1,593 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file roi_align.cc
+ * \brief roi align operator
+ * \author Hang Zhang
+ * Adapted from Caffe2
+*/
+#include "./roi_align-inl.h"
+
+
+namespace mxnet {
+namespace op {
+
+template 
+struct PreCalc {
+  int pos1;
+  int pos2;
+  int pos3;
+  int pos4;
+  T w1;
+  T w2;
+  T w3;
+  T w4;
+};
+
+template 
+void pre_calc_for_bilinear_interpolate(
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int iy_upper,
+const int ix_upper,
+T roi_start_h,
+T roi_start_w,
+T bin_size_h,
+T bin_size_w,
+int roi_bin_grid_h,
+int roi_bin_grid_w,
+std::vector* pre_calc) {
+  int pre_calc_index = 0;
+  for (int ph = 0; ph < pooled_height; ph++) {
+for (int pw = 0; pw < pooled_width; pw++) {
+  for (int iy = 0; iy < iy_upper; iy++) {
+const T yy = roi_start_h + ph * bin_size_h +
+static_cast(iy + .5f) * bin_size_h /
+static_cast(roi_bin_grid_h);  // e.g., 0.5, 1.5
+for (int ix = 0; ix < ix_upper; ix++) {
+  const T xx = roi_start_w + pw * bin_size_w +
+  static_cast(ix + .5f) * bin_size_w /
+  static_cast(roi_bin_grid_w);
+
+  T x = xx;
+  T y = yy;
+  // deal with: inverse elements are out of feature map boundary
+  if (y < -1.0 || y > height || x < -1.0 || x > width) {
+// empty
+PreCalc pc;
+pc.pos1 = 0;
+pc.pos2 = 0;
+pc.pos3 = 0;
+pc.pos4 = 0;
+pc.w1 = 0;
+pc.w2 = 0;
+pc.w3 = 0;
+pc.w4 = 0;
+pre_calc->at(pre_calc_index) = pc;
+pre_calc_index += 1;
+continue;
+  }
+
+  if (y <= 0) {
+y = 0;
+  }
+  if (x <= 0) {
+x = 0;
+  }
+
+  int y_low = static_cast(y);
+  int x_low = static_cast(x);
+  int y_high;
+  int x_high;
+
+  if (y_low >= height - 1) {
+y_high = y_low = height - 1;
+y = (T)y_low;
+  } else {
+y_high = y_low + 1;
+  }
+
+  if (x_low >= width - 1) {
+x_high = x_low = width - 1;
+x = (T)x_low;
+  } else {
+x_high = x_low + 1;
+  }
+
+  T ly = y - y_low;
+  T lx = x - x_low;
+  T hy = 1. - ly, hx = 1. - lx;
+  T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
+
+  // save weights and indeces
+  PreCalc pc;
+  pc.pos1 = y_low * width + x_low;
+  pc.pos2 = y_low * width + x_high;
+  pc.pos3 = y_high * width + x_low;
+  pc.pos4 = y_high * width + x_high;
+  pc.w1 = w1;
+  pc.w2 = w2;
+  pc.w3 = w3;
+  pc.w4 = w4;
+  pre_calc->at(pre_calc_index) = pc;
+
+  pre_calc_index += 1;
+}
+  }
+}
+  }
+}
+
+template 
+void ROIAlignForward(
+const int nthreads,
+const T* bottom_data,
+const T& spatial_scale,
+const int channels,
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int sampling_ratio,
+const T* bottom_rois,
+int roi_cols,
+T* top_data) {
+  DCHECK(roi_cols == 4 || roi_cols == 5);
+
+  int n_rois = nthreads / channels / pooled_width / pooled_height;
+  // (n, c, ph, pw) is an element in the pooled output
+  // can be parallelized using omp
+  // #pragma omp parallel for num_threads(32)
+  for (int n = 0; n < n_rois; n++) {
+int index_n = n * channels * pooled_width * pooled_height;
+
+// roi could have 4 or 5 columns
+const T* offset_bottom_rois = bottom_rois + n * roi_cols;
+int roi_batch_ind = 0;
+if (roi_cols == 5) {
+  roi_batch_ind = 

[GitHub] zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI Align

2018-05-18 Thread GitBox
zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI 
Align
URL: https://github.com/apache/incubator-mxnet/pull/10852#discussion_r189396430
 
 

 ##
 File path: src/operator/contrib/roi_align.cc
 ##
 @@ -0,0 +1,593 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file roi_align.cc
+ * \brief roi align operator
+ * \author Hang Zhang
+ * Adapted from Caffe2
+*/
+#include "./roi_align-inl.h"
+
+
+namespace mxnet {
+namespace op {
+
+template 
+struct PreCalc {
+  int pos1;
+  int pos2;
+  int pos3;
+  int pos4;
+  T w1;
+  T w2;
+  T w3;
+  T w4;
+};
+
+template 
+void pre_calc_for_bilinear_interpolate(
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int iy_upper,
+const int ix_upper,
+T roi_start_h,
+T roi_start_w,
+T bin_size_h,
+T bin_size_w,
+int roi_bin_grid_h,
+int roi_bin_grid_w,
+std::vector* pre_calc) {
+  int pre_calc_index = 0;
+  for (int ph = 0; ph < pooled_height; ph++) {
+for (int pw = 0; pw < pooled_width; pw++) {
+  for (int iy = 0; iy < iy_upper; iy++) {
+const T yy = roi_start_h + ph * bin_size_h +
+static_cast(iy + .5f) * bin_size_h /
+static_cast(roi_bin_grid_h);  // e.g., 0.5, 1.5
+for (int ix = 0; ix < ix_upper; ix++) {
+  const T xx = roi_start_w + pw * bin_size_w +
+  static_cast(ix + .5f) * bin_size_w /
+  static_cast(roi_bin_grid_w);
+
+  T x = xx;
+  T y = yy;
+  // deal with: inverse elements are out of feature map boundary
+  if (y < -1.0 || y > height || x < -1.0 || x > width) {
+// empty
+PreCalc pc;
+pc.pos1 = 0;
+pc.pos2 = 0;
+pc.pos3 = 0;
+pc.pos4 = 0;
+pc.w1 = 0;
+pc.w2 = 0;
+pc.w3 = 0;
+pc.w4 = 0;
+pre_calc->at(pre_calc_index) = pc;
+pre_calc_index += 1;
+continue;
+  }
+
+  if (y <= 0) {
+y = 0;
+  }
+  if (x <= 0) {
+x = 0;
+  }
+
+  int y_low = static_cast(y);
+  int x_low = static_cast(x);
+  int y_high;
+  int x_high;
+
+  if (y_low >= height - 1) {
+y_high = y_low = height - 1;
+y = (T)y_low;
+  } else {
+y_high = y_low + 1;
+  }
+
+  if (x_low >= width - 1) {
+x_high = x_low = width - 1;
+x = (T)x_low;
+  } else {
+x_high = x_low + 1;
+  }
+
+  T ly = y - y_low;
+  T lx = x - x_low;
+  T hy = 1. - ly, hx = 1. - lx;
+  T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
+
+  // save weights and indeces
+  PreCalc pc;
+  pc.pos1 = y_low * width + x_low;
+  pc.pos2 = y_low * width + x_high;
+  pc.pos3 = y_high * width + x_low;
+  pc.pos4 = y_high * width + x_high;
+  pc.w1 = w1;
+  pc.w2 = w2;
+  pc.w3 = w3;
+  pc.w4 = w4;
+  pre_calc->at(pre_calc_index) = pc;
+
+  pre_calc_index += 1;
+}
+  }
+}
+  }
+}
+
+template 
+void ROIAlignForward(
+const int nthreads,
+const T* bottom_data,
+const T& spatial_scale,
+const int channels,
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int sampling_ratio,
+const T* bottom_rois,
+int roi_cols,
+T* top_data) {
+  DCHECK(roi_cols == 4 || roi_cols == 5);
+
+  int n_rois = nthreads / channels / pooled_width / pooled_height;
+  // (n, c, ph, pw) is an element in the pooled output
+  // can be parallelized using omp
+  // #pragma omp parallel for num_threads(32)
+  for (int n = 0; n < n_rois; n++) {
+int index_n = n * channels * pooled_width * pooled_height;
+
+// roi could have 4 or 5 columns
+const T* offset_bottom_rois = bottom_rois + n * roi_cols;
+int roi_batch_ind = 0;
+if (roi_cols == 5) {
+  roi_batch_ind = 

[GitHub] zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI Align

2018-05-17 Thread GitBox
zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI 
Align
URL: https://github.com/apache/incubator-mxnet/pull/10852#discussion_r189138583
 
 

 ##
 File path: src/operator/contrib/roi_align.cc
 ##
 @@ -0,0 +1,586 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file roi_align.cc
+ * \brief roi align operator
+ * \author Hang Zhang
+ * Adapted from Caffe2
+*/
+#include "./roi_align-inl.h"
+
+
+namespace mxnet {
+namespace op {
+
+template 
+struct PreCalc {
+  int pos1;
+  int pos2;
+  int pos3;
+  int pos4;
+  T w1;
+  T w2;
+  T w3;
+  T w4;
+};
+
+template 
+void pre_calc_for_bilinear_interpolate(
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int iy_upper,
+const int ix_upper,
+T roi_start_h,
+T roi_start_w,
+T bin_size_h,
+T bin_size_w,
+int roi_bin_grid_h,
+int roi_bin_grid_w,
+std::vector* pre_calc) {
+  int pre_calc_index = 0;
+  for (int ph = 0; ph < pooled_height; ph++) {
+for (int pw = 0; pw < pooled_width; pw++) {
+  for (int iy = 0; iy < iy_upper; iy++) {
+const T yy = roi_start_h + ph * bin_size_h +
+static_cast(iy + .5f) * bin_size_h /
+static_cast(roi_bin_grid_h);  // e.g., 0.5, 1.5
+for (int ix = 0; ix < ix_upper; ix++) {
+  const T xx = roi_start_w + pw * bin_size_w +
+  static_cast(ix + .5f) * bin_size_w /
+  static_cast(roi_bin_grid_w);
+
+  T x = xx;
+  T y = yy;
+  // deal with: inverse elements are out of feature map boundary
+  if (y < -1.0 || y > height || x < -1.0 || x > width) {
+// empty
+PreCalc pc;
+pc.pos1 = 0;
+pc.pos2 = 0;
+pc.pos3 = 0;
+pc.pos4 = 0;
+pc.w1 = 0;
+pc.w2 = 0;
+pc.w3 = 0;
+pc.w4 = 0;
+pre_calc->at(pre_calc_index) = pc;
+pre_calc_index += 1;
+continue;
+  }
+
+  if (y <= 0) {
+y = 0;
+  }
+  if (x <= 0) {
+x = 0;
+  }
+
+  int y_low = static_cast(y);
+  int x_low = static_cast(x);
+  int y_high;
+  int x_high;
+
+  if (y_low >= height - 1) {
+y_high = y_low = height - 1;
+y = (T)y_low;
+  } else {
+y_high = y_low + 1;
+  }
+
+  if (x_low >= width - 1) {
+x_high = x_low = width - 1;
+x = (T)x_low;
+  } else {
+x_high = x_low + 1;
+  }
+
+  T ly = y - y_low;
+  T lx = x - x_low;
+  T hy = 1. - ly, hx = 1. - lx;
+  T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
+
+  // save weights and indeces
+  PreCalc pc;
+  pc.pos1 = y_low * width + x_low;
+  pc.pos2 = y_low * width + x_high;
+  pc.pos3 = y_high * width + x_low;
+  pc.pos4 = y_high * width + x_high;
+  pc.w1 = w1;
+  pc.w2 = w2;
+  pc.w3 = w3;
+  pc.w4 = w4;
+  pre_calc->at(pre_calc_index) = pc;
+
+  pre_calc_index += 1;
+}
+  }
+}
+  }
+}
+
+template 
+void ROIAlignForward(
+const int nthreads,
+const T* bottom_data,
+const T& spatial_scale,
+const int channels,
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int sampling_ratio,
+const T* bottom_rois,
+int roi_cols,
+T* top_data) {
+  DCHECK(roi_cols == 4 || roi_cols == 5);
+
+  int n_rois = nthreads / channels / pooled_width / pooled_height;
+  // (n, c, ph, pw) is an element in the pooled output
+  // can be parallelized using omp
+  int n;
+#pragma omp parallel for private(n) \
 
 Review comment:
   Are you suggesting removing this omp?


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For 

[GitHub] zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI Align

2018-05-17 Thread GitBox
zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI 
Align
URL: https://github.com/apache/incubator-mxnet/pull/10852#discussion_r189138585
 
 

 ##
 File path: src/operator/contrib/roi_align.cc
 ##
 @@ -0,0 +1,586 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file roi_align.cc
+ * \brief roi align operator
+ * \author Hang Zhang
+ * Adapted from Caffe2
+*/
+#include "./roi_align-inl.h"
+
+
+namespace mxnet {
+namespace op {
+
+template 
+struct PreCalc {
+  int pos1;
+  int pos2;
+  int pos3;
+  int pos4;
+  T w1;
+  T w2;
+  T w3;
+  T w4;
+};
+
+template 
+void pre_calc_for_bilinear_interpolate(
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int iy_upper,
+const int ix_upper,
+T roi_start_h,
+T roi_start_w,
+T bin_size_h,
+T bin_size_w,
+int roi_bin_grid_h,
+int roi_bin_grid_w,
+std::vector* pre_calc) {
+  int pre_calc_index = 0;
+  for (int ph = 0; ph < pooled_height; ph++) {
+for (int pw = 0; pw < pooled_width; pw++) {
+  for (int iy = 0; iy < iy_upper; iy++) {
+const T yy = roi_start_h + ph * bin_size_h +
+static_cast(iy + .5f) * bin_size_h /
+static_cast(roi_bin_grid_h);  // e.g., 0.5, 1.5
+for (int ix = 0; ix < ix_upper; ix++) {
+  const T xx = roi_start_w + pw * bin_size_w +
+  static_cast(ix + .5f) * bin_size_w /
+  static_cast(roi_bin_grid_w);
+
+  T x = xx;
+  T y = yy;
+  // deal with: inverse elements are out of feature map boundary
+  if (y < -1.0 || y > height || x < -1.0 || x > width) {
+// empty
+PreCalc pc;
+pc.pos1 = 0;
+pc.pos2 = 0;
+pc.pos3 = 0;
+pc.pos4 = 0;
+pc.w1 = 0;
+pc.w2 = 0;
+pc.w3 = 0;
+pc.w4 = 0;
+pre_calc->at(pre_calc_index) = pc;
+pre_calc_index += 1;
+continue;
+  }
+
+  if (y <= 0) {
+y = 0;
+  }
+  if (x <= 0) {
+x = 0;
+  }
+
+  int y_low = static_cast(y);
+  int x_low = static_cast(x);
+  int y_high;
+  int x_high;
+
+  if (y_low >= height - 1) {
+y_high = y_low = height - 1;
+y = (T)y_low;
+  } else {
+y_high = y_low + 1;
+  }
+
+  if (x_low >= width - 1) {
+x_high = x_low = width - 1;
+x = (T)x_low;
+  } else {
+x_high = x_low + 1;
+  }
+
+  T ly = y - y_low;
+  T lx = x - x_low;
+  T hy = 1. - ly, hx = 1. - lx;
+  T w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
+
+  // save weights and indeces
+  PreCalc pc;
+  pc.pos1 = y_low * width + x_low;
+  pc.pos2 = y_low * width + x_high;
+  pc.pos3 = y_high * width + x_low;
+  pc.pos4 = y_high * width + x_high;
+  pc.w1 = w1;
+  pc.w2 = w2;
+  pc.w3 = w3;
+  pc.w4 = w4;
+  pre_calc->at(pre_calc_index) = pc;
+
+  pre_calc_index += 1;
+}
+  }
+}
+  }
+}
+
+template 
+void ROIAlignForward(
+const int nthreads,
+const T* bottom_data,
+const T& spatial_scale,
+const int channels,
+const int height,
+const int width,
+const int pooled_height,
+const int pooled_width,
+const int sampling_ratio,
+const T* bottom_rois,
+int roi_cols,
+T* top_data) {
+  DCHECK(roi_cols == 4 || roi_cols == 5);
+
+  int n_rois = nthreads / channels / pooled_width / pooled_height;
+  // (n, c, ph, pw) is an element in the pooled output
+  // can be parallelized using omp
+  int n;
+#pragma omp parallel for private(n) \
+num_threads(engine::OpenMP::Get()->GetRecommendedOMPThreadCount())
+  for (n = 0; n < n_rois; n++) {
+int index_n = n * channels * pooled_width * pooled_height;
+
+// roi could have 4 or 5 columns
+const T* offset_bottom_rois = bottom_rois + n * roi_cols;
+int roi_batch_ind 

[GitHub] zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI Align

2018-05-17 Thread GitBox
zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI 
Align
URL: https://github.com/apache/incubator-mxnet/pull/10852#discussion_r189103423
 
 

 ##
 File path: src/operator/contrib/roi_align-inl.h
 ##
 @@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file roi_align-inl.h
+ * \brief roi align operator and symbol
+ * \author Hang Zhang
+ * modified from Caffe2
+*/
+#ifndef MXNET_OPERATOR_CONTRIB_ROI_ALIGN_INL_H_
+#define MXNET_OPERATOR_CONTRIB_ROI_ALIGN_INL_H_
+
+#include 
+#include 
+#include "../mshadow_op.h"
+#include "../tensor/init_op.h"
+
+
+namespace mxnet {
+namespace op {
+
+
+// Declare enumeration of input order to make code more intuitive.
+// These enums are only visible within this header
+namespace roialign {
+enum ROIAlignOpInputs {kData, kBox};
+enum ROIAlignOpOutputs {kOut};
+}  // roialign
+
+
+struct ROIAlignParam : public dmlc::Parameter {
+  TShape pooled_size;
+  float spatial_scale;
+  DMLC_DECLARE_PARAMETER(ROIAlignParam) {
+DMLC_DECLARE_FIELD(pooled_size)
+.set_expect_ndim(2).enforce_nonzero()
+.describe("fix pooled size: (h, w)");
 
 Review comment:
   The output roi feature sizes. Name is compatible with ROIPooling


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI Align

2018-05-16 Thread GitBox
zhanghang1989 commented on a change in pull request #10852: [MXNET-411] Add ROI 
Align
URL: https://github.com/apache/incubator-mxnet/pull/10852#discussion_r188783355
 
 

 ##
 File path: tests/python/unittest/test_operator.py
 ##
 @@ -6018,6 +6019,24 @@ def test_context_num_gpus():
 if str(e).find("CUDA") == -1:
 raise e
 
+
+@with_seed()
+def test_op_roi_align():
+ctx=default_context()
+data = mx.symbol.Variable(name='data')
+rois = mx.symbol.Variable(name='rois')
+test = mx.symbol.contrib.ROIAlign(data=data, rois=rois, pooled_size=(4, 
4), spatial_scale=1)
+
+x1 = np.random.rand(4, 1, 12, 12).astype('float64')
+x2 = np.array([[0, 1.1, 1.1, 6.2, 6.2], [2, 6.1, 2.1, 8.2, 11.2], [1, 3.1, 
1.1, 5.2, 10.2]], dtype='float64')
+
+check_numeric_gradient(sym=test, location=[x1, x2],
+   grad_nodes={'data':'write', 'rois':'null'},
+   numeric_eps=1e-4, rtol=1e-1, atol=1e-4)
+check_numeric_gradient(sym=test, location=[x1, x2],
+   grad_nodes={'data':'add', 'rois':'null'},
+   numeric_eps=1e-4, rtol=1e-1, atol=1E-4)
+
 
 Review comment:
   Yeah, will a forward result check soon  


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services