Using 1D array to get the data from user function
authorjijoong.moon <jijoong.moon@samsung.com>
Fri, 22 May 2020 10:02:09 +0000 (19:02 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 25 May 2020 04:37:25 +0000 (13:37 +0900)
It is not good to get the user data as 3D std::vector format. C does
not support it. Therefore it is much better to get the data as 1D
float array (float *). In this PR, re-define function pointer to get
data as float * format.

**Self evaluation:**
1. Build test:  [X]Passed [ ]Failed [ ]Skipped
2. Run test:  [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: jijoong.moon <jijoong.moon@samsung.com>
Applications/Classification/jni/main_func.cpp
nntrainer/include/databuffer_func.h
nntrainer/include/neuralnet.h
nntrainer/src/databuffer_func.cpp
nntrainer/src/neuralnet.cpp

index d378d3a173deda1c0f591c7f993edc86ad06757b..40184c0075f32f0b44626a9c819eef42cb8c5812 100644 (file)
@@ -140,9 +140,7 @@ bool getData(std::ifstream &F, std::vector<float> &outVec,
  * @param[out] status for error handling
  * @retval true/false
  */
-bool getMiniBatch_train(std::vector<std::vector<std::vector<float>>> &outVec,
-                        std::vector<std::vector<std::vector<float>>> &outLabel,
-                        int &status) {
+bool getMiniBatch_train(float *outVec, float *outLabel, int *status) {
   std::vector<int> memI;
   std::vector<int> memJ;
   unsigned int count = 0;
@@ -174,8 +172,6 @@ bool getMiniBatch_train(std::vector<std::vector<std::vector<float>>> &outVec,
   }
 
   for (unsigned int i = 0; i < count; i++) {
-    std::vector<std::vector<float>> out;
-    std::vector<std::vector<float>> outL;
     std::vector<float> o;
     std::vector<float> l;
 
@@ -184,11 +180,10 @@ bool getMiniBatch_train(std::vector<std::vector<std::vector<float>>> &outVec,
 
     getData(F, o, l, memI[i]);
 
-    out.push_back(o);
-    outL.push_back(l);
-
-    outVec.push_back(out);
-    outLabel.push_back(outL);
+    for (unsigned int j = 0; j < feature_size; ++j)
+      outVec[i * feature_size + j] = o[j];
+    for (unsigned int j = 0; j < total_label_size; ++j)
+      outLabel[i * total_label_size + j] = l[j];
   }
 
   F.close();
@@ -202,9 +197,8 @@ bool getMiniBatch_train(std::vector<std::vector<std::vector<float>>> &outVec,
  * @param[out] status for error handling
  * @retval true/false false : end of data
  */
-bool getMiniBatch_val(std::vector<std::vector<std::vector<float>>> &outVec,
-                      std::vector<std::vector<std::vector<float>>> &outLabel,
-                      int &status) {
+bool getMiniBatch_val(float *outVec, float *outLabel, int *status) {
+
   std::vector<int> memI;
   std::vector<int> memJ;
   unsigned int count = 0;
@@ -236,8 +230,6 @@ bool getMiniBatch_val(std::vector<std::vector<std::vector<float>>> &outVec,
   }
 
   for (unsigned int i = 0; i < count; i++) {
-    std::vector<std::vector<float>> out;
-    std::vector<std::vector<float>> outL;
     std::vector<float> o;
     std::vector<float> l;
 
@@ -246,11 +238,10 @@ bool getMiniBatch_val(std::vector<std::vector<std::vector<float>>> &outVec,
 
     getData(F, o, l, memI[i]);
 
-    out.push_back(o);
-    outL.push_back(l);
-
-    outVec.push_back(out);
-    outLabel.push_back(outL);
+    for (unsigned int j = 0; j < feature_size; ++j)
+      outVec[i * feature_size + j] = o[j];
+    for (unsigned int j = 0; j < total_label_size; ++j)
+      outLabel[i * total_label_size + j] = l[j];
   }
 
   F.close();
index 2d8fe599b2d47fb9298eec139537a082df7b7153..aaa780ffb786af3b5c56cd8cbf9fdfc2235fd8ce 100644 (file)
@@ -67,7 +67,7 @@ public:
    * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
    */
   int setFunc(BufferType type,
-              std::function<bool(vec_3d &, vec_3d &, int &)> func);
+              std::function<bool(float *, float *, int *)> func);
 
   /**
    * @brief     Update Data Buffer ( it is for child thread )
@@ -86,9 +86,9 @@ private:
    * @retval true / false generate all data for this epoch
    *
    */
-  std::function<bool(vec_3d &, vec_3d &, int &)> callback_train;
-  std::function<bool(vec_3d &, vec_3d &, int &)> callback_val;
-  std::function<bool(vec_3d &, vec_3d &, int &)> callback_test;
+  std::function<bool(float *, float *, int *)> callback_train;
+  std::function<bool(float *, float *, int *)> callback_val;
+  std::function<bool(float *, float *, int *)> callback_test;
 };
 } // namespace nntrainer
 #endif /* __cplusplus */
index 0792f04c9c6a579fe92d1e76936fb834a5a46a99..9e0632a259f1b19539069c51db3db62365bce974 100644 (file)
@@ -211,9 +211,9 @@ public:
    * @retval #ML_ERROR_NONE Successful.
    * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
    */
-  int train(std::function<bool(vec_3d &, vec_3d &, int &)> function_train,
-            std::function<bool(vec_3d &, vec_3d &, int &)> function_val,
-            std::function<bool(vec_3d &, vec_3d &, int &)> function_test);
+  int train(std::function<bool(float *, float *, int *)> function_train,
+            std::function<bool(float *, float *, int *)> function_val,
+            std::function<bool(float *, float *, int *)> function_test);
 
   /**
    * @brief     check neural network whether the hyper-parameters are set.
index 30af206b0e7e1ea9550914b53e904eccf2599b34..898b9741366da2a6a3f5985c8ff2d3ba8bd5b7eb 100644 (file)
@@ -96,7 +96,7 @@ int DataBufferFromCallback::init() {
 }
 
 int DataBufferFromCallback::setFunc(
-  BufferType type, std::function<bool(vec_3d &, vec_3d &, int &)> func) {
+  BufferType type, std::function<bool(float *, float *, int *)> func) {
 
   int status = ML_ERROR_NONE;
   switch (type) {
@@ -131,7 +131,7 @@ void DataBufferFromCallback::updateData(BufferType type, int &status) {
   bool *running = NULL;
   std::vector<std::vector<float>> *data = NULL;
   std::vector<std::vector<float>> *datalabel = NULL;
-  std::function<bool(vec_3d &, vec_3d &, int &)> callback;
+  std::function<bool(float *, float *, int *)> callback;
 
   switch (type) {
   case BUF_TRAIN: {
@@ -172,31 +172,35 @@ void DataBufferFromCallback::updateData(BufferType type, int &status) {
     return;
   }
 
+  float *vec =
+    (float *)malloc(sizeof(float) * input_dim.batch() * input_dim.channel() *
+                    input_dim.height() * input_dim.width());
+  float *veclabel =
+    (float *)malloc(sizeof(float) * input_dim.batch() * class_num);
+
   while ((*running)) {
     if (buf_size - (*cur_size) > 0) {
-      vec_3d vec;
-      vec_3d veclabel;
-
-      bool endflag = callback(vec, veclabel, status);
+      bool endflag = callback(vec, veclabel, &status);
       if (!endflag)
         break;
 
-      if (vec.size() != veclabel.size()) {
-        status = ML_ERROR_INVALID_PARAMETER;
-      }
-
-      for (unsigned int i = 0; i < vec.size(); ++i) {
+      for (unsigned int i = 0; i < input_dim.batch(); ++i) {
         std::vector<float> v;
         std::vector<float> vl;
-        for (unsigned int j = 0; j < vec[i].size(); ++j) {
-          for (unsigned int k = 0; k < vec[i][j].size(); ++k) {
-            v.push_back(vec[i][j][k]);
+        unsigned int I =
+          i * input_dim.channel() * input_dim.height() * input_dim.width();
+        for (unsigned int j = 0; j < input_dim.channel(); ++j) {
+          unsigned int J = j * input_dim.height() * input_dim.width();
+          for (unsigned int k = 0; k < input_dim.height() * input_dim.width();
+               ++k) {
+            unsigned int K = I + J + k;
+            v.push_back(vec[K]);
           }
         }
-        for (unsigned int j = 0; j < veclabel[i].size(); ++j) {
-          for (unsigned int k = 0; k < veclabel[i][j].size(); ++k) {
-            vl.push_back(veclabel[i][j][k]);
-          }
+
+        I = i * class_num;
+        for (unsigned int j = 0; j < class_num; ++j) {
+          vl.push_back(veclabel[I + j]);
         }
 
         data_lock.lock();
@@ -229,6 +233,8 @@ void DataBufferFromCallback::updateData(BufferType type, int &status) {
       }
     }
   }
+  free(vec);
+  free(veclabel);
 }
 
 } /* namespace nntrainer */
index 1e4d098669504bd5b2ffcdbbe68710427d7eee1f..a3f4d8b5a1adb4ea9db2c8f7b376e1344a847423 100644 (file)
@@ -634,9 +634,9 @@ int NeuralNetwork::train(std::vector<std::string> values) {
  * @brief     Run NeuralNetwork train
  */
 int NeuralNetwork::train(
-  std::function<bool(vec_3d &, vec_3d &, int &)> train_func,
-  std::function<bool(vec_3d &, vec_3d &, int &)> val_func,
-  std::function<bool(vec_3d &, vec_3d &, int &)> test_func) {
+  std::function<bool(float *, float *, int *)> train_func,
+  std::function<bool(float *, float *, int *)> val_func,
+  std::function<bool(float *, float *, int *)> test_func) {
 
   int status = ML_ERROR_NONE;