code: """
  Some codes from https://github.com/Newmu/dcgan_code
  """
  from __future__ import division
  import math
  import json
  import random
  import pprint
  import scipy.misc
  import numpy as np
  from time import gmtime, strftime
  from six.moves import xrange
  from glob import glob
  import cv2
  import imageio
  
  import tensorflow as tf
  import tensorflow.contrib.slim as slim
  
  pp = pprint.PrettyPrinter()
  
  get_stddev = lambda x, k_h, k_w: 1/math.sqrt(k_w*k_h*x.get_shape()[-1])
  
  def show_all_variables():
    model_vars = tf.trainable_variables()
    slim.model_analyzer.analyze_vars(model_vars, print_info=True)
  
  def get_image(image_path, input_height, input_width,
                resize_height=64, resize_width=64,
                crop=True, grayscale=False):
    image = imread(image_path, grayscale)
    try:
      return transform(image, input_height, input_width,
                     resize_height, resize_width, crop)
    except ValueError :
      print("Bad image. filepath: ", image_path)
    except AttributeError:
      print("Bad image. filepath: ", image_path)
  def save_images(images, size, image_path):
    return imsave(inverse_transform(images), size, image_path)
  
  def imread(path, grayscale = False):
    try:
      if (grayscale):
        img = cv2.imread(path)
        new_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        return cv2.imread(new_img, flatten = True).astype(np.float)
      else:
        img = cv2.imread(path)
        new_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        return new_img.astype(np.float)
    except(TypeError):
      print(path)
  
  #Do
  def test_images(path_glob):
      for path in path_glob:
          imread(path)
  
  def merge_images(images, size):
    return inverse_transform(images)
  
  def merge(images, size):
    h, w = images.shape[1], images.shape[2]
    if (images.shape[3] in (3,4)):
      c = images.shape[3]
      img = np.zeros((h * size[0], w * size[1], c))
      for idx, image in enumerate(images):
        i = idx % size[1]
        j = idx // size[1]
        img[j * h:j * h + h, i * w:i * w + w, :] = image
      return img
    elif images.shape[3]==1:
      img = np.zeros((h * size[0], w * size[1]))
      for idx, image in enumerate(images):
        i = idx % size[1]
        j = idx // size[1]
        img[j * h:j * h + h, i * w:i * w + w] = image[:,:,0]
      return img
    else:
      raise ValueError('in merge(images,size) images parameter '
                       'must have dimensions: HxW or HxWx3 or HxWx4')
  
  def imsave(images, size, path):
    image = np.squeeze(merge(images, size))
    return scipy.misc.imsave(path, image)
  
  def center_crop(x, crop_h, crop_w,
                  resize_h=64, resize_w=64):
    if crop_w is None:
      crop_w = crop_h
    h, w = x.shape[:2]
    j = int(round((h - crop_h)/2.))
    i = int(round((w - crop_w)/2.))
    return scipy.misc.imresize(
        x[j:j+crop_h, i:i+crop_w], [resize_h, resize_w])
  
  def transform(image, input_height, input_width,
                resize_height=64, resize_width=64, crop=True):
    if crop:
      cropped_image = center_crop(
        image, input_height, input_width,
        resize_height, resize_width)
    else:
      cropped_image = cv2.resize(image, (resize_height, resize_width))
    return np.array(cropped_image)/127.5 - 1.
  
  def inverse_transform(images):
    return (images+1.)/2.
  
  def make_gif(images, fname, duration=2, true_image=False):
    import moviepy.editor as mpy
  
    def make_frame(t):
      try:
        x = images[int(len(images)/duration*t)]
      except:
        x = images[-1]
  
      if true_image:
        return x.astype(np.uint8)
      else:
        return ((x+1)/2*255).astype(np.uint8)
  
    clip = mpy.VideoClip(make_frame, duration=duration)
    clip.write_gif(fname, fps = len(images) / duration)
  
  def visualize(sess, dcgan, config, option):
    image_frame_dim = int(math.ceil(config.batch_size**.5))
    if option == 0:
      z_sample = np.random.normal(0, 1, size=(config.batch_size, dcgan.z_dim))
      z_sample /= np.linalg.norm(z_sample, axis=0)
      samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
      save_images(samples, [image_frame_dim, image_frame_dim], '/content/gdrive/My Drive/samples/test_%s.png' % strftime("%Y%m%d%H%M%S", gmtime()))
    elif option == 1:
      values = np.arange(0, 1, 1./config.batch_size)
      for idx in xrange(100):
        print(" [*] %d" % idx)
        z_sample = np.zeros([config.batch_size, dcgan.z_dim])
        for kdx, z in enumerate(z_sample):
          z[idx] = values[kdx]
  
        if config.dataset == "mnist":
          y = np.random.choice(10, config.batch_size)
          y_one_hot = np.zeros((config.batch_size, 10))
          y_one_hot[np.arange(config.batch_size), y] = 1
  
          samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample, dcgan.y: y_one_hot})
        elif config.dataset == 'wikiart':
          y = np.random.choice(27, config.batch_size)
          y_one_hot = np.zeros((config.batch_size, 27))
          y_one_hot[np.arange(config.batch_size), y] = 1
  
          samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample, dcgan.y: y_one_hot})
        save_images(samples, [image_frame_dim, image_frame_dim], './samples/test_arange_%s.png' % (idx))
    elif option == 2:
      values = np.arange(0, 1, 1./config.batch_size)
      for idx in [random.randint(0, 99) for _ in xrange(100)]:
        print(" [*] %d" % idx)
        z = np.random.uniform(-0.2, 0.2, size=(dcgan.z_dim))
        z_sample = np.tile(z, (config.batch_size, 1))
        #z_sample = np.zeros([config.batch_size, dcgan.z_dim])
        for kdx, z in enumerate(z_sample):
          z[idx] = values[kdx]
  
        if config.dataset == "mnist":
          y = np.random.choice(10, config.batch_size)
          y_one_hot = np.zeros((config.batch_size, 10))
          y_one_hot[np.arange(config.batch_size), y] = 1
  
          samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample, dcgan.y: y_one_hot})
        elif config.dataset == 'wikiart':
          y = np.random.choice(27, config.batch_size)
          y_one_hot = np.zeros((config.batch_size, 27))
          y_one_hot[np.arange(config.batch_size), y] = 1
  
          samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample, dcgan.y: y_one_hot})
  
        try:
          make_gif(samples, './samples/test_gif_%s.gif' % (idx))
        except:
          save_images(samples, [image_frame_dim, image_frame_dim], './samples/test_%s.png' % strftime("%Y%m%d%H%M%S", gmtime()))
    elif option == 3:
      values = np.arange(0, 1, 1./config.batch_size)
      for idx in xrange(100):
        print(" [*] %d" % idx)
        z_sample = np.zeros([config.batch_size, dcgan.z_dim])
        for kdx, z in enumerate(z_sample):
          z[idx] = values[kdx]
  
        samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
        make_gif(samples, './samples/test_gif_%s.gif' % (idx))
    elif option == 4:
      image_set = []
      values = np.arange(0, 1, 1./config.batch_size)
  
      for idx in xrange(100):
        print(" [*] %d" % idx)
        z_sample = np.zeros([config.batch_size, dcgan.z_dim])
        for kdx, z in enumerate(z_sample): z[idx] = values[kdx]
  
        image_set.append(sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample}))
        make_gif(image_set[-1], './samples/test_gif_%s.gif' % (idx))
  
      new_image_set = [merge(np.array([images[idx] for images in image_set]), [10, 10]) \
          for idx in range(64) + range(63, -1, -1)]
      make_gif(new_image_set, './samples/test_gif_merged.gif', duration=8)
  def get_max_end(path_dir, num_len=3, fname_pattern='*.jpg'):
    max_ = 0
    for f in glob(path_dir + fname_pattern):
      curr = int(f[-num_len-4:-4])
      if curr > max_:
        max_ = curr
    return max_
  
  def image_manifold_size(num_images):
    print(num_images)
    manifold_h = int(np.floor(np.sqrt(num_images)))
    manifold_w = int(np.ceil(np.sqrt(num_images)))
    assert manifold_h * manifold_w == num_images
    return manifold_h, manifold_w
  
  if __name__ == '__main__':
    print('Getting image!')
    import time
    start = time.time()
    get_image("albert-gleizes_acrobats-1916.jpg",256,256,256,256)
    end = (time.time() - start)
    print ('Took : {.:%4f}'.format(end))
apis: numpy.sqrt numpy.random.uniform numpy.tile numpy.array numpy.zeros numpy.random.choice tensorflow.trainable_variables tensorflow.contrib.slim.model_analyzer.analyze_vars numpy.arange numpy.random.normal numpy.linalg.norm