Safemotion Lib
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
fastreid.data.samplers.data_sampler.TrainingSampler Class Reference
Inheritance diagram for fastreid.data.samplers.data_sampler.TrainingSampler:

Public Member Functions

 __init__ (self, int size, bool shuffle=True, Optional[int] seed=None)
 
 __iter__ (self)
 

Protected Member Functions

 _infinite_indices (self)
 

Protected Attributes

 _size
 
 _shuffle
 
 _seed
 
 _rank
 
 _world_size
 

Detailed Description

In training, we only care about the "infinite stream" of training data.
So this sampler produces an infinite stream of indices and
all workers cooperate to correctly shuffle the indices and sample different indices.
The samplers in each worker effectively produces `indices[worker_id::num_workers]`
where `indices` is an infinite stream of indices consisting of
`shuffle(range(size)) + shuffle(range(size)) + ...` (if shuffle is True)
or `range(size) + range(size) + ...` (if shuffle is False)

Definition at line 15 of file data_sampler.py.

Constructor & Destructor Documentation

◆ __init__()

fastreid.data.samplers.data_sampler.TrainingSampler.__init__ ( self,
int size,
bool shuffle = True,
Optional[int] seed = None )
Args:
    size (int): the total number of data of the underlying dataset to sample from
    shuffle (bool): whether to shuffle the indices or not
    seed (int): the initial seed of the shuffle. Must be the same
        across all workers. If None, will use a random seed shared
        among workers (require synchronization among all workers).

Definition at line 26 of file data_sampler.py.

26 def __init__(self, size: int, shuffle: bool = True, seed: Optional[int] = None):
27 """
28 Args:
29 size (int): the total number of data of the underlying dataset to sample from
30 shuffle (bool): whether to shuffle the indices or not
31 seed (int): the initial seed of the shuffle. Must be the same
32 across all workers. If None, will use a random seed shared
33 among workers (require synchronization among all workers).
34 """
35 self._size = size
36 assert size > 0
37 self._shuffle = shuffle
38 if seed is None:
39 seed = comm.shared_random_seed()
40 self._seed = int(seed)
41
42 self._rank = comm.get_rank()
43 self._world_size = comm.get_world_size()
44

Member Function Documentation

◆ __iter__()

fastreid.data.samplers.data_sampler.TrainingSampler.__iter__ ( self)

Definition at line 45 of file data_sampler.py.

45 def __iter__(self):
46 start = self._rank
47 yield from itertools.islice(self._infinite_indices(), start, None, self._world_size)
48

◆ _infinite_indices()

fastreid.data.samplers.data_sampler.TrainingSampler._infinite_indices ( self)
protected

Definition at line 49 of file data_sampler.py.

49 def _infinite_indices(self):
50 np.random.seed(self._seed)
51 while True:
52 if self._shuffle:
53 yield from np.random.permutation(self._size)
54 else:
55 yield from np.arange(self._size)
56
57

Member Data Documentation

◆ _rank

fastreid.data.samplers.data_sampler.TrainingSampler._rank
protected

Definition at line 42 of file data_sampler.py.

◆ _seed

fastreid.data.samplers.data_sampler.TrainingSampler._seed
protected

Definition at line 40 of file data_sampler.py.

◆ _shuffle

fastreid.data.samplers.data_sampler.TrainingSampler._shuffle
protected

Definition at line 37 of file data_sampler.py.

◆ _size

fastreid.data.samplers.data_sampler.TrainingSampler._size
protected

Definition at line 35 of file data_sampler.py.

◆ _world_size

fastreid.data.samplers.data_sampler.TrainingSampler._world_size
protected

Definition at line 43 of file data_sampler.py.


The documentation for this class was generated from the following file: