• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using MindSpore Lite JavaScript APIs to Develop AI Applications
2
3## Scenarios
4
5You can use the JavaScript APIs provided by MindSpore Lite to directly integrate MindSpore Lite capabilities into the UI code. This way, you can quickly deploy AI algorithms for AI model inference.
6
7## Basic Concepts
8
9Before getting started, you need to understand the following basic concepts:
10
11**Tensor**: a special data structure that is similar to arrays and matrices. It is basic data structure used in MindSpore Lite network operations.
12
13**Float16 inference mode**: a mode that uses half-precision inference. Float16 uses 16 bits to represent a number and therefore it is also called half-precision.
14
15## Available APIs
16APIs involved in MindSpore Lite model inference are categorized into context APIs, model APIs, and tensor APIs. For more APIs, see [@ohos.ai.mindSporeLite](../reference/apis/js-apis-mindSporeLite.md).
17
18| API       | Description       |
19| ------------------ | ----------------- |
20|loadModelFromFile(model: string, context?: Context): Promise<Model>|Loads a model from a file.|
21|getInputs(): MSTensor[]|Obtains the model input.|
22|predict(inputs: MSTensor[]): Promise<MSTensor[]>|Performs model inference.|
23| getData(): ArrayBuffer                 | Obtains tensor data.|
24| setData(inputArray: ArrayBuffer): void | Sets the tensor data.|
25
26## How to Develop
27
28Assume that you have prepared a model in the **.ms** format. The key steps in model inference are model reading, model building, model inference, and memory release. The development procedure is described as follows:
29
301. Create a context, and set parameters such as the number of runtime threads and device type.
312. Load the model. In this example, the model is read from the file.
323. Load data. Before executing a model, you need to obtain the model input and then fill data in the input tensor.
334. Perform model inference by calling **predict**, and read the output.
34
35```ts
36// Construct a singleton object.
37export class GlobalContext {
38  private constructor() {}
39  private static instance: GlobalContext;
40  private _objects = new Map<string, Object>();
41
42  public static getContext(): GlobalContext {
43    if (!GlobalContext.instance) {
44      GlobalContext.instance = new GlobalContext();
45    }
46    return GlobalContext.instance;
47  }
48
49  getObject(value: string): Object | undefined {
50    return this._objects.get(value);
51  }
52
53  setObject(key: string, objectClass: Object): void {
54    this._objects.set(key, objectClass);
55  }
56
57}
58```
59
60```ts
61import { GlobalContext } from '../GlobalContext';
62import mindSporeLite from '@ohos.ai.mindSporeLite';
63import common from '@ohos.app.ability.common';
64export class Test {
65  value:number = 0;
66  foo(): void {
67    GlobalContext.getContext().setObject("value", this.value);
68  }
69}
70let globalContext = GlobalContext.getContext().getObject("value") as common.UIAbilityContext;
71let inputBuffer : ArrayBuffer | null = null;
72let inputName: string = 'mnet_caffemodel_nhwc.bin';
73
74globalContext.resourceManager.getRawFileContent(inputName).then((buffer : Uint8Array) => {
75  inputBuffer = buffer.buffer;
76  console.log('=========input bin byte length: ' + buffer.byteLength)
77})
78// 1. Create a context.
79let context: mindSporeLite.Context = {};
80context.target = ['cpu'];
81context.cpu = {}
82context.cpu.threadNum = 1;
83context.cpu.threadAffinityMode = 0;
84context.cpu.precisionMode = 'enforce_fp32';
85
86// 2. Load the model.
87let modelFile = '/data/storage/el2/base/haps/entry/files/mnet.caffemodel.ms';
88let msLiteModel : mindSporeLite.Model = await mindSporeLite.loadModelFromFile(modelFile, context);
89
90// 3. Set the input data.
91let modelInputs : mindSporeLite.MSTensor[] = msLiteModel.getInputs();
92if (inputBuffer != null) {
93  modelInputs[0].setData(inputBuffer as ArrayBuffer);
94}
95
96// 4. Perform inference and print the output.
97console.log('=========MSLITE predict start=====')
98msLiteModel.predict(modelInputs).then((modelOutputs : mindSporeLite.MSTensor[]) => {
99  let output0 = new Float32Array(modelOutputs[0].getData());
100  for (let i = 0; i < output0.length; i++) {
101    console.log(output0[i].toString());
102  }
103  })
104console.log('=========MSLITE predict success=====')
105```
106
107## Debugging and Verification
108
1091. On DevEco Studio, connect to the device, click **Run entry**, and compile your own HAP. The following information is displayed:
110
111   ```shell
112   Launching com.example.myapptfjs
113   $ hdc uninstall com.example.myapptfjs
114   $ hdc install -r "path/to/xxx.hap"
115   $ hdc shell aa start -a EntryAbility -b com.example.myapptfjs
116   ```
117
1182. Use hdc to connect to the device, and push **mnet.caffemodel.ms** to the sandbox directory on the device. **mnet\_caffemodel\_nhwc.bin** is stored in the **rawfile** directory of the local project.
119
120   ```shell
121   hdc -t your_device_id file send .\mnet.caffemodel.ms /data/app/el2/100/base/com.example.myapptfjs/haps/entry/files/
122   ```
1233. Click **Test\_MSLiteModel\_predict** on the device screen to run the test case. The following information is displayed in the HiLog printing result:
124
125   ```shell
126   08-27 23:25:50.278 31782-31782/? I C03d00/JSAPP: =========MSLITE predict start=====
127   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.10046602040529252
128   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.07535600662231445
129   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.06326554715633392
130   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.0015114173293113708
131   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.026745859533548355
132   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.055590517818927765
133   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.05325715243816376
134   08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.04629542678594589
135   ...
136   08-27 23:25:52.881 31782-31782/? I C03d00/JSAPP: 0.23317644000053404
137   08-27 23:25:52.881 31782-31782/? I C03d00/JSAPP: 0.17999525368213654
138   08-27 23:25:50.372 31782-31782/? I C03d00/JSAPP: =========MSLITE predict success=====
139   ```
140