1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 # ==============================================================================
15 """Tests for slim.inception_v4."""
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
20 import tensorflow as tf
22 from nets import inception
25 class InceptionTest(tf.test.TestCase):
27 def testBuildLogits(self):
29 height, width = 299, 299
31 inputs = tf.random_uniform((batch_size, height, width, 3))
32 logits, end_points = inception.inception_v4(inputs, num_classes)
33 auxlogits = end_points['AuxLogits']
34 predictions = end_points['Predictions']
35 self.assertTrue(auxlogits.op.name.startswith('InceptionV4/AuxLogits'))
36 self.assertListEqual(auxlogits.get_shape().as_list(),
37 [batch_size, num_classes])
38 self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
39 self.assertListEqual(logits.get_shape().as_list(),
40 [batch_size, num_classes])
41 self.assertTrue(predictions.op.name.startswith(
42 'InceptionV4/Logits/Predictions'))
43 self.assertListEqual(predictions.get_shape().as_list(),
44 [batch_size, num_classes])
46 def testBuildWithoutAuxLogits(self):
48 height, width = 299, 299
50 inputs = tf.random_uniform((batch_size, height, width, 3))
51 logits, endpoints = inception.inception_v4(inputs, num_classes,
52 create_aux_logits=False)
53 self.assertFalse('AuxLogits' in endpoints)
54 self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
55 self.assertListEqual(logits.get_shape().as_list(),
56 [batch_size, num_classes])
58 def testAllEndPointsShapes(self):
60 height, width = 299, 299
62 inputs = tf.random_uniform((batch_size, height, width, 3))
63 _, end_points = inception.inception_v4(inputs, num_classes)
64 endpoints_shapes = {'Conv2d_1a_3x3': [batch_size, 149, 149, 32],
65 'Conv2d_2a_3x3': [batch_size, 147, 147, 32],
66 'Conv2d_2b_3x3': [batch_size, 147, 147, 64],
67 'Mixed_3a': [batch_size, 73, 73, 160],
68 'Mixed_4a': [batch_size, 71, 71, 192],
69 'Mixed_5a': [batch_size, 35, 35, 384],
70 # 4 x Inception-A blocks
71 'Mixed_5b': [batch_size, 35, 35, 384],
72 'Mixed_5c': [batch_size, 35, 35, 384],
73 'Mixed_5d': [batch_size, 35, 35, 384],
74 'Mixed_5e': [batch_size, 35, 35, 384],
76 'Mixed_6a': [batch_size, 17, 17, 1024],
77 # 7 x Inception-B blocks
78 'Mixed_6b': [batch_size, 17, 17, 1024],
79 'Mixed_6c': [batch_size, 17, 17, 1024],
80 'Mixed_6d': [batch_size, 17, 17, 1024],
81 'Mixed_6e': [batch_size, 17, 17, 1024],
82 'Mixed_6f': [batch_size, 17, 17, 1024],
83 'Mixed_6g': [batch_size, 17, 17, 1024],
84 'Mixed_6h': [batch_size, 17, 17, 1024],
86 'Mixed_7a': [batch_size, 8, 8, 1536],
87 # 3 x Inception-C blocks
88 'Mixed_7b': [batch_size, 8, 8, 1536],
89 'Mixed_7c': [batch_size, 8, 8, 1536],
90 'Mixed_7d': [batch_size, 8, 8, 1536],
91 # Logits and predictions
92 'AuxLogits': [batch_size, num_classes],
93 'PreLogitsFlatten': [batch_size, 1536],
94 'Logits': [batch_size, num_classes],
95 'Predictions': [batch_size, num_classes]}
96 self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
97 for endpoint_name in endpoints_shapes:
98 expected_shape = endpoints_shapes[endpoint_name]
99 self.assertTrue(endpoint_name in end_points)
100 self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
103 def testBuildBaseNetwork(self):
105 height, width = 299, 299
106 inputs = tf.random_uniform((batch_size, height, width, 3))
107 net, end_points = inception.inception_v4_base(inputs)
108 self.assertTrue(net.op.name.startswith(
109 'InceptionV4/Mixed_7d'))
110 self.assertListEqual(net.get_shape().as_list(), [batch_size, 8, 8, 1536])
111 expected_endpoints = [
112 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
113 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
114 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
115 'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
116 'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
117 self.assertItemsEqual(end_points.keys(), expected_endpoints)
118 for name, op in end_points.iteritems():
119 self.assertTrue(op.name.startswith('InceptionV4/' + name))
121 def testBuildOnlyUpToFinalEndpoint(self):
123 height, width = 299, 299
125 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
126 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
127 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
128 'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
129 'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
130 for index, endpoint in enumerate(all_endpoints):
131 with tf.Graph().as_default():
132 inputs = tf.random_uniform((batch_size, height, width, 3))
133 out_tensor, end_points = inception.inception_v4_base(
134 inputs, final_endpoint=endpoint)
135 self.assertTrue(out_tensor.op.name.startswith(
136 'InceptionV4/' + endpoint))
137 self.assertItemsEqual(all_endpoints[:index+1], end_points)
139 def testVariablesSetDevice(self):
141 height, width = 299, 299
143 inputs = tf.random_uniform((batch_size, height, width, 3))
144 # Force all Variables to reside on the device.
145 with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
146 inception.inception_v4(inputs, num_classes)
147 with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
148 inception.inception_v4(inputs, num_classes)
149 for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
150 self.assertDeviceEqual(v.device, '/cpu:0')
151 for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
152 self.assertDeviceEqual(v.device, '/gpu:0')
154 def testHalfSizeImages(self):
156 height, width = 150, 150
158 inputs = tf.random_uniform((batch_size, height, width, 3))
159 logits, end_points = inception.inception_v4(inputs, num_classes)
160 self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
161 self.assertListEqual(logits.get_shape().as_list(),
162 [batch_size, num_classes])
163 pre_pool = end_points['Mixed_7d']
164 self.assertListEqual(pre_pool.get_shape().as_list(),
165 [batch_size, 3, 3, 1536])
167 def testUnknownBatchSize(self):
169 height, width = 299, 299
171 with self.test_session() as sess:
172 inputs = tf.placeholder(tf.float32, (None, height, width, 3))
173 logits, _ = inception.inception_v4(inputs, num_classes)
174 self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
175 self.assertListEqual(logits.get_shape().as_list(),
177 images = tf.random_uniform((batch_size, height, width, 3))
178 sess.run(tf.global_variables_initializer())
179 output = sess.run(logits, {inputs: images.eval()})
180 self.assertEquals(output.shape, (batch_size, num_classes))
182 def testEvaluation(self):
184 height, width = 299, 299
186 with self.test_session() as sess:
187 eval_inputs = tf.random_uniform((batch_size, height, width, 3))
188 logits, _ = inception.inception_v4(eval_inputs,
191 predictions = tf.argmax(logits, 1)
192 sess.run(tf.global_variables_initializer())
193 output = sess.run(predictions)
194 self.assertEquals(output.shape, (batch_size,))
196 def testTrainEvalWithReuse(self):
199 height, width = 150, 150
201 with self.test_session() as sess:
202 train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
203 inception.inception_v4(train_inputs, num_classes)
204 eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
205 logits, _ = inception.inception_v4(eval_inputs,
209 predictions = tf.argmax(logits, 1)
210 sess.run(tf.global_variables_initializer())
211 output = sess.run(predictions)
212 self.assertEquals(output.shape, (eval_batch_size,))
215 if __name__ == '__main__':