pedestrian_attribute_recognition.en.md 24 KB


comments: true

Pedestrian Attribute Recognition Module Development Tutorial

I. Overview

Pedestrian attribute recognition is a crucial component in computer vision systems, responsible for locating and labeling specific attributes of pedestrians in images or videos, such as gender, age, clothing color, and type. The performance of this module directly impacts the accuracy and efficiency of the entire computer vision system. The pedestrian attribute recognition module typically outputs attribute information for each pedestrian, which is then passed as input to other modules (e.g., pedestrian tracking, pedestrian re-identification) for subsequent processing.

II. Supported Model List

ModelModel Download Link mA (%) GPU Inference Time (ms)
[Normal Mode / High-Performance Mode]
CPU Inference Time (ms)
[Normal Mode / High-Performance Mode]
Model Size (M) Description
PP-LCNet_x1_0_pedestrian_attributeInference Model/Trained Model 92.2 2.35 / 0.49 3.17 / 1.25 6.7 M PP-LCNet_x1_0_pedestrian_attribute is a lightweight pedestrian attribute recognition model based on PP-LCNet, covering 26 categories
Note: The above accuracy metrics are mA on PaddleX's internal self-built dataset. GPU inference time is based on an NVIDIA Tesla T4 machine with FP32 precision. CPU inference speed is based on an Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz with 8 threads and FP32 precision. ## III. Quick Integration > ❗ Before quick integration, please install the PaddleX wheel package. For detailed instructions, refer to the [PaddleX Local Installation Guide](../../../installation/installation.en.md) After installing the wheel package, a few lines of code can complete the inference of the pedestrian attribute recognition module. You can easily switch models under this module and integrate the model inference of pedestrian attribute recognition into your project. Before running the following code, please download the [demo image](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/pedestrian_attribute_006.jpg) to your local machine. ```python from paddlex import create_model model = create_model(model_name="PP-LCNet_x1_0_pedestrian_attribute") output = model.predict("pedestrian_attribute_006.jpg", batch_size=1) for res in output: res.print(json_format=False) res.save_to_img("./output/") res.save_to_json("./output/res.json") ``` After running, the obtained result is: ```bash {'res': {'input_path': 'pedestrian_attribute_006.jpg', 'page_index': None, 'class_ids': array([10, ..., 23]), 'scores': array([1. , ..., 0.54777]), 'label_names': ['LongCoat(长外套)', 'Age18-60(年龄在18-60岁之间)', 'Trousers(长裤)', 'Front(面朝前)']}} ``` 运行结果参数含义如下: - `input_path`:表示输入待预测多类别图像的路径 - `page_index`:如果输入是PDF文件,则表示当前是PDF的第几页,否则为 `None` - `class_ids`:表示行人属性图像的预测标签ID - `scores`:表示行人属性图像的预测标签置信度 - `label_names`:表示行人属性图像的预测标签名称 可视化图片如下: Pedestrian Attribute Result 相关方法、参数等说明如下: * `create_model`实例化行人属性识别模型(此处以`PP-LCNet_x1_0_pedestrian_attribute`为例),具体说明如下:
参数 参数说明 参数类型 可选项 默认值
model_name 模型名称 str PP-LCNet_x1_0_pedestrian_attribute
model_dir 模型存储路径 str
threshold 行人属性识别阈值 float/list/dict
  • float类型变量,任意[0-1]之间浮点数:0.5
  • list类型变量,由多个[0-1]之间浮点数组成的列表:[0.5,0.5,...]
  • dict类型变量,指定不同类别使用不同的阈值,其中"default"为必须包含的键:{"default":0.5,1:0.1,...}
  • 0.5
    • 其中,model_name 必须指定,指定 model_name 后,默认使用 PaddleX 内置的模型参数,在此基础上,指定 model_dir 时,使用用户自定义的模型。

    • 其中,threshold 参数用于设置多标签分类的阈值,默认为0.7。当设置为浮点数时,表示所有类别均使用该阈值;当设置为列表时,表示不同类别使用不同的阈值,此时需保持列表长度与类别数量一致;当设置为字典时,default 为必须包含的键, 表示所有类别的默认阈值,其它类别使用各自的阈值。例如:{"default":0.5,1:0.1}。

    • 调用多标签分类模型的 predict() 方法进行推理预测,predict() 方法参数有 input , batch_sizethreshold,具体说明如下:

    Parameter Parameter Description Parameter Type Optional Default Value
    input Data to be predicted, supporting multiple input types Python Var/str/list
    • Python variable, such as image data represented by numpy.ndarray
    • File path, such as the local path of an image file: /root/data/img.jpg
    • URL link, such as the network URL of an image file: Example
    • Local directory, the directory should contain data files to be predicted, such as the local path: /root/data/
    • List, elements of the list should be data of the above types, such as [numpy.ndarray, numpy.ndarray], [\"/root/data/img1.jpg\", \"/root/data/img2.jpg\"], [\"/root/data1\", \"/root/data2\"]
    None
    batch_size Batch size int Any integer 1
    threshold Threshold for pedestrian attribute recognition float/list/dict
    • Float variable, any floating-point number between [0-1]: 0.5
    • List variable, a list composed of multiple floating-point numbers between [0-1]: [0.5,0.5,...]
    • Dict variable, specifying different thresholds for different categories, where "default" is a required key: {"default":0.5,1:0.1,...}
    0.5
    • Process the prediction results. Each sample's prediction result is a corresponding Result object, and it supports operations such as printing, saving as an image, and saving as a json file:
    Method Method Description Parameter Parameter Type Parameter Description Default Value
    print() Print the result to the terminal format_json bool Whether to format the output content using JSON indentation True
    indent int Specify the indentation level to beautify the output JSON data, making it more readable, only effective when format_json is True 4
    ensure_ascii bool Control whether to escape non-ASCII characters to Unicode. When set to True, all non-ASCII characters will be escaped; False retains the original characters, only effective when format_json is True False
    save_to_json() Save the result as a JSON file save_path str The file path to save the result. When it is a directory, the saved file name will be consistent with the input file name None
    indent int Specify the indentation level to beautify the output JSON data, making it more readable, only effective when format_json is True 4
    ensure_ascii bool Control whether to escape non-ASCII characters to Unicode. When set to True, all non-ASCII characters will be escaped; False retains the original characters, only effective when format_json is True False
    save_to_img() Save the result as an image file save_path str The file path to save the result. When it is a directory, the saved file name will be consistent with the input file name None
    • Additionally, it also supports obtaining the visualized image with results and the prediction results through attributes, as follows:
    Attribute Attribute Description
    json Get the prediction result in json format
    img Get the visualized image in dict format

    For more information on the usage of PaddleX single-model inference APIs, you can refer to the PaddleX Single-Model Python Script Usage Instructions.

    IV. Custom Development

    If you seek higher accuracy from existing models, you can leverage PaddleX's custom development capabilities to develop better pedestrian attribute recognition models. Before developing pedestrian attribute recognition with PaddleX, ensure you have installed the classification-related model training plugins for PaddleX. The installation process can be found in the custom development section of the PaddleX Local Installation Guide.

    4.1 Data Preparation

    Before model training, you need to prepare a dataset for the specific task module. PaddleX provides data validation functionality for each module, and only data that passes validation can be used for model training. Additionally, PaddleX provides demo datasets for each module, which you can use to complete subsequent development. If you wish to use a private dataset for model training, refer to the PaddleX Multi-Label Classification Task Module Data Annotation Tutorial.

    4.1.1 Demo Data Download

    You can use the following commands to download the demo dataset to a specified folder:

    wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/pedestrian_attribute_examples.tar -P ./dataset
    tar -xf ./dataset/pedestrian_attribute_examples.tar -C ./dataset/
    

    4.1.2 Data Validation

    Run a single command to complete data validation:

    python main.py -c paddlex/configs/modules/pedestrian_attribute_recognition/PP-LCNet_x1_0_pedestrian_attribute.yaml \
        -o Global.mode=check_dataset \
        -o Global.dataset_dir=./dataset/pedestrian_attribute_examples
    

    After executing the above command, PaddleX will validate the dataset and summarize its basic information. If the command runs successfully, it will print Check dataset passed ! in the log. The validation results file is saved in ./output/check_dataset_result.json, and related outputs are saved in the ./output/check_dataset directory in the current directory, including visual examples of sample images and sample distribution histograms.

    👉 Details of Validation Results (Click to Expand)

    The specific content of the validation result file is:

    {
      "done_flag": true,
      "check_pass": true,
      "attributes": {
        "label_file": "../../dataset/pedestrian_attribute_examples/label.txt",
        "num_classes": 26,
        "train_samples": 1000,
        "train_sample_paths": [
          "check_dataset/demo_img/020907.jpg",
          "check_dataset/demo_img/004274.jpg",
          "check_dataset/demo_img/009412.jpg",
          "check_dataset/demo_img/026873.jpg",
          "check_dataset/demo_img/030560.jpg",
          "check_dataset/demo_img/022846.jpg",
          "check_dataset/demo_img/009055.jpg",
          "check_dataset/demo_img/015399.jpg",
          "check_dataset/demo_img/006435.jpg",
          "check_dataset/demo_img/055307.jpg"
        ],
        "val_samples": 500,
        "val_sample_paths": [
          "check_dataset/demo_img/080381.jpg",
          "check_dataset/demo_img/080469.jpg",
          "check_dataset/demo_img/080146.jpg",
          "check_dataset/demo_img/080003.jpg",
          "check_dataset/demo_img/080283.jpg",
          "check_dataset/demo_img/080104.jpg",
          "check_dataset/demo_img/080149.jpg",
          "check_dataset/demo_img/080313.jpg",
          "check_dataset/demo_img/080131.jpg",
          "check_dataset/demo_img/080412.jpg"
        ]
      },
      "analysis": {
        "histogram": "check_dataset/histogram.png"
      },
      "dataset_path": "pedestrian_attribute_examples",
      "show_type": "image",
      "dataset_type": "MLClsDataset"
    }
    

    In the above validation results, check_pass being True indicates that the dataset format meets the requirements. Explanations for other indicators are as follows:

    • attributes.num_classes: The number of classes in this dataset is 26;
    • attributes.train_samples: The number of samples in the training set of this dataset is 1000;
    • attributes.val_samples: The number of samples in the validation set of this dataset is 500;
    • attributes.train_sample_paths: The list of relative paths to the visualization images of samples in the training set of this dataset;
    • attributes.val_sample_paths: The list of relative paths to the visualization images of samples in the validation set of this dataset;

    Additionally, the dataset verification also analyzes the distribution of the length and width of all images in the dataset and plots a histogram (histogram.png):

    4.1.3 Dataset Format Conversion/Dataset Splitting (Optional)

    After completing data validation, you can convert the dataset format or re-split the training/validation ratio of the dataset by modifying the configuration file or appending hyperparameters.

    👉 Dataset Format Conversion/Dataset Splitting Details (Click to Expand)

    (1) Dataset Format Conversion

    Pedestrian attribute recognition does not support data format conversion.

    (2) Dataset Splitting

    The dataset splitting parameters can be set by modifying the fields under CheckDataset in the configuration file. An example of part of the configuration file is shown below:

    • CheckDataset:
    • split:
    • enable: Whether to re-split the dataset. Set to True to enable dataset splitting, default is False;
    • train_percent: If re-splitting the dataset, set the percentage of the training set. The type is any integer between 0-100, ensuring the sum with val_percent is 100;

    For example, if you want to re-split the dataset with a 90% training set and a 10% validation set, modify the configuration file as follows:

    ......
    CheckDataset:
      ......
      split:
        enable: True
        train_percent: 90
        val_percent: 10
      ......
    

    Then execute the command:

    python main.py -c paddlex/configs/modules/pedestrian_attribute_recognition/PP-LCNet_x1_0_pedestrian_attribute.yaml \
        -o Global.mode=check_dataset \
        -o Global.dataset_dir=./dataset/pedestrian_attribute_examples
    

    After the data splitting is executed, the original annotation files will be renamed to xxx.bak in the original path.

    The above parameters also support being set by appending command-line arguments:

    python main.py -c paddlex/configs/modules/pedestrian_attribute_recognition/PP-LCNet_x1_0_pedestrian_attribute.yaml  \
        -o Global.mode=check_dataset \
        -o Global.dataset_dir=./dataset/pedestrian_attribute_examples \
        -o CheckDataset.split.enable=True \
        -o CheckDataset.split.train_percent=90 \
        -o CheckDataset.split.val_percent=10
    

    4.2 Model Training

    Model training can be completed with a single command. Taking the training of the PP-LCNet pedestrian attribute recognition model (PP-LCNet_x1_0_pedestrian_attribute) as an example:

    python main.py -c paddlex/configs/modules/pedestrian_attribute_recognition/PP-LCNet_x1_0_pedestrian_attribute.yaml \
        -o Global.mode=train \
        -o Global.dataset_dir=./dataset/pedestrian_attribute_examples
    

    the following steps are required:

    • Specify the path of the model's .yaml configuration file (here it is PP-LCNet_x1_0_pedestrian_attribute.yaml,When training other models, you need to specify the corresponding configuration files. The relationship between the model and configuration files can be found in the PaddleX Model List (CPU/GPU))
    • Specify the mode as model training: -o Global.mode=train
    • Specify the path of the training dataset: -o Global.dataset_dir. Other related parameters can be set by modifying the fields under Global and Train in the .yaml configuration file, or adjusted by appending parameters in the command line. For example, to specify training on the first 2 GPUs: -o Global.device=gpu:0,1; to set the number of training epochs to 10: -o Train.epochs_iters=10. For more modifiable parameters and their detailed explanations, refer to the configuration file parameter instructions for the corresponding task module of the model PaddleX Common Model Configuration File Parameters.
    👉 More Details (Click to Expand)
    • During model training, PaddleX automatically saves the model weight files, with the default being output. If you need to specify a save path, you can set it through the -o Global.output field in the configuration file.
    • PaddleX shields you from the concepts of dynamic graph weights and static graph weights. During model training, both dynamic and static graph weights are produced, and static graph weights are selected by default for model inference.
    • After completing the model training, all outputs are saved in the specified output directory (default is ./output/), typically including:

    • train_result.json: Training result record file, recording whether the training task was completed normally, as well as the output weight metrics, related file paths, etc.;

    • train.log: Training log file, recording changes in model metrics and loss during training;
    • config.yaml: Training configuration file, recording the hyperparameter configuration for this training session;
    • .pdparams, .pdema, .pdopt.pdstate, .pdiparams, .pdmodel: Model weight-related files, including network parameters, optimizer, EMA, static graph network parameters, static graph network structure, etc.;

    4.3 Model Evaluation

    After completing model training, you can evaluate the specified model weights file on the validation set to verify the model's accuracy. Using PaddleX for model evaluation can be done with a single command:

    python main.py -c paddlex/configs/modules/pedestrian_attribute_recognition/PP-LCNet_x1_0_pedestrian_attribute.yaml \
        -o Global.mode=evaluate \
        -o Global.dataset_dir=./dataset/pedestrian_attribute_examples
    

    Similar to model training, the following steps are required:

    • Specify the path to the model's .yaml configuration file (here it is PP-LCNet_x1_0_pedestrian_attribute.yaml)
    • Specify the mode as model evaluation: -o Global.mode=evaluate
    • Specify the path to the validation dataset: -o Global.dataset_dir Other related parameters can be set by modifying the Global and Evaluate fields in the .yaml configuration file. For details, refer to PaddleX Common Model Configuration File Parameter Description.
    👉 More Details (Click to Expand)

    When evaluating the model, you need to specify the model weights file path. Each configuration file has a default weight save path built-in. If you need to change it, simply set it by appending a command line parameter, such as -o Evaluate.weight_path=./output/best_model/best_model.pdparams.

    After completing the model evaluation, an evaluate_result.json file will be produced, which records the evaluation results, specifically, whether the evaluation task was completed successfully and the model's evaluation metrics, including MultiLabelMAP;

    4.4 Model Inference and Integration

    After completing model training and evaluation, you can use the trained model weights for inference prediction or Python integration.

    4.4.1 Model Inference

    To perform inference prediction through the command line, simply use the following command. Before running the following code, please download the demo image to your local machine.

    python main.py -c paddlex/configs/modules/pedestrian_attribute_recognition/PP-LCNet_x1_0_pedestrian_attribute.yaml \
        -o Global.mode=predict \
        -o Predict.model_dir="./output/best_model/inference" \
        -o Predict.input="pedestrian_attribute_006.jpg"
    

    Similar to model training and evaluation, the following steps are required:

    • Specify the path to the model's .yaml configuration file (here it is PP-LCNet_x1_0_pedestrian_attribute.yaml)
    • Specify the mode as model inference prediction: -o Global.mode=predict
    • Specify the model weights path: -o Predict.model_dir="./output/best_model/inference"
    • Specify the input data path: -o Predict.input="..." . Other related parameters can be set by modifying the Global and Predict fields in the .yaml configuration file. For details, refer to PaddleX Common Model Configuration File Parameter Description.

    4.4.2 Model Integration

    The model can be directly integrated into the PaddleX pipeline or directly into your own project.

    1.Pipeline Integration

    The pedestrian attribute recognition module can be integrated into the Pedestrian Attribute Recognition Pipeline of PaddleX. Simply replace the model path to update the pedestrian attribute recognition module of the relevant pipeline. In pipeline integration, you can use high-performance inference and service-oriented deployment to deploy your model.

    2.Module Integration

    The weights you produce can be directly integrated into the pedestrian attribute recognition module. Refer to the Python example code in Quick Integration and simply replace the model with the path to your trained model.