cifar10_test.py 6.87 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
# 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.
# ==============================================================================

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

from tempfile import mkstemp

import numpy as np
Karmel Allison's avatar
Karmel Allison committed
23
import tensorflow as tf  # pylint: disable=g-bad-import-order
24

25
from official.resnet import cifar10_main
26
from official.utils.testing import integration
27
28
29

tf.logging.set_verbosity(tf.logging.ERROR)

30
_BATCH_SIZE = 128
Neal Wu's avatar
Neal Wu committed
31
32
33
_HEIGHT = 32
_WIDTH = 32
_NUM_CHANNELS = 3
34

35
36

class BaseTest(tf.test.TestCase):
Karmel Allison's avatar
Karmel Allison committed
37
38
  """Tests for the Cifar10 version of Resnet.
  """
39

40
41
42
43
  def tearDown(self):
    super(BaseTest, self).tearDown()
    tf.gfile.DeleteRecursively(self.get_temp_dir())

44
45
46
  def test_dataset_input_fn(self):
    fake_data = bytearray()
    fake_data.append(7)
Neal Wu's avatar
Neal Wu committed
47
48
    for i in range(_NUM_CHANNELS):
      for _ in range(_HEIGHT * _WIDTH):
49
50
51
        fake_data.append(i)

    _, filename = mkstemp(dir=self.get_temp_dir())
52
53
54
    data_file = open(filename, 'wb')
    data_file.write(fake_data)
    data_file.close()
55

56
    fake_dataset = tf.data.FixedLengthRecordDataset(
Karmel Allison's avatar
Karmel Allison committed
57
        filename, cifar10_main._RECORD_BYTES)  # pylint: disable=protected-access
58
59
    fake_dataset = fake_dataset.map(
        lambda val: cifar10_main.parse_record(val, False))
60
61
    image, label = fake_dataset.make_one_shot_iterator().get_next()

Neal Wu's avatar
Neal Wu committed
62
63
    self.assertAllEqual(label.shape, (10,))
    self.assertAllEqual(image.shape, (_HEIGHT, _WIDTH, _NUM_CHANNELS))
64
65
66
67
68
69
70
71

    with self.test_session() as sess:
      image, label = sess.run([image, label])

      self.assertAllEqual(label, np.array([int(i == 7) for i in range(10)]))

      for row in image:
        for pixel in row:
72
          self.assertAllClose(pixel, np.array([-1.225, 0., 1.225]), rtol=1e-3)
73

74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
114
115
116
117
118
119
120
121
122
123
  def _cifar10_model_fn_helper(self, mode, version, dtype, multi_gpu=False):
    with tf.Graph().as_default() as g:
      input_fn = cifar10_main.get_synth_input_fn()
      dataset = input_fn(True, '', _BATCH_SIZE)
      iterator = dataset.make_one_shot_iterator()
      features, labels = iterator.get_next()
      spec = cifar10_main.cifar10_model_fn(
          features, labels, mode, {
              'dtype': dtype,
              'resnet_size': 32,
              'data_format': 'channels_last',
              'batch_size': _BATCH_SIZE,
              'version': version,
              'loss_scale': 128 if dtype == tf.float16 else 1,
              'multi_gpu': multi_gpu
          })

      predictions = spec.predictions
      self.assertAllEqual(predictions['probabilities'].shape,
                          (_BATCH_SIZE, 10))
      self.assertEqual(predictions['probabilities'].dtype, tf.float32)
      self.assertAllEqual(predictions['classes'].shape, (_BATCH_SIZE,))
      self.assertEqual(predictions['classes'].dtype, tf.int64)

      if mode != tf.estimator.ModeKeys.PREDICT:
        loss = spec.loss
        self.assertAllEqual(loss.shape, ())
        self.assertEqual(loss.dtype, tf.float32)

      if mode == tf.estimator.ModeKeys.EVAL:
        eval_metric_ops = spec.eval_metric_ops
        self.assertAllEqual(eval_metric_ops['accuracy'][0].shape, ())
        self.assertAllEqual(eval_metric_ops['accuracy'][1].shape, ())
        self.assertEqual(eval_metric_ops['accuracy'][0].dtype, tf.float32)
        self.assertEqual(eval_metric_ops['accuracy'][1].dtype, tf.float32)

      for v in tf.trainable_variables():
        self.assertEqual(v.dtype.base_dtype, tf.float32)

      tensors_to_check = ('initial_conv:0', 'block_layer1:0', 'block_layer2:0',
                          'block_layer3:0', 'final_reduce_mean:0',
                          'final_dense:0')

      for tensor_name in tensors_to_check:
        tensor = g.get_tensor_by_name('resnet_model/' + tensor_name)
        self.assertEqual(tensor.dtype, dtype,
                         'Tensor {} has dtype {}, while dtype {} was '
                         'expected'.format(tensor, tensor.dtype,
                                           dtype))

124
  def cifar10_model_fn_helper(self, mode, version, multi_gpu=False):
125
126
127
128
    self._cifar10_model_fn_helper(mode=mode, version=version, dtype=tf.float32,
                                  multi_gpu=multi_gpu)
    self._cifar10_model_fn_helper(mode=mode, version=version, dtype=tf.float16,
                                  multi_gpu=multi_gpu)
129

130
131
  def test_cifar10_model_fn_train_mode_v1(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.TRAIN, version=1)
132

133
134
  def test_cifar10_model_fn_trainmode__v2(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.TRAIN, version=2)
Karmel Allison's avatar
Karmel Allison committed
135

136
137
138
  def test_cifar10_model_fn_train_mode_multi_gpu_v1(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.TRAIN, version=1,
                                 multi_gpu=True)
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  def test_cifar10_model_fn_train_mode_multi_gpu_v2(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.TRAIN, version=2,
                                 multi_gpu=True)

  def test_cifar10_model_fn_eval_mode_v1(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.EVAL, version=1)

  def test_cifar10_model_fn_eval_mode_v2(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.EVAL, version=2)

  def test_cifar10_model_fn_predict_mode_v1(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.PREDICT, version=1)

  def test_cifar10_model_fn_predict_mode_v2(self):
    self.cifar10_model_fn_helper(tf.estimator.ModeKeys.PREDICT, version=2)
155

156
  def _test_cifar10model_shape(self, version):
Neal Wu's avatar
Neal Wu committed
157
158
159
    batch_size = 135
    num_classes = 246

160
161
162
163
164
165
166
167
168
    model = cifar10_main.Cifar10Model(32, data_format='channels_last',
                                      num_classes=num_classes, version=version)
    fake_input = tf.random_uniform([batch_size, _HEIGHT, _WIDTH, _NUM_CHANNELS])
    output = model(fake_input, training=True)

    self.assertAllEqual(output.shape, (batch_size, num_classes))

  def test_cifar10model_shape_v1(self):
    self._test_cifar10model_shape(version=1)
Neal Wu's avatar
Neal Wu committed
169

170
171
  def test_cifar10model_shape_v2(self):
    self._test_cifar10model_shape(version=2)
Neal Wu's avatar
Neal Wu committed
172

173
  def test_cifar10_end_to_end_synthetic_v1(self):
174
175
176
177
    integration.run_synthetic(
        main=cifar10_main.main, tmp_root=self.get_temp_dir(),
        extra_flags=['-v', '1']
    )
178
179

  def test_cifar10_end_to_end_synthetic_v2(self):
180
181
182
183
    integration.run_synthetic(
        main=cifar10_main.main, tmp_root=self.get_temp_dir(),
        extra_flags=['-v', '2']
    )
184

185
186
187

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