视觉任务服务器端LinuxSDK集成文档-Python
简介
本文档介绍 BML 的 Linux Python SDK 的使用方法
- 网络类型支持:图像分类,物体检测
-
硬件支持:
- Linux x86_64 CPU (基础版)
- Linux x86_64 Nvidia GPU (基础版)
- 语言支持:Python 3.5, 3.6, 3.7
Release Notes
时间 | 版本 | 说明 |
---|---|---|
2021.01.27 | 1.2.1 | EasyDL分类高性能模型升级;支持更多模型 |
2020.12.18 | 1.2.0 | 推理引擎升级;接口升级;性能优化 |
2020.09.17 | 1.1.19 | 支持更多模型 |
2020.08.11 | 1.1.18 | 性能优化 |
2020.06.23 | 1.1.17 | 支持更多EasyDL专业版模型 |
2020.04.16 | 1.1.15 | 技术优化;升级 OpenVINO 版本 |
2020.03.12 | 1.1.14 | 新增声音识别python sdk |
2020.02.12 | 1.1.13 | 新增口罩模型支持 |
2020.01.16 | 1.1.12 | 预测函数默认使用推荐阈值 |
2019.12.26 | 1.1.11 | EasyDL 专业版支持 SDK 加速版 |
2019.12.04 | 1.1.10 | 支持图像分割 |
2019.10.21 | 1.1.9 | 支持 EasyDL 专业版 |
2019.08.29 | 1.1.8 | CPU 加速版支持 |
2019.07.19 | 1.1.7 | 提供模型更新工具 |
2019.05.16 | 1.1.3 | NVIDIA GPU 支持 |
2019.03.15 | 1.1.0 | 架构与功能完善 |
2019.02.28 | 1.0.6 | 引擎功能完善 |
2019.02.13 | 1.0.5 | paddlepaddle 支持 |
2018.11.30 | 1.0.0 | 第一版! |
2020-12-18: 【接口升级】 序列号的配置接口从1.2.0版本开始已升级为新接口,以前的方式被置为deprecated,并将在未来的版本中移除。请尽快考虑升级为新的接口方式,具体使用方式可以参考下文介绍以及demo工程示例,谢谢。
快速开始
1. 安装依赖
- 根据引擎的不同,SDK 依赖了不同的底层引擎。根据所需自行安装。
- 使用声音分类SDK需要安装额外依赖
* pip 安装
resampy pydub
音频默认格式支持wav文件预测,如果需要预测mp3等其他音频格式的数据需要系统额外安装ffmpeg
(windows系统的ffmpeg已基在sdk中无需额外安装,linux系统需要手动安装)
安装 paddlepaddle
- 使用
x86_64 CPU 基础版
预测时必须安装:
pip3 install -U paddlepaddle
若 CPU 为特殊型号,如赛扬处理器(一般用于深度定制的硬件中),请关注 CPU 是否支持 avx 指令集。如果不支持,请在paddle官网安装 noavx 版本
- 使用
NVIDIA GPU 基础版
预测时必须安装:
pip3 install -U paddlepaddle-gpu
如果环境非 cuda9 cudnn7,请参考paddle文档安装合适的 paddle 版本。 不被 paddle 支持的 cuda 和 cudnn 版本,EasyEdge 暂不支持
安装 openvino
使用x86_64 CPU 加速版
SDK 预测时,必须安装 OpenVINO 预测引擎,两种方式:
- 使用 OpenVINO™ toolkit 安装,请参考 OpenVINO toolkit 文档安装 2020.3.1LTS(必须)版本 安装时可忽略
Configure the Model Optimizer
及后续部分。 -
使用源码编译安装,请参考 Openvino Inference Engine文档编译安装 2020.3.1(必须)版本
安装完毕,运行之前,请按照OpenVinono的文档 设置环境变量
source /opt/intel/openvino/bin/setupvars.sh
安装 cuda、cudnn
- 使用
Nvidia GPU 加速版
预测时必须安装。 依赖的版本为 cuda9.0、cudnn7。版本号必须正确。
2. 安装 easyedge python wheel 包
pip3 install -U BaiduAI_EasyEdge_SDK-{版本号}-cp36-cp36m-linux_x86_64.whl
具体名称以 SDK 包中的 whl 为准。
3. 使用序列号激活
获取序列号
修改demo.py
填写序列号
pred = edge.Program()
pred.set_auth_license_key("这里填写序列号")
4. GPU 加速版
使用 GPU 加速版,在安装完 whl 之后,必须:
- 从这里下载 TensorRT7.0.0.11 for cuda9.0,并把解压后的 lib 放到 C++ SDK 的 lib 目录或系统 lib 目录
- 运行时,必须在系统库路径中包含 C++ SDK 下的
lib
目录。如设置LD_LIBRARY_PATH
cd ${SDK_ROOT}
# 1. 安装 python wheel 包
tar -xzvf python/*.tar.gz
pip install -U {对应 Python 版本的 wheel 包}
# 2. 设置 LD_LIBRARY_PATH
tar -xzvf cpp/*.tar.gz
export EDGE_ROOT=$(readlink -f $(ls -h | grep "baidu_easyedge_linux_cpp"))
export LD_LIBRARY_PATH=$EDGE_ROOT/lib
# 3. 运行 demo
python3 demo.py {RES文件夹路径} {测试图片路径}
如果是使用 C++ SDK 自带的编译安装的 OpenCV,LD_LIBRARY_PATH 还需要包括 C++ SDK的 build 目录下的 thirdparty/lib
目录
如果没有正确设置 LD_LIBRARY_PATH
,运行时可能报错:
ImportError: libeasyedge.so.0.4.3: cannot open shared object file: No such file or directory
ImportError: libopencv_core.so.3.4: cannot open shared object file: No such file or directory
5. 测试 Demo
输入对应的模型文件夹(默认为RES
)和测试图片路径,运行:
python3 demo.py {model_dir} {image_name.jpg}
测试效果:
使用说明
使用流程
import BaiduAI.EasyEdge as edge
pred = edge.Program()
pred.set_auth_license_key("这里填写序列号")
pred.init(model_dir={RES文件夹路径}, device=edge.Device.CPU, engine=edge.Engine.PADDLE_FLUID)
pred.infer_image({numpy.ndarray的图片})
pred.close()
初始化
- 接口
def init(self,
model_dir,
device=Device.LOCAL,
engine=Engine.PADDLE_FLUID,
config_file='conf.json',
preprocess_file='preprocess_args.json',
model_file='model',
params_file='params',
graph_file='graph.ncsmodel',
label_file='label_list.txt',
device_id=0
):
"""
Args:
device: Device.CPU
engine: Engine.PADDLE_FLUID
model_dir: str
model dir
preprocess_file: str
model_file: str
params_file: str
graph_file: str
label_file: str
device_id: int
Raises:
RuntimeError, IOError
Returns:
bool: True if success
"""
使用 NVIDIA GPU 预测时,必须满足:
- 机器已安装 cuda, cudnn
- 已正确安装对应 cuda 版本的 paddle 版本
- 通过设置环境变量
FLAGS_fraction_of_gpu_memory_to_use
设置合理的初始内存使用比例
使用 CPU 预测时,可以通过在 init 中设置 thread_num 使用多线程预测。如:
pred.init(model_dir=_model_dir, device=edge.Device.CPU, engine=edge.Engine.PADDLE_FLUID, thread_num=1)
预测图像
- 接口
def infer_image(self, img,
threshold=0.3,
channel_order='HWC',
color_format='BGR',
data_type='numpy'):
"""
Args:
img: np.ndarray or bytes
threshold: float
only return result with confidence larger than threshold
channel_order: string
channel order HWC or CHW
color_format: string
color format order RGB or BGR
data_type: string
image data type
Returns:
list
"""
- 返回格式:
[dict1, dict2, ...]
字段 | 类型 | 取值 | 说明 |
---|---|---|---|
confidence | float | 0~1 | 分类或检测的置信度 |
label | string | 分类或检测的类别 | |
index | number | 分类或检测的类别 | |
x1, y1 | float | 0~1 | 物体检测,矩形的左上角坐标 (相对长宽的比例值) |
x2, y2 | float | 0~1 | 物体检测,矩形的右下角坐标(相对长宽的比例值) |
mask | string/numpy.ndarray | 图像分割的mask |
关于矩形坐标
x1 * 图片宽度 = 检测框的左上角的横坐标
y1 * 图片高度 = 检测框的左上角的纵坐标
x2 * 图片宽度 = 检测框的右下角的横坐标
y2 * 图片高度 = 检测框的右下角的纵坐标
可以参考 demo 文件中使用 opencv 绘制矩形的逻辑。
结果示例
- i) 图像分类
{
"index": 736,
"label": "table",
"confidence": 0.9
}
- ii) 物体检测
{
"y2": 0.91211,
"label": "cat",
"confidence": 1.0,
"x2": 0.91504,
"index": 8,
"y1": 0.12671,
"x1": 0.21289
}
- iii) 图像分割
{
"name": "cat",
"score": 1.0,
"location": {
"left": ...,
"top": ...,
"width": ...,
"height": ...,
},
"mask": ...
}
mask字段中,data_type为numpy
时,返回图像掩码的二维数组
{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
其中1代表为目标区域,0代表非目标区域
data_type为string
时,mask的游程编码,解析方式可参考 demo
预测声音
- 使用声音分类SDK需要安装额外依赖
pip 安装
resampy pydub
音频默认格式支持wav文件预测,如果需要预测mp3等其他音频格式的数据需要系统额外安装ffmpeg
(windows系统的ffmpeg已集成在sdk中无需额外安装,linux系统需要手动安装) - 接口
def infer_image(self, sound_binary,
threshold=0.3):
"""
Args:
sound_binary: sound_binary
threshold: confidence
Returns:
list
"""
- 返回格式:
[dict1, dict2, ...]
字段 | 类型 | 取值 | 说明 |
---|---|---|---|
confidence | float | 0~1 | 分类的置信度 |
label | string | 分类的类别 | |
index | number | 分类的类别 |
升级模型
适用于经典版升级模型,执行bash update_model.sh
,根据提示,输入模型路径、激活码、模型ID、模型版本,等待模型更新完毕即可。
FAQ
Q: EasyDL 离线 SDK 与云服务效果不一致,如何处理?
A: 后续我们会消除这部分差异,如果开发者发现差异较大,可联系我们协助处理。
Q: 运行时报错 "非法指令" 或 " illegal instruction"
A: 可能是 CPU 缺少 avx 指令集支持,请在paddle官网 下载 noavx 版本覆盖安装
Q: NVIDIA GPU预测时,报错显存不足:
A: 如以下错误字样:
paddle.fluid.core.EnforceNotMet: Enforce failed. Expected allocating <= available, but received allocating:20998686233 > available:19587333888.
Insufficient GPU memory to allocation. at [/paddle/paddle/fluid/platform/gpu_info.cc:170]
请在运行 Python 前设置环境变量,通过export FLAGS_fraction_of_gpu_memory_to_use=0.3
来限制SDK初始使用的显存量,0.3表示初始使用30%的显存。如果设置的初始显存较小,SDK 会自动尝试 allocate 更多的显存。
Q: 我想使用多线程预测,怎么做?
如果需要多线程预测,可以每个线程启动一个Progam实例,进行预测。 demo.py文件中有相关示例代码。
注意: 对于CPU预测,SDK内部是可以使用多线程,最大化硬件利用率。参考init的thread_num参数。