InferenceEngine API
高层推理引擎接口,简化模型加载和推理流程。
InferenceEngine
构造函数
typescript
import { InferenceEngine } from 'tiny-dl-inference';
const engine = new InferenceEngine(context);1
2
3
2
3
参数:
context: GPUContext- GPU 上下文实例
loadModel(url)
从 URL 加载模型。
typescript
await engine.loadModel('https://example.com/model.json');1
参数:
url: string- 模型文件 URL
返回: Promise<void>
infer(input)
运行推理。
typescript
const output = await engine.infer(input);1
参数:
input: Tensor- 输入张量
返回: Promise<Tensor> - 输出张量
dispose()
释放引擎资源。
typescript
engine.dispose();1
示例
完整推理流程
typescript
import { GPUContext, InferenceEngine, Tensor } from 'tiny-dl-inference';
// 1. 初始化
const context = new GPUContext();
await context.init();
// 2. 创建引擎
const engine = new InferenceEngine(context);
// 3. 加载模型
await engine.loadModel('https://example.com/mnist-model.json');
// 4. 准备输入
const inputData = new Float32Array(1 * 1 * 28 * 28);
// ... 填充图像数据 ...
const input = Tensor.fromArray(context, inputData, [1, 1, 28, 28]);
// 5. 运行推理
const output = await engine.infer(input);
const result = await output.download();
// 6. 处理结果
const predictedClass = result.indexOf(Math.max(...result));
console.log('预测类别:', predictedClass);
// 7. 清理
input.destroy();
output.destroy();
engine.dispose();
context.destroy();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
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
ModelLoader
模型加载工具类。
loadModel(url)
加载模型定义。
typescript
import { ModelLoader } from 'tiny-dl-inference';
const model = await ModelLoader.loadModel(url);1
2
3
2
3
相关资源
- 引擎指南 - 使用示例
- GPUContext API - 资源管理
- Tensor API - 数据结构