code: def set_network(self, Gs, minibatch_size=1):
      assert minibatch_size == 1
      self._Gs = Gs
      self._minibatch_size = minibatch_size
      if self._Gs is None:
          return
      if self.clone_net:
          self._Gs = self._Gs.clone()
  
      # Find dlatent stats.
      self._info(
          "Finding W midpoint and stddev using %d samples..."
          % self.dlatent_avg_samples
      )
      latent_samples = np.random.RandomState(123).randn(
          self.dlatent_avg_samples, *self._Gs.input_shapes[0][1:]
      )
      dlatent_samples = self._Gs.components.mapping.run(
          latent_samples, None
      )[
          :, :1, :
      ]  # [N, 1, 512]
      self._dlatent_avg = np.mean(
          dlatent_samples, axis=0, keepdims=True
      )  # [1, 1, 512]
      self._dlatent_std = (
          np.sum((dlatent_samples - self._dlatent_avg) ** 2)
          / self.dlatent_avg_samples
      ) ** 0.5
      self._info("std = %g" % self._dlatent_std)
  
      # Find noise inputs.
      self._info("Setting up noise inputs...")
      self._noise_vars = []
      noise_init_ops = []
      noise_normalize_ops = []
      while True:
          n = "G_synthesis/noise%d" % len(self._noise_vars)
          if not n in self._Gs.vars:
              break
          v = self._Gs.vars[n]
          self._noise_vars.append(v)
          noise_init_ops.append(
              tf.assign(
                  v, tf.random_normal(tf.shape(v), dtype=tf.float32)
              )
          )
          noise_mean = tf.reduce_mean(v)
          noise_std = tf.reduce_mean((v - noise_mean) ** 2) ** 0.5
          noise_normalize_ops.append(
              tf.assign(v, (v - noise_mean) / noise_std)
          )
          self._info(n, v)
      self._noise_init_op = tf.group(*noise_init_ops)
      self._noise_normalize_op = tf.group(*noise_normalize_ops)
  
      # Image output graph.
      self._info("Building image output graph...")
      self._dlatents_var = tf.Variable(
          tf.zeros(
              [self._minibatch_size] + list(self._dlatent_avg.shape[1:])
          ),
          name="dlatents_var",
      )
      self._noise_in = tf.placeholder(tf.float32, [], name="noise_in")
      dlatents_noise = (
          tf.random.normal(shape=self._dlatents_var.shape)
          * self._noise_in
      )
      self._dlatents_expr = tf.tile(
          self._dlatents_var + dlatents_noise,
          [1, self._Gs.components.synthesis.input_shape[1], 1],
      )
      self._images_expr = self._Gs.components.synthesis.get_output_for(
          self._dlatents_expr, randomize_noise=False
      )
  
      # Downsample image to 256x256 if it's larger than that. VGG was built for 224x224 images.
      proc_images_expr = (self._images_expr + 1) * (255 / 2)
      sh = proc_images_expr.shape.as_list()
      if sh[2] > 256:
          factor = sh[2] // 256
          proc_images_expr = tf.reduce_mean(
              tf.reshape(
                  proc_images_expr,
                  [
                      -1,
                      sh[1],
                      sh[2] // factor,
                      factor,
                      sh[2] // factor,
                      factor,
                  ],
              ),
              axis=[3, 5],
          )
  
      # Loss graph.
      self._info("Building loss graph...")
      self._target_images_var = tf.Variable(
          tf.zeros(proc_images_expr.shape), name="target_images_var"
      )
      if self._lpips is None:
          self._lpips = misc.load_pkl(
              "https://drive.google.com/uc?id=1N2-m9qszOeVC9Tq77WxsLnuWwOedQiD2"
          )  # vgg16_zhang_perceptual.pkl
      self._dist = self._lpips.get_output_for(
          proc_images_expr, self._target_images_var
      )
      self._loss = tf.reduce_sum(self._dist)
  
      # Noise regularization graph.
      self._info("Building noise regularization graph...")
      reg_loss = 0.0
      for v in self._noise_vars:
          sz = v.shape[2]
          while True:
              reg_loss += (
                  tf.reduce_mean(v * tf.roll(v, shift=1, axis=3)) ** 2
                  + tf.reduce_mean(v * tf.roll(v, shift=1, axis=2)) ** 2
              )
              if sz <= 8:
                  break  # Small enough already
              v = tf.reshape(
                  v, [1, 1, sz // 2, 2, sz // 2, 2]
              )  # Downscale
              v = tf.reduce_mean(v, axis=[3, 5])
              sz = sz // 2
      self._loss += reg_loss * self.regularize_noise_weight
  
      # Optimizer.
      self._info("Setting up optimizer...")
      self._lrate_in = tf.placeholder(tf.float32, [], name="lrate_in")
      self._opt = dnnlib.tflib.Optimizer(learning_rate=self._lrate_in)
      self._opt.register_gradients(
          self._loss, [self._dlatents_var] + self._noise_vars
      )
      self._opt_step = self._opt.apply_updates()
  
file_path: insightface/reconstruction/ostec/external/stylegan2/projector.py
imports: import numpy
  import tensorflow
  import dnnlib
  import dnnlib.tflib
  from training import misc
label_desc: Use image node_id 3 for calling the Projector obj's underlying member method code with example usage: obj.set_network(Gs, minibatch_size) without return types