目前本项目支持四种推理模型格式,具体请参考下表:
| 模型计算图 | 模型参数 |
|---|---|
| model | params |
| __model__ | * |
| __model__ | __params__ |
| *.pdmodel | *.pdiparams |
$ pip install ppqi -i https://pypi.python.org/simple
$ pip install [path to whl]
import numpy as np
from ppqi import InferenceModel
# 加载推理模型
model = InferenceModel([Inference Model Path])
model.eval()
# 准备数据
inputs = np.random.randn(8, 64, 64, 3).astype(np.float32)
# 前向计算
outputs = model(inputs)
'''
modelpath:推理模型路径
use_gpu:是否使用GPU进行推理
gpu_id:设置使用的GPU ID
use_mkldnn:是否使用MKLDNN库进行CPU推理加速
cpu_threads:设置计算库的所使用CPU线程数
还可以通过InferenceModel.config来对其他选项进行配置
如配置tensorrt:
model.config.enable_tensorrt_engine(
workspace_size = 1 << 20,
max_batch_size = 1,
min_subgraph_size = 3,
precision_mode=paddle.inference.PrecisionType.Float32,
use_static = False,
use_calib_mode = False
)
'''
model = InferenceModel(
modelpath=[Inference Model Path],
use_gpu=False,
gpu_id=0,
use_mkldnn=False,
cpu_threads=1
)
'''
将模型设置为推理模式
实际上是使用Config创建Predictor
'''
model.eval()
'''
创建完Predictor后
可打印出模型的输入输出节点的数量和名称
'''
print(model)
'''
根据输入节点的数量和名称准备好数据
数据格式为Ndarray
'''
input_datas = np.random.randn(8, 64, 64, 3).astype(np.float32)
'''
模型前向计算
根据输入节点顺序传入输入数据
batch_size:推理数据批大小
返回结果格式为所有输出节点的输出(Ndarray)
'''
outputs = model(input_datas, batch_size=4)
$ claude mcp add PaddleQuickInference \
-- python -m otcore.mcp_server <graph>