pip3 install paddleclas==2.2.1
python3 setup.py bdist_wheel
pip3 install dist/*
ResNet50
model provided by PaddleClas, the following image('docs/images/whl/demo.jpg'
) as an example.from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
infer_imgs='docs/images/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, 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/whl/demo.jpg"
>>> result
filename: docs/images/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!
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
.True
.8000
。False
.False
.--use_gpu
is False
and --enable_mkldnn
is True
, default by 10
.1
.resize_short
, default by 256
.crop_size
, default by 224
.topk
prediction results, default by 5
.ImageNet1K
dataset's mapping.None
, that is, not to save.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/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/whl/demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/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/whl/demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
paddleclas --inference_model_dir='./inference/' --infer_imgs='docs/images/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/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/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 image data must be 3 channels.
import cv2
from paddleclas import PaddleClas
clas = PaddleClas(model_name='ResNet50')
infer_imgs = cv2.imread("docs/images/whl/demo.jpg")
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/whl/' # 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/whl/' --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/whl/demo.jpg'
result=clas.predict(infer_imgs)
print(next(result))
paddleclas --model_name='ResNet50' --infer_imgs='docs/images/whl/demo.jpg' --class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt'
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。