Fill feature object with random numbers (#472)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 6 Apr 2018 00:50:54 +0000 (09:50 +0900)
committer김정현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh0822.kim@samsung.com>
Fri, 6 Apr 2018 00:50:54 +0000 (09:50 +0900)
This commit renames nnfw::util::feature::RandomObject as nnfw::util::feature::Object,
and revises it to initialize its content with user-defined number generator.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
include/util/feature/Index.h [new file with mode: 0644]
include/util/feature/Object.h [new file with mode: 0644]
include/util/feature/RandomObject.h [deleted file]
tools/nnapi_unittests/tests/conv_1.cpp

diff --git a/include/util/feature/Index.h b/include/util/feature/Index.h
new file mode 100644 (file)
index 0000000..a58fe04
--- /dev/null
@@ -0,0 +1,44 @@
+#ifndef __NNFW_UTIL_FEATURE_INDEX_H__
+#define __NNFW_UTIL_FEATURE_INDEX_H__
+
+#include <cstdint>
+
+namespace nnfw
+{
+namespace util
+{
+namespace feature
+{
+
+class Index
+{
+public:
+  Index() = default;
+
+public:
+  Index(int32_t ch, int32_t row, int32_t col) : _ch{ch}, _row{row}, _col{col}
+  {
+    // DO NOTHING    
+  }
+
+public:
+  int32_t ch(void) const { return _ch; }
+  int32_t row(void) const { return _row; }
+  int32_t col(void) const { return _col; }
+
+public:
+  int32_t &ch(void) { return _ch; }
+  int32_t &row(void) { return _row; }
+  int32_t &col(void) { return _col; }
+
+private:
+  int32_t _ch;
+  int32_t _row;
+  int32_t _col;
+};
+
+} // namespace feature
+} // namespace util
+} // namespace nnfw
+
+#endif // __NNFW_UTIL_FEATURE_INDEX_H__
diff --git a/include/util/feature/Object.h b/include/util/feature/Object.h
new file mode 100644 (file)
index 0000000..24e9342
--- /dev/null
@@ -0,0 +1,63 @@
+#ifndef __NNFW_UTIL_FEATURE_OBJECT_H__
+#define __NNFW_UTIL_FEATURE_OBJECT_H__
+
+#include "util/feature/Shape.h"
+#include "util/feature/Index.h"
+#include "util/feature/Reader.h"
+
+#include <vector>
+
+namespace nnfw
+{
+namespace util
+{
+namespace feature
+{
+
+template<typename T> class Object final : public Reader<T>
+{
+public:
+  using Generator = std::function<T (const Shape &shape, const Index &index)>;
+
+public:
+  Object(const Shape &shape, const Generator &fn) : _shape{shape}
+  {
+    _value.resize(_shape.C * _shape.H * _shape.W);
+
+    for (int32_t ch = 0; ch < _shape.C; ++ch)
+    {
+      for (int32_t row = 0; row < _shape.H; ++row)
+      {
+        for (int32_t col = 0; col < _shape.W; ++col)
+        {
+          _value.at(offsetOf(ch, row, col)) = fn(_shape, Index{ch, row, col});
+        }
+      }
+    }
+  }
+
+public:
+  const Shape &shape(void) const { return _shape; }
+
+public:
+  T at(uint32_t ch, uint32_t row, uint32_t col) const override
+  {
+    return _value.at(offsetOf(ch, row, col));
+  }
+
+private:
+  uint32_t offsetOf(uint32_t ch, uint32_t row, uint32_t col) const
+  {
+    return ch * _shape.H * _shape.W + row * _shape.W + col;
+  }
+
+private:
+  Shape _shape;
+  std::vector<T> _value;
+};
+
+} // namespace feature
+} // namespace util
+} // namespace nnfw
+
+#endif // __NNFW_UTIL_FEATURE_OBJECT_H__
diff --git a/include/util/feature/RandomObject.h b/include/util/feature/RandomObject.h
deleted file mode 100644 (file)
index e31a6ae..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-#ifndef __NNFW_UTIL_FEATURE_RANDOM_OBJECT_H__
-#define __NNFW_UTIL_FEATURE_RANDOM_OBJECT_H__
-
-#include "util/feature/Shape.h"
-#include "util/feature/Reader.h"
-
-#include <vector>
-
-namespace nnfw
-{
-namespace util
-{
-namespace feature
-{
-
-template<typename T> class RandomObject final : public Reader<T>
-{
-public:
-  RandomObject(const Shape &shape) : _shape{shape}
-  {
-    const auto size = _shape.C * _shape.H * _shape.W;
-
-    // TODO Use random number
-    for (uint32_t off = 0; off < size; ++off)
-    {
-      _value.emplace_back(off);
-    }
-  }
-
-public:
-  const Shape &shape(void) const { return _shape; }
-
-public:
-  T at(uint32_t ch, uint32_t row, uint32_t col) const override
-  {
-    return _value.at(ch * _shape.H * _shape.W + row * _shape.W + col);
-  }
-
-private:
-  Shape _shape;
-  std::vector<T> _value;
-};
-
-
-} // namespace feature
-} // namespace util
-} // namespace nnfw
-
-
-#endif // __NNFW_UTIL_FEATURE_RANDOM_OBJECT_H__
index 3719a41..8005fc8 100644 (file)
@@ -6,16 +6,20 @@
 
 #include "util/vector/Object.h"
 #include "util/feature/IndexIterator.h"
-#include "util/feature/RandomObject.h"
+#include "util/feature/Object.h"
 #include "util/feature/Reader.h"
 #include "util/kernel/RandomObject.h"
 #include "util/fp32.h"
+#include "util/environment.h"
 
 #include "support/tflite/FeatureView.h"
 
 #include <iostream>
 #include <cassert>
 
+#include <chrono>
+#include <random>
+
 using namespace tflite;
 using namespace tflite::ops::builtin;
 
@@ -24,6 +28,11 @@ using nnfw::support::tflite::OutputIndex;
 
 int main(int argc, char **argv)
 {
+  // Set random seed
+  int SEED = std::chrono::system_clock::now().time_since_epoch().count();
+
+  nnfw::util::env::IntAccessor("SEED").access(SEED);
+
 #define INT_VALUE(NAME, VALUE) IntVar NAME##_Value(#NAME, VALUE);
 #include "conv_1.lst"
 #undef INT_VALUE
@@ -44,13 +53,26 @@ int main(int argc, char **argv)
   const int32_t OFM_H = (IFM_H - KER_H) / STRIDE_H + 1;
   const int32_t OFM_W = (IFM_W - KER_W) / STRIDE_W + 1;
 
-  const nnfw::util::feature::RandomObject<float> ifm{nnfw::util::feature::Shape{IFM_C, IFM_H, IFM_W}};
+  // Initialize random number generator
+  std::minstd_rand random(SEED);
+
+  // Fill IFM with random numbers
+  auto ifm_gen = [&random] (const nnfw::util::feature::Shape &, const nnfw::util::feature::Index &)
+  {
+    std::normal_distribution<float> dist(0.0f,2.0f);
+    return dist(random);
+  };
+  const nnfw::util::feature::Shape ifm_shape{IFM_C, IFM_H, IFM_W};
+  const nnfw::util::feature::Object<float> ifm{ifm_shape, ifm_gen};
   const nnfw::util::kernel::RandomObject<float> kernel{nnfw::util::kernel::Shape{KER_N, KER_C, KER_H, KER_W}};
   const nnfw::util::vector::Object<float> bias{KER_N, [] (uint32_t, uint32_t) { return 0.0f; }};
 
   std::cout << "Configurations:" << std::endl;
 #define PRINT_NEWLINE() { std::cout << std::endl; }
 #define PRINT_VALUE(value) { std::cout << "  " << #value << ": " << (value) << std::endl; }
+  PRINT_VALUE(SEED);
+  PRINT_NEWLINE();
+
   PRINT_VALUE(STRIDE_H);
   PRINT_VALUE(STRIDE_W);
   PRINT_NEWLINE();