diff options
| author | jules@lens <julescarbon@gmail.com> | 2019-12-02 02:13:44 +0100 |
|---|---|---|
| committer | jules@lens <julescarbon@gmail.com> | 2019-12-02 02:13:44 +0100 |
| commit | 1d3c7428068c46568638db5ab547c8aeb2308b57 (patch) | |
| tree | 29a255d53824b18eb3ad94e70ddc136f665267ea | |
| parent | f4b6e02846b7068d5cc951f9341d0104fb2c3fa4 (diff) | |
getting random gan images
| -rwxr-xr-x | .gitignore | 3 | ||||
| -rw-r--r-- | cli/app/commands/process/random.py | 48 |
2 files changed, 46 insertions, 5 deletions
@@ -58,4 +58,7 @@ data_store/imagenet/imagenet_images/ *.gif *.jpeg *.png +*.mp4 + +*.swp diff --git a/cli/app/commands/process/random.py b/cli/app/commands/process/random.py index 994afce..a1e5aff 100644 --- a/cli/app/commands/process/random.py +++ b/cli/app/commands/process/random.py @@ -5,9 +5,18 @@ from app.settings import app_cfg from os.path import join import time +import numpy as np from PIL import Image +def image_to_uint8(x): + """Converts [-1, 1] float array to [0, 255] uint8.""" + x = np.asarray(x) + x = (256. / 2.) * (x + 1.) + x = np.clip(x, 0, 255) + x = x.astype(np.uint8) + return x + @click.command('') # @click.option('-i', '--input', 'opt_dir_in', required=True, # help='Path to input image glob directory') @@ -20,20 +29,49 @@ def cli(ctx): import tensorflow as tf import tensorflow_hub as hub - module = hub.Module('https://tfhub.dev/deepmind/bigbigan-resnet50/1') + #tf.compat.v1.disable_eager_execution() + #g = tf.compat.v1.get_default_graph() # Sample a batch of 8 random latent vectors (z) from the Gaussian prior. Then # call the generator on the latent samples to generate a batch of images with # shape [8, 128, 128, 3] and range [-1, 1]. + #recons = module(z, signature='generate', as_dict=True)['upsampled'] + + #info = module.get_input_info_dict('encode')['x'] + #enc_ph = tf.placeholder(dtype=info.dtype, shape=info.get_shape()) + + #z = bigbigan.encode(enc_ph, return_all_features=True)['z_mean'] + #recons = bigbigan.generate(z, upsample=True) + #recons = outputs['upsampled'] + + #if return_all_features else outputs['z_sample'] + + #fp_img_out = "{}.png".format(int(time.time() * 1000)) + print("Loading module...") + module = hub.Module('https://tfhub.dev/deepmind/bigbigan-resnet50/1') z = tf.random.normal([8, 120]) # latent samples - gen_samples = module(z, signature='generate', as_dict=True)['upsampled'] + outputs = module(z, signature='generate', as_dict=True) - print(gen_samples) + with tf.Session() as sess: + sess.run(tf.compat.v1.global_variables_initializer()) + sess.run(tf.compat.v1.tables_initializer()) + results = sess.run(outputs) - for sample in gen_samples: + for sample in results['upsampled']: + sample = image_to_uint8(sample) img = Image.fromarray(sample, "RGB") - fp_img_out = int(time.time() * 1000) + '.png' + fp_img_out = "{}.png".format(int(time.time() * 1000)) img.save(join(app_cfg.DIR_OUTPUTS, fp_img_out)) + #print(result) + + #tf.keras.preprocessing.image.save_img( + # join(app_cfg.DIR_OUTPUTS, fp_img_out), + # gen_samples, + #) + #with tf.Session() as sess: + # gen_samples = gen_samples.eval() + # print(gen_samples) + # # Given a batch of 256x256 RGB images in range [-1, 1], call the encoder to # # compute predicted latents z and other features (e.g. for use in downstream # # recognition tasks). |
