pengzhao-intel commented on a change in pull request #16017: Add RROIAlign
URL: https://github.com/apache/incubator-mxnet/pull/16017#discussion_r317920058
 
 

 ##########
 File path: src/operator/rroi_align.cc
 ##########
 @@ -0,0 +1,313 @@
+/*
+ * 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) 2015 by Contributors
+ * \file rroi_align.cc
+ * \brief rroi align operator
+ * \author Yixin Bao
+ * Adapted from Caffe2
+*/
+#include "./rroi_align-inl.h"
+#include <mshadow/tensor.h>
+#include "math.h"
+
+using std::max;
+using std::min;
+using std::floor;
+using std::ceil;
+
+namespace mxnet {
+namespace op {
+
+template <typename DType>
+struct position_for_bilinear_interpolate {
+  // 4 positions and corresponding weights for
+  // computing bilinear interpolation
+  int pos1, pos2, pos3, pos4;
+  DType w1, w2, w3, w4;
+};
+
+template <typename DType>
+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, DType roi_start_h, DType 
roi_start_w,
+    DType bin_size_h, DType bin_size_w, int roi_bin_grid_h, int roi_bin_grid_w,
+    DType roi_center_h, DType roi_center_w, DType theta,
+    std::vector<position_for_bilinear_interpolate<DType>> *pre_calc) {
+  int pre_calc_index = 0;
+  DType cosTheta = cos(theta);
+  DType sinTheta = sin(theta);
+  for (int ph = 0; ph < pooled_height; ph++) {
+    for (int pw = 0; pw < pooled_width; pw++) {
+      // calc bin grid position (xx,yy)
+      for (int iy = 0; iy < iy_upper; iy++) {
+        const DType yy = roi_start_h + ph * bin_size_h +
+            static_cast<DType>(iy + .5f) * bin_size_h /
+                static_cast<DType>(roi_bin_grid_h);  // e.g., 0.5, 1.5
+        for (int ix = 0; ix < ix_upper; ix++) {
+          const DType xx = roi_start_w + pw * bin_size_w +
+              static_cast<DType>(ix + .5f) * bin_size_w /
+                  static_cast<DType>(roi_bin_grid_w);
+
+          // Rotate by theta around the center and translate
+          DType x = xx * cosTheta + yy * sinTheta + roi_center_w;
+          DType y = yy * cosTheta - xx * sinTheta + roi_center_h;
+
+          // deal with: inverse elements are out of feature map boundary
+          if (y < -1.0 || y > height || x < -1.0 || x > width) {
+            // empty
+            position_for_bilinear_interpolate<DType> 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;
+          }
+
+          // calc 4 points for interpolation
+          int y_low = static_cast<int>(y);
+          int x_low = static_cast<int>(x);
+          int y_high;
+          int x_high;
+          if (y_low >= height - 1) {
+            y_high = y_low = height - 1;
+            y = (DType)y_low;
+          } else {
+            y_high = y_low + 1;
+          }
+          if (x_low >= width - 1) {
+            x_high = x_low = width - 1;
+            x = (DType)x_low;
+          } else {
+            x_high = x_low + 1;
+          }
+          DType ly = y - y_low;
+          DType lx = x - x_low;
+          DType hy = 1. - ly, hx = 1. - lx;
+          DType w1 = hy * hx, w2 = hy * lx, w3 = ly * hx, w4 = ly * lx;
+
+          // Save weights and indices
+          position_for_bilinear_interpolate<DType> 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 <typename DType>
+inline void RROIAlignForward(const OpContext &ctx, const RROIAlignParam &param,
+                             const std::vector<TBlob> &in_data, const 
std::vector<OpReqType> &req,
+                             const std::vector<TBlob> &out_data) {
+  // data: [batch_size, c, h, w]
+  const TBlob &data = in_data[rroialign::kData];
+  const TBlob &bbox = in_data[rroialign::kBox];
+  const DType *bottom_data = data.dptr<DType>();
+  const int channels_ = data.size(1);
+  const int height_ = data.size(2);
+  const int width_ = data.size(3);
+  const index_t data_size_c = height_ * width_;
+  const index_t data_size = channels_ * data_size_c;
+
+  // bbox: [num_rois, 6] (6: [batch_index, x, y, w, h, theta])
+  const DType *bottom_rois = bbox.dptr<DType>();
+  const int num_rois = bbox.size(0);
+  const float spatial_scale_ = param.spatial_scale;
+  const int sampling_ratio_ = param.sampling_ratio;
+
+  // out: [num_rois, c, pooled_h, pooled_w]
+  const TBlob &out = out_data[rroialign::kOut];
+  DType *top_data = out.dptr<DType>();
+  const int pooled_height_ = out.size(2);
+  const int pooled_width_ = out.size(3);
+  const index_t out_size_c = pooled_height_ * pooled_width_;
+  const index_t out_size = channels_ * out_size_c;
+
+  for (int n = 0; n < num_rois; ++n) {
 
 Review comment:
   Parallel in here?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to