You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/03/20 20:12:43 UTC

[GitHub] zhreshold commented on a change in pull request #9939: [MXNET-40]add multi proposal operator (cpu version) and fix the bug in proposal op (gpu version)

zhreshold commented on a change in pull request #9939: [MXNET-40]add multi proposal operator (cpu version) and fix the bug in proposal op (gpu version)
URL: https://github.com/apache/incubator-mxnet/pull/9939#discussion_r175906284
 
 

 ##########
 File path: src/operator/contrib/multi_proposal.cc
 ##########
 @@ -22,11 +22,262 @@
  * Licensed under The Apache-2.0 License [see LICENSE for details]
  * \file multi_proposal.cc
  * \brief
- * \author Xizhou Zhu
+ * \author Xizhou Zhu, Kan Wu
 */
 
 #include "./multi_proposal-inl.h"
 
+//============================
+// Bounding Box Transform Utils
+//============================
+namespace mxnet {
+namespace op {
+namespace utils {
+
+// bbox prediction and clip to the image borders
+inline void BBoxTransformInv(const mshadow::Tensor<cpu, 2>& boxes,
+                             const mshadow::Tensor<cpu, 3>& deltas,
+                             const float im_height,
+                             const float im_width,
+                             const int real_height,
+                             const int real_width,
+                             mshadow::Tensor<cpu, 2> *out_pred_boxes) {
+  CHECK_GE(boxes.size(1), 4);
+  CHECK_GE(out_pred_boxes->size(1), 4);
+  int anchors = deltas.size(0) / 4;
+  int heights = deltas.size(1);
+  int widths = deltas.size(2);
+
+  #pragma omp parallel for num_threads(engine::OpenMP::Get()->GetRecommendedOMPThreadCount())
+  for (int index = 0; index < anchors * heights * widths; ++index) {
+    // index_t index = h * (widths * anchors) + w * (anchors) + a;
+    int a = index % anchors;
+    int w = (index / anchors) % widths;
+    int h = index / (widths * anchors);
+
+    float width = boxes[index][2] - boxes[index][0] + 1.0;
+    float height = boxes[index][3] - boxes[index][1] + 1.0;
+    float ctr_x = boxes[index][0] + 0.5 * (width - 1.0);
+    float ctr_y = boxes[index][1] + 0.5 * (height - 1.0);
+
+    float dx = deltas[a*4 + 0][h][w];
+    float dy = deltas[a*4 + 1][h][w];
+    float dw = deltas[a*4 + 2][h][w];
+    float dh = deltas[a*4 + 3][h][w];
+
+    float pred_ctr_x = dx * width + ctr_x;
+    float pred_ctr_y = dy * height + ctr_y;
+    float pred_w = exp(dw) * width;
+    float pred_h = exp(dh) * height;
+
+    float pred_x1 = pred_ctr_x - 0.5 * (pred_w - 1.0);
+    float pred_y1 = pred_ctr_y - 0.5 * (pred_h - 1.0);
+    float pred_x2 = pred_ctr_x + 0.5 * (pred_w - 1.0);
+    float pred_y2 = pred_ctr_y + 0.5 * (pred_h - 1.0);
+
+    pred_x1 = std::max(std::min(pred_x1, im_width - 1.0f), 0.0f);
+    pred_y1 = std::max(std::min(pred_y1, im_height - 1.0f), 0.0f);
+    pred_x2 = std::max(std::min(pred_x2, im_width - 1.0f), 0.0f);
+    pred_y2 = std::max(std::min(pred_y2, im_height - 1.0f), 0.0f);
+
+    (*out_pred_boxes)[index][0] = pred_x1;
+    (*out_pred_boxes)[index][1] = pred_y1;
+    (*out_pred_boxes)[index][2] = pred_x2;
+    (*out_pred_boxes)[index][3] = pred_y2;
+
+    if (h >= real_height || w >= real_width) {
+      (*out_pred_boxes)[index][4] = -1.0;
+    }
+  }
+}
+
+// iou prediction and clip to the image border
+inline void IoUTransformInv(const mshadow::Tensor<cpu, 2>& boxes,
+                            const mshadow::Tensor<cpu, 3>& deltas,
+                            const float im_height,
+                            const float im_width,
+                            const int real_height,
+                            const int real_width,
+                            mshadow::Tensor<cpu, 2> *out_pred_boxes) {
+  CHECK_GE(boxes.size(1), 4);
+  CHECK_GE(out_pred_boxes->size(1), 4);
+  int anchors = deltas.size(0) / 4;
+  int heights = deltas.size(1);
+  int widths = deltas.size(2);
+
+  #pragma omp parallel for num_threads(engine::OpenMP::Get()->GetRecommendedOMPThreadCount())
+  for (int index = 0; index < anchors * heights * widths; ++index) {
+    // index_t index = h * (widths * anchors) + w * (anchors) + a;
+    int a = index % anchors;
+    int w = (index / anchors) % widths;
+    int h = index / (widths * anchors);
+
+    float x1 = boxes[index][0];
+    float y1 = boxes[index][1];
+    float x2 = boxes[index][2];
+    float y2 = boxes[index][3];
+
+    float dx1 = deltas[a * 4 + 0][h][w];
+    float dy1 = deltas[a * 4 + 1][h][w];
+    float dx2 = deltas[a * 4 + 2][h][w];
+    float dy2 = deltas[a * 4 + 3][h][w];
+
+    float pred_x1 = x1 + dx1;
+    float pred_y1 = y1 + dy1;
+    float pred_x2 = x2 + dx2;
+    float pred_y2 = y2 + dy2;
+
+    pred_x1 = std::max(std::min(pred_x1, im_width - 1.0f), 0.0f);
+    pred_y1 = std::max(std::min(pred_y1, im_height - 1.0f), 0.0f);
+    pred_x2 = std::max(std::min(pred_x2, im_width - 1.0f), 0.0f);
+    pred_y2 = std::max(std::min(pred_y2, im_height - 1.0f), 0.0f);
+
+    (*out_pred_boxes)[index][0] = pred_x1;
+    (*out_pred_boxes)[index][1] = pred_y1;
+    (*out_pred_boxes)[index][2] = pred_x2;
+    (*out_pred_boxes)[index][3] = pred_y2;
+
+    if (h >= real_height || w >= real_width) {
+      (*out_pred_boxes)[index][4] = -1.0f;
+    }
+  }
+}
+
+// filter box by set confidence to zero
+// * height or width < rpn_min_size
+inline void FilterBox(mshadow::Tensor<cpu, 2> *dets,
+                      const float min_size) {
+  #pragma omp parallel for num_threads(engine::OpenMP::Get()->GetRecommendedOMPThreadCount())
+  for (int i = 0; i < static_cast<int>(dets->size(0)); ++i) {
+    float iw = (*dets)[i][2] - (*dets)[i][0] + 1.0f;
+    float ih = (*dets)[i][3] - (*dets)[i][1] + 1.0f;
+    if (iw < min_size || ih < min_size) {
+      (*dets)[i][0] -= min_size / 2;
+      (*dets)[i][1] -= min_size / 2;
+      (*dets)[i][2] += min_size / 2;
+      (*dets)[i][3] += min_size / 2;
+      (*dets)[i][4] = -1.0f;
+    }
+  }
+}
+
+}  // namespace utils
+}  // namespace op
+}  // namespace mxnet
+
+//=====================
+// NMS Utils
+//=====================
+namespace mxnet {
 
 Review comment:
   Did you check the sort_op in src/operator//tensor/sort_op.h?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services