Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / docs / howto / how-to-use-nnfw-api.md
1 # How to Use NNFW API
2
3 ## Prepare nnpackage
4
5 ### Convert tensorflow pb file to nnpackage
6 Follow the [compiler guide](https://github.com/Samsung/ONE/blob/master/docs/nncc/v1.0.0/tutorial.md) to generate nnpackge from tensorflow pb file
7
8 ### Convert tflite file to nnpackage
9 Please see [model2nnpkg](https://github.com/Samsung/ONE/tree/master/tools/nnpackage_tool/model2nnpkg) for converting from tflite model file.
10
11 ## Build app with NNFW API
12
13 Here are basic steps to build app with [NNFW C API](https://github.com/Samsung/ONE/blob/master/runtime/onert/api/include/nnfw.h)
14
15 1) Initialize nnfw_session
16 ``` c
17 nnfw_session *session = nullptr;
18 nnfw_create_session(&session);
19 ```
20 2) Load nnpackage
21 ``` c
22 nnfw_load_model_from_file(session, nnpackage_path);
23 ```
24 3) (Optional) Assign a specific backend to operations
25 ``` c
26   // Use 'acl_neon' backend for CONV_2D and 'cpu' for otherwise.
27   // Note that defalut backend is 'cpu'.
28   nnfw_set_op_backend(session, "CONV_2D", "acl_neon");
29 ```
30
31 4) Compilation
32 ``` c
33   // Compile model
34   nnfw_prepare(session);
35 ```
36
37 5) Prepare Input/Output
38 ``` c
39   // Prepare input. Here we just allocate dummy input arrays.
40   std::vector<float> input;
41   nnfw_tensorinfo ti;
42   nnfw_input_tensorinfo(session, 0, &ti); // get first input's info
43   uint32_t input_elements = num_elems(&ti);
44   input.resize(input_elements);
45   // TODO: Please add initialization for your input.
46   nnfw_set_input(session, 0, ti.dtype, input.data(), sizeof(float) * input_elements);
47
48   // Prepare output
49   std::vector<float> output;
50   nnfw_output_tensorinfo(session, 0, &ti); // get first output's info
51   uint32_t output_elements = num_elems(&ti);
52   output.resize(output_elements);
53   nnfw_set_output(session, 0, ti.dtype, output.data(), sizeof(float) * output_elements);
54 ```
55 6) Inference
56 ``` c
57   // Do inference
58   nnfw_run(session);
59 ```
60
61 ## Run Inference with app on the target devices
62 reference app : [minimal app](https://github.com/Samsung/ONE/blob/master/runtime/onert/sample/minimal)
63
64 ```
65 $ ./minimal path_to_nnpackage_directory
66 ```