PaddleClas supports Python wheel package for prediction. At present, PaddleClas wheel supports image classification including ImagetNet1k models and PULC models, but does not support mainbody detection, feature extraction and vector retrieval.
NumPy.array
format imagepip3 install paddleclas
pip install -v -e .
Using the ResNet50
model provided by PaddleClas, the following image('docs/images/inference_deployment/whl_demo.jpg'
) as an example.
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))
Note: PaddleClas.predict()
is a generator
. Therefore you need to use next()
or for
call it iteratively. It will perform a prediction by batch_size
and return the prediction result(s) when called. Examples of returned results are as follows:
>>> result
[{'class_ids': [8, 7, 86, 82, 80], 'scores': [0.97968, 0.02028, 3e-05, 1e-05, 0.0], 'label_names': ['hen', 'cock', 'partridge', 'ruffed grouse, partridge, Bonasa umbellus', 'black grouse'], 'filename': 'docs/images/inference_deployment/whl_demo.jpg'}]
paddleclas --model_name=ResNet50 --infer_imgs="docs/images/inference_deployment/whl_demo.jpg"
>>> result
class_ids: [8, 7, 86, 82, 80], scores: [0.97968, 0.02028, 3e-05, 1e-05, 0.0], label_names: ['hen', 'cock', 'partridge', 'ruffed grouse, partridge, Bonasa umbellus', 'black grouse'], filename: docs/images/inference_deployment/whl_demo.jpg
Predict complete!
PULC integrates various state-of-the-art algorithms such as backbone network, data augmentation and distillation, etc., and finally can automatically obtain a lightweight and high-precision image classification model.
PaddleClas provides a series of test cases, which contain demos of different scenes about people, cars, OCR, etc. Click here to download the data.
Prection using the PULC "Human Exists Classification" model provided by PaddleClas:
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
means there is no one in the image, someone
means there is someone in the image. Therefore, the prediction result indicates that there is no one in the figure.
Note: model.predict()
is a generator, so next()
or for
is needed to call it. This would to predict by batch that length is batch_size
, default by 1. You can specify the argument batch_size
and model_name
when instantiating PaddleClas object, for example: model = paddleclas.PaddleClas(model_name="person_exists", batch_size=2)
. Please refer to Supported Model List for the supported model list.
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!
Note: The "--infer_imgs" argument specify the image(s) to be predict, and you can also specify a directoy contains images. If use other model, you can specify the --model_name
argument. Please refer to Supported Model List for the supported model list.
Supported Model List
The name of PULC series models are as follows:
Name | Intro |
---|---|
person_exists | Human Exists Classification |
person_attribute | Pedestrian Attribute Classification |
safety_helmet | Classification of Whether Wearing Safety Helmet |
traffic_sign | Traffic Sign Classification |
vehicle_attribute | Vehicle Attribute Classification |
car_exists | Car Exists Classification |
text_image_orientation | Text Image Orientation Classification |
textline_orientation | Text-line Orientation Classification |
language_classification | Language Classification |
Please refer to Human Exists Classification、Pedestrian Attribute Classification、Classification of Whether Wearing Safety Helmet、Traffic Sign Classification、Vehicle Attribute Classification、Car Exists Classification、Text Image Orientation Classification、Text-line Orientation Classification、Language Classification for more information about different scenarios.
The following parameters can be specified in Command Line or used as parameters of the constructor when instantiating the PaddleClas object in Python.
model_name
is not specified. The directory should contain inference.pdmodel
and inference.pdiparams
.--use_gpu
is False
and --enable_mkldnn
is True
.resize_short
.crop_size
.topk
prediction results when Topk postprocess is used.Note: If you want to use Transformer series models
, such as DeiT_***_384
, ViT_***_384
, etc., please pay attention to the input size of model, and need to set resize_short=384
, resize=384
. The following is a demo.
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
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ViT_base_patch16_384', resize_short=384, crop_size=384)
PaddleClas provides two ways to use:
paddleclas -h
You can use the inference model provided by PaddleClas to predict, and only need to specify model_name
. In this case, PaddleClas will automatically download files of specified model and save them in the directory ~/.paddleclas/
.
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'
You can use the local model files trained by yourself to predict, and only need to specify inference_model_dir
. Note that the directory must contain inference.pdmodel
and inference.pdiparams
.
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'
You can predict by batch, only need to specify batch_size
when infer_imgs
is direcotry contain image files.
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
You can predict the Internet image, only need to specify URL of Internet image by infer_imgs
. In this case, the image file will be downloaded and saved in the directory ~/.paddleclas/images/
.
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'
In Python code, you can predict the NumPy.array
format image, only need to use the infer_imgs
to transfer variable of image data. Note that the models in PaddleClas only support to predict 3 channels image data, and channels order is RGB
.
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))
You can save the prediction result(s) as pre-label, only need to use pre_label_out_dir
to specify the directory to save.
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/'
You can specify the mapping between class id and label name, only need to use class_id_map_file
to specify the mapping file. PaddleClas uses ImageNet1K's mapping by default.
The content format of mapping file shall be:
class_id<space>class_name<\n>
For example:
0 tench, Tinca tinca
1 goldfish, Carassius auratus
2 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias
......
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'
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )