Created
August 10, 2020 14:10
-
-
Save stevesie88/11bc2387f075f9ee9478d382c0fde9f2 to your computer and use it in GitHub Desktop.
StyleGAN Face Generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original source modified from: | |
# https://github.com/NVlabs/stylegan/blob/master/pretrained_example.py | |
import os | |
import pickle | |
import numpy as np | |
import PIL.Image | |
import dnnlib | |
import dnnlib.tflib as tflib | |
import config | |
NUM_IMAGES_TO_GENERATE = 10 | |
def main(): | |
# Initialize TensorFlow. | |
tflib.init_tf() | |
# Load pre-trained network. | |
url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl | |
with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: | |
_G, _D, Gs = pickle.load(f) | |
# _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. | |
# _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. | |
# Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. | |
# Print network details. | |
Gs.print_layers() | |
def make_image(i): | |
# Pick latent vector. | |
rnd = np.random.RandomState() | |
latents = rnd.randn(1, Gs.input_shape[1]) | |
# Generate image. | |
fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) | |
images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) | |
# Save image. | |
os.makedirs(config.result_dir, exist_ok=True) | |
png_filename = os.path.join(config.result_dir, 'example_{}.png'.format(1)) | |
PIL.Image.fromarray(images[0], 'RGB').save(png_filename) | |
for i in range(NUM_IMAGES_TO_GENERATE): | |
make_image(i) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment