本文整理汇总了Python中mxnet.gluon.data.Sampler方法的典型用法代码示例。如果您正苦于以下问题:Python data.Sampler方法的具体用法?Python data.Sampler怎么用?Python data.Sampler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.gluon.data
的用法示例。
在下文中一共展示了data.Sampler方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from mxnet.gluon import data [as 别名]
# 或者: from mxnet.gluon.data import Sampler [as 别名]
def __init__(self, batch_size, cls_idx_dict1, cls_idx_dict2, ratio=1):
"""
Balanced Two Steam Sampler, use cls_idx_dict1 as main dictinary and list
:param batch_size: batch size
:param cls_idx_dict1: class index dictionary
:param cls_idx_dict2: class index dictionary
:param ratio: negative / positive flag
"""
self.batch_size = batch_size
self.cls_idx_dict1 = cls_idx_dict1
self.cls_idx_dict2 = cls_idx_dict2
self.ratio = ratio
assert set(cls_idx_dict1.keys()) == set(cls_idx_dict2.keys()), 'The labels of two classes are not consistent'
self.n_cls = len(cls_idx_dict1.keys())
self.n_samples = self.batch_size // self.n_cls
assert self.batch_size >= self.n_cls, "batch size should equal or larger than number of classes"
self.length = self.cal_len()
示例2: forward
# 需要导入模块: from mxnet.gluon import data [as 别名]
# 或者: from mxnet.gluon.data import Sampler [as 别名]
def forward(self, matches, ious):
"""Quota Sampler
Parameters:
----------
matches : NDArray or Symbol
Matching results, positive number for positive matching, -1 for not matched.
ious : NDArray or Symbol
IOU overlaps with shape (N, M), batching is supported.
Returns:
--------
NDArray or Symbol
Sampling results with same shape as ``matches``.
1 for positive, -1 for negative, 0 for ignore.
"""
F = mx.nd
max_pos = int(round(self._pos_ratio * self._num_sample))
max_neg = int(self._neg_ratio * self._num_sample)
results = []
for i in range(matches.shape[0]):
# init with 0s, which are ignored
result = F.zeros_like(matches[0])
# positive samples
ious_max = ious.max(axis=-1)[i]
result = F.where(matches[i] >= 0, F.ones_like(result), result)
result = F.where(ious_max >= self._pos_thresh, F.ones_like(result), result)
# negative samples with label -1
neg_mask = ious_max < self._neg_thresh_high
neg_mask = neg_mask * (ious_max >= self._neg_thresh_low)
result = F.where(neg_mask, F.ones_like(result) * -1, result)
# re-balance if number of positive or negative exceed limits
result = result.asnumpy()
num_pos = int((result > 0).sum())
if num_pos > max_pos:
disable_indices = np.random.choice(
np.where(result > 0)[0], size=(num_pos - max_pos), replace=False)
result[disable_indices] = 0 # use 0 to ignore
num_neg = int((result < 0).sum())
if self._fill_negative:
# if pos_sample is less than quota, we can have negative samples filling the gap
max_neg = max(self._num_sample - min(num_pos, max_pos), max_neg)
if num_neg > max_neg:
disable_indices = np.random.choice(
np.where(result < 0)[0], size=(num_neg - max_neg), replace=False)
result[disable_indices] = 0
results.append(mx.nd.array(result))
return mx.nd.stack(*results, axis=0)