Print out max relative difference (#473)
author최형규/동작제어Lab(SR)/Senior Engineer/삼성전자 <hk0110.choi@samsung.com>
Fri, 6 Apr 2018 02:22:00 +0000 (11:22 +0900)
committer김정현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh0822.kim@samsung.com>
Fri, 6 Apr 2018 02:22:00 +0000 (11:22 +0900)
* Print out max non-zero observed relative difference

- Print out max non-zero observed relative difference
  for both MATCHED and UNMATCHED

Signed-off-by: Hyung-Kyu Choi <hk0110.choi@samsung.com>
* Apply review feedbacks

- Apply review feedbacks

Signed-off-by: Hyung-Kyu Choi <hk0110.choi@samsung.com>
tools/nnapi_test/src/nnapi_test.cc

index a46629c..15fda3c 100644 (file)
@@ -98,6 +98,7 @@ struct TfLiteTensorDiff
   nnfw::util::tensor::Index index;
   float expected;
   float obtained;
+  float relative_diff;
 
   TfLiteTensorDiff(const nnfw::util::tensor::Index &i) : index(i)
   {
@@ -115,7 +116,8 @@ public:
 
 public:
   std::vector<TfLiteTensorDiff> compare(const nnfw::support::tflite::TensorView<float> &expected,
-                                        const nnfw::support::tflite::TensorView<float> &obtained) const;
+                                        const nnfw::support::tflite::TensorView<float> &obtained,
+                                        TfLiteTensorDiff *max_diff = nullptr) const;
 
 private:
   std::function<bool (float lhs, float rhs)> _compare_fn;
@@ -123,7 +125,8 @@ private:
 
 std::vector<TfLiteTensorDiff>
 TfLiteTensorComparator::compare(const nnfw::support::tflite::TensorView<float> &expected,
-                                const nnfw::support::tflite::TensorView<float> &obtained) const
+                                const nnfw::support::tflite::TensorView<float> &obtained,
+                                TfLiteTensorDiff *max_diff) const
 {
   std::vector<TfLiteTensorDiff> res;
 
@@ -133,6 +136,7 @@ TfLiteTensorComparator::compare(const nnfw::support::tflite::TensorView<float> &
   {
     const auto expected_value = expected.at(index);
     const auto obtained_value = obtained.at(index);
+    const auto relative_diff = nnfw::util::fp32::relative_diff(expected_value, obtained_value);
 
     if (!_compare_fn(expected_value, obtained_value))
     {
@@ -140,9 +144,22 @@ TfLiteTensorComparator::compare(const nnfw::support::tflite::TensorView<float> &
 
       diff.expected = expected_value;
       diff.obtained = obtained_value;
+      diff.relative_diff = relative_diff;
 
       res.emplace_back(diff);
     }
+
+    // Update max_diff_index, if necessary
+    if (max_diff != nullptr)
+    {
+      if (max_diff->relative_diff < relative_diff)
+      {
+        max_diff->index = index;
+        max_diff->expected = expected_value;
+        max_diff->obtained = obtained_value;
+        max_diff->relative_diff =  relative_diff;
+      }
+    }
   };
 
   return res;
@@ -187,8 +204,10 @@ int main(const int argc, char **argv)
   {
     const auto expected = nnfw::support::tflite::TensorView<float>::make(*pure, id);
     const auto obtained = nnfw::support::tflite::TensorView<float>::make(*delegated, id);
+    TfLiteTensorDiff max_diff(0);
+    max_diff.relative_diff = 0;
 
-    auto diffs = comparator.compare(expected, obtained);
+    auto diffs = comparator.compare(expected, obtained, &max_diff);
 
     if (diffs.size() == 0)
     {
@@ -197,7 +216,19 @@ int main(const int argc, char **argv)
     else
     {
       std::cout << "  Tensor #" << id << ": UMMATCHED" << std::endl;
+    }
 
+    // Print out max_diff
+    if (max_diff.relative_diff > 0)
+    {
+      std::cout << "  Max Diff at [" << TensorIndexFormatter(max_diff.index) << "]" << std::endl;
+      std::cout << "       expected: " << max_diff.expected << std::endl;
+      std::cout << "       obtained: " << max_diff.obtained << std::endl;
+      std::cout << "  relative diff: " << max_diff.relative_diff << std::endl;
+    }
+
+    if (diffs.size() > 0)
+    {
       if (verbose != 0)
       {
         for (const auto &diff : diffs)
@@ -205,7 +236,7 @@ int main(const int argc, char **argv)
           std::cout << "    Diff at [" << TensorIndexFormatter(diff.index) << "]" << std::endl;
           std::cout << "      expected: " << diff.expected << std::endl;
           std::cout << "      obtained: " << diff.obtained << std::endl;
-          std::cout << "      relative diff: " << nnfw::util::fp32::relative_diff(diff.expected, diff.obtained) << std::endl;
+          std::cout << "      relative diff: " << diff.relative_diff << std::endl;
         }
       }