当前位置: 首页 > AI > 文章内容页

行人重识别:基于度量学习的行人重识别算法

时间:2025-07-21    作者:游乐小编    

本文介绍行人重识别项目,基于Paddle最新Metric Learning模型开发,修复Bug、优化代码并增加多个BackBone及模型导出脚本。使用Market-1501数据集,经解压、预处理后,用arcmargin loss训练,可微调提升性能,评估用Recall@Rank-1指标,未微调模型达96.65%,还涉及预测与模型导出。

行人重识别:基于度量学习的行人重识别算法 - 游乐网

引入

行人重识别(Person Re-IDentification,简称Re-ID),也称行人再识别,是利用计算机视觉技术判断图像或者视频序列中是否存在特定行人的技术广泛被认为是一个图像检索的子问题。给定一个监控行人图像,检索跨设备下的该行人图像旨在弥补目前固定的摄像头的视觉局限,并可与行人检测/行人跟踪技术相结合,可广泛应用于智能视频监控、智能安保等领域具体任务为:一个区域有多个摄像头拍摄视频序列,ReID的要求对一个摄像头下感兴趣的行人,检索到该行人在其他摄像头下出现的所有图片

项目说明

本项目基于Paddle最新模型库中的Metric Learning模型开发修复了几个小Bug,清理和优化了代码增加了几个BackBone:GhostNet、MobileNetV3、ResNetVd增加模型导出脚本,导出模型可用于多目标追踪:DeepSort_Paddle项目

Metric Learning

Metric Learning即度量学习也叫作相似度学习将度量学习应用于图像领域,就可以用作图像的相似度评估行人重识别恰好可以通过评估两张行人图像的相似度来实现所以本次就使用Paddle最新模型库中的Metric Learning模型实现一个简单的行人重识别算法

数据集介绍

本次使用的数据集是经典的行人重识别数据集Market-1501Market-1501 数据集在清华大学校园中采集,夏天拍摄,在 2015 年构建并公开它包括由6个摄像头(其中5个高清摄像头和1个低清摄像头)拍摄到的 1501 个行人、32668 个检测到的行人矩形框每个行人至少由2个摄像头捕获到,并且在一个摄像头中可能具有多张图像训练集有 751 人,包含 12,936 张图像,平均每个人有 17.2 张训练数据;测试集有 750 人,包含 19,732 张图像,平均每个人有 26.3 张测试数据3368 张查询图像的行人检测矩形框是人工绘制的,而 gallery 中的行人检测矩形框则是使用DPM检测器检测得到的该数据集提供的固定数量的训练集和测试集均可以在single-shot或multi-shot测试设置下使用

解压数据集

In [ ]
%cd ~/work/metric_learning/data!tar -xf ~/data/data1884/Market-1501-v15.09.15.tar!mv Market-1501-v15.09.15 Market-1501
登录后复制        
/home/aistudio/work/metric_learning/data
登录后复制        

数据预处理

生成数据列表分割训练集和验证集In [ ]
%cd ~import osimport randomimgs = os.listdir('work/metric_learning/data/Market-1501/gt_bbox')imgs.sort()image_id = 1super_class_id = 1class_id = 1class_dict = {}for img in imgs:    if 'jpg' in img:        person_id = img[:4]        if person_id not in class_dict:            class_dict[person_id] = class_id            class_id += 1total = []for img in imgs:    if 'jpg' in img:        person_id = img[:4]        path = 'gt_bbox/'+img        line = '%s %s %s %s\n' % (image_id, class_dict[person_id], super_class_id, path)        image_id += 1        total.append(line)# random.shuffle(total)train = total[:-1014]dev = total[-1014:]with open('work/metric_learning/data/Market-1501/train.txt', 'w', encoding='UTF-8') as f:    f.write('image_id class_id super_class_id path\n')    for line in train:        f.write(line)    with open('work/metric_learning/data/Market-1501/test.txt', 'w', encoding='UTF-8') as f:    f.write('image_id class_id super_class_id path\n')    for line in dev:        f.write(line)
登录后复制        
/home/aistudio
登录后复制        

下载预训练模型

这里使用ResNet50_vd做演示可根据需要选择其他更合适的BackBone其他预训练模型可在PaddleClas套件的模型库中找到In [ ]
%cd ~/work/metric_learning/pretrained_model!wget https://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_vd_ssld_v2_pretrained.tar!tar -xf ResNet50_vd_ssld_v2_pretrained.tar
登录后复制        
/home/aistudio/work/metric_learning/pretrained_model--2020-11-18 00:03:50--  https://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_vd_ssld_v2_pretrained.tarResolving paddle-imagenet-models-name.bj.bcebos.com (paddle-imagenet-models-name.bj.bcebos.com)... 182.61.200.195, 182.61.200.229, 2409:8c00:6c21:10ad:0:ff:b00e:67dConnecting to paddle-imagenet-models-name.bj.bcebos.com (paddle-imagenet-models-name.bj.bcebos.com)|182.61.200.195|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 95165219 (91M) [application/x-tar]Saving to: ‘ResNet50_vd_ssld_v2_pretrained.tar’ResNet50_vd_ssld_v2 100%[===================>]  90.76M  48.0MB/s    in 1.9s    2020-11-18 00:03:52 (48.0 MB/s) - ‘ResNet50_vd_ssld_v2_pretrained.tar’ saved [95165219/95165219]
登录后复制        

