Python 预测 API介绍¶
Paddle提供了高度优化的C++预测库,为了方便使用,我们也提供了C++预测库对应的Python接口,下面是详细的使用说明。
如果您在使用2.0之前的Paddle,请参考旧版API文档。
Python预测相关数据结构¶
使用Python预测API与C++预测API相似,主要包括Tensor, DataType, Config和Predictor,分别对应于C++ API中同名的类型。
DataType¶
class paddle.inference.DataType
DataType定义了Tensor的数据类型,由传入Tensor的numpy数组类型确定,包括以下成员
INT64: 64位整型INT32: 32位整型FLOAT32: 32位浮点型
PrecisionType¶
class paddle.3.inference.PrecisionType
PrecisionType定义了Predictor运行的精度模式,包括一下成员
Float32: fp32模式运行Half: fp16模式运行Int8: int8模式运行
Tensor¶
class paddle.inference.Tensor
Tensor是Predictor的一种输入/输出数据结构,通过predictor获取输入/输出handle得到,主要提供以下方法
copy_from_cpu: 从cpu获取模型运行所需输入数据copy_to_cpu: 获取模型运行输出结果lod: 获取lod信息set_lod: 设置lod信息shape: 获取shape信息reshape: 设置shape信息type: 获取DataType信息
# 创建predictor
predictor = create_predictor(config)
# 获取输入的名称
input_names = predictor.get_input_names()
input_tensor = predictor.get_input_handle(input_names[0])
# 设置输入
fake_input = numpy.random.randn(1, 3, 318, 318).astype("float32")
input_tensor.copy_from_cpu(fake_input)
# 运行predictor
predictor.run()
# 获取输出
output_names = predictor.get_output_names()
output_tensor = predictor.get_output_handle(output_names[0])
output_data = output_tensor.copy_to_cpu() # numpy.ndarray类型
Config¶
class paddle.inference.Config
Config是创建预测引擎的配置,提供了模型路径设置、预测引擎运行设备选择以及多种优化预测流程的选项,主要包括以下方法
set_model: 设置模型的路径model_dir: 返回模型文件夹路径prog_file: 返回模型文件路径params_file: 返回参数文件路径enable_use_gpu: 设置GPU显存(单位M)和Device IDdisable_gpu: 禁用GPUgpu_device_id: 返回使用的GPU IDswitch_ir_optim: IR优化(默认开启)enable_tensorrt_engine: 开启TensorRTenable_mkldnn: 开启MKLDNNdisable_glog_info: 禁用预测中的glog日志delete_pass: 预测的时候删除指定的pass
代码示例¶
设置模型和参数路径有两种形式:
当模型文件夹下存在一个模型文件和多个参数文件时,传入模型文件夹路径,模型文件名默认为
__model__
config = Config("./model")
当模型文件夹下只有一个模型文件和一个参数文件时,传入模型文件和参数文件路径
config = Config("./model/model", "./model/params")
使用set_model方法设置模型和参数路径方式同上
其他预测引擎配置选项示例如下
config.enable_use_gpu(100, 0) # 初始化100M显存,使用gpu id为0
config.gpu_device_id() # 返回正在使用的gpu id
config.disable_gpu() # 禁用gpu
config.switch_ir_optim(True) # 开启IR优化
config.enable_tensorrt_engine(precision_mode=PrecisionType.Float32,
use_calib_mode=True) # 开启TensorRT预测,精度为fp32,开启int8离线量化
config.enable_mkldnn() # 开启MKLDNN
Predictor¶
class paddle.inference.Predictor
Predictor是运行预测的引擎,由paddle.inference.create_predictor(config)创建,主要提供以下方法
run(): 运行预测引擎,返回预测结果get_input_names(): 获取输入的名称get_input_handle(input_name: str): 根据输入的名称获取对应的Tensorget_output_names(): 获取输出的名称get_output_handle(output_name: str): 根据输出的名称获取对应的Tensor
代码示例¶
# 设置完AnalysisConfig后创建预测引擎PaddlePredictor
predictor = create_predictor(config)
# 获取输入的名称
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
# 设置输入
fake_input = numpy.random.randn(1, 3, 318, 318).astype("float32")
input_handle.reshape([1, 3, 318, 318])
input_handle.copy_from_cpu(fake_input)
# 运行predictor
predictor.run()
# 获取输出
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])
完整使用示例¶
下面是使用Paddle Inference Python API进行预测的一个完整示例,使用resnet50模型
下载resnet50模型并解压,运行如下命令将会调用预测引擎
python resnet50_infer.py --model_file ./model/model --params_file ./model/params --batch_size 2
resnet50_infer.py 的内容是
import argparse
import numpy as np
from paddle.inference import Config
from paddle.inference import create_predictor
def main():
args = parse_args()
# 设置AnalysisConfig
config = set_config(args)
# 创建PaddlePredictor
predictor = create_predictor(config)
# 获取输入的名称
input_names = predictor.get_input_names()
input_handle = predictor.get_input_handle(input_names[0])
# 设置输入
fake_input = np.random.randn(1, 3, 318, 318).astype("float32")
input_handle.reshape([1, 3, 318, 318])
input_handle.copy_from_cpu(fake_input)
# 运行predictor
predictor.run()
# 获取输出
output_names = predictor.get_output_names()
output_handle = predictor.get_output_handle(output_names[0])
output_data = output_handle.copy_to_cpu() # numpy.ndarray类型
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model_file", type=str, help="model filename")
parser.add_argument("--params_file", type=str, help="parameter filename")
parser.add_argument("--batch_size", type=int, default=1, help="batch size")
return parser.parse_args()
def set_config(args):
config = Config(args.model_file, args.params_file)
config.disable_gpu()
config.switch_use_feed_fetch_ops(False)
config.switch_specify_input_names(True)
return config
if __name__ == "__main__":
main()
支持方法列表¶
Tensor
copy_from_cpu(input: numpy.ndarray) -> Nonecopy_to_cpu() -> numpy.ndarrayreshape(input: numpy.ndarray|List[int]) -> Noneshape() -> List[int]set_lod(input: numpy.ndarray|List[List[int]]) -> Nonelod() -> List[List[int]]type() -> PaddleDType
Config
set_model(model_dir: str) -> Noneset_model(prog_file: str, params_file: str) -> Noneset_model_buffer(model: str, model_size: int, param: str, param_size: int) -> Nonemodel_dir() -> strprog_file() -> strparams_file() -> strmodel_from_memory() -> boolset_cpu_math_library_num_threads(num: int) -> Noneenable_use_gpu(memory_pool_init_size_mb: int, device_id: int) -> Noneuse_gpu() -> boolgpu_device_id() -> intswitch_ir_optim(x: bool = True) -> Noneswitch_ir_debug(x: int=True) -> Noneir_optim() -> boolenable_tensorrt_engine(workspace_size: int = 1 << 20, max_batch_size: int, min_subgraph_size: int, precision_mode: AnalysisConfig.precision, use_static: bool, use_calib_mode: bool) -> Noneset_trt_dynamic_shape_info(min_input_shape: Dict[str, List[int]]={}, max_input_shape: Dict[str, List[int]]={}, optim_input_shape: Dict[str, List[int]]={}, disable_trt_plugin_fp16: bool=False) -> Nonetensorrt_engine_enabled() -> boolenable_mkldnn() -> Noneenable_mkldnn_bfloat16() -> Nonemkldnn_enabled() -> boolset_mkldnn_cache_capacity(capacity: int=0) -> Noneset_mkldnn_op(ops: Set[str]) -> Noneset_optim_cache_dir(dir: str) -> Nonedisable_glog_info() -> Nonepass_builder() -> paddle::PassStrategydelete_pass(pass_name: str) -> Nonecpu_math_library_num_threads() -> intdisable_gpu() -> Noneenable_lite_engine(precision: PrecisionType, zero_copy: bool, passes_filter: List[str]=[], ops_filter: List[str]=[]) -> Nonelite_engine_enabled() -> boolenable_memory_optim() -> Noneenable_profile() -> Noneenable_quantizer() -> Nonequantizer_config() -> paddle::MkldnnQuantizerConfigfraction_of_gpu_memory_for_pool() -> floatmemory_pool_init_size_mb() -> intglog_info_disabled() -> boolgpu_device_id() -> intspecify_input_name() -> boolswitch_specify_input_names(x: bool=True) -> Nonespecify_input_name(q) -> boolswitch_use_feed_fetch_ops(x: int=True) -> Noneuse_feed_fetch_ops_enabled() -> boolto_native_config() -> paddle.fluid.core_avx.NativeConfig
create_predictor(config: Config) -> PredictorPredictor
run() -> Noneget_input_names() -> List[str]get_input_handle(input_name: str) -> Tensorget_output_names() -> List[str]get_output_handle(output_name: str) -> Tensorclear_intermediate_tensor() -> Nonetry_shrink_memory() -> Noneclone() -> Predictor
PredictorPool
retrive(idx: int) -> Predictor
可参考对应的C++预测接口,其中定义了每个接口的参数和返回值