Skip to main content

由 PaddlePaddle 提供支持的视觉识别宝箱。

项目描述

PaddleClas 轮组

PaddleClas 支持 Python 轮子包进行预测。目前PaddleClas轮支持的图像分类包括ImagetNet1k模型和PULC模型,但不支持主体检测、特征提取和向量检索。


目录

1.安装

  • 从 pypi 安装
pip3 install paddleclas==2.2.1
  • 构建自己的 whl 包并安装
python3 setup.py bdist_wheel
pip3 install dist/*

2. 快速入门

2.1 ImageNet1k 模型

使用ResNet50PaddleClas 提供的模型,以下图('docs/images/inference_deployment/whl_demo.jpg')为例。

  • Python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
infer_imgs='docs/images/inference_deployment/whl_demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))

PaddleClas.predict()是一个generator。因此,您需要迭代地使用next()或调用它。for它将执行预测batch_size并在调用时返回预测结果。返回结果示例如下:

>>> result
[{'class_ids': [8, 7, 136, 80, 84], 'scores': [0.79368, 0.16329, 0.01853, 0.00959, 0.00239], 'label_names': ['hen', 'cock', 'European gallinule, Porphyrio porphyrio', 'black grouse', 'peacock']}]
  • 命令行界面
paddleclas --model_name=ResNet50  --infer_imgs="docs/images/inference_deployment/whl_demo.jpg"
>>> result
filename: docs/images/inference_deployment/whl_demo.jpg, top-5, class_ids: [8, 7, 136, 80, 84], scores: [0.79368, 0.16329, 0.01853, 0.00959, 0.00239], label_names: ['hen', 'cock', 'European gallinule, Porphyrio porphyrio', 'black grouse', 'peacock']
Predict complete!

2.2 PULC 模型

PULC集成了骨干网络、数据增强和蒸馏等各种最先进的算法,最终可以自动获得轻量级、高精度的图像分类模型。

PaddleClas提供了一系列的测试用例,包含了人、车、OCR等不同场景的demo。点击这里下载数据。

使用 PaddleClas 提供的 PULC“人类存在分类”模型进行预测:

  • Python
import paddleclas
model = paddleclas.PaddleClas(model_name="person_exists")
result = model.predict(input_data="pulc_demo_imgs/person_exists/objects365_01780782.jpg")
print(next(result))
>>> result
[{'class_ids': [0], 'scores': [0.9955421453341842], 'label_names': ['nobody'], 'filename': 'pulc_demo_imgs/person_exists/objects365_01780782.jpg'}]

Nobody表示图中没有人,someone表示图中有人。因此,预测结果表明图中没有人。

注意:model.predict()是一个生成器,所以需要next()orfor来调用它。这将按批次预测长度为batch_size,默认为 1。您可以指定参数batch_sizemodel_name在实例化 PaddleClas 对象时,例如:model = paddleclas.PaddleClas(model_name="person_exists", batch_size=2)支持型号列表请参考支持型号列表。

  • 命令行界面
paddleclas --model_name=person_exists --infer_imgs=pulc_demo_imgs/person_exists/objects365_01780782.jpg
>>> result
class_ids: [0], scores: [0.9955421453341842], label_names: ['nobody'], filename: pulc_demo_imgs/person_exists/objects365_01780782.jpg
Predict complete!

注意:“--infer_imgs”参数指定要预测的图像,您还可以指定包含图像的目录。如果使用其他模型,您可以指定--model_name参数。支持型号列表请参考支持型号列表。

支持的型号列表

PULC系列型号名称如下:

姓名 介绍
人存在 人类存在分类
个人属性 行人属性分类
安全帽 天气佩戴安全帽的分类
交通标志 交通标志分类
车辆属性 车辆属性分类
car_exists 汽车存在分类
text_image_orientation 文本图像方向分类
textline_orientation 文本行方向分类
语言分类 语言分类

有关不同场景的更多信息,请参阅人类存在分类行人属性分类天气佩戴安全头盔分类、交通标志分类车辆属性分类汽车存在分类文本图像方向分类文本行方向分类语言分类

三、参数定义

以下参数可以在命令行中指定,也可以在 Python 中实例化 PaddleClas 对象时用作构造函数的参数。

  • model_name(str):如果使用Paddle提供的基于ImageNet1k的推理模型,请通过参数指定模型的名称。
  • inference_model_dir(str):本地模型文件目录,model_name不指定时有效。该目录应包含inference.pdmodelinference.pdiparams
  • infer_imgs(str):待预测图片的路径,或者图片文件所在的目录,或者图片的网络地址。
  • use_gpu(bool):是否使用 GPU。
  • gpu_mem(int):GPU 内存使用情况。
  • use_tensorrt(bool):是否开启TensorRT。使用它可以大大提高预测性能。
  • enable_mkldnn(bool):是否启用 MKLDNN。
  • cpu_num_threads(int):分配cpu线程数,--use_gpuisFalse--enable_mkldnnis时有效True
  • batch_size(int):批量大小。
  • resize_short(int):将高度和宽度之间的最小值调整为resize_short.
  • crop_size(int):将裁剪图像居中crop_size
  • topk(int):打印(返回)topk使用 Topk 后处理时的预测结果。
  • threshold(float):使用后处理时 ThreshOutput 的阈值。
  • class_id_map_file(str):类ID和标签的映射文件。
  • save_dir(str):保存预测结果的目录,可以作为pre-label。

注意:如果要使用Transformer series models,如DeiT_***_384ViT_***_384等,请注意模型的输入大小,并需要设置resize_short=384resize=384。下面是一个演示。

  • 命令行:
from paddleclas import PaddleClas, get_default_confg
paddleclas --model_name=ViT_base_patch16_384 --infer_imgs='docs/images/inference_deployment/whl_demo.jpg' --resize_short=384 --crop_size=384
  • Python:
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ViT_base_patch16_384', resize_short=384, crop_size=384)

4. 用法

PaddleClas 提供了两种使用方式:

  1. Python 交互编程;
  2. Bash 命令行编程。

4.1 查看帮助信息

  • 命令行界面
paddleclas -h

4.2 使用 PaddleClas 提供的推理模型进行预测

可以使用 PaddleClas 提供的推理模型进行预测,只需要指定model_name. 在这种情况下,PaddleClas 会自动下载指定模型的文件并保存在目录中~/.paddleclas/

  • Python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
  • 命令行界面
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg'

4.3 使用本地模型文件进行预测

可以使用自己训练的本地模型文件进行预测,只需要指定inference_model_dir. 请注意,该目录必须包含inference.pdmodelinference.pdiparams

  • Python
from paddleclas import PaddleClas
clas = PaddleClas(inference_model_dir='./inference/')
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
  • 命令行界面
paddleclas --inference_model_dir='./inference/' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg'

4.4 批量预测

您可以批量预测,只需要指定目录batch_size何时infer_imgs包含图像文件。

  • Python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50', batch_size=2)
infer_imgs = 'docs/images/'
result=clas.predict(infer_imgs)
for r in result:
    print(r)
  • 命令行界面
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/' --batch_size 2

4.5 互联网图像预测

可以预测网络图片,只需要指定网络图片的URL infer_imgs。在这种情况下,图像文件将被下载并保存在目录中~/.paddleclas/images/

  • Python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
infer_imgs = 'https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/inference_deployment/whl_demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
  • 命令行界面
paddleclas --model_name='ResNet50' --infer_imgs='https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/inference_deployment/whl_demo.jpg'

4.6 NumPy.array 格式图像的预测

在 Python 代码中,可以预测NumPy.array图像的格式,只需要使用infer_imgs来传递图像数据的变量。请注意,PaddleClas 中的模型仅支持预测 3 通道图像数据,通道顺序为RGB.

  • Python
import cv2
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
infer_imgs = cv2.imread("docs/en/inference_deployment/whl_deploy_en.md")[:, :, ::-1]
result=clas.predict(infer_imgs)
print(next(result))

4.7 保存预测结果

您可以将预测结果保存为预标签,只需使用pre_label_out_dir指定要保存的目录即可。

  • Python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50', save_dir='./output_pre_label/')
infer_imgs = 'docs/images/' # it can be infer_imgs folder path which contains all of images you want to predict.
result=clas.predict(infer_imgs)
print(next(result))
  • 命令行界面
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/' --save_dir='./output_pre_label/'

4.8 指定类id和标签名的映射关系

可以指定class id和label name的映射关系,只需要使用class_id_map_file指定映射文件即可。PaddleClas 默认使用 ImageNet1K 的映射。

映射文件内容格式为:

class_id<space>class_name<\n>

例如:

0 tench, Tinca tinca
1 goldfish, Carassius auratus
2 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias
......
  • Python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50', class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt')
infer_imgs = 'docs/images/inference_deployment/whl_demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
  • 命令行界面
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/inference_deployment/whl_demo.jpg' --class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt'

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

内置分布

paddleclas-2.4.3-py3-none-any.whl (326.7 kB 查看哈希

已上传 py3