Package: paddleclas
PaddleClas wheel packagePaddleClas 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.Catalogue1. Installation2. Quick Start2.1 ImageNet1k models2.2 PULC models3. Definition of Parameters4. More usage4.1 View help information4.2 Prediction using inference model provide by PaddleClas4.3 Prediction using local model files4.4 Prediction by batch4.5 Prediction of Internet image4.6 Prediction ofNumPy.arrayformat image4.7 Save the prediction result(s)4.8 Specify the mapping between class id and label name1. Installation[Recommended]Installing from PyPI:pip3installpaddleclasPlease build and install locally if you need to use the develop branch of PaddleClas to experience the latest functions, or need to redevelop based on PaddleClas. The command is as follows:python3setup.pyinstall2. Quick Start2.1 ImageNet1k modelsUsing theResNet50model provided by PaddleClas, the following image('docs/images/inference_deployment/whl_demo.jpg') as an example.PythonfrompaddleclasimportPaddleClasclas=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 agenerator. Therefore you need to usenext()orforcall it iteratively. It will perform a prediction bybatch_sizeand 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'}]CLIpaddleclas--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!2.2 PULC modelsPULC 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. Clickhereto download the data.Prection using the PULC "Human Exists Classification" model provided by PaddleClas:Pythonimportpaddleclasmodel=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'}]Nobodymeans there is no one in the image,someonemeans 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, sonext()orforis needed to call it. This would to predict by batch that length isbatch_size, default by 1. You can specify the argumentbatch_sizeandmodel_namewhen instantiating PaddleClas object, for example:model = paddleclas.PaddleClas(model_name="person_exists",  batch_size=2). Please refer toSupported Model Listfor the supported model list.CLIpaddleclas--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_nameargument. Please refer toSupported Model Listfor the supported model list.Supported Model ListThe name of PULC series models are as follows:NameIntroperson_existsHuman Exists Classificationperson_attributePedestrian Attribute Classificationsafety_helmetClassification of Wheather Wearing Safety Helmettraffic_signTraffic Sign Classificationvehicle_attributeVehicle Attribute Classificationcar_existsCar Exists Classificationtext_image_orientationText Image Orientation Classificationtextline_orientationText-line Orientation Classificationlanguage_classificationLanguage ClassificationPlease refer toHuman Exists Classification、Pedestrian Attribute Classification、Classification of Wheather Wearing Safety Helmet、Traffic Sign Classification、Vehicle Attribute Classification、Car Exists Classification、Text Image Orientation Classification、Text-line Orientation Classification、Language Classificationfor more information about different scenarios.3. Definition of ParametersThe following parameters can be specified in Command Line or used as parameters of the constructor when instantiating the PaddleClas object in Python.model_name(str): If using inference model based on ImageNet1k provided by Paddle, please specify the model's name by the parameter.inference_model_dir(str): Local model files directory, which is valid whenmodel_nameis not specified. The directory should containinference.pdmodelandinference.pdiparams.infer_imgs(str): The path of image to be predicted, or the directory containing the image files, or the URL of the image from Internet.use_gpu(bool): Whether to use GPU or not.gpu_mem(int): GPU memory usages.use_tensorrt(bool): Whether to open TensorRT or not. Using it can greatly promote predict preformance.enable_mkldnn(bool): Whether enable MKLDNN or not.cpu_num_threads(int): Assign number of cpu threads, valid when--use_gpuisFalseand--enable_mkldnnisTrue.batch_size(int): Batch size.resize_short(int): Resize the minima between height and width intoresize_short.crop_size(int): Center crop image tocrop_size.topk(int): Print (return) thetopkprediction results when Topk postprocess is used.threshold(float): The threshold of ThreshOutput when postprocess is used.class_id_map_file(str): The mapping file between class ID and label.save_dir(str): The directory to save the prediction results that can be used as pre-label.Note: If you want to useTransformer series models, such asDeiT_***_384,ViT_***_384, etc., please pay attention to the input size of model, and need to setresize_short=384,resize=384. The following is a demo.CLI:frompaddleclasimportPaddleClas,get_default_confg
paddleclas--model_name=ViT_base_patch16_384--infer_imgs='docs/images/inference_deployment/whl_demo.jpg'--resize_short=384--crop_size=384Python:frompaddleclasimportPaddleClasclas=PaddleClas(model_name='ViT_base_patch16_384',resize_short=384,crop_size=384)4. UsagePaddleClas provides two ways to use:Python interative programming;Bash command line programming.4.1 View help informationCLIpaddleclas-h4.2 Prediction using inference model provide by PaddleClasYou can use the inference model provided by PaddleClas to predict, and only need to specifymodel_name. In this case, PaddleClas will automatically download files of specified model and save them in the directory~/.paddleclas/.PythonfrompaddleclasimportPaddleClasclas=PaddleClas(model_name='ResNet50')infer_imgs='docs/images/inference_deployment/whl_demo.jpg'result=clas.predict(infer_imgs)print(next(result))CLIpaddleclas--model_name='ResNet50'--infer_imgs='docs/images/inference_deployment/whl_demo.jpg'4.3 Prediction using local model filesYou can use the local model files trained by yourself to predict, and only need to specifyinference_model_dir. Note that the directory must containinference.pdmodelandinference.pdiparams.PythonfrompaddleclasimportPaddleClasclas=PaddleClas(inference_model_dir='./inference/')infer_imgs='docs/images/inference_deployment/whl_demo.jpg'result=clas.predict(infer_imgs)print(next(result))CLIpaddleclas--inference_model_dir='./inference/'--infer_imgs='docs/images/inference_deployment/whl_demo.jpg'4.4 Prediction by batchYou can predict by batch, only need to specifybatch_sizewheninfer_imgsis direcotry contain image files.PythonfrompaddleclasimportPaddleClasclas=PaddleClas(model_name='ResNet50',batch_size=2)infer_imgs='docs/images/'result=clas.predict(infer_imgs)forrinresult:print(r)CLIpaddleclas--model_name='ResNet50'--infer_imgs='docs/images/'--batch_size24.5 Prediction of Internet imageYou can predict the Internet image, only need to specify URL of Internet image byinfer_imgs. In this case, the image file will be downloaded and saved in the directory~/.paddleclas/images/.PythonfrompaddleclasimportPaddleClasclas=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))CLIpaddleclas--model_name='ResNet50'--infer_imgs='https://raw.githubusercontent.com/paddlepaddle/paddleclas/release/2.2/docs/images/inference_deployment/whl_demo.jpg'4.6 Prediction of NumPy.array format imageIn Python code, you can predict theNumPy.arrayformat image, only need to use theinfer_imgsto transfer variable of image data. Note that the models in PaddleClas only support to predict 3 channels image data, and channels order isRGB.pythonimportcv2frompaddleclasimportPaddleClasclas=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 Save the prediction result(s)You can save the prediction result(s) as pre-label, only need to usepre_label_out_dirto specify the directory to save.pythonfrompaddleclasimportPaddleClasclas=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))CLIpaddleclas--model_name='ResNet50'--infer_imgs='docs/images/'--save_dir='./output_pre_label/'4.8 Specify the mapping between class id and label nameYou can specify the mapping between class id and label name, only need to useclass_id_map_fileto 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
......PythonfrompaddleclasimportPaddleClasclas=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))CLIpaddleclas--model_name='ResNet50'--infer_imgs='docs/images/inference_deployment/whl_demo.jpg'--class_id_map_file='./ppcls/utils/imagenet1k_label_list.txt'