<jupyter_start><jupyter_text><jupyter_code>%tensorflow_version 1.x
from google.colab import drive
drive.mount('/content/drive/')
import os

os.getcwd()
with open('/content/drive/My Drive/foo.txt', 'w') as f:
  f.write('Hello Google Drive!')
!cat /content/drive/My\ Drive/foo.txt
!git clone https://github.com/NVlabs/stylegan2.git
import sys
sys.path.insert(0, "/content/stylegan2")
import dnnlib
import argparse
import numpy as np
import PIL.Image
import dnnlib.tflib as tflib
import re

import pretrained_networks

#Takes short seed number, expands it
def expand_seed(seeds, vector_size):
  results = []

  #Takes the seed states and makes it into a number of length vectorsize from that seed state, this way we can duplicate the randomness
  for seed in seeds:
    rnd = np.random.RandomState(seed)
    results.append(rnd.randn(1, vector_size))
    
  return results

#Based on code from the nvidia git library, GS -> NN, seeds = seed vectors, truncation_psi is what stylegan2 uses to make images better res 
def generate_images(GS, seeds, truncation_psi):
  #Noise will account for wrinkles and hair movement etc on seeds generated
  noise_vars = [var for name, var in GS.components.synthesis.vars.items() if name.startswith('noise')]

  #Converts 
  GS_kwargs = dnnlib.EasyDict()
  GS_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
  GS_kwargs.randomize_noise = False

  if truncation_psi is not None:
    GS_kwargs.truncation_psy = truncation_psi

  for seed_index, seed in enumerate(seeds):
    print('Generating image for seed %d/%d' % (seed_index, len(seeds)))
    rnd = np.random.RandomState() #fix it so while it stays the same face, it will introduce small variations like hair placement
    tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) #height and width of images
    #makes images and stores them
    images = GS.run(seed, None, **GS_kwargs) #Minibatch, height, width, channel
    path = f"/content/drive/My Drive/projects/tmp/image{seed_index}.png"
    PIL.Image.fromarray(images[0], 'RGB').save(path)
#Load the neural network
sc = dnnlib.SubmitConfig()
sc.num_gpus = 1
sc.submit_target = dnnlib.SubmitTarget.LOCAL
sc.local.do_not_copy_source_files = True
sc.run_dir_root = "/content/drive/My Drive/projects"
sc.run_desc = 'generate-images'
network_pki = 'gdrive:networks/stylegan2-ffhq-config-f.pkl'

print('Loading networks from %s' % network_pki)
_G, _D, GS = pretrained_networks.load_networks(network_pki)
vector_size = GS.input_shape[1:][0]
seeds = expand_seed(range(8000,8020), vector_size)
generate_images(GS, seeds, truncation_psi=0.5)
#Load the neural network
sc = dnnlib.SubmitConfig()
sc.num_gpus = 1
sc.submit_target = dnnlib.SubmitTarget.LOCAL
sc.local.do_not_copy_source_files = True
sc.run_dir_root = "/content/drive/My Drive/projects"
sc.run_desc = 'generate-images'
network_pkl = 'gdrive:networks/stylegan2-ffhq-config-f.pkl'

print("Loading networks from the %s" % network_pkl)
_G, _D, GS = pretrained_networks.load_networks(network_pkl)
vector_size = GS.input_shape[1:][0]
seeds = expand_seed([4256, 4269], vector_size)
steps = 300
diff = seeds[1] - seeds[0]
step = diff/steps
current = seeds[0].copy()

seeds2 = []
for i in range(steps):
  seeds2.append(current)
  current = current + step


generate_images(GS, seeds2, truncation_psi=1.0)
! ffmpeg -r 30 -i /content/drive/My\ Drive/projects/tmp/image%d.png -vcodec mpeg4 -y /content/drive/My\ Drive/projects/movie.mp4
! file.download(/content/drive/My\ Drive/projects/movie.mp4)<jupyter_output>/bin/bash: -c: line 0: syntax error near unexpected token `/content/drive/My\ Drive/projects/movie.mp4'
/bin/bash: -c: line 0: ` file.download(/content/drive/My\ Drive/projects/movie.mp4)'
{'fraction_non_alphanumeric': 0.11771000535045478, 'fraction_numerical': 0.014446227929373997, 'mean_word_length': 4.906793048973144, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '265203', 'license': 'Apache-2.0', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1301, 'n_words': 335, 'path': 'Face_Generator.ipynb', 'repo_name': 'AjaybirRandhawa/Face-Generator', 'size': '3738'}