aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/eager/python/examples/revnet/ops_test.py
blob: 5bc2641faf5a5d26262de683e52e36b1f42b3a7b (plain)
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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2018 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 basic ops used in eager mode RevNet."""

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

import tensorflow as tf
from tensorflow.contrib.eager.python.examples.revnet import ops
tfe = tf.contrib.eager


class OpsTest(tf.test.TestCase):

  def test_downsample(self):
    """Test `possible_down_sample` function with mock object."""

    batch_size = 100
    # NHWC format
    x = tf.random_normal(shape=[batch_size, 32, 32, 3])
    # HW doesn't change but number of features increased
    y = ops.downsample(x, filters=5, strides=(1, 1), axis=3)
    self.assertEqual(y.shape, [batch_size, 32, 32, 5])
    # Feature map doesn't change but HW reduced
    y = ops.downsample(x, filters=3, strides=(2, 2), axis=3)
    self.assertEqual(y.shape, [batch_size, 16, 16, 3])
    # Number of feature increased and HW reduced
    y = ops.downsample(x, filters=5, strides=(2, 2), axis=3)
    self.assertEqual(y.shape, [batch_size, 16, 16, 5])

    # Test gradient flow
    x = tf.random_normal(shape=[batch_size, 32, 32, 3])
    with tfe.GradientTape() as tape:
      tape.watch(x)
      y = ops.downsample(x, filters=3, strides=(1, 1))
    self.assertEqual(y.shape, x.shape)
    dy = tf.random_normal(shape=[batch_size, 3, 32, 32])
    grad, = tape.gradient(y, [x], output_gradients=[dy])
    self.assertEqual(grad.shape, x.shape)

    # Default NCHW format
    if tf.test.is_gpu_available():
      x = tf.random_normal(shape=[batch_size, 3, 32, 32])
      # HW doesn't change but feature map reduced
      y = ops.downsample(x, filters=5, strides=(1, 1))
      self.assertEqual(y.shape, [batch_size, 5, 32, 32])
      # Feature map doesn't change but HW reduced
      y = ops.downsample(x, filters=3, strides=(2, 2))
      self.assertEqual(y.shape, [batch_size, 3, 16, 16])
      # Both feature map and HW reduced
      y = ops.downsample(x, filters=5, strides=(2, 2))
      self.assertEqual(y.shape, [batch_size, 5, 16, 16])

      # Test gradient flow
      x = tf.random_normal(shape=[batch_size, 3, 32, 32])
      with tfe.GradientTape() as tape:
        tape.watch(x)
        y = ops.downsample(x, filters=3, strides=(1, 1))
      self.assertEqual(y.shape, x.shape)
      dy = tf.random_normal(shape=[batch_size, 3, 32, 32])
      grad, = tape.gradient(y, [x], output_gradients=[dy])
      self.assertEqual(grad.shape, x.shape)


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