cyclegan_test.py 4.17 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
29
30
31
32
33
# 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.
# ==============================================================================
"""Tests for tensorflow.contrib.slim.nets.cyclegan."""

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

import tensorflow as tf

from nets import cyclegan


# TODO(joelshor): Add a test to check generator endpoints.
class CycleganTest(tf.test.TestCase):

  def test_generator_inference(self):
    """Check one inference step."""
    img_batch = tf.zeros([2, 32, 32, 3])
    model_output, _ = cyclegan.cyclegan_generator_resnet(img_batch)
    with self.test_session() as sess:
34
      sess.run(tf.compat.v1.global_variables_initializer())
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      sess.run(model_output)

  def _test_generator_graph_helper(self, shape):
    """Check that generator can take small and non-square inputs."""
    output_imgs, _ = cyclegan.cyclegan_generator_resnet(tf.ones(shape))
    self.assertAllEqual(shape, output_imgs.shape.as_list())

  def test_generator_graph_small(self):
    self._test_generator_graph_helper([4, 32, 32, 3])

  def test_generator_graph_medium(self):
    self._test_generator_graph_helper([3, 128, 128, 3])

  def test_generator_graph_nonsquare(self):
    self._test_generator_graph_helper([2, 80, 400, 3])

  def test_generator_unknown_batch_dim(self):
    """Check that generator can take unknown batch dimension inputs."""
53
    img = tf.compat.v1.placeholder(tf.float32, shape=[None, 32, None, 3])
54
55
56
57
58
    output_imgs, _ = cyclegan.cyclegan_generator_resnet(img)

    self.assertAllEqual([None, 32, None, 3], output_imgs.shape.as_list())

  def _input_and_output_same_shape_helper(self, kernel_size):
59
    img_batch = tf.compat.v1.placeholder(tf.float32, shape=[None, 32, 32, 3])
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    output_img_batch, _ = cyclegan.cyclegan_generator_resnet(
        img_batch, kernel_size=kernel_size)

    self.assertAllEqual(img_batch.shape.as_list(),
                        output_img_batch.shape.as_list())

  def input_and_output_same_shape_kernel3(self):
    self._input_and_output_same_shape_helper(3)

  def input_and_output_same_shape_kernel4(self):
    self._input_and_output_same_shape_helper(4)

  def input_and_output_same_shape_kernel5(self):
    self._input_and_output_same_shape_helper(5)

  def input_and_output_same_shape_kernel6(self):
    self._input_and_output_same_shape_helper(6)

  def _error_if_height_not_multiple_of_four_helper(self, height):
    self.assertRaisesRegexp(
80
        ValueError, 'The input height must be a multiple of 4.',
81
        cyclegan.cyclegan_generator_resnet,
82
        tf.compat.v1.placeholder(tf.float32, shape=[None, height, 32, 3]))
83
84
85
86
87
88
89
90
91
92
93
94

  def test_error_if_height_not_multiple_of_four_height29(self):
    self._error_if_height_not_multiple_of_four_helper(29)

  def test_error_if_height_not_multiple_of_four_height30(self):
    self._error_if_height_not_multiple_of_four_helper(30)

  def test_error_if_height_not_multiple_of_four_height31(self):
    self._error_if_height_not_multiple_of_four_helper(31)

  def _error_if_width_not_multiple_of_four_helper(self, width):
    self.assertRaisesRegexp(
95
        ValueError, 'The input width must be a multiple of 4.',
96
        cyclegan.cyclegan_generator_resnet,
97
        tf.compat.v1.placeholder(tf.float32, shape=[None, 32, width, 3]))
98
99
100
101
102
103
104
105
106
107
108
109
110

  def test_error_if_width_not_multiple_of_four_width29(self):
    self._error_if_width_not_multiple_of_four_helper(29)

  def test_error_if_width_not_multiple_of_four_width30(self):
    self._error_if_width_not_multiple_of_four_helper(30)

  def test_error_if_width_not_multiple_of_four_width31(self):
    self._error_if_width_not_multiple_of_four_helper(31)


if __name__ == '__main__':
  tf.test.main()