ReID pedestrian re-identification (training + detection, with code), can be used for image retrieval, stranger retrieval and other projects

Using ReID and target detection to detect videos can re-identify people in videos, and support replacement of data sets for vehicle re-identification, etc. Can be applied to image, video retrieval, pedestrian tracking , etc.

When I was learning ReID before, I followed the following video to learn. The paper and code can also refer to GitHub - michuanhaohao/ReID_tutorial_slides: "Deep Learning and Pedestrian Re-Identification" Courseware "Deep Learning and Pedestrian Re-Identification" Dr. Luo Hao from Zhejiang University _哔哩哔哩_bilibili https://www.bilibili.com/video/BV1Pg4y1q7sN?from=search&seid=12319613973768358764&spm_id_from=333.337.0.0

The theoretical part is not described here. The explanation is very clear in the video. You can follow the video to learn. Here we only talk about the use of code.

The above code can perform normal training, but there is no video or image detection code, and then when looking for relevant information, I found that this article combines yolov3 and ReID, but there is no training code in this article https: // zhuanlan.zhihu.com/p/82398949

To this end, the codes of the above two projects have been integrated , thanks to the contributions of the above two bloggers. I made a slight modification to the code, and I also trained the se_renext50 network, which can be detected normally. Videos or images can be retrieved, and the effect is as follows [It can be seen that even after occlusion, the retrieval can continue]:

Table of contents

train 

detection


train 

step 1:

config folder:

        In defaults.py (some default configuration)

GPU settings:

        _C.MODEL.DEVICE = "cuda" whether to use GPU

        _C.MODEL.DEVICE_ID = '0' GPU ID

Network settings:

         _C.MODEL.NAME = 'resnet50_ibn_a'

        _C.MODEL.PRETRAIN_PATH =r'./pretrained.pth' # Pre-weighted path (the selected weight should correspond to your model above)

Hyperparameter settings:

         _C.SOLVER.OPTIMIZER_NAME = "Adam" # Select optimizer
        _C.SOLVER.MAX_EPOCHS = 120 # Training maximum epoch number
        _C.SOLVER.BASE_LR = 3e-4 # Initial learning rate

Step 2:

configs folder:

        softmax_triplet_with_center.yml中

        # in softmax_triplet.yml (the loss function used in this code training)

MODEL:

        PRETRAIN_PATH: # Pre-training weight (I put this weight in ReID/weights/r50_ibn_2.pth, this can be changed according to your actual situation)

SOLVER:

        OPTIMIZER_NAME: 'Adam' # optimizer

        MAX_EPOCHS: 120 # Maximum epoch

        BASE_LR: 0.00035 # initial learning rate

Mainly set the weight storage period and record log and eval period [I set it to 1, so that the log and weight will be saved once every round, and the mAP and rank will be calculated once every round]

        CHECKPOINT_PERIOD: 1

        LOG_PERIOD: 1

        EVAL_PERIOD: 1

OUTPUT_DIR:r'./logs' # output path

Step 3:

The data folder is used to store the Market1501 dataset

Step 4:

Enter the command to start training:

tools/train.py --config_file='configs/softmax_triplet.yml' MODEL.DEVICE_ID "('0')" DATASETS.NAMES "('market1501')" DATASETS.ROOT_DIR "(r'./data')" OUTPUT_DIR "('E:/ReID/logs')"
=> Market1501 loaded
Dataset statistics:
  ----------------------------------------
  subset   | # ids | # images | # cameras
  ----------------------------------------
  train    |   751 |    12936 |         6
  query    |   750 |     3368 |         6
  gallery  |   751 |    15913 |         6
  ----------------------------------------
