sandeep-krishnamurthy commented on a change in pull request #13611: add image 
resize operator and unit test
URL: https://github.com/apache/incubator-mxnet/pull/13611#discussion_r240763010
 
 

 ##########
 File path: src/operator/image/image_random-inl.h
 ##########
 @@ -147,6 +150,140 @@ void Normalize(const nnvm::NodeAttrs &attrs,
   });
 }
 
+struct ResizeParam : public dmlc::Parameter<ResizeParam> {
+  nnvm::Tuple<int> size;
+  bool keep_ratio;
+  int interp;
+  DMLC_DECLARE_PARAMETER(ResizeParam) {
+    DMLC_DECLARE_FIELD(size)
+    .set_default(nnvm::Tuple<int>())
+    .describe("Size of new image. Could be (width, height) or (size)");
+    DMLC_DECLARE_FIELD(keep_ratio)
+    .describe("Whether to resize the short edge or both edges to `size`, "
+      "if size is give as an integer.");
+    DMLC_DECLARE_FIELD(interp)
+    .set_default(1)
+    .describe("Interpolation method for resizing. By default uses bilinear"
+        "interpolation. See OpenCV's resize function for available choices.");
+  }
+};
+
+inline std::tuple<int, int> GetHeightAndWidth(int data_h,
+                                              int data_w,
+                                              const ResizeParam& param) {
+  CHECK_LE(param.size.ndim(), 2)
+      << "Input size dimension must be 1 or 2, but got "
+      << param.size.ndim();
+  int resized_h;
+  int resized_w;
+  if (param.size.ndim() == 1) {
+    CHECK_GT(param.size[0], 0)
+      << "Input size should greater than 0, but got "
+      << param.size[0];
+    if (!param.keep_ratio) {
+      resized_h = param.size[0];
+      resized_w = param.size[0];
+    } else {
+      if (data_h > data_w) {
+        resized_w = param.size[0];
+        resized_h = static_cast<int>(data_h * resized_w / data_w);
+      } else {
+        resized_h = param.size[0];
+        resized_w = static_cast<int>(data_w * resized_h / data_h);
+      }
+    }
+  } else {
+    CHECK_GT(param.size[0], 0)
+      << "Input width should greater than 0, but got "
+      << param.size[0];
+    CHECK_GT(param.size[1], 0)
+      << "Input height should greater than 0, but got "
+      << param.size[1];
+    resized_h = param.size[1];
+    resized_w = param.size[0];
+  }
+
+  return std::make_tuple(resized_h, resized_w);
+}
+
+bool ResizeShape(const nnvm::NodeAttrs& attrs,
+                             std::vector<TShape> *in_attrs,
+                             std::vector<TShape> *out_attrs) {
+  // the input attrs should only be (h, w, c) or (n, h, w, c)                  
           
+  CHECK_LE(in_attrs->at(0).ndim(), 4U);
+  CHECK_GE(in_attrs->at(0).ndim(), 3U);
+  const auto& ishape = (*in_attrs)[0];
+  const ResizeParam& param = nnvm::get<ResizeParam>(attrs.parsed);
+  std::tuple<int, int> t;
+  if (ishape.ndim() == 3) {
+    t = GetHeightAndWidth(ishape[0], ishape[1], param);
+    out_attrs->clear();
+    out_attrs->push_back(mshadow::Shape3(std::get<0>(t), std::get<1>(t), 
ishape[2]));
+  } else {
+    t = GetHeightAndWidth(ishape[1], ishape[2], param);
+    out_attrs->clear();
+    out_attrs->push_back(mshadow::Shape4(ishape[0], std::get<0>(t), 
std::get<1>(t), ishape[3]));
+  }
+  return true;
+}
+
+void ResizeImpl(const std::vector<TBlob> &inputs,
+                      const std::vector<TBlob> &outputs,
+                      const int height,
+                      const int width,
+                      const int interp,
+                      const int input_index = 0,
+                      const int output_index = 0) {
+#if MXNET_USE_OPENCV
+  CHECK_NE(inputs[0].type_flag_, mshadow::kFloat16) << "image resize doesn't 
support fp16";
+  const int DTYPE[] = {CV_32F, CV_64F, -1, CV_8U, CV_32S};
 
 Review comment:
   Please add comment on what is this and why is it required.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to