docstr: Modulate the data ratio in the batch.
  For example, when select_data is "MJ-ST" and batch_ratio is "0.5-0.5",
  the 50% of the batch is filled with MJ and the other 50% of the batch is filled with ST.
docstr_tok: ["Modulate","the","data","ratio","in","the","batch",".","For","example",",","when","select_data","is","``","MJ-ST","''","and","batch_ratio","is","``","0.5-0.5","''",",","the","50","%","of","the","batch","is","filled","with","MJ","and","the","other","50","%","of","the","batch","is","filled","with","ST","."]
code: def __init__(self, opt):
      """
      Modulate the data ratio in the batch.
      For example, when select_data is "MJ-ST" and batch_ratio is "0.5-0.5",
      the 50% of the batch is filled with MJ and the other 50% of the batch is filled with ST.
      """
      log = open(
          f"./saved_models/{opt.experiment_name}/log_dataset.txt", "a"
      )
      dashed_line = "-" * 80
      print(dashed_line)
      log.write(dashed_line + "\n")
      print(
          f"dataset_root: {opt.train_data}\nopt.select_data: {opt.select_data}\nopt.batch_ratio: {opt.batch_ratio}"
      )
      log.write(
          f"dataset_root: {opt.train_data}\nopt.select_data: {opt.select_data}\nopt.batch_ratio: {opt.batch_ratio}\n"
      )
      assert len(opt.select_data) == len(opt.batch_ratio)
  
      _AlignCollate = AlignCollate(
          imgH=opt.imgH,
          imgW=opt.imgW,
          keep_ratio_with_pad=opt.PAD,
          contrast_adjust=opt.contrast_adjust,
      )
      self.data_loader_list = []
      self.dataloader_iter_list = []
      batch_size_list = []
      Total_batch_size = 0
      for selected_d, batch_ratio_d in zip(
          opt.select_data, opt.batch_ratio
      ):
          _batch_size = max(
              round(opt.batch_size * float(batch_ratio_d)), 1
          )
          print(dashed_line)
          log.write(dashed_line + "\n")
          _dataset, _dataset_log = hierarchical_dataset(
              root=opt.train_data, opt=opt, select_data=[selected_d]
          )
          total_number_dataset = len(_dataset)
          log.write(_dataset_log)
  
          """
              The total number of data can be modified with opt.total_data_usage_ratio.
              ex) opt.total_data_usage_ratio = 1 indicates 100% usage, and 0.2 indicates 20% usage.
              See 4.2 section in our paper.
              """
          number_dataset = int(
              total_number_dataset * float(opt.total_data_usage_ratio)
          )
          dataset_split = [
              number_dataset,
              total_number_dataset - number_dataset,
          ]
          indices = range(total_number_dataset)
          _dataset, _ = [
              Subset(_dataset, indices[offset - length : offset])
              for offset, length in zip(
                  _accumulate(dataset_split), dataset_split
              )
          ]
          selected_d_log = f"num total samples of {selected_d}: {total_number_dataset} x {opt.total_data_usage_ratio} (total_data_usage_ratio) = {len(_dataset)}\n"
          selected_d_log += f"num samples of {selected_d} per batch: {opt.batch_size} x {float(batch_ratio_d)} (batch_ratio) = {_batch_size}"
          print(selected_d_log)
          log.write(selected_d_log + "\n")
          batch_size_list.append(str(_batch_size))
          Total_batch_size += _batch_size
  
          _data_loader = torch.utils.data.DataLoader(
              _dataset,
              batch_size=_batch_size,
              shuffle=True,
              num_workers=int(
                  opt.workers
              ),  # prefetch_factor=2,persistent_workers=True,
              collate_fn=_AlignCollate,
              pin_memory=True,
          )
          self.data_loader_list.append(_data_loader)
          self.dataloader_iter_list.append(iter(_data_loader))
  
      Total_batch_size_log = f"{dashed_line}\n"
      batch_size_sum = "+".join(batch_size_list)
      Total_batch_size_log += (
          f"Total_batch_size: {batch_size_sum} = {Total_batch_size}\n"
      )
      Total_batch_size_log += f"{dashed_line}"
      opt.batch_size = Total_batch_size
  
      print(Total_batch_size_log)
      log.write(Total_batch_size_log + "\n")
      log.close()
  
imports: import os
  import sys
  import re
  import six
  import math
  import torch
  import pandas
  from natsort import natsorted
  from PIL import Image
  import numpy
  from torch.utils.data import Dataset, ConcatDataset, Subset
  from torch._utils import _accumulate
  import torchvision.transforms
label_desc: Use image node_id 1 to create a new Batch_Balanced_Dataset object from inherited base classes: object with example: obj = Batch_Balanced_Dataset(opt)