zhreshold commented on a change in pull request #8582: Yolo2 operator
URL: https://github.com/apache/incubator-mxnet/pull/8582#discussion_r165778530
 
 

 ##########
 File path: src/operator/contrib/yolo_output-inl.h
 ##########
 @@ -0,0 +1,703 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file yolo_output-inl.h
+ * \brief yolo-v2 output layer, v1 to be added
+ * \author Joshua Zhang
+*/
+#ifndef MXNET_OPERATOR_CONTRIB_YOLO_OUTPUT_INL_H_
+#define MXNET_OPERATOR_CONTRIB_YOLO_OUTPUT_INL_H_
+#include <dmlc/logging.h>
+#include <dmlc/parameter.h>
+#include <mxnet/operator.h>
+#include <mxnet/base.h>
+#include <nnvm/tuple.h>
+#include <map>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include <utility>
+#include "../operator_common.h"
+#include "../mshadow_op.h"
+#include "../mxnet_op.h"
+#include "../tensor/sort_op.h"
+
+namespace mxnet {
+namespace op {
+namespace yolo2out_enum {
+enum Yolo2OutputOpInputs {kData, kLabel};
+enum Yolo2OutputOpOutputs {kOut, kTemp, kCopy};
+enum Yolo2OutputOpAuxiliary {kCounter};
+enum Yolo2OutputOpResource {kTempSpace};
+}  // namespace yolo2out_enum
+
+struct Yolo2OutputParam : public dmlc::Parameter<Yolo2OutputParam> {
+  int num_class;
+  int num_anchor;
+  float overlap_thresh;
+  float object_grad_scale;
+  float background_grad_scale;
+  float class_grad_scale;
+  float coord_grad_scale;
+  nnvm::Tuple<float> anchors;
+  int warmup_samples;
+  float warmup_grad_scale;
+  float nms_threshold;
+  int nms_topk;
+  bool force_suppress;
+  DMLC_DECLARE_PARAMETER(Yolo2OutputParam) {
+    DMLC_DECLARE_FIELD(num_class).set_lower_bound(1)
+    .describe("Number of object classes.");
+    DMLC_DECLARE_FIELD(num_anchor).set_default(5)
+    .set_lower_bound(1)
+    .describe("Number of anchors.");
+    DMLC_DECLARE_FIELD(overlap_thresh).set_default(0.6)
+    .describe("Positive overlap threshold.");
+    DMLC_DECLARE_FIELD(object_grad_scale).set_default(1.0)
+    .describe("Gradient scale for positive objects.");
+    DMLC_DECLARE_FIELD(background_grad_scale).set_default(1.0)
+    .describe("Gradient scale for background.");
+    DMLC_DECLARE_FIELD(class_grad_scale).set_default(1.0)
+    .describe("Gradient scale for positive objects.");
+    DMLC_DECLARE_FIELD(coord_grad_scale).set_default(1.0)
+    .describe("Gradient scale for box offsets.");
+    DMLC_DECLARE_FIELD(anchors)
+    .set_default({1.08f, 1.19f, 3.42f, 4.41f, 6.63f, 11.38f, 9.42f, 5.11f, 
16.62f, 10.52f})
+    .describe("Predefined anchor box widths and heights.");
+    DMLC_DECLARE_FIELD(warmup_samples).set_default(12800)
+    .describe("Number of images to warm up towards averaging position for box "
+    "predictions when starting a new training. ");
+    DMLC_DECLARE_FIELD(warmup_grad_scale).set_default(0.01)
+    .describe("Gradient scale for non-critical anchors during warm-up stage.");
+    DMLC_DECLARE_FIELD(nms_threshold).set_default(0.5f)
+    .describe("Non-maximum suppression threshold.");
+    DMLC_DECLARE_FIELD(force_suppress).set_default(false)
+    .describe("Suppress all detections regardless of class_id.");
+    DMLC_DECLARE_FIELD(nms_topk).set_default(-1)
+    .describe("Keep maximum top k detections before nms, -1 for no limit.");
+  }
+};  // struct Yolo2OutputParam
+
+template<typename DType>
+MSHADOW_XINLINE DType Intersect(DType l1, DType r1, DType l2, DType r2) {
+  DType left = l1 > l2 ? l1 : l2;
+  DType right = r1 < r2 ? r1 : r2;
+  DType w = right - left;
+  return w > 0 ? w : DType(0);
+}
+
+template<typename DType>
+MSHADOW_XINLINE DType Area(DType l1, DType t1, DType r1, DType b1) {
+  DType width = r1 - l1;
+  DType height = b1 - t1;
+  if (width <= 0 || height <= 0) return DType(0);
+  return width * height;
+}
+
+template<typename DType>
+MSHADOW_XINLINE DType IOU(DType l1, DType t1, DType r1, DType b1,
+  DType l2, DType t2, DType r2, DType b2) {
+  DType inter_area = Intersect(l1, r1, l2, r2) * Intersect(t1, b1, t2, b2);
+  if (inter_area <= 0) return DType(0);
+  DType area1 = Area(l1, t1, r1, b1);
+  DType area2 = Area(l2, t2, r2, b2);
+  return inter_area / (area1 + area2 - inter_area);
+}
+
+// compute intersection-over-union overlap between two boxes
+struct calc_overlap {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, DType* out,
+      const DType* L1, const DType* T1, const DType* R1, const DType* B1,
+      const DType* L2, const DType* T2, const DType* R2, const DType* B2) {
+    out[i] = IOU(L1[i], T1[i], R1[i], B1[i], L2[i], T2[i], R2[i], B2[i]);
+  }
+};
+
+struct clip_zero_one {
+  template<typename DType>
+  MSHADOW_XINLINE static DType Map(DType a) {
+    if (a < 0.f) return DType(0.f);
+    if (a > 1.f) return DType(1.f);
+    return DType(a);
+  }
+};  // struct clip_zero_one
+
+
+// find best anchor box per ground-truth, and calculate grad
+struct box_grad {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, DType* grad, DType* out_label,
+      const DType* label, const DType* anchor, const DType* pred,
+      const index_t label_width, const index_t label_offset,
+      const index_t pred_width, const index_t pred_offset,
+      const index_t grad_width, const index_t grad_offset,
+      const index_t num_anchor, const index_t num_label,
+      const index_t width, const index_t height,
+      const float box_scale, const float object_scale) {
+    for (int n = 0; n < static_cast<int>(num_label); ++n) {
 
 Review comment:
   ints are passed to potentially cuda kernels, which is perferrable than 
size_t or index_t(defined in mshadow) to avoid troubles, and int is 
considerably large enough for the purpose of this operator.

----------------------------------------------------------------
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

Reply via email to