class_docstr: A dataset to prepare the instance and class images with the prompts for fine-tuning the model.
  It pre-processes the images and the tokenizes prompts.
  
  :param instance_data_root: required, a directory with images files of the object
  :param instance_prompt: captions with special token associated with instance images
  :param with_prior_preservation: whether to regularize the model finetuning with the original inference output from the backbone
  :param reg_data_root: a directory to save inference images from the backbone
  :param  reg_prompt: prompt used to generate regularization images
  :param size: resizing images for training data pipeline
  :param center_crop: whether performing center cropping on input images
  :param load_cache_latents: when set to True, images will be converted to cached latents which will be directly loaded for training
  :param vae: vae instance to encode imamges from pixel space to latent space
class_docstr_tok: ["A","dataset","to","prepare","the","instance","and","class","images","with","the","prompts","for","fine-tuning","the","model",".","It","pre-processes","the","images","and","the","tokenizes","prompts",".",":","param","instance_data_root",":","required",",","a","directory","with","images","files","of","the","object",":","param","instance_prompt",":","captions","with","special","token","associated","with","instance","images",":","param","with_prior_preservation",":","whether","to","regularize","the","model","finetuning","with","the","original","inference","output","from","the","backbone",":","param","reg_data_root",":","a","directory","to","save","inference","images","from","the","backbone",":","param","reg_prompt",":","prompt","used","to","generate","regularization","images",":","param","size",":","resizing","images","for","training","data","pipeline",":","param","center_crop",":","whether","performing","center","cropping","on","input","images",":","param","load_cache_latents",":","when","set","to","True",",","images","will","be","converted","to","cached","latents","which","will","be","directly","loaded","for","training",":","param","vae",":","vae","instance","to","encode","imamges","from","pixel","space","to","latent","space"]
code: def __init__(
      self,
      instance_data_root,
      instance_prompt,
      with_prior_preservation=False,
      reg_data_root=None,
      reg_prompt=None,
      size=512,
      center_crop=True,
      repeat=10000,
      load_cache_latents=False,
      cached_instance_data_root=None,
      cached_reg_data_root=None,
      vae=None,
      text_encoder=None,
  ):
      self.size = size
      self.center_crop = center_crop
  
      assert (
          instance_data_root or cached_instance_data_root
      ), "must provide instance images to start training."
      self.instance_data_root = Path(instance_data_root)
      self.cached_instance_data_root = cached_instance_data_root
      self.cached_reg_data_root = cached_reg_data_root
  
      self.instance_images_path = list(
          Path(instance_data_root).iterdir()
      )
      self.num_instance_images = len(self.instance_images_path)
      self.instance_prompt = instance_prompt
      self._length = self.num_instance_images * repeat
      self.load_cache_latents = load_cache_latents
      self.with_prior_preservation = with_prior_preservation
  
      if reg_data_root is not None:
          self.reg_data_root = Path(reg_data_root)
          self.reg_images_path = list(self.reg_data_root.iterdir())
          self.num_reg_images = len(self.reg_images_path)
          self.reg_prompt = reg_prompt
      else:
          self.reg_data_root = None
  
      assert (
          TORCHVISION_AVAILABLE
      ), "Torchvision imports failed but they are required."
      self.image_transforms = transforms.Compose(
          [
              transforms.Resize(
                  size,
                  interpolation=transforms.InterpolationMode.BILINEAR,
              ),
              transforms.CenterCrop(size)
              if center_crop
              else transforms.RandomCrop(size),
              transforms.ToTensor(),
              transforms.Normalize([0.5], [0.5]),
          ]
      )
  
      if self.load_cache_latents:
          if (self.cached_instance_data_root is None) or (
              self.with_prior_preservation
              and self.cached_reg_data_root is None
          ):
              self.cache_latents(vae, text_encoder)
  
              self.cached_instance_data_root = (
                  f"{self.instance_data_root}_cached"
              )
              self.cached_reg_data_root = f"{self.reg_data_root}_cached"
              self.instance_images_path = list(
                  Path(self.cached_instance_data_root).iterdir()
              )
              self.num_instance_images = len(self.instance_images_path)
  
              if self.with_prior_preservation:
                  self.reg_images_path = list(
                      Path(self.cached_reg_data_root).iterdir()
                  )
                  self.num_reg_images = len(self.reg_images_path)
  
          if self.cached_instance_data_root:
              self.instance_images_path = list(
                  Path(self.cached_instance_data_root).iterdir()
              )
              self.num_instance_images = len(self.instance_images_path)
          if self.with_prior_preservation and self.cached_reg_data_root:
              self.reg_images_path = list(
                  Path(self.cached_reg_data_root).iterdir()
              )
              self.num_reg_images = len(self.reg_images_path)
  
file_path: NeMo/nemo/collections/multimodal/data/dreambooth/dreambooth_dataset.py
imports: import os
  from pathlib import Path
  import torch
  from PIL import Image
  from pytorch_lightning.utilities import rank_zero_only
  from torch.utils.data import Dataset
  from tqdm import tqdm
label_desc: Use image node_id 1 to create a new DreamBoothDataset object from inherited base classes: Dataset with example: obj = DreamBoothDataset(instance_data_root, instance_prompt, with_prior_preservation, reg_data_root, reg_prompt, size, center_crop, repeat, load_cache_latents, cached_instance_data_root, cached_reg_data_root, vae, text_encoder)