class_docstr: Sampler that restricts data loading to the label of the dataset.
  
  A class-aware sampling strategy to effectively tackle the
  non-uniform class distribution. The length of the training data is
  consistent with source data. Simple improvements based on `Relay
  Backpropagation for Effective Learning of Deep Convolutional
  Neural Networks <https://arxiv.org/abs/1512.05830>`_
  
  The implementation logic is referred to
  https://github.com/Sense-X/TSD/blob/master/mmdet/datasets/samplers/distributed_classaware_sampler.py
  
  Args:
      dataset: Dataset used for sampling.
      seed (int, optional): random seed used to shuffle the sampler.
          This number should be identical across all
          processes in the distributed group. Defaults to None.
      num_sample_class (int): The number of samples taken from each
          per-label list. Defaults to 1.
class_docstr_tok: ["Sampler","that","restricts","data","loading","to","the","label","of","the","dataset",".","A","class-aware","sampling","strategy","to","effectively","tackle","the","non-uniform","class","distribution",".","The","length","of","the","training","data","is","consistent","with","source","data",".","Simple","improvements","based","on","`","Relay","Backpropagation","for","Effective","Learning","of","Deep","Convolutional","Neural","Networks","<","https",":","\/\/arxiv.org\/abs\/1512.05830",">","`","_","The","implementation","logic","is","referred","to","https",":","\/\/github.com\/Sense-X\/TSD\/blob\/master\/mmdet\/datasets\/samplers\/distributed_classaware_sampler.py","Args",":","dataset",":","Dataset","used","for","sampling",".","seed","(","int",",","optional",")",":","random","seed","used","to","shuffle","the","sampler",".","This","number","should","be","identical","across","all","processes","in","the","distributed","group",".","Defaults","to","None",".","num_sample_class","(","int",")",":","The","number","of","samples","taken","from","each","per-label","list",".","Defaults","to","1","."]
code: def __init__(
      self,
      dataset: BaseDataset,
      seed: Optional[int] = None,
      num_sample_class: int = 1,
  ) -> None:
      rank, world_size = get_dist_info()
      self.rank = rank
      self.world_size = world_size
  
      self.dataset = dataset
      self.epoch = 0
      # Must be the same across all workers. If None, will use a
      # random seed shared among workers
      # (require synchronization among all workers)
      if seed is None:
          seed = sync_random_seed()
      self.seed = seed
  
      # The number of samples taken from each per-label list
      assert num_sample_class > 0 and isinstance(num_sample_class, int)
      self.num_sample_class = num_sample_class
      # Get per-label image list from dataset
      self.cat_dict = self.get_cat2imgs()
  
      self.num_samples = int(
          math.ceil(len(self.dataset) * 1.0 / world_size)
      )
      self.total_size = self.num_samples * self.world_size
  
      # get number of images containing each category
      self.num_cat_imgs = [len(x) for x in self.cat_dict.values()]
      # filter labels without images
      self.valid_cat_inds = [
          i for i, length in enumerate(self.num_cat_imgs) if length != 0
      ]
      self.num_classes = len(self.valid_cat_inds)
  
file_path: mmdetection/mmdet/datasets/samplers/class_aware_sampler.py
imports: import math
  from typing import Dict, Iterator, Optional, Union
  import numpy
  import torch
  from mmengine.dataset import BaseDataset
  from mmengine.dist import get_dist_info, sync_random_seed
  from torch.utils.data import Sampler
  from mmdet.registry import DATA_SAMPLERS
label_desc: Use image node_id 1 to create a new ClassAwareSampler object from inherited base classes: Sampler with example: obj = ClassAwareSampler(dataset, seed, num_sample_class)