From 466c0072997a8016168eb9447a22496eebe7c13b Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Fri, 1 Sep 2023 16:38:08 +0900 Subject: [PATCH] [python] init dimension as 0 This patch removes filling the dimension value with 1. Tensor dimension initial value is 0. Signed-off-by: Yelin Jeong --- ext/nnstreamer/extra/nnstreamer_python3_helper.cc | 7 ++----- tests/test_models/models/scaler.py | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ext/nnstreamer/extra/nnstreamer_python3_helper.cc b/ext/nnstreamer/extra/nnstreamer_python3_helper.cc index 777f22c..1d0b872 100644 --- a/ext/nnstreamer/extra/nnstreamer_python3_helper.cc +++ b/ext/nnstreamer/extra/nnstreamer_python3_helper.cc @@ -396,6 +396,7 @@ parseTensorsInfo (PyObject *result, GstTensorsInfo *info) if (PyList_Size (result) < 0) return -1; + gst_tensors_info_init (info); info->num_tensors = PyList_Size (result); for (i = 0; i < info->num_tensors; i++) { /** don't own the reference */ @@ -466,7 +467,7 @@ parseTensorsInfo (PyObject *result, GstTensorsInfo *info) return -EINVAL; } - if (val <= 0) { + if (val < 0) { Py_ERRMSG ("The %u'th dimension value of the %u'th tensor is invalid (%d).", j + 1, i + 1, val); Py_SAFEDECREF (shape_dims); @@ -476,10 +477,6 @@ parseTensorsInfo (PyObject *result, GstTensorsInfo *info) info->info[i].dimension[j] = (uint32_t) val; } - /* Fill remained dims */ - for (; j < NNS_TENSOR_RANK_LIMIT; j++) - info->info[i].dimension[j] = 1U; - info->info[i].name = NULL; Py_SAFEDECREF (shape_dims); } diff --git a/tests/test_models/models/scaler.py b/tests/test_models/models/scaler.py index 263f6b0..1cec548 100644 --- a/tests/test_models/models/scaler.py +++ b/tests/test_models/models/scaler.py @@ -6,6 +6,8 @@ # @file scaler.py # @brief Python custom filter example: scaler # @author Dongju Chae +# @note This filter is an example of Python custom filter +# and only supports rank 2,3 models. import numpy as np import nnstreamer_python as nns @@ -55,17 +57,28 @@ class CustomFilter(object): # @return Output tensors: list of output numpy array def invoke(self, input_array): # reshape to n-D array (in reverse order) - input_tensor = np.reshape(input_array[0], self.input_dims[0].getDims()[::-1]) - output_tensor = np.empty(self.output_dims[0].getDims()[::-1], dtype=self.output_dims[0].getType()) in_dims = self.input_dims[0].getDims() out_dims = self.output_dims[0].getDims() - for z in range(out_dims[3]): + + reversed_in_dims = np.flip(in_dims) + reversed_out_dims = np.flip(out_dims) + + input_tensor = np.reshape(input_array[0], reversed_in_dims[~np.in1d(reversed_in_dims, np.array([0, 1]))]) + output_tensor = np.empty(reversed_out_dims[~np.in1d(reversed_out_dims, np.array([0, 1]))], dtype=self.output_dims[0].getType()) + + if len(output_tensor.shape) == 3: for y in range(out_dims[2]): for x in range(out_dims[1]): for c in range(out_dims[0]): ix = int(x * in_dims[1] / out_dims[1]) iy = int(y * in_dims[2] / out_dims[2]) - output_tensor[0][0][0][0][z][y][x][c] = input_tensor[0][0][0][0][z][iy][ix][c] + output_tensor[y][x][c] = input_tensor[iy][ix][c] + elif len(output_tensor.shape) == 2: + for x in range(out_dims[1]): + for c in range(out_dims[0]): + ix = int(x * in_dims[1] / out_dims[1]) + iy = int(in_dims[2] / out_dims[2]) + output_tensor[x][c] = input_tensor[ix][c] # to 1-D array return [np.ravel(output_tensor)] -- 2.7.4