Skip to content

Commit

Permalink
[Tizen] Add example app showing ml-service and rpk updates
Browse files Browse the repository at this point in the history
- Add a simple app showing how to use API `ml_service_new` and
  `ml_service_request`.
- This app also demonstrate that how rpk updates can be used in AI app.

Signed-off-by: Yongjoo Ahn <[email protected]>
  • Loading branch information
anyj0527 authored and myungjoo committed Jul 11, 2024
1 parent a2672fc commit 3b023f7
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 2 deletions.
92 changes: 92 additions & 0 deletions Tizen.native/ml-service-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Example of using ML Service APIs with RPKs

## org.example.sum_and_add

This app prints invoke results of `sum_and_add` model from rpk. Depend on the rpk version, the output should be different.

## RPK "org.resource.model.sum_and_add" v1

This RPK contains `sum_and_add` model file, `sum_and_add_one.tflite`. Made by this python script:

```python
import torch
import ai_edge_torch

class AddModelOne(torch.nn.Module):
def __init__(self):
super().__init__()

def forward(self, input_tensor):
output = torch.sum(input_tensor, (1)) + 1.0
return output # sum all value of input tensor and add 1.0

sum_and_add_one = AddModelOne().eval()
sum_and_add_one(torch.tensor([[1.0, 2.0, 3.0, 4.0]])) # output: tensor([11.])

sample_inputs = (torch.randn(1, 4),) # restrict the shape of input as (1, 4)
edge_model = ai_edge_torch.convert(sum_and_add_one, sample_inputs) # convert the model
edge_model(torch.tensor([[1.0, 2.0, 3.0, 4.0]])) # validate the output: tensor([11.])
edge_model.export("sum_and_add_one.tflite") # save as tflite model file
```

## RPK "org.resource.model.sum_and_add" v2

This RPK contains `sum_and_add` model file, `sum_and_add_two.tflite`. Made by:

```python
class AddModelTwo(torch.nn.Module):
def __init__(self):
super().__init__()

def forward(self, input_tensor):
output = torch.sum(input_tensor, (1)) + 2.0
return output # sum all value of input tensor and add 2.0

sum_and_add_two = AddModelTwo().eval()
sum_and_add_two(torch.tensor([[1.0, 2.0, 3.0, 4.0]])) # output: tensor([12.])

sample_inputs = (torch.randn(1, 4),) # restrict the shape of input as (1, 4)
edge_model = ai_edge_torch.convert(sum_and_add_two, sample_inputs) # convert the model
edge_model(torch.tensor([[1.0, 2.0, 3.0, 4.0]])) # validate the output: tensor([12.])
edge_model.export("sum_and_add_two.tflite") # save as tflite model file
```

## Scenario

The app `org.example.sum_and_add` uses AI defined in the RPK `org.resource.model.sum_and_add`.

1. install RPK `org.resource.model.sum_and_add` V1

```bash
cd sum_and_add_model_rpk_v1
tizen package -t rpk -- .
tizen install -n "./Package/org.resource.model.sum_and_add-1.0.0.rpk"
```

2. Install and run the app `org.example.sum_and_add`

```bash
cd sum_and_add_app
tizen build-native -r tizen-9.0-device64.core -- .
tizen package -t tpk -- ./Debug
tizen install -n "./Debug/org.example.sum_and_add-1.0.0-aarch64.tpk"
tizen run -p "org.example.sum_and_add"
```

![v1](./screenshot_with_rpk_v1.png)

3. Update the RPK with V2 and run the app again

```bash
cd sum_and_add_model_rpk_v2
tizen package -t rpk -- .
tizen install -n "./Package/org.resource.model.sum_and_add-2.0.0.rpk"
tizen run -p "org.example.sum_and_add"
```

The result of app is changed:
![v2](./screenshot_with_rpk_v2.png)

## Note

The app `org.example.sum_and_add` is packaged and deployed only once. Developers can update the app's AI model with RPK update.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions Tizen.native/ml-service-example/sum_and_add_app/.exportMap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
global: main;
_IO_*;
local: *;
};
29 changes: 29 additions & 0 deletions Tizen.native/ml-service-example/sum_and_add_app/inc/sum_and_add.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @file sum_and_add.h
* @date 4 Jul 2024
* @brief Tizen ML Service API example with RPK
* @see https://github.com/nnstreamer/nnstreamer
* https://github.com/nnstreamer/api
* @author Yongjoo Ahn <[email protected]>
* @bug No known bugs except for NYI items.
*/

#ifndef __sum_and_add_H__
#define __sum_and_add_H__

#include <app.h>
#include <Elementary.h>
#include <system_settings.h>
#include <efl_extension.h>
#include <dlog.h>

#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "sum_and_add"

#if !defined(PACKAGE)
#define PACKAGE "org.example.sum_and_add"
#endif

#endif /* __sum_and_add_H__ */
11 changes: 11 additions & 0 deletions Tizen.native/ml-service-example/sum_and_add_app/project_def.prop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
APPNAME = sum_and_add

type = app
profile = tizeniot-9.0

USER_SRCS = src/sum_and_add.c
USER_DEFS =
USER_INC_DIRS = inc
USER_OBJS =
USER_LIBS =
USER_EDCS =
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3b023f7

Please sign in to comment.