diff options
Diffstat (limited to 'cli/app/commands/biggan/random.py')
| -rw-r--r-- | cli/app/commands/biggan/random.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/cli/app/commands/biggan/random.py b/cli/app/commands/biggan/random.py new file mode 100644 index 0000000..42db0b8 --- /dev/null +++ b/cli/app/commands/biggan/random.py @@ -0,0 +1,58 @@ +import click + +from app.utils import click_utils +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') +# @click.option('-r', '--recursive', 'opt_recursive', is_flag=True) +@click.pass_context +def cli(ctx): + """ + Generate a random BigGAN image + """ + import tensorflow as tf + import tensorflow_hub as hub + + print("Loading module...") + module = hub.Module('https://tfhub.dev/deepmind/biggan-128/2') + # module = hub.Module('https://tfhub.dev/deepmind/biggan-256/2') + # module = hub.Module('https://tfhub.dev/deepmind/biggan-512/2') + + batch_size = 8 + truncation = 0.5 # scalar truncation value in [0.02, 1.0] + + z = truncation * tf.random.truncated_normal([batch_size, 120]) # noise sample + + y_index = tf.random.uniform([batch_size], maxval=1000, dtype=tf.int32) + y = tf.one_hot(y_index, 1000) + + outputs = module(dict(y=y, z=z, truncation=truncation)) + + with tf.Session() as sess: + sess.run(tf.compat.v1.global_variables_initializer()) + sess.run(tf.compat.v1.tables_initializer()) + results = sess.run(outputs) + + print(results) + + for sample in results['default']: + sample = image_to_uint8(sample) + img = Image.fromarray(sample, "RGB") + fp_img_out = "{}.png".format(int(time.time() * 1000)) + img.save(join(app_cfg.DIR_OUTPUTS, fp_img_out)) |
