Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
7d944dd9
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7d944dd9
编写于
6月 13, 2023
作者:
F
fangzhou12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
jsapi doc bugfix
Signed-off-by:
N
fangzhou12
<
fangzhou12@huawei.com
>
上级
3ca9c14a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
56 addition
and
49 deletion
+56
-49
zh-cn/application-dev/ai/mindspore-lite-js-guidelines.md
zh-cn/application-dev/ai/mindspore-lite-js-guidelines.md
+11
-9
zh-cn/application-dev/reference/apis/js-apis-mindSporeLite.md
...n/application-dev/reference/apis/js-apis-mindSporeLite.md
+45
-40
未找到文件。
zh-cn/application-dev/ai/mindspore-lite-js-guidelines.md
浏览文件 @
7d944dd9
...
...
@@ -50,21 +50,23 @@ build() {
.
fontWeight
(
FontWeight
.
Bold
)
.
onClick
(
async
()
=>
{
//1.模型准备
let
context
=
globalThis
.
context
;
context
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
error
,
buffer
)
=>
{
let
sys
context
=
globalThis
.
context
;
syscontext
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
buffer
)
=>
{
this
.
inputBuffer
=
buffer
;
console
.
log
(
'
=========input bin byte length:
'
+
this
.
inputBuffer
.
byteLength
)
}).
catch
(
error
)
{
}).
catch
(
error
=>
{
console
.
error
(
'
Failed to get buffer, error code: ${error.code},message:${error.message}.
'
);
}
}
)
//2.创建上下文
let
options
=
{
'
target
'
:
[
'
cpu
'
]}
options
.
cpu
.
threadNum
=
1
;
options
.
cpu
.
threadAffinityMode
=
0
;
options
.
cpu
.
precisionMode
=
'
enforce_fp32
'
;
let
context
:
mindSporeLite
.
Context
=
{};
context
.
target
=
[
'
cpu
'
];
context
.
cpu
=
{}
context
.
cpu
.
threadNum
=
1
;
context
.
cpu
.
threadAffinityMode
=
0
;
context
.
cpu
.
precisionMode
=
'
enforce_fp32
'
;
//3.加载模型
let
modelFile
=
'
/data/storage/el2/base/haps/entry/files/mnet.caffemodel.ms
'
;
let
msLiteModel
=
await
mindSporeLite
.
loadModelFromFile
(
modelFile
);
let
msLiteModel
=
await
mindSporeLite
.
loadModelFromFile
(
modelFile
,
context
);
//4.加载数据
const
modelInputs
=
msLiteModel
.
getInputs
();
modelInputs
[
0
].
setData
(
this
.
inputBuffer
.
buffer
);
...
...
zh-cn/application-dev/reference/apis/js-apis-mindSporeLite.md
浏览文件 @
7d944dd9
...
...
@@ -31,7 +31,7 @@ import mindSporeLite from '@ohos.ai.mindSporeLite';
**示例:**
```
js
let
context
=
{}
as
any
;
let
context
:
mindSporeLite
.
Context
=
{}
;
context
.
target
=
[
'
cpu
'
,
'
nnrt
'
];
```
...
...
@@ -55,7 +55,8 @@ CPU后端设备选项。
**示例:**
```
js
let
context
=
{}
as
any
;
let
context
:
mindSporeLite
.
Context
=
{};
context
.
cpu
=
{};
context
.
target
=
[
'
cpu
'
];
context
.
cpu
.
threadAffinityMode
=
0
;
context
.
cpu
.
precisionMode
=
'
preferred_fp16
'
;
...
...
@@ -123,9 +124,8 @@ loadModelFromFile(model: string, context: Context, callback: Callback<Model&g
**示例:**
```
js
let
context
=
{
'
target
'
:
[
'
cpu
'
]
};
let
context
:
mindSporeLite
.
Context
=
{};
let
context
=
{
'
target
'
:
[
'
cpu
'
]};
let
model_file
=
'
/path/to/xxx.ms
'
;
mindSporeLite
.
loadModelFromFile
(
model_file
,
context
,
(
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
...
...
@@ -136,7 +136,7 @@ mindSporeLite.loadModelFromFile(model_file, context, (result) => {
loadModelFromFile(model: string, context?: Context): Promise
<
Model
>
从完整路径加载输入模型用于推理。使用Promise异步
回调
。
从完整路径加载输入模型用于推理。使用Promise异步
函数
。
**系统能力:**
SystemCapability.AI.MindSporeLite
...
...
@@ -182,11 +182,12 @@ loadModelFromBuffer(model: ArrayBuffer, callback: Callback<Model>): void
```
js
import
resourceManager
from
'
@ohos.resourceManager
'
@
State
modelName
:
string
=
'
xxx.ms
'
;
context
.
resourceManager
.
getRawFileContent
(
this
.
modelName
).
then
((
error
,
buffer
)
=>
{
let
syscontext
=
globalThis
.
context
;
syscontext
.
resourceManager
.
getRawFileContent
(
this
.
modelName
).
then
((
buffer
)
=>
{
this
.
modelBuffer
=
buffer
;
}).
catch
(
error
)
{
}).
catch
(
error
=>
{
console
.
error
(
'
Failed to get buffer, error code: ${error.code},message:${error.message}.
'
);
}
}
)
mindSporeLite
.
loadModelFromBuffer
(
this
.
modelBuffer
.
buffer
,
(
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
...
...
@@ -213,12 +214,14 @@ loadModelFromBuffer(model: ArrayBuffer, context: Context, callback: Callback<
```
js
import
resourceManager
from
'
@ohos.resourceManager
'
@
State
modelName
:
string
=
'
xxx.ms
'
;
context
.
resourceManager
.
getRawFileContent
(
this
.
modelName
).
then
((
error
,
buffer
)
=>
{
let
syscontext
=
globalThis
.
context
;
syscontext
.
resourceManager
.
getRawFileContent
(
this
.
modelName
).
then
((
error
,
buffer
)
=>
{
this
.
modelBuffer
=
buffer
;
}).
catch
(
error
)
{
}).
catch
(
error
=>
{
console
.
error
(
'
Failed to get buffer, error code: ${error.code},message:${error.message}.
'
);
}
let
context
=
{
'
target
'
:
[
'
cpu
'
]};
})
let
context
:
mindSporeLite
.
Context
=
{};
context
=
{
'
target
'
:
[
'
cpu
'
]};
mindSporeLite
.
loadModelFromBuffer
(
this
.
modelBuffer
.
buffer
,
context
,
(
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
...
...
@@ -228,7 +231,7 @@ mindSporeLite.loadModelFromBuffer(this.modelBuffer.buffer, context, (result) =>
loadModelFromBuffer(model: ArrayBuffer, context?: Context): Promise
<
Model
>
从内存加载输入模型用于推理。使用Promise异步
回调
。
从内存加载输入模型用于推理。使用Promise异步
函数
。
**系统能力:**
SystemCapability.AI.MindSporeLite
...
...
@@ -250,11 +253,12 @@ loadModelFromBuffer(model: ArrayBuffer, context?: Context): Promise<Model>
```
js
import
resourceManager
from
'
@ohos.resourceManager
'
@
State
modelName
:
string
=
'
xxx.ms
'
;
context
.
resourceManager
.
getRawFileContent
(
this
.
modelName
).
then
((
error
,
buffer
)
=>
{
let
syscontext
=
globalThis
.
context
;
syscontext
.
resourceManager
.
getRawFileContent
(
this
.
modelName
).
then
((
buffer
)
=>
{
this
.
modelBuffer
=
buffer
;
}).
catch
(
error
)
{
}).
catch
(
error
=>
{
console
.
error
(
'
Failed to get buffer, error code: ${error.code},message:${error.message}.
'
);
}
}
)
mindSporeLite
.
loadModelFromBuffer
(
model_file
).
then
((
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
...
...
@@ -280,11 +284,10 @@ loadModelFromFd(model: number, callback: Callback<Model>): void
```
js
import
fs
from
'
@ohos.file.fs
'
;
let
model_file
=
'
/path/to/xxx.ms
'
;
fs
.
open
(
model_file
,
0
,
async
function
(
err
,
file
)
{
mindSporeLite
.
loadModelFromFd
(
file
.
fd
,
(
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
})
let
file
=
await
fs
.
open
(
model_file
,
0
);
mindSporeLite
.
loadModelFromFd
(
file
.
fd
,
(
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
})
```
## mindSporeLite.loadModelFromFd
...
...
@@ -308,19 +311,19 @@ loadModelFromFd(model: number, context: Context, callback: Callback<Model>
```
js
import
fs
from
'
@ohos.file.fs
'
;
let
model_file
=
'
/path/to/xxx.ms
'
;
let
context
=
{
'
target
'
:
[
'
cpu
'
]
};
fs
.
open
(
model_file
,
0
,
async
function
(
err
,
file
)
{
mindSporeLite
.
loadModelFromFd
(
file
.
fd
,
context
,
(
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
})
let
context
:
mindSporeLite
.
Context
=
{
};
context
=
{
'
target
'
:
[
'
cpu
'
]};
let
file
=
await
fs
.
open
(
model_file
,
0
);
mindSporeLite
.
loadModelFromFd
(
file
.
fd
,
context
,
(
result
)
=>
{
const
modelInputs
=
result
.
getInputs
(
);
console
.
log
(
modelInputs
[
0
].
name
);
})
```
## mindSporeLite.loadModelFromFd
loadModelFromFd(model: number, context?: Context): Promise
<
Model
>
从文件描述符加载输入模型用于推理。使用Promise异步
回调
。
从文件描述符加载输入模型用于推理。使用Promise异步
函数
。
**系统能力:**
SystemCapability.AI.MindSporeLite
...
...
@@ -342,12 +345,11 @@ loadModelFromFd(model: number, context?: Context): Promise< Model>
```
js
import
fs
from
'
@ohos.file.fs
'
;
let
model_file
=
'
/path/to/xxx.ms
'
;
fs
.
open
(
model_file
,
0
,
async
function
(
err
,
file
)
{
let
mindSporeLiteModel
=
await
mindSporeLite
.
loadModelFromFd
(
file
.
fd
);
mindSporeLite
.
loadModelFromFd
(
file
.
fd
).
then
((
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
})
let
file
=
await
fs
.
open
(
model_file
,
0
);
let
mindSporeLiteModel
=
await
mindSporeLite
.
loadModelFromFd
(
file
.
fd
);
mindSporeLite
.
loadModelFromFd
(
file
.
fd
).
then
((
result
)
=>
{
const
modelInputs
=
result
.
getInputs
();
console
.
log
(
modelInputs
[
0
].
name
);
})
```
## Model
...
...
@@ -399,7 +401,8 @@ predict(inputs: MSTensor[], callback: Callback<Model>): void
```
js
import
resourceManager
from
'
@ohos.resourceManager
'
@
State
inputName
:
string
=
'
input_data.bin
'
;
context
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
error
,
buffer
)
=>
{
let
syscontext
=
globalThis
.
context
;
syscontext
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
buffer
)
=>
{
this
.
inputBuffer
=
buffer
;
let
model_file
=
'
/path/to/xxx.ms
'
;
let
mindSporeLiteModel
=
await
mindSporeLite
.
loadModelFromFile
(
model_file
);
...
...
@@ -417,7 +420,7 @@ context.resourceManager.getRawFileContent(this.inputName).then((error,buffer) =>
predict(inputs: MSTensor[]): Promise
<
MSTensor[]
>
执行推理模型。使用Promise异步
回调
。
执行推理模型。使用Promise异步
函数
。
**系统能力:**
SystemCapability.AI.MindSporeLite
...
...
@@ -438,7 +441,8 @@ predict(inputs: MSTensor[]): Promise<MSTensor[]>
```
js
import
resourceManager
from
'
@ohos.resourceManager
'
@
State
inputName
:
string
=
'
input_data.bin
'
;
context
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
error
,
buffer
)
=>
{
let
syscontext
=
globalThis
.
context
;
syscontext
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
buffer
)
=>
{
this
.
inputBuffer
=
buffer
;
let
model_file
=
'
/path/to/xxx.ms
'
;
let
mindSporeLiteModel
=
await
mindSporeLite
.
loadModelFromFile
(
model_file
);
...
...
@@ -534,7 +538,7 @@ getData(): ArrayBuffer
**示例:**
```
js
//如果已经获取了输
出
张量
//如果已经获取了输
入
张量
result
.
predict
(
modelInputs
,
(
result
)
=>
{
let
output
=
new
Float32Array
(
result
[
0
].
getData
());
for
(
let
i
=
0
;
i
<
output
.
length
;
i
++
)
{
...
...
@@ -562,7 +566,8 @@ setData(inputArray: ArrayBuffer): void
```
js
import
resourceManager
from
'
@ohos.resourceManager
'
@
State
inputName
:
string
=
'
input_data.bin
'
;
context
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
error
,
buffer
)
=>
{
let
syscontext
=
globalThis
.
context
;
syscontext
.
resourceManager
.
getRawFileContent
(
this
.
inputName
).
then
((
buffer
)
=>
{
this
.
inputBuffer
=
buffer
;
})
let
model_file
=
'
/path/to/xxx.ms
'
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录