summaryrefslogtreecommitdiff
path: root/docs/nnfw/howto/HowToUseNNFWAPI.md
blob: e093432751ac559723b030eab92a2d3ff0189d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Prepare nnpackage

## Convert tensorflow pb file to nnpackage
Follow the [compiler guide](https://github.sec.samsung.net/STAR/nnfw/blob/master/docs/nncc/Release_2019/tutorial.md) to generate nnpackge from tensorflow pb file

## Convert tflite file to nnpackage
Please see [model2nnpkg](https://github.sec.samsung.net/STAR/nnfw/tree/master/tools/nnpackage_tool/model2nnpkg) for converting from tflite model file.

# Build app with nnfw API

Here are basic steps to build app with [nnfw C API](https://github.sec.samsung.net/STAR/nnfw/blob/master/runtime/neurun/api/include/nnfw.h)

1) Initialize nnfw_session
``` c
nnfw_session *session = nullptr;
nnfw_create_session(&session);
```
2) Load nnpackage
``` c
nnfw_load_model_from_file(session, nnpackage_path);
```
3) (Optional) Assign a specific backend to operations
``` c
  // Use acl_neon backend for CONV_2D and acl_cl for otherwise.
  // Note that defalut backend is acl_cl
  nnfw_set_op_backend(session, "CONV_2D", "acl_neon");
```

4) Compilation
``` c
  // Compile model
  nnfw_prepare(session);
```

5) Prepare Input/Output
``` c
  // Prepare input. Here we just allocate dummy input arrays.
  std::vector<float> input;
  nnfw_tensorinfo ti;
  nnfw_input_tensorinfo(session, 0, &ti); // get first input's info
  uint32_t input_elements = num_elems(&ti);
  input.resize(input_elements);
  // TODO: Please add initialization for your input.
  nnfw_set_input(session, 0, ti.dtype, input.data(), sizeof(float) * input_elements);

  // Prepare output
  std::vector<float> output;
  nnfw_output_tensorinfo(session, 0, &ti); // get first output's info
  uint32_t output_elements = num_elems(&ti);
  output.resize(output_elements);
  nnfw_set_output(session, 0, ti.dtype, output.data(), sizeof(float) * output_elements);
```
6) Inference
``` c
  // Do inference
  nnfw_run(session);
```
## Run Inference with app on the target devices
reference app : [minimal app](https://github.sec.samsung.net/STAR/nnfw/blob/master/runtime/neurun/sample/minimal)

```
$ ./minimal path_to_nnpackage_directory
```