Loading pretrained ImageNet model......
2022-02-18 16:17:54,983 reid_baseline.train INFO: Epoch[1] Iteration[1/1484] Loss: 7.667, Acc: 0.000, Base Lr: 3.82e-05
2022-02-18 16:17:55,225 reid_baseline.train INFO: Epoch[1] Iteration[2/1484] Loss: 7.671, Acc: 0.000, Base Lr: 3.82e-05
2022-02-18 16:17:55,436 reid_baseline.train INFO: Epoch[1] Iteration[3/1484] Loss: 7.669, Acc: 0.003, Base Lr: 3.82e-05
2022-02-18 16:17:55,646 reid_baseline.train INFO: Epoch[1] Iteration[4/1484] Loss: 7.663, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:55,856 reid_baseline.train INFO: Epoch[1] Iteration[5/1484] Loss: 7.663, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:56,069 reid_baseline.train INFO: Epoch[1] Iteration[6/1484] Loss: 7.658, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:56,277 reid_baseline.train INFO: Epoch[1] Iteration[7/1484] Loss: 7.654, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:56,490 reid_baseline.train INFO: Epoch[1] Iteration[8/1484] Loss: 7.660, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:56,699 reid_baseline.train INFO: Epoch[1] Iteration[9/1484] Loss: 7.653, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:56,906 reid_baseline.train INFO: Epoch[1] Iteration[10/1484] Loss: 7.651, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:57,110 reid_baseline.train INFO: Epoch[1] Iteration[11/1484] Loss: 7.645, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:57,316 reid_baseline.train INFO: Epoch[1] Iteration[12/1484] Loss: 7.643, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:57,526 reid_baseline.train INFO: Epoch[1] Iteration[13/1484] Loss: 7.644, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:57,733 reid_baseline.train INFO: Epoch[1] Iteration[14/1484] Loss: 7.638, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:57,942 reid_baseline.train INFO: Epoch[1] Iteration[15/1484] Loss: 7.634, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:58,148 reid_baseline.train INFO: Epoch[1] Iteration[16/1484] Loss: 7.630, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:58,355 reid_baseline.train INFO: Epoch[1] Iteration[17/1484] Loss: 7.634, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:58,564 reid_baseline.train INFO: Epoch[1] Iteration[18/1484] Loss: 7.627, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:58,770 reid_baseline.train INFO: Epoch[1] Iteration[19/1484] Loss: 7.629, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:58,980 reid_baseline.train INFO: Epoch[1] Iteration[20/1484] Loss: 7.624, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:59,186 reid_baseline.train INFO: Epoch[1] Iteration[21/1484] Loss: 7.619, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:59,397 reid_baseline.train INFO: Epoch[1] Iteration[22/1484] Loss: 7.614, Acc: 0.002, Base Lr: 3.82e-05
2022-02-18 16:17:59,605 reid_baseline.train INFO: Epoch[1] Iteration[23/1484] Loss: 7.608, Acc: 0.002, Base Lr: 3.82e-05
··············································

 After training, the trained weights will be saved in the logs file

Note:

I use softmax_triplet.yml, if you use this, just change the PRETRAIN_PATH pre-weight in the file to your path, because I chose the resnet50_ibn_a model (the model selection is in ReID/config/defaults.py/ _C.MODEL.NAME in ), so the PRETRAIN_PATH is filled with the r50_ibn_2.pth weight. If you choose the resnet50 network, then your pre-weight should also choose the corresponding weight [attach some official links of the weight]. In fact, there is a place in the code to automatically download the weights, but some people download them from the official website of pytorch, which will lead to poor Internet access. If you can download them through the built-in url, the weights will be stored in the C drive by default. So I changed the code a bit, downloaded it and put it in my project, and just modify the path in the configuration file when loading.

'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',

detection

Go to the person_search folder

Detect image:

Put the image to be detected into the query folder, and put the image or video to be detected under data/sampes/ [note that the image to be detected and the query image here are different, just like you want to find one from a bunch of pictures or videos People, you now have a photo of this person, put it in the query, then the program can retrieve this person from a bunch of pictures or videos under the sampers file],

Set up search.py. images is the picture or video that needs to be detected, and dist_thres is the distance between two samples calculated in the ReID metric matching. If the distance is less than this, it means that the similarity is very high . This needs to be manually adjusted according to different videos.

def detect(cfg,
           data,
           weights,
           images='data/samples',  # input folder
           output='output',  # output folder
           fourcc='mp4v',  # video codec
           img_size=416,
           conf_thres=0.5,
           nms_thres=0.5,
           dist_thres=1.0,  # 距离阈值
           save_txt=False,
           save_images=True):

run search.py

The final search results will be output to output [Note: If you put pictures in the query, the naming format needs to be the same as market1501]

Detection video:

Set the video path first in query_get.py

After running, press the space bar (by frame) to continue playing the video, press the left mouse button to take a screenshot (the image will be automatically saved and named under the query file)

Similarly, put the video to be detected into data/samples, run search.py ​​after setting the parameters, and output the detected results to the person_search/output file.

--------------------------------------------------------------------------------------------------------------------------------

[Instructions for modifying the original code]

engine folder: trainer.py is mainly to define some training functions to save network weights on the basis of the original code . The weight of the original code is to save the parameter settings such as the optimizer as weights, which are not needed for loading into the original network. , will report keys error, so I directly save the network weights for easy loading

Modeling folder baseline.py If you modify the backbone network [to facilitate future magic modification] , keys error will be reported when reloading weights, so adding the following code can solve this problem pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict.keys() == pretrained_dict.keys()}

Code and weight Baidu cloud: https://pan.baidu.com/s/1p5C5mCVxGK61_eYc7HHpHA 
Extraction code: yypn 

Or visit github:https://github.com/YINYIPENG-EN/ReID.git https://github.com/YINYIPENG-EN/ReID.git

I have to emphasize it again here! ! ! The ReID/config/defaults.py and person_search/reid/config/defaults.py configuration files in training are somewhat different. Be sure to pay attention when running the search.py ​​file, otherwise an error will be reported!

Guess you like

Origin blog.csdn.net/z240626191s/article/details/123004326