summaryrefslogtreecommitdiff
path: root/cli/app/commands/bigbigan/random.py
blob: a1fd65fc89c9271ba18c2efa98fb5865579e069c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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):
  """
  """
  import tensorflow as tf
  import tensorflow_hub as hub

  print("Loading module...")
  module = hub.Module('https://tfhub.dev/deepmind/bigbigan-resnet50/1')
  z = tf.random.normal([8, 120])  # latent samples
  outputs = module(z, signature='generate', as_dict=True)

  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 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))