From dfcd06a5c1d6e3dc0367cc220c0b8783b8182c24 Mon Sep 17 00:00:00 2001 From: Jaeyun Date: Mon, 5 Aug 2019 14:28:49 +0900 Subject: [PATCH] [Android/Api] method to get data size add method to calculate byte size of tensor data. Signed-off-by: Jaeyun Jung --- .../samsung/android/nnstreamer/TensorsData.java | 34 +------------ .../samsung/android/nnstreamer/TensorsInfo.java | 55 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/api/android/api/src/com/samsung/android/nnstreamer/TensorsData.java b/api/android/api/src/com/samsung/android/nnstreamer/TensorsData.java index b3f2972..8c66732 100644 --- a/api/android/api/src/com/samsung/android/nnstreamer/TensorsData.java +++ b/api/android/api/src/com/samsung/android/nnstreamer/TensorsData.java @@ -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)); } diff --git a/api/android/api/src/com/samsung/android/nnstreamer/TensorsInfo.java b/api/android/api/src/com/samsung/android/nnstreamer/TensorsInfo.java index 0c0a7f8..457ae4f 100644 --- a/api/android/api/src/com/samsung/android/nnstreamer/TensorsInfo.java +++ b/api/android/api/src/com/samsung/android/nnstreamer/TensorsInfo.java @@ -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; + } } } -- 2.7.4