class_docstr: Image Classification Dataset.
  
  
  This base class can be used to represent any image classification dataset which is stored in a way that meets the
  expectations of `torchvision.datasets.ImageFolder`. New image classification datasets can be derived from this
  similar to ImageNetDataset (imagenet.py) or Places365Dataset (places365.py) and overwrite the data transformations
  as needed. This dataset also supports sampling a random subset of the training set to be used for training. The
  subset size is determined by the arguments `dataset.num_samples_per_category` and `dataset.percentage_of_samples`
  in the input `opts`. Only one of these two should be specified. When `dataset.percentage_of_samples` is specified,
  data is sampled from all classes according to this percentage such that the distribution of classes does not change.
   The randomness in sampling is controlled by the `dataset.sample_selection_random_seed` in the input `opts`.
  
  Args:
      opts: An argparse.Namespace instance.
class_docstr_tok: ["Image","Classification","Dataset",".","This","base","class","can","be","used","to","represent","any","image","classification","dataset","which","is","stored","in","a","way","that","meets","the","expectations","of","`","torchvision.datasets.ImageFolder","`",".","New","image","classification","datasets","can","be","derived","from","this","similar","to","ImageNetDataset","(","imagenet.py",")","or","Places365Dataset","(","places365.py",")","and","overwrite","the","data","transformations","as","needed",".","This","dataset","also","supports","sampling","a","random","subset","of","the","training","set","to","be","used","for","training",".","The","subset","size","is","determined","by","the","arguments","`","dataset.num_samples_per_category","`","and","`","dataset.percentage_of_samples","`","in","the","input","`","opts","`",".","Only","one","of","these","two","should","be","specified",".","When","`","dataset.percentage_of_samples","`","is","specified",",","data","is","sampled","from","all","classes","according","to","this","percentage","such","that","the","distribution","of","classes","does","not","change",".","The","randomness","in","sampling","is","controlled","by","the","`","dataset.sample_selection_random_seed","`","in","the","input","`","opts","`",".","Args",":","opts",":","An","argparse.Namespace","instance","."]
code: def __init__(
      self,
      opts: argparse.Namespace,
      *args,
      **kwargs,
  ) -> None:
      BaseImageDataset.__init__(
          self,
          opts=opts,
          *args,
          **kwargs,
      )
      root = self.root
      ImageFolder.__init__(
          self,
          root=root,
          transform=None,
          target_transform=None,
          is_valid_file=None,
      )
  
      self.n_classes = len(list(self.class_to_idx.keys()))
      setattr(opts, "model.classification.n_classes", self.n_classes)
  
      for collate_fn in [
          "dataset.collate_fn_name_train",
          "dataset.collate_fn_name_val",
          "dataset.collate_fn_name_test",
      ]:
          if not hasattr(opts, collate_fn):
              setattr(
                  opts,
                  collate_fn,
                  "image_classification_data_collate_fn",
              )
  
      master = is_master(self.opts)
      if master:
          logger.log("Number of categories: {}".format(self.n_classes))
          logger.log(
              "Total number of samples: {}".format(len(self.samples))
          )
  
      num_samples_per_category = getattr(
          self.opts, "dataset.num_samples_per_category"
      )
      percentage_of_samples = getattr(
          self.opts, "dataset.percentage_of_samples"
      )
  
      if self.is_training and (
          num_samples_per_category > 0
          or (0 < percentage_of_samples < 100)
      ):
          if num_samples_per_category > 0 and (
              0 < percentage_of_samples < 100
          ):
              raise ValueError(
                  "Both `dataset.num_samples_per_category` and `dataset.percentage_of_samples` are specified. "
                  "Please specify only one."
              )
  
          random_seed = getattr(
              self.opts, "dataset.sample_selection_random_seed"
          )
          if num_samples_per_category > 0:
              selected_sample_indices = select_samples_by_category(
                  sample_category_labels=self.targets,
                  random_seed=random_seed,
                  num_samples_per_category=num_samples_per_category,
              )
              if master:
                  logger.log(
                      "Using {} samples per category.".format(
                          num_samples_per_category
                      )
                  )
          else:
              selected_sample_indices = select_samples_by_category(
                  sample_category_labels=self.targets,
                  random_seed=random_seed,
                  percentage_of_samples_per_category=percentage_of_samples,
              )
              if master:
                  logger.log(
                      "Using {} percentage of samples per category.".format(
                          percentage_of_samples
                      )
                  )
  
          self.samples = [
              self.samples[ind] for ind in selected_sample_indices
          ]
          self.imgs = [
              self.imgs[ind] for ind in selected_sample_indices
          ]
          self.targets = [
              self.targets[ind] for ind in selected_sample_indices
          ]
      elif master:
          logger.log("Using all samples in the dataset.")
  
imports: import argparse
  from typing import Any, Dict, Tuple, Union
  import torch
  from torchvision.datasets import ImageFolder
  from data.datasets.dataset_base import BaseImageDataset
  from data.datasets.utils.common import select_samples_by_category
  from data.transforms import image_pil
  from data.transforms.common import Compose
  from utils import logger
  from utils.ddp_utils import is_master
label_desc: Use image node_id 1 to create a new BaseImageClassificationDataset object from inherited base classes: BaseImageDataset, ImageFolder with example: obj = BaseImageClassificationDataset(opts)