Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
806ec9a1
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 1 年 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
806ec9a1
编写于
5月 31, 2021
作者:
F
Felix
提交者:
GitHub
5月 31, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add files via upload
上级
d3e233de
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
76 addition
and
0 deletion
+76
-0
ppcls/data/samplers/DistributedRandomIdentitySampler.py
ppcls/data/samplers/DistributedRandomIdentitySampler.py
+76
-0
未找到文件。
ppcls/data/samplers/DistributedRandomIdentitySampler.py
0 → 100644
浏览文件 @
806ec9a1
from
__future__
import
absolute_import
from
__future__
import
division
from
collections
import
defaultdict
import
numpy
as
np
import
copy
import
random
from
paddle.io
import
DistributedBatchSampler
,
Sampler
class
DistributedRandomIdentitySampler
(
DistributedBatchSampler
):
"""
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).
- num_instances (int): number of instances per identity in a batch.
- batch_size (int): number of examples in a batch.
"""
def
__init__
(
self
,
dataset
,
batch_size
,
num_instances
,
drop_last
,
**
args
):
self
.
dataset
=
dataset
self
.
batch_size
=
batch_size
self
.
num_instances
=
num_instances
self
.
drop_last
=
drop_last
self
.
num_pids_per_batch
=
self
.
batch_size
//
self
.
num_instances
self
.
index_dic
=
defaultdict
(
list
)
for
index
,
pid
in
enumerate
(
self
.
dataset
.
labels
):
self
.
index_dic
[
pid
].
append
(
index
)
self
.
pids
=
list
(
self
.
index_dic
.
keys
())
# estimate number of examples in an epoch
self
.
length
=
0
for
pid
in
self
.
pids
:
idxs
=
self
.
index_dic
[
pid
]
num
=
len
(
idxs
)
if
num
<
self
.
num_instances
:
num
=
self
.
num_instances
self
.
length
+=
num
-
num
%
self
.
num_instances
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
)
_sample_iter
=
iter
(
final_idxs
)
batch_indices
=
[]
for
idx
in
_sample_iter
:
batch_indices
.
append
(
idx
)
if
len
(
batch_indices
)
==
self
.
batch_size
:
yield
batch_indices
batch_indices
=
[]
if
not
self
.
drop_last
and
len
(
batch_indices
)
>
0
:
yield
batch_indices
def
__len__
(
self
):
if
self
.
drop_last
:
return
self
.
length
//
self
.
batch_size
else
:
return
(
self
.
length
+
self
.
batch_size
-
1
)
//
self
.
batch_size
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录