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>
--- /dev/null
+#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__
--- /dev/null
+#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__
+++ /dev/null
-#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__
#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;
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
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();