行人重识别:reid-strong-baseline-master(罗浩)---triplet_sampler.py(数据加载,迭代器构建)

本文主要是介绍行人重识别:reid-strong-baseline-master(罗浩)---triplet_sampler.py(数据加载,迭代器构建),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        首先,reid-strong-baseline代码是罗浩博士在CVPR2019发表的《Bag of Tricks and A Strong Baseline for Deep Person Re-identification》,相关代码链接如下:https://github.com/michuanhaohao/reid-strong-baseline。这篇论文对我启发蛮大,也是我入门的基础。我也是小白,代码功底也不是很好,入门比较慢,目前正在研读他的代码。我的代码是在market1501数据集上跑的。

以下所阐述的内容是从以下博客学习来的基于度量学习的ReID代码实现(1)和行人重识别02-06:fast-reid(BoT)-pytorch编程规范(fast-reid为例)3-迭代器构建,数据加载-1。它们对我启发很大。

一、triplet_sampler.py具体位置

        在reid-strong-baseline-master/tools/train.py文件中找到train函数的make_data_loader函数。

         make_data_loader函数在reid-strong-baseline-master/data/build.py文件中,然后在该函数找到RandomIdentitySampler类。

        RandomIdentifySampler类是在reid-strong-baseline-master/data/samplers/triplet_sampler.py文件中。

 二、triplet_sampler.py解析

注释如下:

"""
@author:  liaoxingyu
@contact: liaoxingyu2@jd.com
"""import copy
import random
import torch
from collections import defaultdictimport numpy as np
from torch.utils.data.sampler import Samplerclass RandomIdentitySampler(Sampler):"""【首先随机采集N个ID,然后每个ID选择K个实例图像】Randomly sample N identities, then for each identity,randomly sample K instances, therefore batch size is N*K.Args:【训练数据的列表,包含了所有训练的数据,也就是多个数据源】- data_source (list): list of (img_path, pid, camid).【在每个batch中,对每个ID采集num_instances图像】- num_instances (int): number of instances per identity in a batch.- batch_size (int): number of examples in a batch."""def __init__(self, data_source, batch_size, num_instances):# 【包含了多个数据集的训练信息,例如图片路径,身份ID,摄像头编号等】self.data_source = data_sourceself.batch_size = batch_size# 【对每个身份采集的图像数目,本文设置(num_instances=4)】self.num_instances = num_instances# 【通过计算获得每个batch需要采集多少个身份ID,16=64/4】self.num_pids_per_batch = self.batch_size // self.num_instances# 【(写了一个dic,dic的key是id,value是各id对应的图片序号)用于存储该图片 序列号 保存于字典,方便查找转换】self.index_dic = defaultdict(list)# 【循环把(key:id==>行人的id,即pid)(value:各个id对应的图片序号)数据保存上述字典中】for index, (_, pid, _) in enumerate(self.data_source):self.index_dic[pid].append(index)# 【把index_dic的键值(身份ID)保存于self.pids中】self.pids = list(self.index_dic.keys())# estimate number of examples in an epochself.length = 0for pid in self.pids:idxs = self.index_dic[pid]num = len(idxs)if num < self.num_instances:num = self.num_instancesself.length += num - num % self.num_instances# 【iter返回的是一个epoch的数据,是一个list】def __iter__(self):batch_idxs_dict = defaultdict(list)for pid in self.pids:idxs = copy.deepcopy(self.index_dic[pid])if len(idxs) < self.num_instances:idxs = np.random.choice(idxs, size=self.num_instances, replace=True)random.shuffle(idxs)batch_idxs = []for idx in idxs:batch_idxs.append(idx)if len(batch_idxs) == self.num_instances:batch_idxs_dict[pid].append(batch_idxs)batch_idxs = []avai_pids = copy.deepcopy(self.pids)final_idxs = []while len(avai_pids) >= self.num_pids_per_batch:selected_pids = random.sample(avai_pids, self.num_pids_per_batch)for pid in selected_pids:batch_idxs = batch_idxs_dict[pid].pop(0)final_idxs.extend(batch_idxs)if len(batch_idxs_dict[pid]) == 0:avai_pids.remove(pid)self.length = len(final_idxs)return iter(final_idxs)def __len__(self):return self.length

1.def __init__()函数中的data_source包含很多信息,调试结果如图:

 2.def __init__()函数中的num_pids_per_batch参数,很重要:

 3.def __init__()函数中for index,(_,pid,_) in enumerate(self.data_source)的解释:

        首先,通过for循环将行人id存储在字典里,调试后可看到index_dic字典内容:

【注:pid从0开始,750结束,pid一共751个,即751个人】

 更加直观从数据集看:

4.def __init__()函数中self.pids = list(self.index_dic.keys())调试如下:

 5.def __init__()函数中RandomIdentitySampler最终取到的值:

目录

一、triplet_sampler.py具体位置

 二、triplet_sampler.py解析


这篇关于行人重识别:reid-strong-baseline-master(罗浩)---triplet_sampler.py(数据加载,迭代器构建)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/572016

相关文章

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

一文带你搞懂Python中__init__.py到底是什么

《一文带你搞懂Python中__init__.py到底是什么》朋友们,今天我们来聊聊Python里一个低调却至关重要的文件——__init__.py,有些人可能听说过它是“包的标志”,也有人觉得它“没... 目录先搞懂 python 模块(module)Python 包(package)是啥?那么 __in

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用