class_docstr: Performs Textual Inversion fine-tuning on a Determined cluster.
class_docstr_tok: ["Performs","Textual","Inversion","fine-tuning","on","a","Determined","cluster","."]
code: def __init__(
      self,
      img_dirs: Union[str, Sequence[str]],
      concept_strs: Union[str, Sequence[str]],
      initializer_strs: Union[str, Sequence[str]],
      learnable_properties: Sequence[Literal["object", "style"]],
      pretrained_model_name_or_path: str = "CompVis/stable-diffusion-v1-4",
      train_batch_size: int = 1,
      gradient_accumulation_steps: int = 4,
      optimizer_name: Literal["adam", "adamw", "sgd"] = "adam",
      learning_rate: float = 5e-04,
      other_optimizer_kwargs: Optional[dict] = None,
      scale_lr: bool = True,
      checkpoint_freq: int = 50,
      metric_report_freq: int = 50,
      beta_start: float = 0.00085,
      beta_end: float = 0.012,
      beta_schedule: Literal[
          "linear", "scaled_linear", "squaredcos_cap_v2"
      ] = "scaled_linear",
      num_train_timesteps: int = 1000,
      train_seed: int = 2147483647,
      norm_reg_weight: Optional[float] = None,
      hidden_reg_weight: Optional[float] = None,
      img_size: int = 512,
      interpolation: Literal[
          "nearest", "bilinear", "bicubic"
      ] = "bicubic",
      flip_p: float = 0.0,
      center_crop: bool = True,
      append_file_name_to_text: bool = False,
      file_name_split_char: str = "_",
      num_blank_prompts: int = 0,
      num_a_prompts: int = 0,
      generate_training_images: bool = True,
      inference_batch_size: int = 1,
      num_pipeline_calls: int = 1,
      inference_prompts: Optional[Union[str, Sequence[str]]] = None,
      inference_scheduler_name: Literal[
          "ddim", "lms-discrete", "pndm"
      ] = "pndm",
      num_inference_steps: int = 50,
      guidance_scale: float = 7.5,
      main_process_generator_seed: int = 2147483647,
      other_inference_scheduler_kwargs: Optional[dict] = None,
  ) -> None:
      # We assume that the Huggingface User Access token has been stored as a HF_AUTH_TOKEN
      # environment variable. See https://huggingface.co/docs/hub/security-tokens
      try:
          self.use_auth_token = os.environ["HF_AUTH_TOKEN"]
      except KeyError:
          raise KeyError(
              "Please set your HF User Access token as the HF_AUTH_TOKEN environment variable."
          )
      self.logger = accelerate.logging.get_logger(__name__)
  
      self.pretrained_model_name_or_path = pretrained_model_name_or_path
  
      if isinstance(learnable_properties, str):
          learnable_properties = [learnable_properties]
      self.learnable_properties = learnable_properties
      if isinstance(concept_strs, str):
          concept_strs = [concept_strs]
      self.concept_strs = concept_strs
      if isinstance(initializer_strs, str):
          initializer_strs = [initializer_strs]
      self.initializer_strs = initializer_strs
      self.img_size = img_size
      self.interpolation = interpolation
      self.flip_p = flip_p
      self.center_crop = center_crop
      self.append_file_name_to_text = append_file_name_to_text
      self.file_name_split_char = file_name_split_char
      self.num_blank_prompts = num_blank_prompts
      self.num_a_prompts = num_a_prompts
  
      self.train_batch_size = train_batch_size
      self.gradient_accumulation_steps = gradient_accumulation_steps
      self._optim_dict = {
          "adam": torch.optim.Adam,
          "adamw": torch.optim.AdamW,
          "sgd": torch.optim.SGD,
      }
      if optimizer_name not in self._optim_dict:
          raise ValueError(
              f"Optimizer must be one of {list(self._optim_dict.keys())}."
          )
      self.optimizer_name = optimizer_name
      self.learning_rate = learning_rate
      self.other_optimizer_kwargs = other_optimizer_kwargs or {}
      self.scale_lr = scale_lr
      self.checkpoint_freq = checkpoint_freq
      self.metric_report_freq = metric_report_freq
      self.beta_start = beta_start
      self.beta_end = beta_end
      self.beta_schedule = beta_schedule
      self.num_train_timesteps = num_train_timesteps
      if isinstance(img_dirs, str):
          img_dirs = [img_dirs]
      self.img_dirs = img_dirs
      self.train_seed = train_seed
      self.norm_reg_weight = norm_reg_weight
      self.hidden_reg_weight = hidden_reg_weight
  
      self.accelerator = accelerate.Accelerator(
          gradient_accumulation_steps=self.gradient_accumulation_steps,
      )
      accelerate.utils.set_seed(self.train_seed)
  
      assert (
          inference_scheduler_name in defaults.NOISE_SCHEDULER_DICT
      ), (
          f"inference_scheduler must be one {list(defaults.NOISE_SCHEDULER_DICT.keys())},"
          f" but got {inference_scheduler_name}"
      )
      self.generate_training_images = generate_training_images
      self.inference_batch_size = inference_batch_size
      self.num_pipeline_calls = num_pipeline_calls
      if isinstance(inference_prompts, str):
          inference_prompts = [inference_prompts]
      self.inference_scheduler_name = inference_scheduler_name
      self.inference_prompts = inference_prompts
      self.num_inference_steps = num_inference_steps
      self.guidance_scale = guidance_scale
      self.main_process_generator_seed = main_process_generator_seed
  
      if other_inference_scheduler_kwargs is None:
          other_inference_scheduler_kwargs = (
              defaults.DEFAULT_SCHEDULER_KWARGS_DICT[
                  self.inference_scheduler_name
              ]
          )
      self.other_inference_scheduler_kwargs = (
          other_inference_scheduler_kwargs
      )
  
      self.steps_completed = 0
      self.metrics_history = defaultdict(list)
      self.last_mean_loss = None
  
      self.effective_global_batch_size = (
          self.gradient_accumulation_steps
          * self.train_batch_size
          * self.accelerator.num_processes
      )
      # If scale_lr, we linearly scale the bare learning rate by the effective batch size.
      if scale_lr:
          self.learning_rate *= self.effective_global_batch_size
          self.logger.info(
              f"Using scaled learning rate {self.learning_rate}"
          )
  
      # The trivial attrs below are instantiated as needed through private methods.
      self.tokenizer = None
      self.text_encoder = None
      self.vae = None
      self.unet = None
      self.safety_checker = None
      self.feature_extractor = None
      self.train_dataset = None
      self.train_dataloader = None
      self.optimizer = None
      self.train_scheduler = None
      self.inference_scheduler_kwargs = None
      self.pipeline = None
      self.init_embedding_norm_mean = None
  
      self.concept_to_initializer_strs_map = {}
      self.concept_to_initializer_ids_map = {}
      self.concept_to_dummy_strs_map = {}
      self.concept_to_dummy_ids_map = {}
  
      self._build_models()
      self._add_new_tokens_and_update_embeddings()
      self._freeze_layers()
      self._build_dataset_and_dataloader()
      self._build_optimizer()
      self._build_train_scheduler()
      self._wrap_and_prepare()
  
