提交 7c266b83 编写于 作者: S shawn_he

update docs

Signed-off-by: Nshawn_he <shawn.he@huawei.com>
上级 266d98ec
# AI
- [Using MindSpore Lite for Model Inference (JS)](mindspore-lite-js-guidelines.md)
# Using MindSpore Lite for Model Inference (JS)
## Scenarios
MindSpore Lite is an AI engine that implements AI model inference for different hardware devices. It has been used in a wide range of fields, such as image classification, target recognition, facial recognition, and character recognition.
This document describes the general development process for implementing MindSpore Lite model inference. For details about how to use native APIs to implement model inference, see [Using MindSpore Lite for Model Inference](../napi/mindspore-lite-guidelines.md).
## Basic Concepts
Before getting started, you need to understand the following basic concepts:
**Tensor**: a special data structure that is similar to arrays and matrices. It is basic data structure used in MindSpore Lite network operations.
**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.
## Available APIs
APIs 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).
| API | Description |
| ------------------ | ----------------- |
|loadModelFromFile(model: string, options: Context): Promise&lt;Model&gt;|Loads a model from a file.|
|getInputs(): MSTensor[]|Obtains the model input.|
|predict(inputs: MSTensor[]): Promise&lt;MSTensor&gt;|Performs model inference.|
| getData(): ArrayBuffer | Obtains tensor data.|
| setData(inputArray: ArrayBuffer): void | Sets the tensor data.|
## How to Develop
The development process consists of the following main steps:
1. Prepare the required model. You can download the required model directly or obtain the model by using the model conversion tool. The required data is read from the `bin` file.
- If the downloaded model is in the `.ms` format, you can use it directly for inference. This document uses `mnet.caffemodel.ms` as an example.
- If the downloaded model uses a third-party framework, such as TensorFlow, TensorFlow Lite, Caffe, or ONNX, you can use the [model conversion tool](https://www.mindspore.cn/lite/docs/en/r2.0/use/downloads.html#1-8-1) to convert it to the `.ms` format.
2. Create a context, and set parameters such as the number of runtime threads and device type.
3. Load the model. In this example, the model is read from the file.
4. Load data. Before executing a model, you need to obtain the model input and then fill data in the input tensor.
5. Perform inference and print the output. Call the **predict** API to perform model inference.
```js
@State inputName: string = 'mnet_caffemodel_nhwc.bin';
@State T_model_predict: string = 'Test_MSLiteModel_predict'
inputBuffer: any = null;
build() {
Row() {
Column() {
Text(this.T_model_predict)
.focusable(true)
.fontSize(30)
.fontWeight(FontWeight.Bold)
.onClick(async () => {
// 1. Prepare for a model.
let syscontext = globalThis.context;
syscontext.resourceManager.getRawFileContent(this.inputName).then((buffer) => {
this.inputBuffer = buffer;
console.log('=========input bin byte length: ' + this.inputBuffer.byteLength)
}).catch(error => {
console.error('Failed to get buffer, error code: ${error.code},message:${error.message}.');
})
// 2. Create a context.
let context: mindSporeLite.Context = {};
context.target = ['cpu'];
context.cpu = {}
context.cpu.threadNum = 1;
context.cpu.threadAffinityMode = 0;
context.cpu.precisionMode = 'enforce_fp32';
// 3. Load the model.
let modelFile = '/data/storage/el2/base/haps/entry/files/mnet.caffemodel.ms';
let msLiteModel = await mindSporeLite.loadModelFromFile(modelFile, context);
// 4. Load data.
const modelInputs = msLiteModel.getInputs();
modelInputs[0].setData(this.inputBuffer.buffer);
// 5. Perform inference and print the output.
console.log('=========MSLITE predict start=====')
msLiteModel.predict(modelInputs).then((modelOutputs) => {
let output0 = new Float32Array(modelOutputs[0].getData());
for (let i = 0; i < output0.length; i++) {
console.log(output0[i].toString());
}
})
console.log('=========MSLITE predict success=====')
})
}
.width('100%')
}
.height('100%')
}
```
## Debugging and Verification
1. Connect to the rk3568 development board on DevEco Studio, click **Run entry**, and compile your own HAP. The following information is displayed:
```shell
Launching com.example.myapptfjs
$ hdc uninstall com.example.myapptfjs
$ hdc install -r "D:\TVOS\JSAPI\MyAppTfjs\entry\build\default\outputs\default\entry-default-signed.hap"
$ hdc shell aa start -a EntryAbility -b com.example.myapptfjs
```
2. Use the hdc tool to connect to the rk3568 development board 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.
```shell
hdc -t 7001005458323933328a00bcdf423800 file send .\mnet.caffemodel.ms /data/app/el2/100/base/com.example.myapptfjs/haps/entry/files/
```
3. Click **Test\_MSLiteModel\_predict** on the screen of the rk3568 development board to run the test case. The following information is displayed in the HiLog printing result:
```shell
08-27 23:25:50.278 31782-31782/? I C03d00/JSAPP: =========MSLITE predict start=====
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.10046602040529252
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.07535600662231445
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.06326554715633392
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: 0.0015114173293113708
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.026745859533548355
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.055590517818927765
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.05325715243816376
08-27 23:25:51.487 31782-31782/? I C03d00/JSAPP: -0.04629542678594589
...
08-27 23:25:52.881 31782-31782/? I C03d00/JSAPP: 0.23317644000053404
08-27 23:25:52.881 31782-31782/? I C03d00/JSAPP: 0.17999525368213654
08-27 23:25:50.372 31782-31782/? I C03d00/JSAPP: =========MSLITE predict success=====
```
......@@ -85,7 +85,7 @@ The development process consists of the following main steps:
The required model can be downloaded directly or obtained using the model conversion tool.
- If the downloaded model is in the `.ms` format, you can use it directly for inference. The following uses the **mobilenetv2.ms** model as an example.
- If the downloaded model uses a third-party framework, such as TensorFlow, TensorFlow Lite, Caffe, or ONNX, you can use the [model conversion tool](https://www.mindspore.cn/lite/docs/zh-CN/r1.5/use/downloads.html#id1) to convert it to the .ms format.
- If the downloaded model uses a third-party framework, such as TensorFlow, TensorFlow Lite, Caffe, or ONNX, you can use the [model conversion tool](https://www.mindspore.cn/lite/docs/en/r1.5/use/downloads.html#id1) to convert it to the .ms format.
2. Create a context, and set parameters such as the number of runtime threads and device type.
......@@ -280,7 +280,3 @@ The development process consists of the following main steps:
output data is:
0.000018 0.000012 0.000026 0.000194 0.000156 0.001501 0.000240 0.000825 0.000016 0.000006 0.000007 0.000004 0.000004 0.000004 0.000015 0.000099 0.000011 0.000013 0.000005 0.000023 0.000004 0.000008 0.000003 0.000003 0.000008 0.000014 0.000012 0.000006 0.000019 0.000006 0.000018 0.000024 0.000010 0.000002 0.000028 0.000372 0.000010 0.000017 0.000008 0.000004 0.000007 0.000010 0.000007 0.000012 0.000005 0.000015 0.000007 0.000040 0.000004 0.000085 0.000023
```
## Samples
The following sample is provided to help you better understand how to use MindSpore Lite:
- [Simple MindSpore Lite Tutorial](https://gitee.com/openharmony/third_party_mindspore/tree/OpenHarmony-3.2-Release/mindspore/lite/examples/quick_start_c)
......@@ -231,7 +231,7 @@ promise.then(data => {
getNetworkSelectionMode\(slotId: number, callback: AsyncCallback\<NetworkSelectionMode\>\): void
Obtains the network selection mode of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
Obtains the network selection mode for the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.Telephony.CoreService
......@@ -268,7 +268,7 @@ radio.getNetworkSelectionMode(slotId, (err, data) => {
getNetworkSelectionMode\(slotId: number\): Promise\<NetworkSelectionMode\>
Obtains the network selection mode of the SIM card in the specified slot. This API uses a promise to return the result.
Obtains the network selection mode for the SIM card in the specified slot. This API uses a promise to return the result.
**System capability**: SystemCapability.Telephony.CoreService
......@@ -957,7 +957,7 @@ promise.then(() => {
getIMEI\(callback: AsyncCallback\<string\>\): void
Obtains the IMEI of the SIM card in a card slot. This API uses an asynchronous callback to return the result.
Obtains the IMEI for the SIM card in a card slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
......@@ -998,7 +998,7 @@ radio.getIMEI((err, data) => {
getIMEI\(slotId: number, callback: AsyncCallback\<string\>\): void
Obtains the IMEI of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
Obtains the IMEI for the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
......@@ -1041,7 +1041,7 @@ radio.getIMEI(slotId, (err, data) => {
getIMEI\(slotId?: number\): Promise\<string\>
Obtains the IMEI of the SIM card in the specified slot. This API uses a promise to return the result.
Obtains the IMEI for the SIM card in the specified slot. This API uses a promise to return the result.
**System API**: This is a system API.
......@@ -1091,7 +1091,7 @@ promise.then(data => {
getMEID\(callback: AsyncCallback\<string\>\): void
Obtains the MEID of the SIM card in a card slot. This API uses an asynchronous callback to return the result.
Obtains the MEID for the SIM card in a card slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
......@@ -1132,7 +1132,7 @@ radio.getMEID((err, data) => {
getMEID\(slotId: number, callback: AsyncCallback\<string\>\): void
Obtains the MEID of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
Obtains the MEID for the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
......@@ -1175,7 +1175,7 @@ radio.getMEID(slotId, (err, data) => {
getMEID\(slotId?: number\): Promise\<string\>
Obtains the MEID of the SIM card in the specified slot. This API uses a promise to return the result.
Obtains the MEID for the SIM card in the specified slot. This API uses a promise to return the result.
**System API**: This is a system API.
......@@ -1225,7 +1225,7 @@ promise.then(data => {
getUniqueDeviceId\(callback: AsyncCallback\<string\>\): void
Obtains the unique device ID of the SIM card in a card slot. This API uses an asynchronous callback to return the result.
Obtains the unique device ID for the SIM card in a card slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
......@@ -1266,7 +1266,7 @@ radio.getUniqueDeviceId((err, data) => {
getUniqueDeviceId\(slotId: number, callback: AsyncCallback\<string\>\): void
Obtains the unique device ID of the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
Obtains the unique device ID for the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
......@@ -1309,7 +1309,7 @@ radio.getUniqueDeviceId(slotId, (err, data) => {
getUniqueDeviceId\(slotId?: number\): Promise\<string\>
Obtains the unique device ID of the SIM card in the specified slot. This API uses a promise to return the result.
Obtains the unique device ID for the SIM card in the specified slot. This API uses a promise to return the result.
**System API**: This is a system API.
......@@ -2867,6 +2867,206 @@ promise.then(data => {
```
## radio.getNetworkCapability<sup>10+</sup>
getNetworkCapability\(slotId: number, type: NetworkCapabilityType, callback: AsyncCallback\<NetworkCapabilityState\>\): void
Obtains the network capability for the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**Required permission**: ohos.permission.GET_TELEPHONY_STATE
**System capability**: SystemCapability.Telephony.CoreService
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -----------------------------------------------------------------------| ---- | ----------------------------------- |
| slotId | number | Yes | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2|
| type | [NetworkCapabilityType](#networkcapabilitytype10) | Yes | Network capability type. |
| callback | AsyncCallback\<[NetworkCapabilityState](#networkcapabilitystate10)\> | Yes | Callback used to return the result. |
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 8300001 | Invalid parameter value. |
| 8300002 | Operation failed. Cannot connect to service. |
| 8300003 | System internal error. |
| 8300999 | Unknown error code. |
**Example**
```js
let slotId = 0;
let type = radio.NetworkCapabilityType.SERVICE_TYPE_NR;
radio.getNetworkCapability(slotId, type, (err, data) => {
console.log(`callback: err->${JSON.stringify(err)}, data->${JSON.stringify(data)}`);
});
```
## radio.getNetworkCapability<sup>10+</sup>
getNetworkCapability\(slotId: number, type: NetworkCapabilityType\): Promise\<NetworkCapabilityState\>
Obtains the network capability for the SIM card in the specified slot. This API uses a promise to return the result.
**System API**: This is a system API.
**Required permission**: ohos.permission.GET_TELEPHONY_STATE
**System capability**: SystemCapability.Telephony.CoreService
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------------------- | ---- | -------------------------------------- |
| slotId | number | Yes | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2|
| type | [NetworkCapabilityType](#networkcapabilitytype10) | Yes | Network capability type. |
**Return value**
| Type | Description |
| ------------------------------------------------------------- | ----------------------- |
| Promise\<[NetworkCapabilityState](#networkcapabilitystate10)\> | Promise used to return the result.|
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 8300001 | Invalid parameter value. |
| 8300002 | Operation failed. Cannot connect to service. |
| 8300003 | System internal error. |
| 8300999 | Unknown error code. |
**Example**
```js
let slotId = 0;
let type = radio.NetworkCapabilityType.SERVICE_TYPE_NR;
let promise = radio.getNetworkCapability(slotId, type);
promise.then(data => {
console.log(`getNetworkCapability success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
console.log(`getNetworkCapability failed, promise: err->${JSON.stringify(err)}`);
});
```
## radio.setNetworkCapability<sup>10+</sup>
setNetworkCapability\(slotId: number, type: NetworkCapabilityType, state: NetworkCapabilityState,
callback: AsyncCallback\<void\>\): void
Sets the network capability for the SIM card in the specified slot. This API uses an asynchronous callback to return the result.
**System API**: This is a system API.
**Required permission**: ohos.permission.SET_TELEPHONY_STATE
**System capability**: SystemCapability.Telephony.CoreService
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------------------- | ---- | -------------------------------------- |
| slotId | number | Yes | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2|
| type | [NetworkCapabilityType](#networkcapabilitytype10) | Yes | Network capability type. |
| state | [NetworkCapabilityState](#networkcapabilitystate10) | Yes | Network capability status. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 8300001 | Invalid parameter value. |
| 8300002 | Operation failed. Cannot connect to service. |
| 8300003 | System internal error. |
| 8300999 | Unknown error code. |
**Example**
```js
let slotId = 0;
let type = radio.NetworkCapabilityType.SERVICE_TYPE_NR;
let state = radio.NetworkCapabilityState.SERVICE_CAPABILITY_ON;
radio.setNetworkCapability(slotId, type, state, (err) => {
console.log(`callback: err->${JSON.stringify(err)}`);
});
```
## radio.setNetworkCapability<sup>10+</sup>
setNetworkCapability\(slotId: number, type: NetworkCapabilityType, state: NetworkCapabilityState\): Promise\<void\>
Sets the network capability for the SIM card in the specified slot. This API uses a promise to return the result.
**System API**: This is a system API.
**Required permission**: ohos.permission.SET_TELEPHONY_STATE
**System capability**: SystemCapability.Telephony.CoreService
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | --------------------------------------------------------------- | ---- | -------------------------------------- |
| slotId | number | Yes | Card slot ID.<br>- **0**: card slot 1<br>- **1**: card slot 2|
| type | [NetworkCapabilityType](#networkcapabilitytype10) | Yes | Network capability type. |
| state | [NetworkCapabilityState](#networkcapabilitystate10) | Yes | Network capability status. |
**Return value**
| Type | Description |
| --------------- | ----------------------- |
| Promise\<void\> | Promise used to return the result.|
**Error codes**
For details about the following error codes, see [Telephony Error Codes](../../reference/errorcodes/errorcode-telephony.md).
| ID| Error Message |
| -------- | -------------------------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 8300001 | Invalid parameter value. |
| 8300002 | Operation failed. Cannot connect to service. |
| 8300003 | System internal error. |
| 8300999 | Unknown error code. |
**Example**
```js
let slotId = 0;
let type = radio.NetworkCapabilityType.SERVICE_TYPE_NR;
let state = radio.NetworkCapabilityState.SERVICE_CAPABILITY_ON;
let promise = radio.setNetworkCapability(slotId, type, state);
promise.then(data => {
console.log(`setNetworkCapability success, promise: data->${JSON.stringify(data)}`);
}).catch(err => {
console.log(`setNetworkCapability failed, promise: err->${JSON.stringify(err)}`);
});
```
## RadioTechnology
Enumerates radio access technologies.
......@@ -3290,3 +3490,29 @@ Enumerates IMS service types.
| TYPE_VIDEO | 1 | Video service.|
| TYPE_UT | 2 | UT service. |
| TYPE_SMS | 3 | SMS service.|
## NetworkCapabilityType<sup>10+</sup>
Network capability type.
**System API**: This is a system API.
**System capability**: SystemCapability.Telephony.CoreService
| Name | Value | Description |
| -----------------| ---- | ---------- |
| SERVICE_TYPE_LTE | 0 | LTE service.|
| SERVICE_TYPE_NR | 1 | NR service.|
## NetworkCapabilityState<sup>10+</sup>
Defines the network capability switch status.
**System API**: This is a system API.
**System capability**: SystemCapability.Telephony.CoreService
| Name | Value | Description |
| -----------------------| ---- | ---------- |
| SERVICE_CAPABILITY_OFF | 0 | The network capability is disabled.|
| SERVICE_CAPABILITY_ON | 1 | The network capability is enabled.|
......@@ -13,6 +13,8 @@
| ArkUI | UX changed | [The hover effect of the \<Button> component is changed from scale-up by 100% to 105% to overlay of 0% to 5% opacity.](changelogs-arkui.md)|
| ArkUI | UX changed | [The alignment mode of multi-line text in toasts is changed from center-aligned to left-aligned.](changelogs-arkui.md)|
| Bundle management subsystem | Mechanism changed | [The HAP is no longer decompressed during HAP installation.](changelogs-bundlemanager.md)|
| Globalization | API added | [The getStringSync and getStringByNameSync APIs are added.](changelogs-global.md)|
| Globalization | Behavior changed | [The meaning of the return value for the API used to obtain the rawfile descriptor is changed.](changelogs-global.md)|
| Security-HUKS | Behavior changed | [HuksKeyStorageType must be passed in for key derivation or key agreement.](changelogs-huks.md) |
| Security-HUKS | Behavior changed | [Permission is required for Using attestKeyItem.](changelogs-huks.md) |
| Web | Input parameter added | [The input parameter type Resource is added for the setResponseData API.](changelogs-web.md) |
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册