docstr: Create a dataset from a folder of images.
  If you pass in a root directory it will be searched for images
  ending in ext (ext can be a list)
docstr_tok: ["Create","a","dataset","from","a","folder","of","images",".","If","you","pass","in","a","root","directory","it","will","be","searched","for","images","ending","in","ext","(","ext","can","be","a","list",")"]
code: def __init__(
      self,
      root_dir,
      caption_file=None,
      image_transforms=[],
      ext="jpg",
      default_caption="",
      postprocess=None,
      return_paths=False,
  ) -> None:
      """Create a dataset from a folder of images.
      If you pass in a root directory it will be searched for images
      ending in ext (ext can be a list)
      """
      self.root_dir = Path(root_dir)
      self.default_caption = default_caption
      self.return_paths = return_paths
      if isinstance(postprocess, DictConfig):
          postprocess = instantiate_from_config(postprocess)
      self.postprocess = postprocess
      if caption_file is not None:
          with open(caption_file, "rt") as f:
              ext = Path(caption_file).suffix.lower()
              if ext == ".json":
                  captions = json.load(f)
              elif ext == ".jsonl":
                  lines = f.readlines()
                  lines = [json.loads(x) for x in lines]
                  captions = {
                      x["file_name"]: x["text"].strip("\n")
                      for x in lines
                  }
              else:
                  raise ValueError(f"Unrecognised format: {ext}")
          self.captions = captions
      else:
          self.captions = None
  
      if not isinstance(ext, (tuple, list, ListConfig)):
          ext = [ext]
  
      # Only used if there is no caption file
      self.paths = []
      for e in ext:
          self.paths.extend(list(self.root_dir.rglob(f"*.{e}")))
      if isinstance(image_transforms, ListConfig):
          image_transforms = [
              instantiate_from_config(tt) for tt in image_transforms
          ]
      image_transforms.extend(
          [
              transforms.ToTensor(),
              transforms.Lambda(
                  lambda x: rearrange(x * 2.0 - 1.0, "c h w -> h w c")
              ),
          ]
      )
      image_transforms = transforms.Compose(image_transforms)
      self.tform = image_transforms
  
imports: import json
  from pathlib import Path
  from typing import Dict
  import torch
  from datasets import load_dataset
  from einops import rearrange
  from ldm.util import instantiate_from_config
  from omegaconf import DictConfig, ListConfig
  from PIL import Image
  from torch.utils.data import Dataset
  from torchvision import transforms
label_desc: Use image node_id 1 to create a new FolderData object from inherited base classes: Dataset with example: obj = FolderData(root_dir, caption_file, image_transforms, ext, default_caption, postprocess, return_paths)