PaddleInference:config类

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

整理官方文档而来,记录备忘。


一、设置预测模型

Config 类为用于配置构建 Predictor 对象的配置信息,如模型路径、是否开启gpu等等。

1.加载预测模型 - 非Combined模型

1.1 API定义如下:

# 设置模型文件路径,当需要从磁盘加载非 Combined 模型时使用
# 参数:model_dir - 模型文件夹路径 - str 类型
# 返回:None
paddle.inference.Config.set_model(model_dir: str)

# 获取非combine模型的文件夹路径
# 参数:None
# 返回:str - 模型文件夹路径
paddle.inference.Config.model_dir()

1.2示例

# 引用 paddle inference 预测库
import paddle.inference as paddle_infer

# 创建 config
config = paddle_infer.Config()

# 加载非Combined模型
config.set_model("./mobilenet_v1")

# 根据 config 创建 predictor
predictor = paddle_infer.create_predictor(config)

2.加载预测模型 - 非Combined模型

2.1 示例

# 引用 paddle inference 预测库
import paddle.inference as paddle_infer

# 创建 config
config = paddle_infer.Config("./mobilenet_v1")

# 根据 config 创建 predictor
predictor = paddle_infer.create_predictor(config)

3.加载预测模型 - Combined模型

3.1 API定义如下:

# 设置模型文件路径,当需要从磁盘加载 Combined 模型时使用
# 参数:prog_file_path - 模型文件路径
#      params_file_path - 参数文件路径
# 返回:None
paddle.inference.Config.set_model(prog_file_path: str, params_file_path: str)

# 设置模型文件路径,当需要从磁盘加载 Combined 模型时使用。
# 参数:x - 模型文件路径
# 返回:None
paddle.inference.Config.set_prog_file(x: str)

# 设置参数文件路径,当需要从磁盘加载 Combined 模型时使用
# 参数:x - 参数文件路径
# 返回:None
paddle.inference.Config.set_params_file(x: str)

# 获取 Combined 模型的模型文件路径
# 参数:None
# 返回:str - 模型文件路径
paddle.inference.Config.prog_file()

# 获取 Combined 模型的参数文件路径
# 参数:None
# 返回:str - 参数文件路径
paddle.inference.Config.params_file()

3.2 示例:

# 引用 paddle inference 预测库
import paddle.inference as paddle_infer

# 创建 config
config = paddle_infer.Config("./mobilenet_v2/__model__", "./mobilenet_v2/__params__")

# 根据 config 创建 predictor
predictor = paddle_infer.create_predictor(config)

三、使用CPU进行预测

尝试使用 Intel 的 MKLDNN 进行 CPU 预测加速,默认 CPU 不启用 MKLDNN。

在 CPU 可用核心数足够时,可以通过设置 set_cpu_math_library_num_threads 将线程数调高一些,默认线程数为 1。

1. CPU设置

1.1 API定义

# 设置 CPU Blas 库计算线程数
# 参数:cpu_math_library_num_threads - blas库计算线程数
# 返回:None
paddle.inference.Config.set_cpu_math_library_num_threads(cpu_math_library_num_threads: int)

# 获取 CPU Blas 库计算线程数
# 参数:None
# 返回:int - cpu blas库计算线程数
paddle.inference.Config.cpu_math_library_num_threads()

1.2 代码示例:

# 引用 paddle inference 预测库
import paddle.inference as paddle_infer

# 创建 config 类
config = paddle_infer.Config()

# 设置 CPU Blas 库线程数为 10
config.set_cpu_math_library_num_threads(10)

# 通过 API 获取 CPU 信息 - 10
print(config.cpu_math_library_num_threads())

2.MKLDNN 设置

启用 MKLDNN 的前提为已经使用 CPU 进行预测,启用 MKLDNN BF16 要求 CPU 型号可以支持 AVX512。

2.1 API定义

# 启用 MKLDNN 进行预测加速
# 参数:None
# 返回:None
paddle.inference.Config.enable_mkldnn()

# 判断是否启用 MKLDNN 
# 参数:None
# 返回:bool - 是否启用 MKLDNN
paddle.inference.Config.mkldnn_enabled()

# 设置 MKLDNN 针对不同输入 shape 的 cache 容量大小
# 参数:int - cache 容量大小
# 返回:None
paddle.inference.Config.set_mkldnn_cache_capacity(capacity: int=0)

# 指定使用 MKLDNN 加速的 OP 集合
# 参数:使用 MKLDNN 加速的 OP 集合
# 返回:None
paddle.inference.Config.set_mkldnn_op(op_list: Set[str])

# 启用 MKLDNN BFLOAT16
# 参数:None
# 返回:None
paddle.inference.Config.enable_mkldnn_bfloat16()

# 指定使用 MKLDNN BFLOAT16 加速的 OP 集合
# 参数:使用 MKLDNN BFLOAT16 加速的 OP 集合
# 返回:None
paddle.inference.Config.set_bfloat16_op(op_list: Set[str])

2.2 代码示例:使用 MKLDNN 进行预测

# 引用 paddle inference 预测库
import paddle.inference as paddle_infer

# 创建 config
config = paddle_infer.Config("./mobilenet_v1")

# 启用 MKLDNN 进行预测
config.enable_mkldnn()

# 通过 API 获取 MKLDNN 启用结果 - true
print(config.mkldnn_enabled())

# 设置 MKLDNN 的 cache 容量大小
config.set_mkldnn_cache_capacity(1)

# 设置启用 MKLDNN 进行加速的 OP 列表
config.set_mkldnn_op({"softmax", "elementwise_add", "relu"})

2.3 使用 MKLDNN BFLOAT16 进行预测

# 引用 paddle inference 预测库
import paddle.inference as paddle_infer

# 创建 config
config = paddle_infer.Config("./mobilenet_v1")

# 启用 MKLDNN 进行预测
config.enable_mkldnn()

# 启用 MKLDNN BFLOAT16 进行预测
config.enable_mkldnn_bfloat16()

# 设置启用 MKLDNN BFLOAT16 的 OP 列表
config.set_bfloat16_op({"conv2d"})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值