模型训练

使用softmax loss或者arcmargin loss进行模型训练In [ ]
%cd ~/work/metric_learning!python train_elem.py \    --model ResNet50_vd \    --embedding_size 128 \    --train_batch_size 256 \    --test_batch_size 256 \    --image_shape 3,128,128 \    --class_dim 1421 \    --lr 0.1 \    --lr_strategy piecewise_decay \    --lr_steps 5000,7000,9000 \    --total_iter_num 10000 \    --display_iter_step 10 \    --test_iter_step 500 \    --save_iter_step 500 \    --use_gpu True \    --pretrained_model pretrained_model/ResNet50_vd_ssld_v2_pretrained \    --model_save_dir save_elem_model \    --loss_name arcmargin \    --arc_scale 80.0 \    --arc_margin 0.15 \    --arc_easy_margin False
登录后复制    

模型微调

上述训练完成后还可对模型进行微调,进一步提升性能In [ ]
%cd ~/work/metric_learning!python train_pair.py \    --model ResNet50_vd \    --embedding_size 128 \    --train_batch_size 64 \    --test_batch_size 64 \    --image_shape 3,128,128 \    --class_dim 1421 \    --lr 0.0001 \    --lr_strategy piecewise_decay \    --lr_steps 3000,6000,9000 \    --total_iter_num 10000 \    --display_iter_step 10 \    --test_iter_step 500 \    --save_iter_step 500 \    --use_gpu True \    --pretrained_model save_elem_model/ResNet50_vd/10000 \    --model_save_dir save_pair_model \    --loss_name eml \    --samples_each_class 4 \    --margin 0.1 \    --npairs_reg_lambda 0.01
登录后复制    

模型评估

对模型的检索效果进行评估,这里使用Recall@Rank-1作为评估指标In [5]
%cd ~/work/metric_learning# arcmargin未微调模型!python eval.py \    --model ResNet50_vd \    --embedding_size 128 \    --batch_size 256 \    --image_shape 3,128,128 \    --use_gpu True \    --pretrained_model pretrained_model/best_model_no_finetune
登录后复制        
/home/aistudio/work/metric_learningYou are using Paddle compiled with TensorRT, but TensorRT dynamic library is not found. Ignore this if TensorRT is not needed.-----------  Configuration Arguments -----------batch_size: 256embedding_size: 128image_shape: 3,128,128model: ResNet50_vdpretrained_model: pretrained_model/best_model_no_finetuneuse_gpu: 1------------------------------------------------W1118 00:06:56.248965  7755 device_context.cc:338] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.1, Runtime API Version: 10.1W1118 00:06:56.254372  7755 device_context.cc:346] device: 0, cuDNN Version: 7.6.val dataset size: 80[2020-11-18 00:07:01] testbatch 0, time 0.14 sec[2020-11-18 00:07:05] End test 1014, test_recall 0.96647
登录后复制        

模型预测

使用模型输出预测图像的特征编码In [2]
%cd ~/work/metric_learning!python infer.py \    --model ResNet50_vd \    --embedding_size 128 \    --batch_size 1 \    --image_shape 3,128,128 \    --use_gpu True \    --pretrained_model pretrained_model/best_model_no_finetune
登录后复制    

模型导出

In [6]
%cd ~/work/metric_learning!python export_model.py \    --model ResNet50_vd \    --embedding_size 128 \    --image_shape 3,128,128 \    --use_gpu True \    --pretrained_model=pretrained_model/best_model_no_finetune \    --model_save_dir=save_inference_model
登录后复制        
/home/aistudio/work/metric_learningYou are using Paddle compiled with TensorRT, but TensorRT dynamic library is not found. Ignore this if TensorRT is not needed.-----------  Configuration Arguments -----------embedding_size: 128image_shape: 3,128,128model: ResNet50_vdmodel_save_dir: save_inference_modelpretrained_model: pretrained_model/best_model_no_finetuneuse_gpu: 1------------------------------------------------W1118 00:07:26.484359  8029 device_context.cc:338] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.1, Runtime API Version: 10.1W1118 00:07:26.489738  8029 device_context.cc:346] device: 0, cuDNN Version: 7.6.Saving the inference model...Finish.
登录后复制        

总结

由于微调训练速度较慢,暂未有训练完成的模型训练完成的模型效果(Recall@Rank-1):

热门推荐

更多

热门文章

更多

首页  返回顶部

本站所有软件都由网友上传,如有侵犯您的版权,请发邮件youleyoucom@outlook.com