Skip to content

Commit

Permalink
Data/Model: Add a method to get NNStreamer filter description
Browse files Browse the repository at this point in the history
This patch adds a method to get the filter description for the
NNStreamer pipeline from the Model class.

Signed-off-by: Wook Song <[email protected]>
  • Loading branch information
wooksong authored and niley7464 committed Sep 5, 2024
1 parent 0e8dbab commit 04ad083
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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=" +
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -111,4 +112,36 @@ data class Model(
var outputInfo: Map<String, List<String>> = mapOf()

var optionalInfo: Map<String, String> = 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
}
}

0 comments on commit 04ad083

Please sign in to comment.