pnasnet_test.py 11.2 KB
Newer Older
maximneumann's avatar
maximneumann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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 slim.pnasnet."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
21
from tensorflow.contrib import slim as contrib_slim
maximneumann's avatar
maximneumann committed
22
23
24

from nets.nasnet import pnasnet

25
slim = contrib_slim
maximneumann's avatar
maximneumann committed
26
27
28
29
30
31
32
33


class PNASNetTest(tf.test.TestCase):

  def testBuildLogitsLargeModel(self):
    batch_size = 5
    height, width = 331, 331
    num_classes = 1000
34
35
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
maximneumann's avatar
maximneumann committed
36
37
38
39
40
41
42
43
44
45
46
    with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
      logits, end_points = pnasnet.build_pnasnet_large(inputs, num_classes)
    auxlogits = end_points['AuxLogits']
    predictions = end_points['Predictions']
    self.assertListEqual(auxlogits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertListEqual(predictions.get_shape().as_list(),
                         [batch_size, num_classes])

47
48
49
50
  def testBuildLogitsMobileModel(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000
51
52
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
53
54
55
56
57
58
59
60
61
62
63
64
65
    with slim.arg_scope(pnasnet.pnasnet_mobile_arg_scope()):
      logits, end_points = pnasnet.build_pnasnet_mobile(inputs, num_classes)
    auxlogits = end_points['AuxLogits']
    predictions = end_points['Predictions']
    self.assertListEqual(auxlogits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertListEqual(predictions.get_shape().as_list(),
                         [batch_size, num_classes])

  def testBuildNonExistingLayerLargeModel(self):
    """Tests that the model is built correctly without unnecessary layers."""
66
67
    inputs = tf.random.uniform((5, 331, 331, 3))
    tf.compat.v1.train.create_global_step()
68
69
    with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
      pnasnet.build_pnasnet_large(inputs, 1000)
70
    vars_names = [x.op.name for x in tf.compat.v1.trainable_variables()]
71
72
73
74
75
    self.assertIn('cell_stem_0/1x1/weights', vars_names)
    self.assertNotIn('cell_stem_1/comb_iter_0/right/1x1/weights', vars_names)

  def testBuildNonExistingLayerMobileModel(self):
    """Tests that the model is built correctly without unnecessary layers."""
76
77
    inputs = tf.random.uniform((5, 224, 224, 3))
    tf.compat.v1.train.create_global_step()
78
79
    with slim.arg_scope(pnasnet.pnasnet_mobile_arg_scope()):
      pnasnet.build_pnasnet_mobile(inputs, 1000)
80
    vars_names = [x.op.name for x in tf.compat.v1.trainable_variables()]
81
82
83
    self.assertIn('cell_stem_0/1x1/weights', vars_names)
    self.assertNotIn('cell_stem_1/comb_iter_0/right/1x1/weights', vars_names)

maximneumann's avatar
maximneumann committed
84
85
86
87
  def testBuildPreLogitsLargeModel(self):
    batch_size = 5
    height, width = 331, 331
    num_classes = None
88
89
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
maximneumann's avatar
maximneumann committed
90
91
92
93
94
95
96
    with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
      net, end_points = pnasnet.build_pnasnet_large(inputs, num_classes)
    self.assertFalse('AuxLogits' in end_points)
    self.assertFalse('Predictions' in end_points)
    self.assertTrue(net.op.name.startswith('final_layer/Mean'))
    self.assertListEqual(net.get_shape().as_list(), [batch_size, 4320])

97
98
99
100
  def testBuildPreLogitsMobileModel(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = None
101
102
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
103
104
105
106
107
108
109
    with slim.arg_scope(pnasnet.pnasnet_mobile_arg_scope()):
      net, end_points = pnasnet.build_pnasnet_mobile(inputs, num_classes)
    self.assertFalse('AuxLogits' in end_points)
    self.assertFalse('Predictions' in end_points)
    self.assertTrue(net.op.name.startswith('final_layer/Mean'))
    self.assertListEqual(net.get_shape().as_list(), [batch_size, 1080])

maximneumann's avatar
maximneumann committed
110
111
112
113
  def testAllEndPointsShapesLargeModel(self):
    batch_size = 5
    height, width = 331, 331
    num_classes = 1000
114
115
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
maximneumann's avatar
maximneumann committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
      _, end_points = pnasnet.build_pnasnet_large(inputs, num_classes)

    endpoints_shapes = {'Stem': [batch_size, 42, 42, 540],
                        'Cell_0': [batch_size, 42, 42, 1080],
                        'Cell_1': [batch_size, 42, 42, 1080],
                        'Cell_2': [batch_size, 42, 42, 1080],
                        'Cell_3': [batch_size, 42, 42, 1080],
                        'Cell_4': [batch_size, 21, 21, 2160],
                        'Cell_5': [batch_size, 21, 21, 2160],
                        'Cell_6': [batch_size, 21, 21, 2160],
                        'Cell_7': [batch_size, 21, 21, 2160],
                        'Cell_8': [batch_size, 11, 11, 4320],
                        'Cell_9': [batch_size, 11, 11, 4320],
                        'Cell_10': [batch_size, 11, 11, 4320],
                        'Cell_11': [batch_size, 11, 11, 4320],
                        'global_pool': [batch_size, 4320],
                        # Logits and predictions
                        'AuxLogits': [batch_size, 1000],
                        'Predictions': [batch_size, 1000],
                        'Logits': [batch_size, 1000],
                       }
    self.assertEqual(len(end_points), 17)
    self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
    for endpoint_name in endpoints_shapes:
141
      tf.compat.v1.logging.info('Endpoint name: {}'.format(endpoint_name))
maximneumann's avatar
maximneumann committed
142
143
144
145
146
      expected_shape = endpoints_shapes[endpoint_name]
      self.assertIn(endpoint_name, end_points)
      self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
                           expected_shape)

147
148
149
150
  def testAllEndPointsShapesMobileModel(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000
151
152
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    with slim.arg_scope(pnasnet.pnasnet_mobile_arg_scope()):
      _, end_points = pnasnet.build_pnasnet_mobile(inputs, num_classes)

    endpoints_shapes = {
        'Stem': [batch_size, 28, 28, 135],
        'Cell_0': [batch_size, 28, 28, 270],
        'Cell_1': [batch_size, 28, 28, 270],
        'Cell_2': [batch_size, 28, 28, 270],
        'Cell_3': [batch_size, 14, 14, 540],
        'Cell_4': [batch_size, 14, 14, 540],
        'Cell_5': [batch_size, 14, 14, 540],
        'Cell_6': [batch_size, 7, 7, 1080],
        'Cell_7': [batch_size, 7, 7, 1080],
        'Cell_8': [batch_size, 7, 7, 1080],
        'global_pool': [batch_size, 1080],
        # Logits and predictions
        'AuxLogits': [batch_size, num_classes],
        'Predictions': [batch_size, num_classes],
        'Logits': [batch_size, num_classes],
    }
    self.assertEqual(len(end_points), 14)
    self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
    for endpoint_name in endpoints_shapes:
176
      tf.compat.v1.logging.info('Endpoint name: {}'.format(endpoint_name))
177
178
179
180
181
      expected_shape = endpoints_shapes[endpoint_name]
      self.assertIn(endpoint_name, end_points)
      self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
                           expected_shape)

maximneumann's avatar
maximneumann committed
182
183
184
185
186
  def testNoAuxHeadLargeModel(self):
    batch_size = 5
    height, width = 331, 331
    num_classes = 1000
    for use_aux_head in (True, False):
187
188
189
      tf.compat.v1.reset_default_graph()
      inputs = tf.random.uniform((batch_size, height, width, 3))
      tf.compat.v1.train.create_global_step()
maximneumann's avatar
maximneumann committed
190
191
192
193
194
195
196
      config = pnasnet.large_imagenet_config()
      config.set_hparam('use_aux_head', int(use_aux_head))
      with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
        _, end_points = pnasnet.build_pnasnet_large(inputs, num_classes,
                                                    config=config)
      self.assertEqual('AuxLogits' in end_points, use_aux_head)

197
198
199
200
201
  def testNoAuxHeadMobileModel(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000
    for use_aux_head in (True, False):
202
203
204
      tf.compat.v1.reset_default_graph()
      inputs = tf.random.uniform((batch_size, height, width, 3))
      tf.compat.v1.train.create_global_step()
205
206
207
208
209
210
211
      config = pnasnet.mobile_imagenet_config()
      config.set_hparam('use_aux_head', int(use_aux_head))
      with slim.arg_scope(pnasnet.pnasnet_mobile_arg_scope()):
        _, end_points = pnasnet.build_pnasnet_mobile(
            inputs, num_classes, config=config)
      self.assertEqual('AuxLogits' in end_points, use_aux_head)

maximneumann's avatar
maximneumann committed
212
213
214
215
  def testOverrideHParamsLargeModel(self):
    batch_size = 5
    height, width = 331, 331
    num_classes = 1000
216
217
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
maximneumann's avatar
maximneumann committed
218
219
220
221
222
223
224
225
    config = pnasnet.large_imagenet_config()
    config.set_hparam('data_format', 'NCHW')
    with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()):
      _, end_points = pnasnet.build_pnasnet_large(
          inputs, num_classes, config=config)
    self.assertListEqual(
        end_points['Stem'].shape.as_list(), [batch_size, 540, 42, 42])

226
227
228
229
  def testOverrideHParamsMobileModel(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000
230
231
    inputs = tf.random.uniform((batch_size, height, width, 3))
    tf.compat.v1.train.create_global_step()
232
233
234
235
236
237
238
239
    config = pnasnet.mobile_imagenet_config()
    config.set_hparam('data_format', 'NCHW')
    with slim.arg_scope(pnasnet.pnasnet_mobile_arg_scope()):
      _, end_points = pnasnet.build_pnasnet_mobile(
          inputs, num_classes, config=config)
    self.assertListEqual(end_points['Stem'].shape.as_list(),
                         [batch_size, 135, 28, 28])

240
241
242
243
244
  def testUseBoundedAcitvationMobileModel(self):
    batch_size = 1
    height, width = 224, 224
    num_classes = 1000
    for use_bounded_activation in (True, False):
245
246
      tf.compat.v1.reset_default_graph()
      inputs = tf.random.uniform((batch_size, height, width, 3))
247
248
249
250
251
      config = pnasnet.mobile_imagenet_config()
      config.set_hparam('use_bounded_activation', use_bounded_activation)
      with slim.arg_scope(pnasnet.pnasnet_mobile_arg_scope()):
        _, _ = pnasnet.build_pnasnet_mobile(
            inputs, num_classes, config=config)
252
      for node in tf.compat.v1.get_default_graph().as_graph_def().node:
253
254
        if node.op.startswith('Relu'):
          self.assertEqual(node.op == 'Relu6', use_bounded_activation)
maximneumann's avatar
maximneumann committed
255
256
257

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