This is an automated email from the ASF dual-hosted git repository.

lanking pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 745a41c  [MXNET-1416] Fix inception inference example for potential 
index out of range error. (#15179)
745a41c is described below

commit 745a41ca1a6d74a645911de8af46dece03db93ea
Author: Amol Lele <19983848+lelea...@users.noreply.github.com>
AuthorDate: Fri Jun 7 15:04:37 2019 -0700

    [MXNET-1416] Fix inception inference example for potential index out of 
range error. (#15179)
    
    * Adding support to get the data from 1D NDArray.
    
    * Added the error handling for index out of range.
---
 cpp-package/example/inference/inception_inference.cpp |  4 +---
 cpp-package/include/mxnet-cpp/ndarray.h               |  6 ++++++
 cpp-package/include/mxnet-cpp/ndarray.hpp             | 13 ++++++++++++-
 3 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/cpp-package/example/inference/inception_inference.cpp 
b/cpp-package/example/inference/inception_inference.cpp
index fa56001..4d7d5f4 100644
--- a/cpp-package/example/inference/inception_inference.cpp
+++ b/cpp-package/example/inference/inception_inference.cpp
@@ -302,13 +302,11 @@ void Predictor::PredictImage(const std::string& 
image_file) {
 
   // The output is available in executor->outputs.
   auto array = executor->outputs[0].Copy(Context::cpu());
-
   /*
    * Find out the maximum accuracy and the index associated with that accuracy.
    * This is done by using the argmax operator on NDArray.
    */
   auto predicted = array.ArgmaxChannel();
-
   /*
    * Wait until all the previous write operations on the 'predicted'
    * NDArray to be complete before we read it.
@@ -317,7 +315,7 @@ void Predictor::PredictImage(const std::string& image_file) 
{
    */
   predicted.WaitToRead();
 
-  int best_idx = predicted.At(0, 0);
+  int best_idx = predicted.At(0);
   float best_accuracy = array.At(0, best_idx);
 
   if (output_labels.empty()) {
diff --git a/cpp-package/include/mxnet-cpp/ndarray.h 
b/cpp-package/include/mxnet-cpp/ndarray.h
index 7953b61..c4d51c5 100644
--- a/cpp-package/include/mxnet-cpp/ndarray.h
+++ b/cpp-package/include/mxnet-cpp/ndarray.h
@@ -321,6 +321,12 @@ class NDArray {
    */
   size_t Offset(size_t c, size_t h, size_t w) const;
   /*!
+  * \brief return value of the element at (index)
+  * \param index  position
+  * \return value of one dimensions array
+  */
+  mx_float At(size_t index) const;
+  /*!
   * \brief return value of the element at (h, w)
   * \param h height position
   * \param w width position
diff --git a/cpp-package/include/mxnet-cpp/ndarray.hpp 
b/cpp-package/include/mxnet-cpp/ndarray.hpp
index 283fff1..ed23c76 100644
--- a/cpp-package/include/mxnet-cpp/ndarray.hpp
+++ b/cpp-package/include/mxnet-cpp/ndarray.hpp
@@ -375,11 +375,15 @@ inline void NDArray::Save(const std::string &file_name,
 }
 
 inline size_t NDArray::Offset(size_t h, size_t w) const {
-  return (h * GetShape()[1]) + w;
+  auto const shape = GetShape();
+  CHECK_EQ(shape.size(), 2) << "The NDArray needs to be 2 dimensional.";
+
+  return (h * shape[1]) + w;
 }
 
 inline size_t NDArray::Offset(size_t c, size_t h, size_t w) const {
   auto const shape = GetShape();
+  CHECK_EQ(shape.size(), 3) << "The NDArray needs to be 3 dimensional.";
   return h * shape[0] * shape[2] + w * shape[0] + c;
 }
 
@@ -391,6 +395,13 @@ inline mx_float NDArray::At(size_t c, size_t h, size_t w) 
const {
   return GetData()[Offset(c, h, w)];
 }
 
+inline mx_float NDArray::At(size_t index) const {
+  auto shape = GetShape();
+  CHECK_EQ(shape.size(), 1) << "The NDArray needs to be 1 dimensional.";
+  CHECK_LT(index, shape[0]) << "Specified index is out of range.";
+  return GetData()[index];
+}
+
 inline size_t NDArray::Size() const {
   size_t ret = 1;
   for (auto &i : GetShape()) ret *= i;

Reply via email to