From d9ceb77e8700312c554cd3205d0c8db775db00c2 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 5 Dec 2019 17:01:44 +0100 Subject: biggan --- cli/app/commands/bigbigan/fetch.py | 57 +++++++++++++++++++++++ cli/app/commands/bigbigan/random.py | 46 +++++++++++++++++++ cli/app/commands/biggan/random.py | 58 +++++++++++++++++++++++ cli/app/commands/process/fetch.py | 57 ----------------------- cli/app/commands/process/random.py | 91 ------------------------------------- 5 files changed, 161 insertions(+), 148 deletions(-) create mode 100644 cli/app/commands/bigbigan/fetch.py create mode 100644 cli/app/commands/bigbigan/random.py create mode 100644 cli/app/commands/biggan/random.py delete mode 100644 cli/app/commands/process/fetch.py delete mode 100644 cli/app/commands/process/random.py (limited to 'cli/app/commands') diff --git a/cli/app/commands/bigbigan/fetch.py b/cli/app/commands/bigbigan/fetch.py new file mode 100644 index 0000000..5b6c102 --- /dev/null +++ b/cli/app/commands/bigbigan/fetch.py @@ -0,0 +1,57 @@ +import click + +from app.utils import click_utils +from app.settings import app_cfg + +from os.path import join +from subprocess import call + +@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): + """ + """ + + # app_cfg.MODELZOO_CFG + import gensim + + # from nltk.corpus import wordnet as wn + # synsets = wordnet.synsets("fir_tree") + # synonyms = [ lemma.name() for lemma in synsets[0].lemmas() ] + + imagenet = Imagenet() + + sentence = "The quick brown fox jumps over the lazy dog" + tokens = gensim.utils.simple_preprocess(sentence) + +class Imagenet: + def __init__(): + tokens = {} + with open(app_cfg.FP_IMAGENET_WORDS, "r") as fp: + for line in fp.readlines(): + wordnet_id, word_list = line.split('\t') + words = [word.trim() for word in word_list.split(',')] + for word in words: + tokens[word] = wordnet_id + self.tokens = tokens + + def get_wordnet_ids_for_words(tokens): + # for token in tokens: + # if token in tokens: + pass + + def images_from_wordnet_id(wordnet_id): + """ + Given a Wordnet ID, download images for this class + """ + call([ + "python", + join(app_cfg.DIR_APP, "../ImageNet-Datasets-Downloader/downloader.py"), + '-data_root', app_cfg.FP_IMAGENET, + '-use_class_list', 'True', + '-class_list', wordnet_id, + '-images_per_class', app_cfg.IMAGENET_IMAGES_PER_CLASS + ]) diff --git a/cli/app/commands/bigbigan/random.py b/cli/app/commands/bigbigan/random.py new file mode 100644 index 0000000..a1fd65f --- /dev/null +++ b/cli/app/commands/bigbigan/random.py @@ -0,0 +1,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)) + 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)) diff --git a/cli/app/commands/process/fetch.py b/cli/app/commands/process/fetch.py deleted file mode 100644 index 5b6c102..0000000 --- a/cli/app/commands/process/fetch.py +++ /dev/null @@ -1,57 +0,0 @@ -import click - -from app.utils import click_utils -from app.settings import app_cfg - -from os.path import join -from subprocess import call - -@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): - """ - """ - - # app_cfg.MODELZOO_CFG - import gensim - - # from nltk.corpus import wordnet as wn - # synsets = wordnet.synsets("fir_tree") - # synonyms = [ lemma.name() for lemma in synsets[0].lemmas() ] - - imagenet = Imagenet() - - sentence = "The quick brown fox jumps over the lazy dog" - tokens = gensim.utils.simple_preprocess(sentence) - -class Imagenet: - def __init__(): - tokens = {} - with open(app_cfg.FP_IMAGENET_WORDS, "r") as fp: - for line in fp.readlines(): - wordnet_id, word_list = line.split('\t') - words = [word.trim() for word in word_list.split(',')] - for word in words: - tokens[word] = wordnet_id - self.tokens = tokens - - def get_wordnet_ids_for_words(tokens): - # for token in tokens: - # if token in tokens: - pass - - def images_from_wordnet_id(wordnet_id): - """ - Given a Wordnet ID, download images for this class - """ - call([ - "python", - join(app_cfg.DIR_APP, "../ImageNet-Datasets-Downloader/downloader.py"), - '-data_root', app_cfg.FP_IMAGENET, - '-use_class_list', 'True', - '-class_list', wordnet_id, - '-images_per_class', app_cfg.IMAGENET_IMAGES_PER_CLASS - ]) diff --git a/cli/app/commands/process/random.py b/cli/app/commands/process/random.py deleted file mode 100644 index a1e5aff..0000000 --- a/cli/app/commands/process/random.py +++ /dev/null @@ -1,91 +0,0 @@ -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): - """ - """ - print("Loading Tensorflow....") - import tensorflow as tf - import tensorflow_hub as hub - - #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 - 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['upsampled']: - 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)) - #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). - # images = tf.placeholder(tf.float32, shape=[None, 256, 256, 3]) - # features = module(images, signature='encode', as_dict=True) - - # # Get the predicted latent sample `z_sample` from the dict of features. - # # Other available features include `avepool_feat` and `bn_crelu_feat`, used in - # # the representation learning results. - # z_sample = features['z_sample'] # shape [?, 120] - - # # Compute reconstructions of the input `images` by passing the encoder's output - # # `z_sample` back through the generator. Note that raw generator outputs are - # # half the resolution of encoder inputs (128x128). To get upsampled generator - # # outputs matching the encoder input resolution (256x256), instead use: - # # recons = module(z_sample, signature='generate', as_dict=True)['upsampled'] - # recons = module(z_sample, signature='generate') # shape [?, 128, 128, 3] -- cgit v1.2.3-70-g09d2