-[2.1.6 Model Inference Deployment](#216-model-inference-deployment)
-[2.1.6.1 Inference model preparation](#2161-inference-model-preparation)
-[2.1.6.2 Inference based on Python prediction engine](#2162-inference-based-on-python-prediction-engine)
-[2.1.6.3 Inference based on C++ prediction engine](#2163-inference-based-on-c-prediction-engine)
-[2.1.7 Service deployment](#217-service-deployment)
-[2.1.8 Device side deployment](#218-device-side-deployment)
-[2.1.9 Paddle2ONNX Model Conversion and Prediction](#219-paddle2onnx-model-conversion-and-prediction)
-[3. Summary](#3-summary)
-[3.1 Method summary and comparison](#31-method-summary-and-comparison)
-[3.2 Usage advice/FAQ](#32-usage-advicefaq)
-[4. References](#4-references)
### 1. Introduction to algorithms/application scenarios
Pedestrian re-identification (Person re-identification), also known as pedestrian re-identification, is the use of [computer vision](https://baike.baidu.com/item/computer vision/2803351) technology to judge [image](https://baike .baidu.com/item/image/773234) or whether there is a [technique] of a particular pedestrian in the video sequence (https://baike.baidu.com/item/technique/13014499). Widely regarded as a sub-problem of [Image Retrieval](https://baike.baidu.com/item/image_retrieval/1150910). Given a surveillance pedestrian image, retrieve the pedestrian image across devices. It aims to make up for the visual limitations of fixed cameras, and can be combined with [pedestrian detection](https://baike.baidu.com/item/pedestrian detection/20590256)/pedestrian tracking technology, which can be widely used in [intelligent video surveillance] ](https://baike.baidu.com/item/intelligent video surveillance/10717227), intelligent security and other fields.
Pedestrian re-identification (Person re-identification), also known as pedestrian re-identification, is the use of [computer vision](https://baike.baidu.com/item/computervision/2803351) technology to judge [image](https://baike.baidu.com/item/image/773234) or whether there is a technique of a particular pedestrian in the video sequence. Widely regarded as a sub-problem of [Image Retrieval](https://baike.baidu.com/item/image_retrieval/1150910). Given a surveillance pedestrian image, retrieve the pedestrian image across devices. It aims to make up for the visual limitations of fixed cameras, and can be combined with [pedestrian detection](https://baike.baidu.com/item/pedestriandetection/20590256)/pedestrian tracking technology, which can be widely used in [intelligent video surveillance](https://baike.baidu.com/item/intelligentvideosurveillance/10717227), intelligent security and other fields.
### 2. Introduction to ReID strong-baseline algorithm
The common person re-identification method extracts the local/global, single-granularity/multi-granularity features of the input image through the feature extraction module, and then obtains a high-dimensional feature vector through the fusion module. Use the classification head to convert the feature vector into the probability of each category during training to optimize the feature extraction model in the way of classification tasks; directly use the high-dimensional feature vector as the image description vector in the retrieval vector library during testing or inference search to get the search results. The ReID strong-baseline algorithm proposes several methods to effectively optimize training and retrieval to improve the overall model performance.
In the past person re-identification methods, the feature extraction module is used to extract the global or multi-granularity features of the image, and then a high-dimensional feature vector is obtained through the fusion module. Use the classification head to map the feature vector into the probability of each category during training to optimize the entire model in the way of classification tasks; directly use the high-dimensional feature vector as an image descriptor in the retrieval library during testing or inference. search to get the search results. The ReID strong-baseline algorithm proposes several methods to effectively optimize training and retrieval to improve the overall model performance.
### 2. ReID algorithm
#### 2.1 Principle of ReID strong-baseline algorithm
#### 2.1 ReID strong-baseline
Paper source: [Bag of Tricks and A Strong Baseline for Deep Person Re-identification](https://openaccess.thecvf.com/content_CVPRW_2019/papers/TRMTMCT/Luo_Bag_of_Tricks_and_a_Strong_Baseline_for_Deep_Person_CVPRW_2019_paper.pdf)
1. Warmup, let the learning rate gradually increase at the beginning of training and then start to decrease, which is conducive to finding better parameters during gradient descent.
2. Random erasing augmentation, random area erasing, enhances the generalization of the model.
3. Label smoothing, label smoothing, enhance the generalization of the model.
4. Last stride=1, cancel the downsampling of the last stage of the feature extraction module, increase the resolution of the output feature map to retain more details and enhance the classification ability of the model.
5. BNNeck, before the feature vector is input into the classification head, it goes through BNNeck, so that the feature vector becomes a normal distribution, which reduces the difficulty of optimizing ID Loss and TripLetLoss at the same time.
6. Center loss, give each category a learnable cluster center feature, and make the intra-class features close to the cluster center during training to reduce intra-class differences and increase inter-class differences.
7. Reranking, consider whether the neighboring candidate objects of the retrieved image also contain retrieval targets during retrieval, so as to optimize the distance matrix and finally improve the retrieval accuracy.
##### 2.1.1 Principle introduction
#### 2.2a Quick start
Based on the commonly used person re-identification model based on ResNet50, the author explores and summarizes the following effective and applicable optimization methods, which greatly improves the indicators on multiple person re-identification datasets.
The quick start chapter mainly takes the `softmax_triplet_with_center.yaml` configuration and trained model file as an example to test on the Market1501 dataset.
1. Warmup: At the beginning of training, let the learning rate gradually increase from a small value and then start to decrease, which is conducive to the stability of gradient descent optimization, so as to find a better parameter model.
2. Random erasing augmentation: Random area erasing, which improves the generalization ability of the model through data augmentation.
3. Label smoothing: Label smoothing to improve the generalization ability of the model.
4. Last stride=1: Set the downsampling of the last stage of the feature extraction module to 1, increase the resolution of the output feature map to retain more details and improve the classification ability of the model.
5. BNNeck: Before the feature vector is input to the classification head, it goes through BNNeck, so that the feature obeys the normal distribution on the surface of the hypersphere, which reduces the difficulty of optimizing IDLoss and TripLetLoss at the same time.
6. Center loss: Give each category a learnable cluster center, and make the intra-class features close to the cluster center during training to reduce intra-class differences and increase inter-class differences.
7. Reranking: Consider the neighbor candidates of the query image during retrieval, optimize the distance matrix according to whether the neighbor images of the candidate object also contain the query image, and finally improve the retrieval accuracy.
1. Download the [Market-1501-v15.09.15.zip](https://pan.baidu.com/s/1ntIi2Op?_at_=1654142245770) dataset, extract it to `PaddleClas/dataset/`, and organize it as follows File structure:
```shell
PaddleClas/dataset/market1501
└── Market-1501-v15.09.15/
├── bounding_box_test/
├── bounding_box_train/
├── gt_bbox/
├── gt_query/
├── query/
├── generate_anno.py
├── bounding_box_test.txt
├── bounding_box_train.txt
├── query.txt
└── readme.txt
```
##### 2.1.2 Accuracy Index
2. Download [reid_strong_baseline_softmax_with_center.epoch_120.pdparams](reid_strong_baseline_softmax_with_center.epoch_120.pdparams) to `PaddleClas/pretrained_models` folder
The following table summarizes the accuracy metrics of the 3 configurations of the recurring ReID strong-baseline on the Market1501 dataset,
It can be seen that the metrics of the `reid_strong_baseline_softmax_with_center.epoch_120.pdparams` model provided by us on the Market1501 dataset are recall@1=0.94270, recall@5=0.98189, mAP=0.85799
Note: The above reference indicators are obtained by using the author's open source code to train on our equipment for many times. Due to different system environment, torch version, CUDA version and other reasons, there may be slight differences with the indicators provided by the author.
#### 2.2b Model training/testing/inference
Next, we mainly take the `softmax_triplet_with_center.yaml` configuration and trained model file as an example to show the process of training, testing, and inference on the Market1501 dataset.
- Model training
##### 2.1.3 Data Preparation
1. Download the [Market-1501-v15.09.15.zip](https://pan.baidu.com/s/1ntIi2Op?_at_=1654142245770) dataset, extract it to `PaddleClas/dataset/`, and organize it as follows File structure:
Download the [Market-1501-v15.09.15.zip](https://pan.baidu.com/s/1ntIi2Op?_at_=1654142245770) dataset, extract it to `PaddleClas/dataset/`, and organize it into the following file structure :
```shell
PaddleClas/dataset/market1501
...
...
@@ -106,7 +84,9 @@ The quick start chapter mainly takes the `softmax_triplet_with_center.yaml` conf
└── readme.txt
```
2. Execute the following command to start training
##### 2.1.4 Model training
1. Execute the following command to start training
@@ -114,9 +94,15 @@ The quick start chapter mainly takes the `softmax_triplet_with_center.yaml` conf
Note: Single card training takes about 1 hour.
- Model testing
2. View training logs and saved model parameter files
During the training process, indicator information such as loss will be printed on the screen in real time, and the log file `train.log`, model parameter file `*.pdparams`, optimizer parameter file `*.pdopt` and other contents will be saved to `Global.output_dir` `Under the specified folder, the default is under the `PaddleClas/output/RecModel/` folder.
##### 2.1.5 Model Evaluation
Prepare the `*.pdparams` model parameter file for evaluation. You can use the trained model or the model saved in [2.1.4 Model training](#214-model-training).
Assuming that the path of the model file to be tested is `./output/RecModel/latest.pdparams` , execute the following command to test
- Take the `latest.pdparams` saved during training as an example, execute the following command to evaluate.
```shell
python3.7 tools/eval.py \
...
...
@@ -124,19 +110,65 @@ The quick start chapter mainly takes the `softmax_triplet_with_center.yaml` conf
- Take the trained model as an example, download [reid_strong_baseline_softmax_with_center.epoch_120.pdparams](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/reid/pretrain/reid_strong_baseline_softmax_with_center.epoch_120.pdparams) Go to the `PaddleClas/pretrained_models` folder and execute the following command to evaluate.
Note: The address filled after `pretrained_model` does not need to be suffixed with `.pdparams`, it will be added automatically when the program is running.
The default evaluation log is saved in `PaddleClas/output/RecModel/eval.log`. You can see that the evaluation metrics of the `reid_strong_baseline_softmax_with_center.epoch_120.pdparams` model we provided on the Market1501 dataset are recall@1=0.94270, recall@5 =0.98189, mAP=0.85799
1. DownloadInference model and extract: [reid_srong_baseline_softmax_with_center.tar](https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/rec/reid/inference/reid_srong_baseline_softmax_with_center.tar)
##### 2.1.6 Model Inference Deployment
###### 2.1.6.1 Inference model preparation
You can choose to use the model file saved during the training process to convert into an inference model and inference, or use the converted inference model we provide for direct inference
- Convert the model file saved during the training process into an inference model, also take `latest.pdparams` as an example, execute the following command to convert
tar xf reid_srong_baseline_softmax_with_center.tar
cd ../
```
2. Modify `PaddleClas/deploy/configs/inference_rec.yaml`. Change the field after `infer_imgs:` to any image in the query folder in Market1501; change the field after `rec_inference_model_dir:` to the path of the extracted reid_srong_baseline_softmax_with_center folder; change the preprocessing configuration under the `transform_ops` field Changed to preprocessing configuration under `Eval.Query.dataset` in `softmax_triplet_with_center.yaml`. As follows
###### 2.1.6.2 Inference based on Python prediction engine
1. Modify `PaddleClas/deploy/configs/inference_rec.yaml`. Change the field after `infer_imgs:` to any image path under the query folder in Market1501 (the code below uses the image path of `0294_c1s1_066631_00.jpg`); change the field after `rec_inference_model_dir:` to the extracted one reid_srong_baseline_softmax_with_center folder path; change the preprocessing configuration under the `transform_ops` field to the preprocessing configuration under `Eval.Query.dataset` in `softmax_triplet_with_center.yaml`. As follows
```yaml
Global:
...
...
@@ -158,7 +190,7 @@ The quick start chapter mainly takes the `softmax_triplet_with_center.yaml` conf
-ResizeImage:
size: [128, 256]
return_numpy: False
interpolation: 'bilinear'
interpolation: "bilinear"
backend: "pil"
- ToTensor:
- Normalize:
...
...
@@ -168,39 +200,59 @@ The quick start chapter mainly takes the `softmax_triplet_with_center.yaml` conf
4. Check the output result. The actual result is a vector of length 2048, which represents the feature vector obtained after the input image is transformed by the model.
3. Check the output result, the actual result is a vector of length 2048, which represents the feature vector obtained after the input image is transformed by the model
The output vector for inference is stored in the `result_dict` variable in [predict_rec.py](../../../deploy/python/predict_rec.py#L134-L135).
### 3. Summary
4. Batch prediction
Change the path after `infer_imgs:` in the configuration file to a folder, such as `../dataset/market1501/Market-1501-v15.09.15/query`, it will predict and output all images under query. Feature vector.
#### 3.1 Method summary, comparison, etc.
###### 2.1.6.3 Inference based on C++ prediction engine
The following table summarizes the accuracy metrics of the 3 configurations of ReID strong-baseline we provide on the Market1501 dataset,
PaddleClas provides an example of inference based on the C++ prediction engine, you can refer to [Server-side C++ prediction](../inference_deployment/cpp_deploy_en.md) to complete the corresponding inference deployment. If you are using the Windows platform, you can refer to the Visual Studio 2019 Community CMake Compilation Guide to complete the corresponding prediction library compilation and model prediction work.
Paddle Serving provides high-performance, flexible and easy-to-use industrial-grade online inference services. Paddle Serving supports RESTful, gRPC, bRPC and other protocols, and provides inference solutions in a variety of heterogeneous hardware and operating system environments. For more introduction to Paddle Serving, please refer to the Paddle Serving code repository.
PaddleClas provides an example of model serving deployment based on Paddle Serving. You can refer to [Model serving deployment](../inference_deployment/paddle_serving_deploy_en.md) to complete the corresponding deployment.
##### 2.1.8 Device side deployment
Paddle Lite is a high-performance, lightweight, flexible and easily extensible deep learning inference framework, positioned to support multiple hardware platforms including mobile, embedded and server. For more introduction to Paddle Lite, please refer to the Paddle Lite code repository.
PaddleClas provides an example of deploying models based on Paddle Lite. You can refer to [Deployment](../inference_deployment/paddle_lite_deploy_en.md) to complete the corresponding deployment.
##### 2.1.9 Paddle2ONNX Model Conversion and Prediction
Paddle2ONNX supports converting PaddlePaddle model format to ONNX model format. The deployment of Paddle models to various inference engines can be completed through ONNX, including TensorRT/OpenVINO/MNN/TNN/NCNN, and other inference engines or hardware that support the ONNX open source format. For more information about Paddle2ONNX, please refer to the Paddle2ONNX code repository.
PaddleClas provides an example of converting an inference model to an ONNX model and making inference prediction based on Paddle2ONNX. You can refer to [Paddle2ONNX model conversion and prediction](../../../deploy/paddle2onnx/readme.md) to complete the corresponding deployment work.
### 3. Summary
#### 3.1 Method summary and comparison
Note: The above reference indicators are obtained by using the author's open source code to train on our equipment for many times. Due to different system environments, torch versions, and CUDA versions, there may be slight differences with the indicators provided by the author.
The above algorithm can be quickly migrated to most ReID models, which can further improve the performance of ReID models.
#### 3.2 Usage advice/FAQ
The Market1501 dataset is relatively small, so you can try to train multiple times to get the highest accuracy.
#### 4 References
### 4. References
1.[Bag of Tricks and A Strong Baseline for Deep Person Re-identification](https://openaccess.thecvf.com/content_CVPRW_2019/papers/TRMTMCT/Luo_Bag_of_Tricks_and_a_Strong_Baseline_for_Deep_Person_CVPRW_2019_paper.pdf)
2.[michuanhaohao/reid-strong-baseline: Bag of Tricks and A Strong Baseline for Deep Person Re-identification (github.com)](https://github.com/michuanhaohao/reid-strong-baseline)
3.[Pedestrian Re-ID dataset Market1501 dataset _star_function blog-CSDN blog _market1501 dataset](https://blog.csdn.net/qq_39220334/article/details/121470106)
3.[Pedestrian Re-ID dataset Market1501Data set _star_function's blog - CSDN blog _market1501 data set](https://blog.csdn.net/qq_39220334/article/details/121470106)
4.[Deep Learning for Person Re-identification: A Survey and Outlook](https://arxiv.org/abs/2001.04193)
论文出处:[Bag of Tricks and A Strong Baseline for Deep Person Re-identification](https://openaccess.thecvf.com/content_CVPRW_2019/papers/TRMTMCT/Luo_Bag_of_Tricks_and_a_Strong_Baseline_for_Deep_Person_CVPRW_2019_paper.pdf)