imports: import json
  import os
  import pathlib
  from collections import defaultdict
  from typing import List, Literal, Optional, Sequence, Union
  import accelerate
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from detsd import data, defaults, layers, utils
  from diffusers import AutoencoderKL, DDPMScheduler, StableDiffusionPipeline, UNet2DConditionModel
  from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
  from torch.utils.data import DataLoader
  from torch.utils.tensorboard import SummaryWriter
  from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
  import determined
  from determined.pytorch import TorchData
label_desc: Use image node_id 1 to create a new DetSDTextualInversionTrainer object  with example: obj = DetSDTextualInversionTrainer(img_dirs, concept_strs, initializer_strs, learnable_properties, pretrained_model_name_or_path, train_batch_size, gradient_accumulation_steps, optimizer_name, learning_rate, other_optimizer_kwargs, scale_lr, checkpoint_freq, metric_report_freq, beta_start, beta_end, beta_schedule, num_train_timesteps, train_seed, norm_reg_weight, hidden_reg_weight, img_size, interpolation, flip_p, center_crop, append_file_name_to_text, file_name_split_char, num_blank_prompts, num_a_prompts, generate_training_images, inference_batch_size, num_pipeline_calls, inference_prompts, inference_scheduler_name, num_inference_steps, guidance_scale, main_process_generator_seed, other_inference_scheduler_kwargs)