infogan_eval.py 6.06 KB
Newer Older
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
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Evaluates an InfoGAN TFGAN trained MNIST model.

The image visualizations, as in https://arxiv.org/abs/1606.03657, show the
effect of varying a specific latent variable on the image. Each visualization
focuses on one of the three structured variables. Columns have two of the three
variables fixed, while the third one is varied. Different rows have different
random samples from the remaining latents.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function


29
30
from absl import app
from absl import flags
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np

import tensorflow as tf

import data_provider
import networks
import util

tfgan = tf.contrib.gan


flags.DEFINE_string('checkpoint_dir', '/tmp/mnist/',
                    'Directory where the model was written to.')

flags.DEFINE_string('eval_dir', '/tmp/mnist/',
                    'Directory where the results are saved to.')

flags.DEFINE_integer('noise_samples', 6,
                     'Number of samples to draw from the continuous structured '
                     'noise.')

flags.DEFINE_integer('unstructured_noise_dims', 62,
                     'The number of dimensions of the unstructured noise.')

flags.DEFINE_integer('continuous_noise_dims', 2,
                     'The number of dimensions of the continuous noise.')

flags.DEFINE_string('classifier_filename', None,
                    'Location of the pretrained classifier. If `None`, use '
                    'default.')

flags.DEFINE_integer('max_number_of_evaluations', None,
                     'Number of times to run evaluation. If `None`, run '
                     'forever.')

66
67
flags.DEFINE_boolean('write_to_disk', True, 'If `True`, run images to disk.')

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
CAT_SAMPLE_POINTS = np.arange(0, 10)
CONT_SAMPLE_POINTS = np.linspace(-2.0, 2.0, 10)
FLAGS = flags.FLAGS


def main(_, run_eval_loop=True):
  with tf.name_scope('inputs'):
    noise_args = (FLAGS.noise_samples, CAT_SAMPLE_POINTS, CONT_SAMPLE_POINTS,
                  FLAGS.unstructured_noise_dims, FLAGS.continuous_noise_dims)
    # Use fixed noise vectors to illustrate the effect of each dimension.
    display_noise1 = util.get_eval_noise_categorical(*noise_args)
    display_noise2 = util.get_eval_noise_continuous_dim1(*noise_args)
    display_noise3 = util.get_eval_noise_continuous_dim2(*noise_args)
    _validate_noises([display_noise1, display_noise2, display_noise3])

  # Visualize the effect of each structured noise dimension on the generated
  # image.
85
86
87
  def generator_fn(inputs):
    return networks.infogan_generator(
        inputs, len(CAT_SAMPLE_POINTS), is_training=False)
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  with tf.variable_scope('Generator') as genscope:  # Same scope as in training.
    categorical_images = generator_fn(display_noise1)
  reshaped_categorical_img = tfgan.eval.image_reshaper(
      categorical_images, num_cols=len(CAT_SAMPLE_POINTS))
  tf.summary.image('categorical', reshaped_categorical_img, max_outputs=1)

  with tf.variable_scope(genscope, reuse=True):
    continuous1_images = generator_fn(display_noise2)
  reshaped_continuous1_img = tfgan.eval.image_reshaper(
      continuous1_images, num_cols=len(CONT_SAMPLE_POINTS))
  tf.summary.image('continuous1', reshaped_continuous1_img, max_outputs=1)

  with tf.variable_scope(genscope, reuse=True):
    continuous2_images = generator_fn(display_noise3)
  reshaped_continuous2_img = tfgan.eval.image_reshaper(
      continuous2_images, num_cols=len(CONT_SAMPLE_POINTS))
  tf.summary.image('continuous2', reshaped_continuous2_img, max_outputs=1)

  # Evaluate image quality.
  all_images = tf.concat(
      [categorical_images, continuous1_images, continuous2_images], 0)
  tf.summary.scalar('MNIST_Classifier_score',
                    util.mnist_score(all_images, FLAGS.classifier_filename))

  # Write images to disk.
  image_write_ops = []
114
115
116
117
118
119
120
  if FLAGS.write_to_disk:
    image_write_ops.append(_get_write_image_ops(
        FLAGS.eval_dir, 'categorical_infogan.png', reshaped_categorical_img[0]))
    image_write_ops.append(_get_write_image_ops(
        FLAGS.eval_dir, 'continuous1_infogan.png', reshaped_continuous1_img[0]))
    image_write_ops.append(_get_write_image_ops(
        FLAGS.eval_dir, 'continuous2_infogan.png', reshaped_continuous2_img[0]))
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

  # For unit testing, use `run_eval_loop=False`.
  if not run_eval_loop: return
  tf.contrib.training.evaluate_repeatedly(
      FLAGS.checkpoint_dir,
      hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir),
             tf.contrib.training.StopAfterNEvalsHook(1)],
      eval_ops=image_write_ops,
      max_number_of_evaluations=FLAGS.max_number_of_evaluations)


def _validate_noises(noises):
  """Sanity check on constructed noise tensors.

  Args:
    noises: List of 3-tuples of noise vectors.
  """
  assert isinstance(noises, (list, tuple))
  for noise_l in noises:
    assert len(noise_l) == 3
    assert isinstance(noise_l[0], np.ndarray)
    batch_dim = noise_l[0].shape[0]
    for i, noise in enumerate(noise_l):
      assert isinstance(noise, np.ndarray)
      # Check that batch dimensions are all the same.
      assert noise.shape[0] == batch_dim

      # Check that shapes for corresponding noises are the same.
      assert noise.shape == noises[0][i].shape


def _get_write_image_ops(eval_dir, filename, images):
  """Create Ops that write images to disk."""
  return tf.write_file(
      '%s/%s'% (eval_dir, filename),
      tf.image.encode_png(data_provider.float_image_to_uint8(images)))


if __name__ == '__main__':
160
  app.run(main)