DL之BigGAN:利用BigGAN算法实现超强炸天效果——画风的确skr、skr、skr,太特么的skr了

DL之BigGAN:利用BigGAN算法实现超强炸天效果——画风的确skr、skr、skr,太特么的skr了

导读
    本博主刚刚利用代码进行测试,结果的确吊(不)炸(可)天(思议)!BigGAN的效果的确不可思议,但是关于GAN到底学了什么,仍需要理论研究支持,这个可视化的过渡阶段,倒是给了一个好的想象空间。还有,BigGAN的以假乱真,到底应该如何应用,这也是一个亟待解决的问题。


相关文章
Paper之BigGAN:ICLR 2019最新论文《LARGE SCALE GAN TRAINING FOR HIGH FIDELITY NATURAL IMAGE SYNTHESIS》论文研究中

输出效果

一、Explore BigGAN samples of a particular category
Try varying the truncation value.

1、ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus
PS:保护北极熊,阻止全球气候变暖,爱护环境,人人有责;绿色出行,低碳生活,从我做起!

2、sloth bear, Melursus ursinus, Ursus ursinus

3、ant, emmet, pismire

4、great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias

5、house finch, linnet, Carpodacus mexicanus

6、tiger cat

二、Interpolate between BigGAN samples
    Try setting different categorys with the same noise_seeds, or the same categorys with different noise_seeds. Or go wild and set both any way you like!

1、pug, pug-dog→Persian cat

2、pug, pug-dogjian→lion, king of beasts, Panthera leo

3、pug, pug-dog → tiger, Panthera tigris

4、pug, pug-dog →brown bear, bruin, Ursus arctos

5、 golden retriever→coral fungus

6、golden retriever→toilet tissue, toilet paper, bathroom tissue

7、golden retriever→pineapple, ananas

8、golden retriever→banana

9、 goldfish, Carassius auratus→red wine

10、golf ball → starfish, sea star

实现代码

import cStringIO
import IPython.display
import numpy as np
import PIL.Image
from scipy.stats import truncnorm
import tensorflow as tf
import tensorflow_hub as hub

input_z = inputs['z']
input_y = inputs['y']
input_trunc = inputs['truncation']

dim_z = input_z.shape.as_list()[1]
vocab_size = input_y.shape.as_list()[1]

def truncated_z_sample(batch_size, truncation=1., seed=None):
  state = None if seed is None else np.random.RandomState(seed)
  values = truncnorm.rvs(-2, 2, size=(batch_size, dim_z), random_state=state)
  return truncation * values

def one_hot(index, vocab_size=vocab_size):
  index = np.asarray(index)
  if len(index.shape) == 0:
    index = np.asarray([index])
  assert len(index.shape) == 1
  num = index.shape[0]
  output = np.zeros((num, vocab_size), dtype=np.float32)
  output[np.arange(num), index] = 1
  return output

def one_hot_if_needed(label, vocab_size=vocab_size):
  label = np.asarray(label)
  if len(label.shape) <= 1:
    label = one_hot(label, vocab_size)
  assert len(label.shape) == 2
  return label

def sample(sess, noise, label, truncation=1., batch_size=8,
           vocab_size=vocab_size):
  noise = np.asarray(noise)
  label = np.asarray(label)
  num = noise.shape[0]
  if len(label.shape) == 0:
    label = np.asarray([label] * num)
  if label.shape[0] != num:
    raise ValueError('Got # noise samples ({}) != # label samples ({})'
                     .format(noise.shape[0], label.shape[0]))
  label = one_hot_if_needed(label, vocab_size)
  ims = []
  for batch_start in xrange(0, num, batch_size):
    s = slice(batch_start, min(num, batch_start + batch_size))
    feed_dict = {input_z: noise[s], input_y: label[s], input_trunc: truncation}
    ims.append(sess.run(output, feed_dict=feed_dict))
  ims = np.concatenate(ims, axis=0)
  assert ims.shape[0] == num
  ims = np.clip(((ims + 1) / 2.0) * 256, 0, 255)
  ims = np.uint8(ims)
  return ims

def interpolate(A, B, num_interps):
  alphas = np.linspace(0, 1, num_interps)
  if A.shape != B.shape:
    raise ValueError('A and B must have the same shape to interpolate.')
  return np.array([(1-a)*A + a*B for a in alphas])

def imgrid(imarray, cols=5, pad=1):
  if imarray.dtype != np.uint8:
    raise ValueError('imgrid input imarray must be uint8')
  pad = int(pad)
  assert pad >= 0
  cols = int(cols)
  assert cols >= 1
  N, H, W, C = imarray.shape
  rows = int(np.ceil(N / float(cols)))
  batch_pad = rows * cols - N
  assert batch_pad >= 0
  post_pad = [batch_pad, pad, pad, 0]
  pad_arg = [[0, p] for p in post_pad]
  imarray = np.pad(imarray, pad_arg, 'constant', constant_values=255)
  H += pad
  W += pad
  grid = (imarray
          .reshape(rows, cols, H, W, C)
          .transpose(0, 2, 1, 3, 4)
          .reshape(rows*H, cols*W, C))
  if pad:
    grid = grid[:-pad, :-pad]
  return grid

def imshow(a, format='png', jpeg_fallback=True):
  a = np.asarray(a, dtype=np.uint8)
  str_file = cStringIO.StringIO()
  PIL.Image.fromarray(a).save(str_file, format)
  png_data = str_file.getvalue()
  try:
    disp = IPython.display.display(IPython.display.Image(png_data))
  except IOError:
    if jpeg_fallback and format != 'jpeg':
      print ('Warning: image was too large to display in format "{}"; '
             'trying jpeg instead.').format(format)
      return imshow(a, format='jpeg')
    else:
      raise
  return disp

tf.reset_default_graph()
print 'Loading BigGAN module from:', module_path
module = hub.Module(module_path)
inputs = {k: tf.placeholder(v.dtype, v.get_shape().as_list(), k)
          for k, v in module.get_input_info_dict().iteritems()}
output = module(inputs)
​
print
print 'Inputs:\n', '\n'.join(
    '  {}: {}'.format(*kv) for kv in inputs.iteritems())
print
print 'Output:', output

tf.reset_default_graph()
print 'Loading BigGAN module from:', module_path
module = hub.Module(module_path)
inputs = {k: tf.placeholder(v.dtype, v.get_shape().as_list(), k)
          for k, v in module.get_input_info_dict().iteritems()}
output = module(inputs)
​
print
print 'Inputs:\n', '\n'.join(
    '  {}: {}'.format(*kv) for kv in inputs.iteritems())
print
print 'Output:', output
(0)

相关推荐