code: def __init__(
      self,
      model,
      batch_size,
      custom_input=None,
      clipping_threshold=2,
      tiled_dlatent=False,
      model_res=1024,
      randomize_noise=False,
  ):
      self.batch_size = batch_size
      self.tiled_dlatent = tiled_dlatent
      self.model_scale = int(
          2 * (math.log(model_res, 2) - 1)
      )  # For example, 1024 -> 18
  
      if tiled_dlatent:
          self.initial_dlatents = np.zeros((self.batch_size, 512))
          model.components.synthesis.run(
              np.zeros((self.batch_size, self.model_scale, 512)),
              randomize_noise=randomize_noise,
              minibatch_size=self.batch_size,
              custom_inputs=[
                  partial(
                      create_variable_for_generator,
                      batch_size=batch_size,
                      tiled_dlatent=True,
                  ),
                  partial(create_stub, batch_size=batch_size),
              ],
              structure="fixed",
          )
      else:
          self.initial_dlatents = np.zeros(
              (self.batch_size, self.model_scale, 512)
          )
          if custom_input is not None:
              model.components.synthesis.run(
                  self.initial_dlatents,
                  randomize_noise=randomize_noise,
                  minibatch_size=self.batch_size,
                  custom_inputs=[
                      partial(
                          custom_input.eval(), batch_size=batch_size
                      ),
                      partial(create_stub, batch_size=batch_size),
                  ],
                  structure="fixed",
              )
          else:
              model.components.synthesis.run(
                  self.initial_dlatents,
                  randomize_noise=randomize_noise,
                  minibatch_size=self.batch_size,
                  custom_inputs=[
                      partial(
                          create_variable_for_generator,
                          batch_size=batch_size,
                          tiled_dlatent=False,
                          model_scale=self.model_scale,
                      ),
                      partial(create_stub, batch_size=batch_size),
                  ],
                  structure="fixed",
              )
  
      self.dlatent_avg_def = model.get_var("dlatent_avg")
      self.reset_dlatent_avg()
      self.sess = tf.get_default_session()
      self.graph = tf.get_default_graph()
  
      self.dlatent_variable = next(
          v
          for v in tf.global_variables()
          if "learnable_dlatents" in v.name
      )
      self._assign_dlatent_ph = tf.placeholder(
          tf.float32, name="assign_dlatent_ph"
      )
      self._assign_dlantent = tf.assign(
          self.dlatent_variable, self._assign_dlatent_ph
      )
      self.set_dlatents(self.initial_dlatents)
  
      def get_tensor(name):
          try:
              return self.graph.get_tensor_by_name(name)
          except KeyError:
              return None
  
      self.generator_output = get_tensor("G_synthesis_1/_Run/concat:0")
      if self.generator_output is None:
          self.generator_output = get_tensor(
              "G_synthesis_1/_Run/concat/concat:0"
          )
      if self.generator_output is None:
          self.generator_output = get_tensor(
              "G_synthesis_1/_Run/concat_1/concat:0"
          )
      # If we loaded only Gs and didn't load G or D, then scope "G_synthesis_1" won't exist in the graph.
      if self.generator_output is None:
          self.generator_output = get_tensor(
              "G_synthesis/_Run/concat:0"
          )
      if self.generator_output is None:
          self.generator_output = get_tensor(
              "G_synthesis/_Run/concat/concat:0"
          )
      if self.generator_output is None:
          self.generator_output = get_tensor(
              "G_synthesis/_Run/concat_1/concat:0"
          )
      if self.generator_output is None:
          for op in self.graph.get_operations():
              print(op)
          raise Exception(
              "Couldn't find G_synthesis_1/_Run/concat tensor output"
          )
      self.generated_image = tflib.convert_images_to_uint8(
          self.generator_output, nchw_to_nhwc=True, uint8_cast=False
      )
      self.generated_image_uint8 = tf.saturate_cast(
          self.generated_image, tf.uint8
      )
  
      # Implement stochastic clipping similar to what is described in https://arxiv.org/abs/1702.04782
      # (Slightly different in that the latent space is normal gaussian here and was uniform in [-1, 1] in that paper,
      # so we clip any vector components outside of [-2, 2]. It seems fine, but I haven't done an ablation check.)
      clipping_mask = tf.math.logical_or(
          self.dlatent_variable > clipping_threshold,
          self.dlatent_variable < -clipping_threshold,
      )
      clipped_values = tf.where(
          clipping_mask,
          tf.random_normal(shape=self.dlatent_variable.shape),
          self.dlatent_variable,
      )
      self.stochastic_clip_op = tf.assign(
          self.dlatent_variable, clipped_values
      )
  
file_path: insightface/reconstruction/ostec/core/generator_model.py
imports: import math
  import tensorflow
  import numpy
  import external.stylegan2.dnnlib.tflib
  from functools import partial
label_desc: Use image node_id 1 to create a new Generator object  with example: obj = Generator(model, batch_size, custom_input, clipping_threshold, tiled_dlatent, model_res, randomize_noise)