[Android/Api] method to get data size
authorJaeyun <jy1210.jung@samsung.com>
Mon, 5 Aug 2019 05:28:49 +0000 (14:28 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Tue, 13 Aug 2019 10:14:15 +0000 (19:14 +0900)
add method to calculate byte size of tensor data.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
api/android/api/src/com/samsung/android/nnstreamer/TensorsData.java
api/android/api/src/com/samsung/android/nnstreamer/TensorsInfo.java

index b3f2972..8c66732 100644 (file)
@@ -60,39 +60,7 @@ public final class TensorsData implements AutoCloseable {
         int count = info.getTensorsCount();
 
         for (int i = 0; i < count; i++) {
-            int type = info.getTesorType(i);
-            int[] dimension = info.getTesorDimension(i);
-
-            int size = 0;
-
-            switch (type) {
-                case NNStreamer.TENSOR_TYPE_INT32:
-                case NNStreamer.TENSOR_TYPE_UINT32:
-                case NNStreamer.TENSOR_TYPE_FLOAT32:
-                    size = 4;
-                    break;
-                case NNStreamer.TENSOR_TYPE_INT16:
-                case NNStreamer.TENSOR_TYPE_UINT16:
-                    size = 2;
-                    break;
-                case NNStreamer.TENSOR_TYPE_INT8:
-                case NNStreamer.TENSOR_TYPE_UINT8:
-                    size = 1;
-                    break;
-                case NNStreamer.TENSOR_TYPE_FLOAT64:
-                case NNStreamer.TENSOR_TYPE_INT64:
-                case NNStreamer.TENSOR_TYPE_UINT64:
-                    size = 8;
-                    break;
-                default:
-                    /* unknown type */
-                    break;
-            }
-
-            for (int j = 0; j < NNStreamer.TENSOR_RANK_LIMIT; j++) {
-                size *= dimension[j];
-            }
-
+            int size = info.getTensorSize(i);
             data.addTensorData(allocateByteBuffer(size));
         }
 
index 0c0a7f8..457ae4f 100644 (file)
@@ -165,6 +165,27 @@ public final class TensorsInfo implements AutoCloseable {
     }
 
     /**
+     * Calculates the byte size of tensor data.
+     *
+     * @param index The index of the tensor information in the list
+     *
+     * @return The byte size of tensor
+     *
+     * @throws IndexOutOfBoundsException if the given index is invalid
+     * @throws IllegalStateException if data type or dimension is invalid
+     */
+    public int getTensorSize(int index) {
+        checkIndexBounds(index);
+
+        int size = mInfoList.get(index).getSize();
+        if (size <= 0) {
+            throw new IllegalStateException("Unknown data type or invalid dimension");
+        }
+
+        return size;
+    }
+
+    /**
      * Internal method to check the index.
      *
      * @throws IndexOutOfBoundsException if the given index is invalid
@@ -242,5 +263,39 @@ public final class TensorsInfo implements AutoCloseable {
         public int[] getDimension() {
             return this.dimension;
         }
+
+        public int getSize() {
+            int size = 0;
+
+            switch (this.type) {
+                case NNStreamer.TENSOR_TYPE_INT32:
+                case NNStreamer.TENSOR_TYPE_UINT32:
+                case NNStreamer.TENSOR_TYPE_FLOAT32:
+                    size = 4;
+                    break;
+                case NNStreamer.TENSOR_TYPE_INT16:
+                case NNStreamer.TENSOR_TYPE_UINT16:
+                    size = 2;
+                    break;
+                case NNStreamer.TENSOR_TYPE_INT8:
+                case NNStreamer.TENSOR_TYPE_UINT8:
+                    size = 1;
+                    break;
+                case NNStreamer.TENSOR_TYPE_FLOAT64:
+                case NNStreamer.TENSOR_TYPE_INT64:
+                case NNStreamer.TENSOR_TYPE_UINT64:
+                    size = 8;
+                    break;
+                default:
+                    /* unknown type */
+                    break;
+            }
+
+            for (int i = 0; i < NNStreamer.TENSOR_RANK_LIMIT; i++) {
+                size *= this.dimension[i];
+            }
+
+            return size;
+        }
     }
 }