code: def __init__(
      self,
      *,
      text=None,
      img=None,
      clip_encoding=None,
      lr=1e-5,
      batch_size=4,
      gradient_accumulate_every=4,
      save_every=100,
      image_width=512,
      num_layers=16,
      epochs=20,
      iterations=1050,
      save_progress=True,
      seed=None,
      open_folder=True,
      save_date_time=False,
      start_image_path=None,
      start_image_train_iters=10,
      start_image_lr=3e-4,
      theta_initial=None,
      theta_hidden=None,
      model_name="ViT-B/32",
      lower_bound_cutout=0.1,  # should be smaller than 0.8
      upper_bound_cutout=1.0,
      saturate_bound=False,
      averaging_weight=0.3,
      create_story=False,
      story_start_words=5,
      story_words_per_epoch=5,
      story_separator=None,
      gauss_sampling=False,
      gauss_mean=0.6,
      gauss_std=0.2,
      do_cutout=True,
      center_bias=False,
      center_focus=2,
      optimizer="AdamP",
      jit=True,
      hidden_size=256,
      save_gif=False,
      save_video=False,
  ):
      super().__init__()
  
      if exists(seed):
          tqdm.write(f"setting seed: {seed}")
          torch.manual_seed(seed)
          torch.cuda.manual_seed(seed)
          random.seed(seed)
          torch.backends.cudnn.deterministic = True
  
      # fields for story creation:
      self.create_story = create_story
      self.words = None
      self.separator = (
          str(story_separator) if story_separator is not None else None
      )
      if self.separator is not None and text is not None:
          # exit if text is just the separator
          if (
              str(text).replace(" ", "").replace(self.separator, "")
              == ""
          ):
              print(
                  "Exiting because the text only consists of the separator! Needs words or phrases that are separated by the separator."
              )
              exit()
          # adds a space to each separator and removes double spaces that might be generated
          text = (
              text.replace(self.separator, self.separator + " ")
              .replace("  ", " ")
              .strip()
          )
      self.all_words = text.split(" ") if text is not None else None
      self.num_start_words = story_start_words
      self.words_per_epoch = story_words_per_epoch
      if create_story:
          assert (
              text is not None
          ), "We need text input to create a story..."
          # overwrite epochs to match story length
          num_words = len(self.all_words)
          self.epochs = (
              1
              + (num_words - self.num_start_words)
              / self.words_per_epoch
          )
          # add one epoch if not divisible
          self.epochs = (
              int(self.epochs)
              if int(self.epochs) == self.epochs
              else int(self.epochs) + 1
          )
          if self.separator is not None:
              if self.separator not in text:
                  print(
                      "Separator '"
                      + self.separator
                      + "' will be ignored since not in text!"
                  )
                  self.separator = None
              else:
                  self.epochs = len(
                      list(filter(None, text.split(self.separator)))
                  )
          print(
              "Running for",
              self.epochs,
              "epochs"
              + (
                  " (split with '"
                  + self.separator
                  + "' as the separator)"
                  if self.separator is not None
                  else ""
              ),
          )
      else:
          self.epochs = epochs
  
      # jit models only compatible with version 1.7.1
      if "1.7.1" not in torch.__version__:
          if jit == True:
              print(
                  "Setting jit to False because torch version is not 1.7.1."
              )
          jit = False
  
      # Load CLIP
      self.device = torch.device(
          "cuda" if torch.cuda.is_available() else "cpu"
      )
      clip_perceptor, norm = load(
          model_name, jit=jit, device=self.device
      )
      self.perceptor = clip_perceptor.eval()
      for param in self.perceptor.parameters():
          param.requires_grad = False
      if jit == False:
          input_res = clip_perceptor.visual.input_resolution
      else:
          input_res = clip_perceptor.input_resolution.item()
      self.clip_transform = create_clip_img_transform(input_res)
  
      self.iterations = iterations
      self.image_width = image_width
      total_batches = (
          self.epochs
          * self.iterations
          * batch_size
          * gradient_accumulate_every
      )
      model = DeepDaze(
          self.perceptor,
          norm,
          input_res,
          total_batches,
          batch_size=batch_size,
          image_width=image_width,
          num_layers=num_layers,
          theta_initial=theta_initial,
          theta_hidden=theta_hidden,
          lower_bound_cutout=lower_bound_cutout,
          upper_bound_cutout=upper_bound_cutout,
          saturate_bound=saturate_bound,
          gauss_sampling=gauss_sampling,
          gauss_mean=gauss_mean,
          gauss_std=gauss_std,
          do_cutout=do_cutout,
          center_bias=center_bias,
          center_focus=center_focus,
          hidden_size=hidden_size,
          averaging_weight=averaging_weight,
      ).to(self.device)
      self.model = model
      self.scaler = GradScaler()
      siren_params = model.model.parameters()
      if optimizer == "AdamP":
          self.optimizer = AdamP(siren_params, lr)
      elif optimizer == "Adam":
          self.optimizer = torch.optim.Adam(siren_params, lr)
      elif optimizer == "DiffGrad":
          self.optimizer = DiffGrad(siren_params, lr)
      self.gradient_accumulate_every = gradient_accumulate_every
      self.save_every = save_every
      self.save_date_time = save_date_time
      self.open_folder = open_folder
      self.save_progress = save_progress
      self.text = text
      self.image = img
      self.textpath = create_text_path(
          self.perceptor.context_length,
          text=text,
          img=img,
          encoding=clip_encoding,
          separator=story_separator,
      )
      self.filename = self.image_output_path()
  
      # create coding to optimize for
      self.clip_encoding = self.create_clip_encoding(
          text=text, img=img, encoding=clip_encoding
      )
  
      self.start_image = None
      self.start_image_train_iters = start_image_train_iters
      self.start_image_lr = start_image_lr
      if exists(start_image_path):
          file = Path(start_image_path)
          assert (
              file.exists()
          ), f"file does not exist at given starting image path {start_image_path}"
          image = Image.open(str(file))
          start_img_transform = T.Compose(
              [
                  T.Resize(image_width),
                  T.CenterCrop((image_width, image_width)),
                  T.ToTensor(),
              ]
          )
          image_tensor = (
              start_img_transform(image).unsqueeze(0).to(self.device)
          )
          self.start_image = image_tensor
  
      self.save_gif = save_gif
      self.save_video = save_video
  
imports: import os
  import subprocess
  import sys
  import random
  from datetime import datetime
  from pathlib import Path
  import torch
  import torch.nn.functional
  from siren_pytorch import SirenNet, SirenWrapper
  from torch import nn
  from torch.cuda.amp import GradScaler, autocast
  from torch_optimizer import DiffGrad, AdamP
  import numpy
  from PIL import Image
  from imageio import imread, mimsave
  import torchvision.transforms
  from tqdm import trange, tqdm
  from .clip import load, tokenize
label_desc: Use image node_id 1 to create a new Imagine object from inherited base classes: nn with example: obj = Imagine()