From 04ad083e246674109207802d77a2549be5d4b360 Mon Sep 17 00:00:00 2001 From: Wook Song Date: Thu, 5 Sep 2024 13:35:16 +0900 Subject: [PATCH] Data/Model: Add a method to get NNStreamer filter description This patch adds a method to get the filter description for the NNStreamer pipeline from the Model class. Signed-off-by: Wook Song --- .../ml/inference/offloading/MainService.kt | 19 +---------- .../ml/inference/offloading/data/Model.kt | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt index 4d6d4de..7e4ea9b 100644 --- a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt +++ b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt @@ -375,24 +375,7 @@ class MainService : Service() { val serviceId = runBlocking { preferencesDataStore.getIncrementalCounter() } - // TODO: The following code for generation of the NNStreamer pipeline string - val mFile = App.context().getExternalFilesDir("models")?.resolve(model.models) - val inTypes = model.inputInfo["type"]?.let { - "types=${model.inputInfo["type"]?.joinToString(",")}" - } ?: "" - val inDims = model.inputInfo["dimension"]?.let { - "dimensions=(string)${model.inputInfo["dimension"]?.joinToString(",")}" - } ?: "" - val outTypes = model.outputInfo["type"]?.let { - "types=${model.outputInfo["type"]?.joinToString(",")}" - } ?: "" - val outDims = model.outputInfo["dimension"]?.let { - "dimensions=(string)${model.outputInfo["dimension"]?.joinToString(",")}" - } ?: "" - val filter = - "other/tensors,num_tensors=${model.inputInfo["type"]?.size ?: 1},format=static,${inDims},${inTypes},framerate=0/1 ! " + - "tensor_filter framework=tensorflow-lite model=${mFile} ! " + - "other/tensors,num_tensors=${model.outputInfo["type"]?.size ?: 1},format=static,${outDims},${outTypes},framerate=0/1" + val filter = model.getNNSFilterDesc() val port = findPort() val desc = "tensor_query_serversrc id=" + serviceId.toString() + " host=" + hostAddress + " port=" + diff --git a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/data/Model.kt b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/data/Model.kt index 88d30be..7856a44 100644 --- a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/data/Model.kt +++ b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/data/Model.kt @@ -1,5 +1,6 @@ package ai.nnstreamer.ml.inference.offloading.data +import ai.nnstreamer.ml.inference.offloading.App import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey @@ -111,4 +112,36 @@ data class Model( var outputInfo: Map> = mapOf() var optionalInfo: Map = mapOf() + + /** + * Get NNS filter description string for the given model. This is used to create a new NNS pipeline instance. + * @return NNS filter description string. It includes paths of the model files and other information. + */ + fun getNNSFilterDesc(): String { + val basePath = App.context().getExternalFilesDir("models") + val modelPaths = models.split(",").run { + joinToString( + separator = ",", + transform = { basePath?.resolve(it).toString() } + ) + } + val inTypes = inputInfo["type"]?.let { + "types=${inputInfo["type"]?.joinToString(",")}" + } ?: "" + val inDims = inputInfo["dimension"]?.let { + "dimensions=(string)${inputInfo["dimension"]?.joinToString(",")}" + } ?: "" + val outTypes = outputInfo["type"]?.let { + "types=${outputInfo["type"]?.joinToString(",")}" + } ?: "" + val outDims = outputInfo["dimension"]?.let { + "dimensions=(string)${outputInfo["dimension"]?.joinToString(",")}" + } ?: "" + val filter = + "other/tensors,num_tensors=${inputInfo["type"]?.size ?: 1},format=static,${inDims},${inTypes},framerate=0/1 ! " + + "tensor_filter framework=tensorflow-lite model=${modelPaths} ! " + + "other/tensors,num_tensors=${outputInfo["type"]?.size ?: 1},format=static,${outDims},${outTypes},framerate=0/1" + + return filter + } }