PaddleClas/docs/zh_CN/whl.md

8.7 KiB
Raw Blame History

paddleclas package使用说明

快速上手

安装whl包

pip安装

pip install paddleclas==2.0.0rc3

本地构建并安装

python3 setup.py bdist_wheel
pip3 install dist/paddleclas-x.x.x-py3-none-any.whl # x.x.x是paddleclas的版本号默认为0.0.0

1. 快速开始

  • 指定image_file='docs/images/whl/demo.jpg',使用Paddle提供的inference model,model_name='ResNet50', 使用图片docs/images/whl/demo.jpg

下图是使用的demo图片

from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50', top_k=5)
image_file='docs/images/whl/demo.jpg'
result=clas.predict(image_file)
print(result)
    >>> result
    [{'class_ids': array([ 8,  7, 86, 82, 80]), 'scores': array([9.7967714e-01, 2.0280687e-02, 2.7053760e-05, 6.1860351e-06,
       2.6378802e-06], dtype=float32), 'label_names': ['hen', 'cock', 'partridge', 'ruffed grouse, partridge, Bonasa umbellus', 'black grouse'], 'filename': 'docs/images/whl/demo.jpg'}]
  • 使用命令行式交互方法。直接获得结果。
paddleclas --model_name=ResNet50 --top_k=5 --image_file='docs/images/whl/demo.jpg'
    >>> result
    **********docs/images/whl/demo.jpg**********
    filename: docs/images/whl/demo.jpg; class id: 8, 7, 86, 82, 80; scores: 0.9797, 0.0203, 0.0000, 0.0000, 0.0000; label: hen, cock, partridge, ruffed grouse, partridge, Bonasa umbellus, black grouse
    Predict complete!

2. 参数解释

以下参数可在命令行交互使用时通过参数指定或在Python代码中实例化PaddleClas对象时作为构造函数的参数使用。

  • model_name(str): 模型名称没有指定自定义的model_file和params_file时可以指定该参数使用PaddleClas提供的基于ImageNet1k的inference model默认值为ResNet50。
  • image_file(str or numpy.ndarray): 图像地址支持指定单一图像的路径或图像的网址进行预测支持指定包含图像的文件夹路径支持numpy.ndarray格式的三通道图像数据且通道顺序为[B, G, R]。
  • use_gpu(bool): 是否使用GPU如果使用指定为True。默认为False。
  • use_tensorrt(bool): 是否开启TensorRT预测可提升GPU预测性能需要使用带TensorRT的预测库。当使用TensorRT推理加速指定为True。默认为False。
  • is_preprocessed(bool): 当image_file为numpy.ndarray格式的图像数据时图像数据是否已经过预处理。如果该参数为True则不再对image_file数据进行预处理否则将转换通道顺序后按照resize_shortresizenormalize参数对图像进行预处理。默认值为False。
  • resize_short(int): 将图像的高宽二者中小的值调整到指定的resize_short值大的值按比例放大。默认为256。
  • resize(int): 将图像裁剪到指定的resize值大小默认224。
  • normalize(bool): 是否对图像数据归一化默认True。
  • batch_size(int): 预测时每个batch的样本数量默认为1。
  • model_file(str): 模型.pdmodel的路径若不指定该参数需要指定model_name获得下载的模型。
  • params_file(str): 模型参数.pdiparams的路径若不指定则需要指定model_name,以获得下载的模型。
  • ir_optim(bool): 是否开启IR优化默认为True。
  • gpu_mem(int): 使用的GPU显存大小默认为8000。
  • enable_profile(bool): 是否开启profile功能默认False。
  • top_k(int): 指定的topk打印返回预测结果的前k个类别和对应的分类概率默认为1。
  • enable_mkldnn(bool): 是否开启MKLDNN默认False。
  • cpu_num_threads(int): 指定cpu线程数默认设置为10。
  • label_name_path(str): 指定一个表示所有的label name的文件路径。当用户使用自己训练的模型可指定这一参数打印结果时可以显示图像对应的类名称。若用户使用Paddle提供的inference model则可不指定该参数使用imagenet1k的label_name默认为空字符串。
  • pre_label_image(bool): 是否需要进行预标注。
  • pre_label_out_idr(str): 进行预标注后输出结果的文件路径默认为None。

注意: 如果使用Transformer系列模型,如DeiT_***_384, ViT_***_384等,请注意模型的输入数据尺寸,需要设置参数resize_short=384, reize=384

3. 代码使用方法

提供两种使用方式1、python交互式编程。2、bash命令行式编程

  • 查看帮助信息
bash
paddleclas -h
  • 用户使用自己指定的模型,需要指定模型路径参数model_file和参数params_file
python
from paddleclas import PaddleClas
clas = PaddleClas(model_file='the path of model file',
    params_file='the path of params file')
image_file = 'docs/images/whl/demo.jpg' # image_file 可指定为前缀是https的网络图片也可指定为本地图片
result=clas.predict(image_file)
print(result)
bash
paddleclas --model_file='user-specified model path' --params_file='parmas path' --image_file='docs/images/whl/demo.jpg'
  • 用户使用PaddlePaddle训练好的inference model来预测并通过参数model_name指定。 此时无需指定model_file,模型会根据model_name自动下载指定模型到当前目录,并保存在目录~/.paddleclas/下以model_name命名的文件夹中。
python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
image_file = 'docs/images/whl/demo.jpg' # image_file 可指定为前缀是https的网络图片也可指定为本地图片
result=clas.predict(image_file)
print(result)
bash
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/demo.jpg'
  • 用户可以使用numpy.ndarray格式的图像数据并通过参数image_file指定。注意该图像数据必须为三通道图像数据。如需对图像进行预处理,则图像通道顺序必须为[B, G, R]。
python
import cv2
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
image_file = cv2.imread("docs/images/whl/demo.jpg")
result=clas.predict(image_file)
  • 用户可以将image_file指定为包含图片的文件夹路径。
python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
image_file = 'docs/images/whl/' # it can be image_file folder path which contains all of images you want to predict.
result=clas.predict(image_file)
print(result)
bash
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/'
  • 用户可以指定pre_label_image=True, pre_label_out_idr='./output_pre_label/'将图片按其top1预测结果保存到pre_label_out_dir目录下对应类别的文件夹中。
python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50', pre_label_image=True,pre_label_out_idr='./output_pre_label/')
image_file = 'docs/images/whl/' # it can be image_file folder path which contains all of images you want to predict.
result=clas.predict(image_file)
print(result)
bash
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/' --pre_label_image=True --pre_label_out_idr='./output_pre_label/'
  • 用户可以通过参数label_name_path指定模型的label_dict_file文件路径,文件内容格式应为(class_idclass_name<\n>),例如:
0 tench, Tinca tinca
1 goldfish, Carassius auratus
2 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias
......
  • 用户如果使用Paddle提供的inference model则不需要提供label_name_path,会默认使用ppcls/utils/imagenet1k_label_list.txt。 如果用户希望使用自己的模型,则可以提供label_name_path将label_name与结果一并输出。如果不提供将不会输出label_name信息。
python
from paddleclas import PaddleClas
clas = PaddleClas(model_file='the path of model file', params_file ='the path of params file', label_name_path='./ppcls/utils/imagenet1k_label_list.txt')
image_file = 'docs/images/whl/demo.jpg' # it can be image_file folder path which contains all of images you want to predict.
result=clas.predict(image_file)
print(result)
bash
paddleclas --model_file='the path of model file' --params_file='the path of params file' --image_file='docs/images/whl/demo.jpg' --label_name_path='./ppcls/utils/imagenet1k_label_list.txt'
python
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
image_file = 'docs/images/whl/' # it can be image_file folder path which contains all of images you want to predict.
result=clas.predict(image_file)
print(result)
bash
paddleclas --model_name='ResNet50' --image_file='docs/images/whl/'